For all its many benefits, Google Optimize does suffer from one problem: that it is difficult to create segments based on whether a user is participating in an experiment. To resolve this issue, you need to use a customTask to send an event and custom dimension. This is possible both with Google Analytics and Google Tag Manager.

Whereas you can find Experiment Name and Experiment ID dimensions, these are user-scoped throughout the experiment. It is impossible to know which are sessions where users are seeing experiment content and to create the segments around this data.

Luckily, there is a way around this — it involves using a customTask.

Why a customTask?

A customTask allows you to automatically add a Custom Dimension to all the hits that took part in your experiment. This Custom Dimension is session-scoped rather than user-scoped. When necessary, it also sends an event to Google Analytics to ensure that the data is transferred.

This is possible because when a user arrives to the page where the experiment is running, Google Analytics hits contain the &exp; parameter, which has the value of all the experiment IDs and variant IDs in which the user is participating. Google Analytics uses this to know which user is connected to which experiments.

exp key

What you need to do is add a Custom Dimension to every Page View hit with the &exp; key. You do this by applying a customTask to check requests to Google Analytics to see if they contain the key. Then, the customTask needs to add the Custom Dimension parameter with the experiment string.

With this in place, the experiment data will arrive to Google Analytics in the Custom Dimension that you choose.

Adding the Code

If you’re using an analytics.js snippet, simply paste the following code:

<script>
  // Analytics.js snippet
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new

Date();a=s.createElement(o),
m=s.getElementsByTagName(o)
[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXXXXX-Y', 'auto')

  // NEW: Add customTask field to tracker

  ga('set', 'customTask',  function(model) {

    // Change this to the Custom Dimension index to which you want to send the experiment data!
    var customDimensionIndex = '6';

   // Make sure the new hit is only generated once
    var hasNewHitBeenGenerated = false;

    var globalSendTaskName = ' _ ' + model.get('trackingId') + 'sendHitTask';

    var originalSendTask = window[globalSendTaskName] = 
window[globalSendTaskName] || model.get('sendHitTask');
    model.set('sendHitTask', function(sendModel) {
      var ga = window[window['GoogleAnalyticsObject']];
      var hitPayload = sendModel.get('hitPayload');
      if (sendModel.get('exp')) {
        if (sendModel.get('hitType') === 'data' && !hasNewHitBeenGenerated) {
          var tracker = sendModel.get('name');
          originalSendTask(sendModel);
          ga(tracker + '.send', 'event', 'Optimize', sendModel.get('exp'), {nonInteraction: true});
          hasNewHitBeenGenerated = true;
          return;
        }
        if (hitPayload.indexOf('&cd' + customDimensionIndex + '=') === -1) {
          sendModel.set('hitPayload', hitPayload + '&cd' + customDimensionIndex + '=' + sendModel.get('exp'), true);
        }
      }
      originalSendTask(sendModel);
    });
  });
  // NEW BLOCK ENDS

  ga('require', 'GTM-XXXXXX');
  ga('send', 'pageview');
</script>

This code will ensure that the Custom Dimension is dynamically added to any Google Analytics hits sent with the default tracker that have the &exp; parameter. It will also add the relevant experiment ID string.

In addition to this code, you need to change the var customDimensionIndex to match the Custom Dimension index you created. You’ll likely want this to be session-scoped, but you also have the option to use a hit-scoped Custom Dimension if you want it to be more granular.

Using Google Tag Manager

The process is much the same if you are deploying Optimize with Google Tag Manager. The difference is that instead of a Page View hit to deliver the experiment data to Google Analytics, the Optimize tag uses a hitType === ‘data’ hit. As this data hit type is not exposed in Google Analytics, you cannot use it with a Custom Dimension nor to segment visitors. Instead, you need to send an extra event.

To start, you need to create a new Custom JavaScript variable like this:

function() {

return function(model) {

// Change this to the Custom Dimension index to which you want to send the experiment data!

var customDimensionIndex = ‘6’;

// Make sure the new hit is only generated once

var hasNewHitBeenGenerated = false;

var globalSendTaskName = ‘ _ ‘ + model.get(‘trackingId’) + ‘sendHitTask’;

var originalSendTask = window[globalSendTaskName] =

window[globalSendTaskName] || model.get(‘sendHitTask’);

model.set(‘sendHitTask’, function(sendModel) {

var ga = window[window[‘GoogleAnalyticsObject’]];

var hitPayload = sendModel.get(‘hitPayload’);

if (sendModel.get(‘exp’)) {

if (sendModel.get(‘hitType’) === ‘data’ && !hasNewHitBeenGenerated) {

var tracker = sendModel.get(‘name’);

originalSendTask(sendModel);

ga(tracker + ‘.send’, ‘event’, ‘Optimize’, sendModel.get(‘exp’), {nonInteraction: true});

hasNewHitBeenGenerated = true;

return;

}

if (hitPayload.indexOf(‘&cd’ + customDimensionIndex + ‘=’) === -1) {

sendModel.set(‘hitPayload’, hitPayload + ‘&cd’ + customDimensionIndex + ‘=’ + sendModel.get(‘exp’), true);

}

}

originalSendTask(sendModel);

});

});

Give the variable a name like {{JS – customTask – Optimize experiment}}. Next, head to the Google Optimize tag. In “Fields to set,” create a new field with the name “customTask” and set the value as the name of your variable.

With this set up, when you go to your experiment pages, you should see a ‘data’ type hit followed by an event hit:

Once you can start creating segments for sessions, your Optimize data has much more meaning. For instance, you can create other segments to view more than just the conversion goals and funnels you have in Optimize. This takes the capabilities of Optimize to another level.