I\'m using the Google Tag Manager container for managing scripts. I\'m trying to perform a server-side Optimize/Analytics experiment. I require server-side for performance r
You can set Google Analytics variables on page load by using the 'Fields To Set' option in Google Tag Manager.
Enable overriding settings in this tag
More Settings > Fields to Set
expId
. This field should contain the alphanumeric experiment id XXXXXXXXXXX
. expVar
. This field should contain the experiment variant number (0 for original, 1, 2, 3 etc for custom versions)Important: Make sure that the optimize tag get's triggered before the analytics tag.
In my case I used a Custom Javascript
variable for the expId
and expVar
fields, which used some custom code to get the correct experiment ID and version ID.
I figured the field names out by checking out the 'Analytics Field Reference' page:
https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#expId
https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#expVar
This method is probably preferred over your own answer, since it doesn't require any extra events to be triggered. Besides that you can configure this in GTM completely.
Screenshot for reference:
Solved by explicitly specifying a tracker id.
I used Analytics Debugger Chrome by Google to debug the issue and found that Google Tag Manager (GTM) made the tracker id gtm1
so I had to prefix things with that.
To find out your tracker ID, call ga.getAll()[0].get('name')
(may be gtm1
, gtm2
, etc.).
Changed my setGAExperiment function to the following
function setGAExperimentCX(_expId, _vId){
ga('gtm1.set', 'exp', _expId.toString() + '.' + _vId.toString());
// this forces the above exp set to be sent to GA, you can name the event whatever you want with whatever values you want
ga('gtm1.send', 'event', 'Experiment', 'Trigger', _expId.toString() + '.' + _vId.toString());
}
The function that calls setGAExperimentCX is
function performNewCartExp(_vId) {
if (typeof ga == "undefined") {
if (_performNewCartExp != undefined) { clearTimeout(_performNewCartExp); }
_performNewCartExp = setTimeout(function () { performNewCartExp(_str); }, 250);
} else {
setGAExperimentCX('XXXXXXXXXXX', parseInt(_vId, 10));
}
}
The root cause of this seems to be that when you setup your events with Tag Manager and use the Google Analytics Settings Variable the experiment id and variant aren't sent with the events you setup in GTM. For some reason it just doesn't pick up on the experiment you've set from the server.
When you would setup you GTM to not work with the GA settings variable but put your settings in every GA-tag separately it does work.
We have the same implementation of Optimize, GA and GTM on two different websites but on two different GTM containers. One is setup with a GA settings variable and one with the GA settings in every tag separately. On the one with the GA settings variable the initial setup didn't work while it did work on the other.
Milan's answer solved it.
With Milan's answer you wouldn't have to put your experiment id and variant in the ga('set', 'exp', ...)
. Putting it in the datalayer and then using them as variables should also work I expect (haven't tested it yet). Advantage is that you don't have to work with some exotic custom javascript to get that id and variant out again.