Set up Custom Dimensions using GA, GTM, and the Data Layer

Adding a custom dimension to Google Analytics involves several interconnected parts: the data layer, tag manager setup, and admin settings in GA. This post walks through each step of the process to show how they fit together.

Step 1: Push Values into the Data Layer
Step 2: Set up Custom Dimensions in Google Analytics Admin
Step 3: Set up Google Tag Manager Variables
Step 4: Add Dimensions to the GA Tags in GTM
Step 5: Add a Trigger to your Tag

NEW! API CONNECTOR ADD-ON FOR GOOGLE SHEETS

Check out my new API Connector Add-on to import data from thousands of platforms (e.g. Shopify, Harvest, Mailchimp, ActiveCampaign, VWO, YouTube, etc.) directly into Google Sheets.

STEP 1: PUSH VALUES INTO THE DATA LAYER

The first step is to set up the data layer. The data layer is a key part of working with Google Tag Manager. It holds information you want to process in an object that can be read by Google Tag Manager, where it then gets pushed to Google Analytics.

The data layer needs to be configured by someone with access to your site code, typically a web developer. They should add the data layer to your site’s code before the GTM script, using the following syntax:

Simo Ahava’s always-helpful blog discusses the specifics of this syntax: https://www.simoahava.com/gtm-tips/datalayer-declaration-vs-push/. Also, this post provides some examples of different data layer pushes: GTM dataLayer.push Examples (Standard, Ecommerce, Advanced)

So, if you were interested in tracking page types and login status, it would look something like this:

❓ Note: while event keys aren’t strictly necessary for a data layer push, every tag in GTM needs an event key to trigger it. If you push dimensions without an event key, you can’t access the dimension values at that current state — they will be inaccessible to GTM until the next event key gets set. So as a best practice, always include an event key on every data layer push.

STEP 2: SET UP CUSTOM DIMENSIONS IN GOOGLE ANALYTICS ADMIN

For the next step, enter GA and go to Admin > Property > Custom Definitions > Custom Dimensions > +New Custom Dimension and fill out the name and scope for your variable.  Click Create.

custom-dimensions-img2

On the following screen, click Done and note the Index number on the summary screen. This index number is assigned automatically and we’ll need it later.
custom-dimensions-img3

STEP 3: SET UP GOOGLE TAG MANAGER VARIABLES

Next, set up Data Layer Variables in Google Tag Manager (GTM). These Variables will collect the values pushed into the data layer in step 1.

To set up these Variables, navigate in GTM to Variables > User-Defined Variables > New > Variable Configuration > Choose variable type = Data Layer Variable. Enter in the name of the data layer variable to read from, making sure the name you use here exactly matches the name used in the data layer.

custom-dimensions-img1

STEP 4: ADD DIMENSIONS TO THE GA TAGS IN GTM

The fourth step is sending the dimension value to GA. To send these values, dimensions need to be attached to other hits like pageviews, events, or ecommerce.

In GTM, find the tag you want to attach the dimensions to. Usually this will be the GA page view tag, but other hit types will work exactly the same way. Click the “Enable overriding settings in this tag ” checkbox > More Settings > Custom Dimensions. (Or, alternately, you can set up these custom dimensions in the Google Analytics Settings Variable).

In the Index slot, use the same index number as assigned in GA Admin in step 2. Under Dimension Value, add add the new Variable you created in step 3.
custom-dimensions-img4

STEP 5: ADD A TRIGGER TO YOUR TAG

As described above, all custom dimensions need to be attached to a tag.

Every tag needs a trigger to fire, so in this step make sure you’ve chosen the right trigger. Specifically, you need to choose one that doesn’t fire too soon. For example, if you’re tracking page-related dimensions like page categories, those values need to be ready and available when the page view tag fires. The data layer push often occurs after the initial page load, so if you choose All Pages as your trigger, the values would still be null at the time the tag fired, resulting in page category values of “(not set)” in Google Analytics.

The best way to avoid this problem is to create a Custom Event Trigger for your data layer event key (in the example in step 1, it would be ‘pageview’), like this:

custom-dimensions-img6

Then, use that trigger to fire your Pageview tag, like this:

custom-dimensions-img7

This way will ensure that the Pageview tag fires only after your dimensions are pushed into data layer.

(If you don’t have an event key in your data layer push, you can alternately choose a delayed page view trigger like DOM Ready. However it’s better to use a Custom Event Trigger since it fires your tag more quickly).

CONCLUSION

Following these 5 steps will enable you to populate custom dimensions in Google Analytics via the data layer and Google Tag Manager. Once these dimensions are collecting data, you can view these dimensions as a Secondary Dimension in any regular report, or create a custom report using your new dimension as a primary dimension.

GOOGLE TAG MANAGER CONSULTING

Get your own tag manager! Click for information on my Google Tag Manager consulting service.

Comments:11

  1. I followed your guide to set-up a pages topic – with the topic itself being auto-generated in the datalayer based upon the category selected when creating the WordPress page/post.

    However when I view the secondary dimension within Google Analytics the homepage (and other pages) seems to be picking up sessions against multiple topics which is incorrect as core can only be selected for it (and is confirmed in the HTML).

    Any idea what’s going on?

    Thanks,

    Sam

    1. Hi Sam, is your dimension in GA set to hit-scoped? Session and user-scoped dimensions will take on the final value in a session so could lead to some unexpected results. If that’s not the issue and you’d like me to take a closer look, please email me your URL, a screenshot of your tag setup, and your custom dimension page in GA.

    1. You don’t need a data layer at all for custom dimensions, it’s an optional but (usually) recommended step. If you don’t have or don’t want to use a data layer, you can instead capture values by scraping them off the page. For example, say you want to pass in a custom dimension for login status. You might find that you can identify whether a user is logged in or out by checking the navigation bar, as they have different menu options (For example, the ‘logged out’ menu has an option to sign in, while the ‘logged in’ menu has an option to check your account). In that case you would alter the above steps as follows:
      1) skip step 1 entirely
      2) complete step 2 as written
      3) instead of setting up a Data Layer variable, set up a Custom Javascript variable, and write a function that returns ‘logged in’ or ‘logged out’ based on which version of the menu bar is visible (or whatever other way you’ve found to identify login status).
      4) complete step 4 as written
      5) since you no longer have a data layer event, you’ll need to instead use the built-in triggers like DOM Ready or Window Loaded to make sure your new JS Variable is resolved and available at the time you send it over to GA.

  2. Hi Ana! Thanks a lot for the detailed step-by-step explanation. I’m stuck at step 5 as I’m not sure what my best option is. I already have a GA page view tag that fires with the trigger type page view (but also the trigger type history change because of the way the website is coded). I’m worried that adding the custom event trigger might:
    – not count all page views since we’re waiting for the datalayer push to be completed
    – not mix well with the history change trigger
    To mitigate this issue, I was thinking of creating a separate GA page view tag just for the custom dimensions but then I guess GA will be counting the page views twice?
    Thanks a ton for your feedback!

    1. Hi Solal, the reason I suggest doing it with an event instead of pageview trigger is exactly to avoid the kind of issues you’re mentioning. It won’t affect your pageviews or history change trigger since it’s a totally different tag & trigger, whereas if you create a new page view tag, you’ll be double-counting page views unless you set up blocking triggers to avoid that. I usually just recommend sending this data in a pageview for sites that need to reduce their hit counts (due to GA’s 10 million hits / month limit). If you’re not in that category, I’d stick to the event method described in this post.
      Hope that helps clarify!

Leave a Reply

Your email address will not be published.