We use cookies. You have options. Cookies help us keep the site running smoothly and inform some of our advertising, but if you’d like to make adjustments, you can visit our Cookie Notice page for more information.
We’d like to use cookies on your device. Cookies help us keep the site running smoothly and inform some of our advertising, but how we use them is entirely up to you. Accept our recommended settings or customise them to your wishes.

CRO Best Practice - Implementing Google Optimize

How should I implement Optimize?

There are a couple of ways to implement Optimize. It can either be implemented via GTM, or it can be hardcoded straight onto the site. As best practice - and to provide the best experience for your users - it is strongly recommended that the Optimize snippet is hardcoded on the site.

The Optimize snippet is just a modified version of the Google Analytics tracking code to load the Optimize plugin.

There are currently 2 versions of the Optimize Snippet:

    1. The classic analytics.js snippet
    2. The newer, more recent gtag.js snippet

The correct version to use depends on how GA has been implemented on the site:

    1. Via analytics.js library (hardcoded)
    2. Via gtag.js (hardcoded)
    3. Via GTM (using a Tag manager)

In this article, we’ll dive into the best practice implementation of hardcoding the Optimize snippet onsite, and how to approach this depending on your setup.

Pre-requisites for Google Optimize

Before you start, make sure you have the following:

  • An Optimize account set up
  • GA installed on your site
  • Access to the site code or to developers that can make changes
  • Basic HTML/JavaScript editing experience


What is the Anti-Flicker Snippet?

The Anti-flicker snippet (also commonly known as the Page Hiding snippet) is used to minimize page flicker that users could experience while the changes are being applied. This will temporarily hide the page, for up to 4 seconds, while the Optimize container loads. The timeout setting is adjustable if necessary.

Regardless of the chosen implementation method, it is strongly recommended to also implement the Anti-flicker snippet. This will prevent users seeing a flicker on the page while the Optimize changes are applied and ultimately help prevent users seeing the original variation before the changes appear.

Anti-flicker snippet

Anti-flicker snippet



<!-- Anti-flicker snippet (recommended)  -->
<style>.async-hide { opacity: 0 !important} </style>
<script>(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};


Replace 'OPT_CONTAINER_ID' with your Optimize Container ID.

This should be placed immediately before the Optimize snippet. The dataLayer should never be reassigned any time after the anti-flicker snippet.


1. I’ve hardcoded GA via analytics.js

If Analytics has been hardcoded on site using the analytics.js library, simply add a line of code to your Google Analytics tracking code to load Optimize: ga('require', 'OPT_CONTAINER_ID');


Analytics.js snippet


<!-- Modified Analytics tracking code with Optimize plugin -->
     (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      ga('create', 'UA-XXXXXXXX-X', 'auto');
     ga('require', 'OPT_CONTAINER_ID');
     ga('send', 'pageview');


Add the code to every page of your website as high up in the <head> tag as possible and make sure to:

  • Replace 'OPT_CONTAINER_ID' with your Optimize Container ID (make sure this is your Optimize Container ID and not your GTM ID)
  • Replace and update UA-XXXXXXX-X with your GA Property ID


2. I’ve hardcoded GA via gtag.js

If Analytics is hardcoded via the gtag.js library, simply add in the following parameter in your Google Tracking code to load the Optimize  container: {'optimize_id': 'OPT_CONTAINER_ID'}


Gtag.js snippet


<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async="" src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-XXXXXXXX-X', {'optimize_id': 'OPT_CONTAINER_ID'});


Add the above code to every page of your website as high up in the <head> tag as possible. 

Make sure to:

  • Replace UA-XXXXXXXX-X with your Google Analytics tracking ID
  • Replace OPT_CONTAINER_ID with your Optimize container ID.


3. I’ve implemented GA via GTM

If GA has been implemented via GTM, then either one of the above snippets can be used (Google recommends the newer gtag.js snippet).

Important:  When implementing GA via GTM, it’s important to make sure that the custom trackers between the GA and Optimize tags match.

This can be found in the Google Settings variable in GTM.

If any one of the following are specifically set in the Settings variable, then this must also be included in the Optimize tag:

  • clientId
  • sampleRate
  • siteSpeedSampleRate
  • alwaysSendReferrer 
  • allowAnchor
  • allowLinker
  • cookieName
  • cookieDomain
  • cookieExpires
  • cookiePath
  • legacyCookieDomain
  • legacyHistoryImport
  • storeGac

GA Setting Variable


Analytics.js GTM

Note: The pageview command has been removed


gtag.js GTM


Order of Snippets

During implementation, it is very important to have the order of the snippets set up correctly. The only thing that should appear before the Optimize snippet is:

  • Any data layer initialization.
  • Any scripts that declare JavaScript variables, functions, or set cookies that you wish to use in Optimize experiences. This includes jQuery or any JavaScript library that you wish to use in implementation. (Note: jQuery isn't installed by Optimize by default.)
  • The anti-flicker snippet which can be used to avoid flicker.

The order of the snippets should be as follows: 

Order of snippet


In summary…

The Optimize snippet is a line of code added to your existing Google Analytics code snippet that loads the Optimize container. Our best practice implementation for Optimize is hardcoding the snippet straight onto the site – how you approach this is dependent on how Google Analytics has been implemented on the site.

Although, the Anti-flicker snippet is optional, we highly recommend having this to prevent any page flickering and to provide your visitors with a seamless user experience.

Now that you’ve implemented Optimize, you can start running some exciting A/B tests and personalised experiences on your site!