Install Optimize with the global site tag (gtag.js)

Note: If you use Google Tag Manager, follow these instructions instead.

Step 1: Install the global site tag with Analytics

Note: If you've already tagged your website with the global site tag and Analytics, skip ahead to Step 2 to add your Optimize container ID.

To install Optimize, you need the global site tag (gtag.js) with Google Analytics. You can get the global site tag from the Google Analytics admin console (Admin > Property column > Tracking Info > Tracking Code) or by customizing the example below.

Example: Global site tag with Google Analytics

The following tag loads the gtag.js library, establishes GA_TRACKING_ID as the default Google Analytics property ID, and sends a pageview hit to Google Analytics.

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

  gtag('config', 'GA_TRACKING_ID');
</script>

 

Note: Replace GA_TRACKING_ID with your Google Analytics tracking ID.

Learn more about how to set up Analytics tracking in the Analytics help center.

Step 2: Add Optimize to the global site tag

Once you have the global site tag (gtag.js) with your Google Analytics tracking ID, you need to add your Optimize container ID to the gtag('config' line, e.g.:

   gtag('config', 'GA_TRACKING_ID', { 'optimize_id': 'OPT_CONTAINER_ID'});

You can get your customized global site tag populated with your Analytics and Optimize IDs in Optimize (under Container > Container setup > Install Optimize).

Example: Global site tag with Google Analytics and Optimize

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

  gtag('config', 'GA_TRACKING_ID', { 'optimize_id': 'OPT_CONTAINER_ID'});
</script>
 

Note: Replace GA_TRACKING_ID with your Google Analytics tracking ID and replace OPT_CONTAINER_ID with your Optimize container ID.

Step 3: Install the global site tag on your website

To install the global site tag on your website paste the code snippet immediately after the <head> tag on every page that you wish to optimize.

  • If the global site tag is already installed on your website – add your Optimize container ID (see Step 2 above) and move your existing tag to the top of the <head>.
  • If the global site tag isn't installed on your website – install it at the top of the <head> on every web page you wish to optimize.

Note: Only one global site tag is needed per page.

Snippet placement best practices

For optimal performance place the global site tag at the very top of the <head> tag on every page you wish to optimize.

The only items that should appear above the global site tag are:

  1. Data layer initialization. Note: the dataLayer variable should never be reassigned after the page-hiding snippet (when used).
  2. Any scripts that declare JavaScript variables, functions, or set cookies that you wish to use in Optimize experiences. For example jQuery or any JavaScript library that you wish to use in implementation or targeting. Note: jQuery isn't installed by Optimize by default.
  3. The optional anti-flicker snippet which can be used to avoid flicker.
Was this article helpful?
How can we improve it?