Shawn Rong
Shawn Rong

Reputation: 145

Can't see the Custom Variables on Google Analytics

I want to add a Custom Variables called Custom Variables to my website.And I defined a datalayer on Google Tag Manager.I also add the code snippet on the page which I want to get the Custom Variable.

<script type="text/javascript">
var external_id = jQuery('#edit-external-id').attr("value");
dataLayer = [{
      'externalID': external_id
    }];
</script>

I debuged the configuration of GTM.Here's the picture GTM Debug I got the message.But I can't see the value statics on my Google Analytics.I think the menu path of GA is Audience-Custom-Custom Variable.Am I right?Anyone can help me?

Upvotes: 0

Views: 1660

Answers (2)

Shawn Rong
Shawn Rong

Reputation: 145

First, you must never, ever use dataLayer = [{...}] after the container snippet (preferably don't use it ever). You're overwriting the container snippet by reinitializing it as a new Array. As you can see, in your Preview panel there is no "Page View" event (should be just before DOM Ready), which means e.g. the All Pages Trigger won't work.

Second, "Custom Variables" are deprecated. They've been replaced with Custom Dimensions in Universal Analytics.

Third, did you actually add this variable/dimension to a GA Tag?

Here's how it should work:

1) In your script replace dataLayer = [{...}] with:

window.dataLayer = window.dataLayer || []; window.dataLayer.push({ 'event' : 'variablePushed', 'externalID' : external_id });

This is the safe way to interact with dataLayer.

2) In GA Admin / Property Settings / Custom Definitions / Custom Dimensions, create a new Custom Dimension with the appropriate scope, and make note of the assigned Index number. If you are unfamiliar with Custom Dimensions, you have some reading to do. Start with https://support.google.com/analytics/answer/2709828?hl=en

3) Create a new Custom Event Trigger for Event Name variablePushed, name it e.g. "Event - variablePushed" (without quotes).

4) Create a new Data Layer Variable for Variable Name externalID, name it e.g. {{externalID}} (without curly braces).

5) Create a new Event Tag, with Event Category: External ID, Event Action: {{externalID}}, and set the Non-Interaction field to True.

6) Browse to More Settings -> Custom Dimensions in the Event Tag, and add a new Custom Dimension. Set the Index number from (2) and set the value to {{externalID}}.

7) Add the Trigger you created in (3) to this Event Tag.

What happens now is that when the externalID dataLayer.push() takes place, the Event Tag fires a non-interaction event to GA (so it won't affect Bounce Rate), which piggybacks the Custom Dimension with it.

Upvotes: 1

Eike Pierstorff
Eike Pierstorff

Reputation: 32780

Writing values to the dataLayer does not make them appear in Google Analytics; it just makes them available in GTM.

You need to set up a Google Analytics Tag - there is a tag template for Universal Analytics where you can insert the Google Account id. Use the default "all pages" trigger. Since you are receiving data I assume you already have done this.

Now in GTM go to the "variables" menu, click "new" and select "Data Layer Variable" as type. Enter the key from the dataLayer that you want to be evaluated, i.e. "externalID". Give your variable a name, I suggest to simply use the dataLayer key. Save. Now you can use the variable by calling it via its name surrounded by {{curly brackets}}.

Go back to your GA tag. I assume you want to save the value as custom dimension, since custom variables are deprecated in the current GA version. You need to create a custom dimension in Admin/Property/Custom Definitions/Custom Dimensions. When you create a custom dimension you will get a numeric index to address the dimension by. Go to your GA tag, advanced configuration, custom dimensions and click "add". Enter the numeric index and after that select the variable you want to send.

enter image description here

Custom variables do not show up by default in the standard reports; you can either selected them as "secondary dimension" from the dropdown above the data table, or create a custom report.

Upvotes: 3

Related Questions