Loading the Convert tracking code async while preventing flashing
Install the tracking script synchronously for the least flicker, or load it async with a snippet that keeps the original page from flashing.
THIS ARTICLE WILL HELP YOU:
- Understand sync and async loading
- Choose sync or async: flicker and Core Web Vitals
- Install the tracking script synchronously (recommended)
- Load the tracking script async without flashing
- Choose the timeout
- Check speed tools that delay scripts
- Use the experimental React and Vue components
- Legacy tracking script only: load it async
Sync and Async Loading
In Convert Experiences, the tracking code the app gives you loads synchronously in the <head> of your page. That is the most reliable way to avoid flashing (flicker), where visitors see the original page for a moment before your variation appears. If you need the script to load asynchronously, use the async loader snippet in this article instead of adding async to the script tag.
Each way of loading the script behaves differently:
- Synchronous (the default snippet). The browser runs the tracking script before it draws the page. While the script applies your changes, it hides the page, for at most 2.5 seconds, so visitors normally see your variation as soon as the page appears. The trade-off is that the browser waits for the script to download and run before it draws the page, so the script counts as a render-blocking resource.
- Async with the loader snippet. The browser keeps loading the rest of the page while the script downloads. The snippet hides the page straight away and shows it again as soon as Convert has applied your changes. If that takes longer than the timeout you set, or the script fails to load, the snippet shows the page anyway.
- Async with only the
asyncattribute. Avoid this. The page can appear before the script runs. When the script does run, it hides the page while it applies your changes, so visitors see the original page, a blank moment and then the variation.
Choose Sync or Async: Flicker and Core Web Vitals
We recommend the synchronous snippet, because it keeps the page hidden for the shortest time and gives the least flicker. It does cost some page speed:
- The tracking script usually adds a few hundred milliseconds before the page appears: the time to download it, plus the time to apply your experiences' changes.
- The script grows with the number of active experiences, locations, audiences and goals in the project. The larger it is, the longer the delay.
- Because the browser waits for it, speed tools such as Google PageSpeed Insights can list the script as a render-blocking resource, and it can lower your First Contentful Paint (FCP) and Largest Contentful Paint (LCP) scores.
If your Core Web Vitals scores matter more to you than a few hundred extra milliseconds of the page staying covered, use the async loader snippet. The script then no longer blocks the page from rendering, and the page can stay covered a little longer while the script downloads.
For measured figures on script size and load time, see Performance Trade-Offs in our developer docs.
💡Tip: Whichever way you load it, a smaller script is faster. Archive experiences you no longer use, because paused and completed experiences stay in the script (see What experiments are included with the tracking code snippet?), and see Reducing the goals tracked to increase page performance.
📒Note: When you measure page speed with Lighthouse in Chrome DevTools, turn off the Convert Chrome extension or use a private window without extensions. The extension loads a larger debug version of the tracking script that isn't meant for production, so it lowers the scores. Google PageSpeed Insights isn't affected.
Install the Tracking Script Synchronously (Recommended)
The Basic snippet the app shows for a project looks like this:
<!-- begin Convert Experiences code--><script type="text/javascript" src="//cdn-4.convertexperiments.com/v1/js/ACCOUNT_ID-PROJECT_ID.js"></script><!-- end Convert Experiences code -->
To get your project's snippet:
- Open your project and click the Configuration tab.
- Click Environments & Tracking Code.
- Open the actions menu on the row of the environment you want, and click Install Code Snippet.
- In the Code Snippet dialog, copy the Basic snippet, or the Advanced snippet if you pass page tag values to Convert.
- Paste it in the
<head>section of every page that runs an experience or tracks a goal, just after</title>.
Your snippet already contains your account and project IDs. When you open it from an environment, the script address ends with ?environment= and that environment's key. If your project has an active Custom Domain, the snippet shows your own hostname instead of cdn-4.convertexperiments.com. Always copy the snippet from the app rather than typing the address.
For more on where to place the code, see How to Install the Main Tag / Convert Tracking Code / JavaScript.
Load the Tracking Script Async Without Flashing
Use this snippet in place of the standard tracking code when you need the tracking script to load async. It hides the page, loads the tracking script async, and shows the page again when Convert has applied your changes. It is the same snippet as the Anti-Flicker Async Loading recipe in our developer docs.
<script>
!function(e,t,n="",o=2500){
let d="convert-hide-body",
i=document.createElement("style");
i.id=d,
i.setAttribute("data-convert",""),
i.textContent='body{position:relative;overflow:hidden}body::after{position:absolute;top:0;bottom:0;left:0;right:0;content:"";background:#fff;z-index:2147483647}',
document.head.appendChild(i),
setTimeout(c,o);
let r=document.createElement("script");
function c(){
let e=document.getElementById(d);
e&&e.remove()
}
r.src="//cdn-4.convertexperiments.com/v1/js/"+e+"-"+t+".js?environment="+n,
r.async=!0,
r.onerror=()=>c(),
document.head.appendChild(r)
}("[account_id]","[project_id]","[env]");
</script>
To install it:
- Open your project's snippet in the Code Snippet dialog (see Install the Tracking Script Synchronously). Its script address holds your account ID and project ID. For example, in
//cdn-4.convertexperiments.com/v1/js/1000123-1000456.js, the account ID is1000123and the project ID is1000456. - In the last line of the loader snippet, replace
[account_id]with your account ID and[project_id]with your project ID. Keep the quotes around each value. - Replace
[env]with your environment key, the text after?environment=in your snippet's address. If the address has no?environment=, delete[env]and leave the empty quotes,"". - If your snippet's address starts with a hostname other than
cdn-4.convertexperiments.com, for example your own Custom Domain, replacecdn-4.convertexperiments.comin the loader snippet with that hostname. - Paste the loader snippet in the
<head>of your pages, just after</title>, where the standard tracking code would go. - Remove the standard Convert tracking code from those pages, so the script loads only once.
For example, for account 1000123 and project 1000456 with no environment key, the last line of the snippet reads }("1000123","1000456","");
The snippet shows the page again in whichever of these happens first:
- Convert has applied your changes and shows the page.
- The timeout ends. By default the snippet waits 2500 milliseconds (2.5 seconds), the same limit the tracking script uses for itself. See Choose the Timeout.
- The tracking script fails to load, for example because a browser extension blocks it.
📒Note: The loader snippet already hides the page. Don't add a separate anti-flicker snippet with it, and don't add the async attribute to a standard tracking code tag on the same page.
💡Tip: If you use the Advanced snippet, keep its first <script> block, the one that sets the _conv_ page tag values, and put it above the loader snippet. Replace only the tag that loads the script.
Choose the Timeout
The loader snippet keeps its own cover over the page for at most 2500 milliseconds (2.5 seconds). To change that, add the number of milliseconds as a fourth value in the last line of the snippet, for example }("1000123","1000456","",500); for 500 milliseconds. Choose the value to suit your site:
- A shorter timeout shows the page sooner when the tracking script is slow, but the script is more likely to arrive after the page is visible.
- A longer timeout gives the script more time, but visitors on slow connections wait longer before they see anything.
If the tracking script arrives after the timeout, visitors see the original page first. The script then hides the page briefly while it applies your changes, which is the flashing this snippet is meant to prevent.
The right value depends on what matters more to you:
- Least flicker: keep the default, 2500, the same limit the tracking script uses for itself.
- Core Web Vitals: try
500. In our developers' Lighthouse tests, the page cover lowered Core Web Vitals scores only with longer timeouts, and they suggest no more than 500 milliseconds when scores are the goal. On slow connections, flashing becomes more likely.
Test the value you choose on your own pages, on a slow connection as well as a fast one.
Check Speed Tools That Delay Scripts
Some page speed tools rewrite or delay the scripts on your pages. They can delay the Convert tracking script, or the loader snippet, even when you install it in the <head>, and the page flashes again. If you use one, exclude the Convert script from it.
For Cloudflare Rocket Loader, see Preventing Flicker in Cloudflare Rocket Loader Integration.
Use the Experimental React and Vue Components
For single page apps built with React (including Next.js) or Vue, Convert also publishes two components that load the tracking script async and cover the page the same way the loader snippet does, with a 2500 millisecond default timeout:
@convertcom/tracking-reactfor React and Next.js. In Next.js it works only with client-side rendering: add"use client";at the top of the file that renders the component. See Enhancing Your React Application with Convert A/B Testing Using @convertcom/tracking-react.@convertcom/tracking-vuefor Vue.
⚠️ Caution: Both components are experimental, and their own documentation warns that they can cause flicker if used incorrectly. The synchronous snippet in the <head> remains the recommended install.
Legacy Tracking Script Only: Load It Async
⚠️ Important: This section applies only to projects that still use the legacy tracking script. The legacy script is deprecated, and new installations of it are no longer permitted. Switch to the current tracking script with Migrate from the Legacy Tracking Script to the New Tracking Script (Step-by-Step), then use the async loader in Load the Tracking Script Async Without Flashing.
Your project uses the legacy script if Use Legacy Script is switched on under Configuration > Environments & Tracking Code. Its script address has no /v1/, for example //cdn-4.convertexperiments.com/js/ACCOUNT_ID-PROJECT_ID.js.
To load the legacy script async, put this code in the <head> in place of your current tracking code, keeping your own script address in the last line:
<script>
(function () {
// stops the legacy tracking script from hiding the page a second time
window._conv_prevent_bodyhide = true;
// how long, in milliseconds, the page stays hidden if the tracking script has not shown it yet
var hideTimeout = 500;
var style = document.createElement('style');
style.id = 'convert_hide_body'; // do not change this id
style.textContent = 'body {visibility: hidden !important;}';
document.head.appendChild(style);
setTimeout(function () {
var hide = document.getElementById('convert_hide_body');
if (hide) hide.parentNode.removeChild(hide);
}, hideTimeout);
})();
</script>
<!-- begin Convert Experiences code--><script type="text/javascript" async src="//cdn-4.convertexperiments.com/js/ACCOUNT_ID-PROJECT_ID.js"></script><!-- end Convert Experiences code -->
The first part hides the page and stops the legacy script from hiding it a second time. The legacy script shows the page when it has applied your changes. If it hasn't done so after hideTimeout milliseconds, the snippet shows the page anyway. A shorter timeout shows the page sooner, but makes it more likely that the script arrives after the page is visible.