Search
Clear search
Close search
Google apps
Main menu

Custom dimensions with Google Tag Manager

Using a Lookup Table

Have you ever needed to associate a list of values (such as “Morning,” “Afternoon,” and “Evening,”) to analytics sessions? You can easily do this by setting up a Custom Dimension in Google Analytics using a Lookup Table variable in Google Tag Manager.

Step 1

The first step is to create a Custom Dimension in Google Analytics. To do so, log in to your Google Analytics account, and under ‘Admin,’ search for the ‘Custom Definitions’ section under the property menu. From there, select ‘Custom Dimensions’ and click to add a new dimension.

In this case, we’ll name it ‘Time of Day’ and set it to a session scope. Alternatively, if you'd only like the referring source to be credited for the first pageview of a session, you can set the scope as a 'Hit' scope.  Ensure that it is checked 'Active' and click create.

Once you’ve created a new dimension, you should be given an example code. This is where you should check the dimension number - in the below example you can see that it is ‘dimension1.’ Remember this, as it is the same dimension number you will want to use in your Custom Dimension settings in Google Tag Manager for the ‘Index’ input later.

Step 2

Next, you’ll need to log into Google Tag Manager to set up the needed user-defined variables.

First, we’ll create a variable that tells Google Tag Manager when to start collecting information. In this case, we’ll use the URL as the key, so the first step is to create a URL variable:

  1. Create a new user-defined variable and choose the ‘URL’ type.
  2. We suggest you name it according to the type of variable. In this case, we’ll call it ‘url fragment.’
  3. Choose the component type. We want to use an indicator from the URL, so we’ll use the hashtag sign by using the ‘Fragment’ component type in Tag Manager. This means that when Google Tag Manager detects a hashtag in the URL, it will fire this variable.
  4. Save the variable.

Step 3

Next, you’ll want to create a Lookup Table of your values in Google Tag Manager:

  1. Create a new user-defined variable and choose the ‘Lookup Table’ type.
  2. We suggest you name it according to the type of table (in this case, we’ll use ‘time of day.’)
  3. Choose the input variable that the lookup table will reference to match the appropriate value. In this case, we want to assign a number to a URL and use that number to correspond to a time of day, so we will use the {{url fragment}} variable that we created in the previous step as our key. Again, this means that anything after the “#” sign will be collected as part of the input. A note of caution here: the input needs to exactly match the URL fragment; otherwise, the lookup will not return a valid output. If you were to have more characters after your input variable following the “#,” the output will be invalid.
  4. Fill out the table. In this case, we are separating the time of day output into “Morning,” “Afternoon,” and “Evening.” We also want to have an input called ‘Test’ so that we can troubleshoot. You can see how we set this up in the screenshot below.
  5. Save the variable.

Step 4

The final setup piece is to enable a Custom Dimension in your Universal Analytics tag(s) that are already setup in Google Tag Manager.

For each analytics tag you have set up in Google Tag Manager, expand the ‘More settings’ menu, then the ‘Custom Dimensions’ menu, and click ‘Add Custom Dimension.’

The ‘Index’ input field refers to the Custom Dimension slot in Google Analytics (the same one you created in Google Analytics the first step), and the ‘Dimension Value’ input field refers to the variable you want to input(which in this case, is the {{time of day}} variable you created in the previous step). The Index number will vary depending on other Custom Dimensions you may have set up in Google Analytics; but, in general, it should look like:

Once you’ve set up the index number, click save. To test the new variable you’ve built, load your webpage with “#7” appended to the end of your URL. Then, using Google Tag Manager's Preview Mode, open the ‘Page Load’ tab and look through the Variables menu. You should see the ‘time of day’ and ‘url fragment’ variables fire with ‘Test’ and ‘7’ as values (7 comes from after the hashtag in your URL and ‘Test’ corresponds to ‘7’ from your lookup table). It will look similar to this screenshot:

You’ve now done all of the necessary setup in Google Tag Manager and, once published, should see your new Custom Dimension data in Google Analytics.

