问题
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 reasons. I've performed client-side experiments just fine with the GTM/Optimize containers.
Here's my GTM code:
window.dataLayer = window.dataLayer || [];
....
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXXX');</script>
<!-- End Google Tag Manager -->
I've tried the following different methods to trigger which variation to trigger.
// These fn get called only once GA exists via setTimeout. They get called correctly.
// I've setup the experimentTrigger via GTM container and it triggers correctly to Analytics.
function setGAExperiment1(_expIdvId){
// Matches ga('set', 'exp', '$experimentId.$variationId');
// https://developers.google.com/optimize/devguides/experiments
ga('set', 'exp', _expIdvId);
dataLayer.push({'event': 'experimentTrigger', 'exp': _expIdvId }); // to trigger data send of exp
// I receive the experimentTrigger event with 'exp' value on Analytics but not any experiment data into Optimize/Analytics.
}
function setGAExperiment2(_expIdvId){
// Matches ga('set', 'exp', '$experimentId.$variationId');
// https://developers.google.com/optimize/devguides/experiments
ga('set', 'exp', _expIdvId);
ga('send', 'event', 'experiment', 'view'); // to trigger data send of exp
}
I'm not receiving any experiment data in Google Optimize or Google Analytics -> Behaviors -> Experiments like I should be. How can I fix this?
The closest discussion I've found to this topic is here and here but no concrete answers.
回答1:
You can set Google Analytics variables on page load by using the 'Fields To Set' option in Google Tag Manager.
- Open your Universal Analytics Tag in GTM
- Click
Enable overriding settings in this tag
- Click
More Settings > Fields to Set
- Create a new field called
expId
. This field should contain the alphanumeric experiment idXXXXXXXXXXX
. - Create a new field called
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:
回答2:
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));
}
}
回答3:
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.
来源:https://stackoverflow.com/questions/48386350/google-tag-manager-optimize-server-side-experiment-sending-variation