1

On our website we have a data layer to define some variables to be passed on to Google Analytics. It is defined like this:

deviceCategory: "DESKTOP"
deviceMode: "DESKTOP"
domainName: ".mydomain.com"
environment: "production"
operatingSystem: "UNKNOWN"
page.category.pageType: "Content"
page.category.primaryCategory: "main"
page.pageInfo.language: "en"
page.pageInfo.pageID: "productSelection"
user.profile.address.city: "Los Angeles"
user.profile.address.country: "US"

When I type the following in console it outputs exactly as expected: dataLayer[0].deviceMode --> "DESKTOP"

However, when I try to access any of the page/user variables I get errors: dataLayer[0].page.category.pageType --> Uncaught TypeError: Cannot read property 'category' of undefined

Any idea why this is happening? What is the proper way to access these values?

1 Answer 1

2

The keys you are using are just strings with dots in them. They are not parts of an object, so you would need to access like this:

dataLayer[0]['page.pageInfo.language']

using the square brackets.

Edit: Note that this is not what you would do in GTM though (but perhaps no need to comment more since you're not asking about that).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.