Using Drupal tokens as Google Analytics custom dimensions

 Custom dimensions are an incredibly powerful tool to capture non-Google Analytics data and merge it with your Google Analytics data. Combined with Drupal tokens, this tool can very easily bring Drupal data into your analytics account.

Take this scenario: You want to query Google Analytics data by your Drupal content type. An example of where this could be useful is if you want to run reports for your blog posts, but your blog does not have a standard URL directory, i.e., “/blog” that you can easily query. However, your blog posts do have the same content type in Drupal.

Let’s get started. First, you’ll need to make sure you have the Google Analytics module installed in Drupal and you have permission to add custom dimensions in Google Analytics.

Create the custom dimension

Custom dimensions are property-based. In Google Analytics, go to Admin. Under your property, click on Custom Definitions and then Custom Dimensions.

Add a new custom dimension. Let’s call it Drupal Content Type. Our dimension will be set on each hit, so set the scope to Hit.

Google Analytics will give you a code, but if you have the module installed, this won’t be necessary. All you need to remember is the index number. In this case, it is “1.”

Adding the Drupal token

In Drupal, go to the Google Analytics module. Under Custom Dimensions, go to the index number for the custom dimension you just created.

Set the value to [node:content-type]

That’s it. Reload your page and verify that the code is firing. In the source code, where the Analytics code appears, you should see something like “ga(“set”, “dimension1”, “Blog Post”);”

After a little bit, you will start to see the data flowing into you Analytics account.

Posted date & other dimensions

There are many other great use cases for custom dimensions.

Let’s say you want to store the date a blog article was posted in Google Analytics. Simply set the custom dimension to

[node:created]

or another similar token such as

[node:created:short]

If you ever need to connect data sets, you might need the Drupal node ID stored in Google Analytics data. Here, simply use:

[node:nid]

You may also want to query your Google Analytics data based on a blog author. Unfortunately, the Drupal module will not allow you to use the [node:author] or [node:author:uid] token because this type of data is considered personally identifiable information, which cannot be stored in Google Analytics.

You may be able to find some workarounds with using the user create date (provided they are unique):

[node:author:created]

Alternatively, you could use Google Tag Manager to pass custom data to Google Analytics that is unidentifiable.

You may also like