👈 Home

Install Google Optimize when you've got Google Analytics and Google Tag Manager


If you’re using Google Analytics and Google Tag Manager together, it’s natural to add Google Optimize via GTM as well.

It might work just fine, but you might see unwelcome flicker once you’ve actually built an experiment:

A terrible experiment in so many ways

This happens when the page loads and displays faster than the Google Optimize snippet can load, execute, and make changes. It’s not a great experience, and if it’s bad enough it can compromise your results 😔

If you have this issue, you can combat it with the anti-flicker snippet.

You can’t install that snippet with GTM, though; it has to be installed inline (directly on the page).

And as long as you’re installing scripts inline to make Google Optimize work faster, you might consider installing Google Optimize inline, too.

If you do, it’s as simple as following these instructions with one additional caveat.

Google Optimize will provide you with a code block to add to your page. Something like this:

There’s one issue here - in addition to loading Google Optimize, the call to gtag('config', ...) will send a page view to Google Analytics.

That, plus the page view you’re sending via the Google Analytics Tag in Google Tag Manager, means you’ll be double counting page views 🤦‍♂️.

The fix is to add an extra parameter to your gtag('config', ...) call:

This updated code goes inside the red curly braces. It adds a comma after the closing quote on your Google Optimize container ID, then appends

'send_page_view': false

It prevents the page view from being sent to Google Analytics, so you can leave your Google Tag Manager setup undisturbed.

You’ll find that Google Optimize loads faster, and you won’t find a jumbled mess next time you log in to Google Analytics.

(You can read more about 'send_page_view': false in the gtag.js docs.)


    © 2024 Brian David Hall