I'm trying to use Google Optimize on a Drupal 8 site through Google Tag Manager. I have set up the tags in Google Tag Manager through Google's UI and used the Google Tag Manager module to add the snippets to my site on path /test/*.

However, according to the Google Optimize documentation, I should also add a "page-hiding snippet":

<!-- Page hiding 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),'')};

There are three requirements here.

  1. The snippet needs to come before the Google Tag Manager tag in the HEAD of the page.
  2. The snippet should only be shown on pages with path /test/*
  3. Inside the snippet, the GTM-XXXXXX needs to be different for dev/staging/live environments.

Is there a way to standard way to set this up? I assumed that Google Optimize/Analytics is popular enough there would be some information for integrating it with Drupal, but all I found was this DrupalCon 2019 seminar that hasn't happened yet.

Setting up Google Tag Manager was fine, but I'm lost at how to get this final bit in the right place.

1 Answer 1


Well, since posting my question, there is now a Google Optimize Hide Page module, which provides a code example of how to do this.

There is also a patch to add this functionality to the Google Tag Manager module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.