Using the Data Layer

If you are already using the Data Layer to capture key page elements, it’s easy to create a custom dimension using these Data Layer inputs.

For this example, let’s imagine you are running an A/B test on your website and you want to test a new homepage header image. You have two versions of this page and, for each, you hard-code a dataLayer element for the experiment ID and the experiment test name. Your control variation will have an experiment ID of ‘10001’ and the variation will have an experiment ID of ‘10002’. They will both have an experiment name of ‘Homepage Hero Test’.

You want to send both the experiment name and ID to Google Analytics as a single custom dimension, so that you can analyze your data by this dimension. Here is an example of the dataLayer for the control:

dataLayer = [{
   'experimentID': '10001',
   'experimentName': 'Homepage Hero Test'
}];

And here is an example of the dataLayer for the variation:

dataLayer = [{
   'experimentID': '10002',
   'experimentName': 'Homepage Hero Test'
}];

Step 1

Once you’ve got your dataLayer elements hard-coded on your experiment pages, you’ll want to set up Google Analytics for a new Custom Dimension. To do so, log in to your Google Analytics account, and under ‘Admin,’ search for the ‘Custom Definitions’ section under the property menu. From there, select ‘Custom Dimensions’ and click to add a new dimension.

In this case, we’ll name it ‘Experiment’ and set it to a session scope. Ensure that it is checked “active,” and click “create.” You will be given some example code with a ‘dimension#’. That number corresponds to the Index slot you will use in the next step to set up the new Custom Dimension in Google Tag Manager.

Step 2

Next, you’ll need to log into Google Tag Manager to set up the needed user-defined variables.

First, we’ll want to create a variable that will tell Google Tag Manager when to start collecting information. In this case, we are going to use the Data Layer element as that key, so the first step is to create a Data Layer variable:

  1. Create a new user-defined variable and choose the ‘Data Layer Variable’ type.
  2. We suggest you name by its variable type. In this case, we’ll create two separate variables and call one ‘experimentID’ and the other ‘experimentName’.
  3. Choose the component type. Since we are using the dataLayer element ‘experimentID’, we’ll write the variable to match it (which, in this case, is {{experimentID}}). We’ll do the same thing for ‘experimentName’ so that it uses {{experimentName}}. This means that when Google Tag Manager reads the dataLayer element of ‘experimentID’, it will return the value associated with it (which, in this example, is either ‘10001’ or ‘10002’).

Save the variable.

Step 3

Next, we’ll create the Custom Dimensions in our Google Analytics tag within Google Tag Manager. In the main Universal Analytics tag, expand the ‘More settings’ menu, then the ‘Custom Dimensions’ menu, and click ‘Add Custom Dimension.’

The ‘Index’ input field refers to the Custom Dimension slot in Google Analytics (the same one we created in Google Analytics the first step), and the ‘Dimension Value’ input field refers to the variable we want to input (which, in this case, is the {{experimentID}} and {{experimentName}} variables we created in the previous step). The Index number will vary depending on the other Custom Dimensions we may have set up within Google Analytics.

In this case, we want the Custom Dimension to return values that look like this:

Homepage Hero Test 10001
Homepage Hero Test 10002

To do that, we’ll use both Variables in the input field for the Dimension value in the order we want it reported: {{experimentName}}{{experimentID}}

The benefit of this is that the data in Google Analytics will be filterable by the experiment name (so that you can see all experiment pages with one search), or by the experiment ID (if you are looking for a single variation). This becomes handy when you create segments or filters for your data either in the UI or via the API based on a subset of IDs or test names.

Once you’ve set up the index number, click save. Preview and debug the new variable you’ve built, and once satisfied with the output, publish your tag. Once published, should see your new Custom Dimension data in Google Analytics.

Previous: Event tracking with Google Tag Manager | Next: Implement AdWords, DoubleClick, and Dynamic Remarketing

Was this article helpful?
How can we improve it?
Google Analytics training and support resources

Check out our comprehensive list to learn more about Analytics solutions.