0

I want to use product IDs as Label in an Google Tag Manager Trigger. I have a populated Data Layer. When in Chrome Console I use

dataLayer[1].ecommerce.detail.products[0].id 

I get as output a valid product ID.

When in Google Tag Manager I create a custom javascript variable and try:

function(){ var art_nr = dataLayer[1].ecommerce.detail.products[0].id; return art_nr; } 

This variable later in the debuger just contains 'undefined'.

I did research for a few hours now, but I cant find a simple explanation on how to do this.

2 Answers 2

4

You cannot (or at least I don't believe you can) reference the dataLayer using its own name within a variable. You need to create a DataLayer typed variable and invoke it with the standard GTM variable notation.

Your dataLayer variable should be defined as

ecommerce.detail.products.0.id 

(note the use of dot notation.)

In your custom JS variable, you would invoke it with the double curly-braces format:

function(){ var art_nr = {{variableName}}; return art_nr; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Now I see the according variable in the debugger, I misled myself it seems. ecommerce.detail.products.0.id is indeed correct, also there is no custom Javascript needed, this notation can directly be used in the variable type 'data layer variable'. Just in case somebody stumbles over this question, might help.
0

I'd add to Nyuen answer that you need to select the 'version 2' of the dataLayer.

Keep in mind that the dataLayer is a JavaScript Array which has been designed specifically for GTM.

I found that this post: https://www.simoahava.com/gtm-tips/data-layer-variable-versions-explained/ explains quite well how the dataLayer works with GTM.

A.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.