UPDATE: Read about Google’s latest direction for installing the Google Optimize snippet.

https://www.idimension.com/2020/04/install-google-optimize-with-one-line-of-code/

You have options to deploy the Google Optimize snippet

1) Optimize + Google Analytics tracking code direct on page

Order of tags on the page should be:

  1. Any JavaScript (e.g. jQuery) that you wish to use in Optimize experiments. We recommend you keep these to a minimum.
  2. Initialization of any variables needed for targeting (data layer, JavaScript, cookies, etc).
  3. Page-hiding snippet (place after the META CHARSET declarations).
  4. Modified Analytics tracking code with Optimize plugin (in the HEAD).
  5. Tag Manager container snippet.
  6. Other JavaScript, trackers, and ad tags.

2) Optimize direct on page, Google Analytics injected via tag management solution

Same order of tags as noted above – with one exception:  you remove the

ga('send', 'pageview');

line from the “Modified Analytics tracking code with Optimize plugin” when you fire Analytics tags via Tag Manager.

You can also target variables that are set after Optimize plugin loads, for example with dynamic pages or web apps, by using Activation Events.

3) Deploy Optimize using the “Global Site Tag (gtag.js)

Same order of tags as noted above – with one exception: in the “Modified Analytics tracking code with Optimize plugin” you change the

gtag('config', 'GA_TRACKING_ID');

line to

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

Be sure to replace GA_TRACKING_ID with your Analytic tracking ID and OPT_CONTAINER_ID with your Optimize container ID.

NOTE on option 3:  Using analytics.js is the fastest and recommended way to deploy Optimize. Global Site Tag (gtag.js) may not be as performant with Optimize as analytics.js.