Google has given us a great analytics tool with Google Analytics. But sometimes you have to connect information from different parts of their documentations to find a solution. Tracking a hit scope custom dimension or metric is an example of this. In this post, I’ll connect the dots for you, so you don’t have to.

The Google documentation

Let’s start with the issue. If you read Google’s official documentation on a hit scope custom dimension, this is the code example they give you:

ga('create', 'UA-XXXX-Y', 'auto');

  // Set value for custom dimension at index 2.
  var day = getDayOfTrial();
  ga('set', 'dimension2', day );

  // Send the custom dimension value with a pageview hit.
  ga('send', 'pageview', '/level_1/');
  

The code works but assumes that you won’t measure anything after the hit that measures the custom dimension. The ga('set') function sets the fields for all hits that follow on that page. Because of this, each hit will contain the custom dimension data. In the next example, I’ll show you what happens if you use a similar setup with a second measurement:

ga('set', 'dimension100', 'test');
  ga('send', 'event', 'test', 'hit', '1');

  ga('send', 'event', 'test', 'hit', '2');
  

When you add two hits after the custom dimension code, both will have the custom dimension data:

//screenshot

It would result in incorrect data. The impact may be bigger for metrics than for dimensions. With a hit scope dimension, you might want to add it to all hits anyway (e.g. a page type dimension). With metrics, the impact is bigger. The reported data will be multiplied with every call you add it to unintentionally.

How to fix it

One way to remove custom data for future hits is by setting a dimension as an empty string '', or a metric as 0.

ga('set', 'dimension100', 'test');
  ga('send', 'event', 'test', 'hit', '1');
  ga('set', 'dimension100', '');

  ga('send', 'event', 'test', 'hit', '2');
  

However, this still adds the information to your Google Analytics call:

//screenshot

To completely remove it, you have to set it as undefined:

ga('set', 'dimension100', 'test');
  ga('send', 'event', 'test', 'hit', '1');
  ga('set', 'dimension100', undefined);

  ga('send', 'event', 'test', 'hit', '2');
  

As you can see, the cd100 paremeter is gone now.

// screenshot

This fixes the issue, but if you expand on your implementation, you quickly realise it’s not scalable. Let’s assume we have 3 custom dimensions and 3 custom metrics we want to track for a given hit:

ga('set', 'dimension100', 'test');
  ga('set', 'dimension101', 'test');
  ga('set', 'dimension102', 'test');
  ga('set', 'metric100', 1);
  ga('set', 'metric101', 1);
  ga('set', 'metric102', 1);
  ga('send', 'event', 'test', 'hit', '1');
  ga('set', 'dimension100', undefined);
  ga('set', 'dimension101', undefined);
  ga('set', 'dimension102', undefined);
  ga('set', 'metric100', undefined);
  ga('set', 'metric101', undefined);
  ga('set', 'metric102', undefined);

  ga('send', 'event', 'test', 'hit', '2');
  

We need 12 lines of code to properly capture 6 custom data fields. Luckily, there’s a more scalable solution.

The scalable solution

The best way to track hit scope custom data is by adding it to fieldsObject of the actual hit:

ga('send', 'event', 'test', 'hit', '1', {
    'dimension100': 'test',
    'dimension101': 'test',
    'dimension102': 'test',
    'metric100': 'test',
    'metric101': 'test',
    'metric102': 'test',
  });

  ga('send', 'event', 'test', 'hit', '2');
  

With this setup, you minimise the code required to track the 6 custom data fields.

How about other scopes?

When you use a custom dimension, you can set it as a session or user scope dimension. With either setup, the issue of this post not that relevant. If you set the dimension, you’ll want any future hit to attributed to the value anyway (normally, data processing in Google Analytics handles this for you). If you want to be on the safe side, using the scalable setup of this post won’t hurt your session or user scope dimensions.

You can track custom metrics on a product scope. To correctly track this scope, you have to add it as product information to your enhanced e-commerce product data. If you don’t set it up correctly, it won’t track the data. So no issue here.

Connecting the dots

With Google Analytics documentations, answers to your questions are sometimes spread out over several pages. If you find an answer this way, please share it. We can all benefit from it.

Leave a Reply