AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

3.5K members online now
3.5K members online now
Discover how to select who is eligible to be in your experiment, and when they’re served experiment variations.
Guide Me

Optimize test not firing on custom event.

[ Edited ]
Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hello!  I have set up my experiment to fire on custom event (gtm.historyChange) and a url.  This has worked in the past.  But now I have noticed that my experiment is only firing on page load.  So when I navigate to a route in my spa app the experiment never fires.  If I reload the browser, the experiment does fire.  I have confirmed through my console debugging that gtm.historyChange event is being pushed to the datalayer on each route change and page refresh.  Any ideas on why this is not working anymore?  Did something change with Optimize?



1 Expert replyverified_user

Optimize test not firing on custom event.

Google Employee
# 2
Google Employee

Perhaps there are timing issues and the gtm.historyChange happens just before the page content is actually build?


Are you just using the GTM's native gtm.historyChange event?

Is there anywhere some javascript where you could add a small delay (setTimeout 0 to fire the event)?

One idea that may be worth trying, would be to use mutation observers instead of gtm.historyChange.

You could add perhaps the code below as a HTML tag in GTM and activate experiments with the default optimize.activate event.


This will trigger activation, just after the page changes for any reason (regardless of changes in the URL) and it maybe used to even target temporary dialogs etc.


if (MutationObserver) {
 new MutationObserver(function(){
   setTimeout(function() {

       dataLayer.push({'event': 'optimize.activate'});

   }, 0);
 }).observe(document.body, {subtree: true, attributes: true, characterData: true});



If you still can't figure out a solution, could you share your container ID?

Optimize test not firing on custom event.

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Hi Dimitris,

I tried the solution of adding the MutationObserver script via GTM.  It works a little better, but it's still not firing when I click the back button (even though I do see the optimize.activate event being pushed to the dataLayer).  It's also flickering before it shows which I didn't experience when using gtm.historyChange.  One thing to note is that I'm using Nuxt with server-side rendering.  I'm not sure if that's adding to the issue.  My container id is GTM-KZG2C4R - any help would be appreciated!!

Optimize test not firing on custom event.

Google Employee
# 4
Google Employee



If you use server side rendering, I am not sure that you even need custom activation.

Try the following: navigate through the site going back to the page with the experiment from different paths. Every time use the "view page source" menu option from the browser and search for the content that you are trying to change.

If this does exist in the page source, you shouldn't need to use custom activation.