1. Pick a loading strategy
Both variants require the
data-client-key attribute that you will find in your pagent workspace.
2. Synchronous loading
Add the tag as early as possible inside the<head> tag:
3. Asynchronous loading with anti-flicker
- Paste the anti-flicker snippet right after the opening
<head>tag:
MAX_WAIT if you can accept the risk that variations might not be applied in time.
Note: if the SDK loads while the snippet’s curtain is still up, it takes over the curtain and its own resolved timeout decides when the page shows (see section 6), so a hydration wait is not cut short by MAX_WAIT. MAX_WAIT still applies as the fallback for cases where the SDK itself fails to load.
If you prefer a minified version:
- Load the SDK asynchronously:
4. Skip page hide (no flicker)
If you want to disable the page-hiding mechanism entirely, you can add thedata-skip-page-hide="true" attribute to the SDK script tag. This prevents the SDK from hiding the page during initialization.
async attribute for asynchronous loading:
- ✅ No page hiding – The page remains visible throughout initialization
- ✅ No flicker – Users won’t see the page fade in/out
- ⚠️ Content overwrite – Variations are applied directly to the visible page
- ⚠️ Brief flash possible – Users may see the original content briefly before it’s replaced with variations
- When you prefer no visual hiding/flicker over perfect content replacement
- When your variations are subtle and a brief flash is acceptable
- When you want to avoid any opacity changes to the page body
- When using with asynchronous loading and you don’t want the anti-flicker snippet
data-skip-page-hide="true" with asynchronous loading, you do not need to include the anti-flicker snippet from section 3.
5. Analytics only mode
For scenarios where you only need to track analytics data without running tests, you can use the SDK in “analytics only mode” by adding thedata-analytics-only="true" attribute:
- ✅ Tracks analytics and user behavior data
- ✅ Makes the
_pgntproperty available for custom tracking - ❌ Never loads or runs A/B tests
- ❌ Never overwrites variations or content
- ❌ Never loads anti-flicker mechanisms
- Analytics tracking without experimentation
- Gradual migration from other analytics tools
- Testing pagent’s data collection before enabling experiments
- Compliance scenarios where you need data but not content changes
6. Wait for framework hydration
If your site uses a JavaScript framework like React, Next.js, Gatsby, or Nuxt, the framework “hydrates” the server-rendered HTML after the page loads. This hydration can overwrite any DOM changes the SDK made during initialization. Addingdata-await-hydration="true" tells the SDK to wait until the framework has finished hydrating before applying variations, so your changes persist.
- The SDK detects common framework root elements (e.g.
__next,___gatsby,__nuxt,root) - It waits for the framework to attach to the DOM, confirming hydration is complete
- Once hydrated, the SDK applies variations safely without them being overwritten
- If no framework is detected, the SDK proceeds immediately
- The wait has a budget (500 ms by default, configurable, see below). If hydration hasn’t been confirmed once the budget runs out, the SDK falls back to analytics-only for that page view: no changes are applied, the visitor sees the original page, and analytics keep working
- When using Next.js, Gatsby, Nuxt, or other frameworks with server-side rendering
- When you notice variations briefly appear and then revert after page load
- When your framework hydration overwrites SDK-applied changes
?pagent_await_hydration=true to any preview URL to test the behavior first:
data-await-hydration="true" to your script tag permanently.
Configuring the wait budget
The wait defaults to 500 ms and can be adjusted withdata-hydration-timeout (in milliseconds):
?pagent_hydration_timeout=800.
Choosing what happens on timeout
By default (data-hydration-timeout-mode="drop"), a hydration wait that times out falls back to analytics-only for that page view, as described above. Set data-hydration-timeout-mode="apply" to have the SDK apply variations anyway and defend them instead:
- ✅ Applies variations as soon as the wait times out, instead of dropping the page view
- ✅ Watches the modified elements for about 3 seconds afterward and re-applies the change if a late hydration pass overwrites the content, or even replaces the element
- ⚠️ After that 3-second window, the SDK leaves the page to the framework, so a hydration pass that lands later can still overwrite the change
- ⚠️ Can cause a brief flicker while the framework renders and the SDK restores the change
#root or #__next, or another framework signal) on a document that had finished parsing. A timeout with nothing detected, a still-loading document, or a detection error keeps the default drop behavior.
Important caveat: the SDK can’t tell a hydrating framework root apart from a plain page that happens to use <div id="root"> for its own markup, so that page qualifies for apply mode too. Only enable apply mode on pages where applying changes without confirmed hydration is acceptable.
When to use this:
- Sites where the hydration wait times out on a meaningful share of page views (for example, server-rendered markup in a root container that never finishes hydrating)
- When dropping those visitors from experiments is costing you real traffic
?pagent_hydration_timeout_mode=apply on a preview URL.
Scaling the wait on slow connections
Withdata-await-hydration set, the SDK automatically stretches the wait budget on slow connections so visitors don’t get dropped just because the network is slow. This is on by default:
- 3G: budget doubled
- 2G and slow-2G: budget tripled
- Scaling never pushes the budget past 1.5 seconds; a larger budget you configure yourself is kept as is
- The anti-flicker page hide stretches with it, so the wait is never cut short by its own curtain
data-hydration-connection-scaling="false":
?pagent_hydration_connection_scaling=false on a preview URL.
When you don’t need this:
- Static HTML sites with no JavaScript framework
- Client-side-only SPAs that don’t server-render (no hydration step)
- Sites where the SDK loads after the framework has already hydrated
7. Best practices & troubleshooting
- Keep it fast – The SDK is delivered from our global CDN and is heavily cached. Still, monitor Web Vitals to ensure no regressions.
- Avoid flicker – In both modes the SDK temporarily sets
body{opacity:0}. This usually lasts less than 50 ms, but audit on slow connections. With a hydration wait configured (section 6), the hide can last up to the hydration budget plus a small margin, about 1.75 seconds at most with connection scaling. - Test first – Roll out the integration to a staging environment before production.
- Need help? – Reach out at support@pagent.ai.