Install Optimize with the Google tag (gtag.js)

Deploy the Optimize snippet on your website with the Google tag.
This article includes information about a legacy snippet that isn't supported for new installations.
The Optimize installation overview Includes instructions on how to install Optimize on your website.

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

Step 1: Install the Google tag with Analytics

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

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

Example: Google 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.

<!-- Google tag (gtag.js) -->
<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 Google tag

Once you have the Google 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 Google tag populated with your Analytics and Optimize IDs in Optimize (under Container > Container setup > Install Optimize).

Example: Google tag with Google Analytics and Optimize

<!-- Google tag (gtag.js) -->
<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 Google tag on your website

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

  • If the Google 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 Google 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 Google tag is needed per page.

Snippet placement best practices

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

The only items that should appear above the Google 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 helpful?
How can we improve it?
Search
Clear search
Close search
Google apps
Main menu
Search Help Center
true
101337
false
false