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.
Google Tag Manager is a tag management system (TMS) that allows you to quickly and easily update measurement codes and related code fragments collectively known as tags on your website or mobile app.
Check the website's source code by right-clicking on any of the web pages and selecting 'View page source', then find the GTM container code. If it is present, that means Google Tag Manager is working. 2. Use Google Tag manager's preview and debug mode.
Google Tag Manager delivers simple, reliable, easily integrated tag management solutions— for free.
When you're in the Google tag section of your Google Ads or Google Analytics account, your tag ID is displayed under the Google tag on the left side. A single tag can have multiple tag IDs. Sometimes multiple tag IDs are displayed after combining tags.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With