3 ways to send data to custom metrics in Google Analytics

Using Google Analytics may be challenging when it comes to something which is not implemented by default. Good news is that Google Universal Analytics lets you do a lot of stuff which is not there by default. For example, create your own metrics. In this tutorial I’ll show you how to populate custom metrics with data.

If you don’t know, how to create custom metrics, here’s a quick how-to:

Once you created your custom metrics, you need to populate them with useful data. Here are 3 ways to do that.

Attention! The instruction applies to the latest version of Google Analytics - Universal Analytics (if you Google Analytics snippet contains “analytics.js”, then you are using Universal Analytics; if “ga.js” - then you have old version of Google Analytics installed and I recommend you migrating to newer one).

If your Google Analytics snippet is installed directly in the site code

Which is the case for most small businesses and personal sites. In this case, execute following code when you need to send value to custom metric:


ga('set', {
'metric1': 1,
});

1 in ‘metric1’ is your metric index (you can see it in you custom metrics list).

If you are using Google Tag Manager to integrate your site with Google Analytics

Execute following code on your page, when you need to send value to custom metric:


window.dataLayer = window.dataLayer || [];
dataLayer.push({'event': 'clickOnAd'});

The above code will send an event to GTM. To send data from GTM to your Google Analytics, do following:

  1. Create a trigger (rule) of type “Custom Event” and put 'clickOnAd' (without quotes) as an event name.
    Google Tag Manager trigger
  2. Create a tag, which will send info about the click to your custom metrics. Settings: “Google Analytics”, “Universal Analytics”, track type - “event”, category - “ad”, action - “click”, custom dimension index: 1, value: 1. In the “Fire on” section choose trigger you just created.
  3. Don’t forget to publish your GTM container with new settings.

Through measurement protocol

To send custom metric data through measurement protocol, use following payload data template:
v=1&tid=UA-PROPERTY-ID&cid=GOOGLE-CLIENT-ID&t=event&ec=ad&ea=click&dl=CONVERSION-URL&cmN=1

To actually send data, you will need to change several things in the payload data template:

  1. N in cmN to whatever indexes of your metrics are. Example: cm1=1
  2. UA-PROPERTY-ID to your Google Analytics property id. Example: tid=UA-64099875-1
  3. GOOGLE-CLIENT-ID to Google Client ID. Example: cid=1693898973.1439735546 (you can get Google Client ID from “_ga”; in this cookie it will look like “GA1.2.1693898973.1439735546”, you need to omit “GA1.2.” when using client id in measurement protocol)
  4. CONVERSION-URL to whatever page you want to tie conversion to (this parameter is not required, but if you don’t use it, you won’t see the conversion in content reports). Example: dl=http%3A%2F%2Fstony.me%2Fservices%2F
  5. in “ec” and and “ea” parameters you can use whatever values you want.

This is pretty much it. I hope this tutorial will help you make more use of your Google Analytics account.

If you see this tutorial can be improved in some way, I will be happy to hear that.

If you like my articles,