Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Tag Manager & Optimize Server-Side experiment sending variation

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.

like image 712
SteveGSD Avatar asked Jan 22 '18 16:01

SteveGSD


People also ask

What does Google Tag Manager do?

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.

Do I have Google Tag Manager?

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.

Is Google Tag Manager free to use?

Google Tag Manager delivers simple, reliable, easily integrated tag management solutions— for free.

Where do I find my Google Tag Manager?

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.


1 Answers

You can set Google Analytics variables on page load by using the 'Fields To Set' option in Google Tag Manager.

  1. Open your Universal Analytics Tag in GTM
  2. Click Enable overriding settings in this tag
  3. Click More Settings > Fields to Set
  4. Create a new field called expId. This field should contain the alphanumeric experiment id XXXXXXXXXXX.
  5. 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:

enter image description here

like image 105
Milan Simek Avatar answered Sep 27 '22 19:09

Milan Simek