Data Layer Value Produces No Results when Part of An Array | Community
Skip to main content
March 24, 2022
Solved

Data Layer Value Produces No Results when Part of An Array

  • March 24, 2022
  • 2 replies
  • 2441 views

Hello experts! 

I am trying to capture a simple value from my data layer into an eVar. It is part of any array and does not produce any results only "Unspecified" 

 

the path is data.page.category.categories[1] 

 

as you see i need the value from the 1 of the array but it does not fetch any results. Other data layer objects are fine. What is the trick to fetching this array value? 

 

I have tried variations such as 

data.page.category.categories.1

page.category.categories[1]

etc... to no avail. 

 

Any suggestions? 

It seems it ought to be fairly straightforward and not require custom code, right? 

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Pablo_Childe

So when working with defining building your data later the main key to remember is the final availability of the datalayer element.  Also have you defined in launch the data element? Is this related to a direct call rule? Is this a SPA type of page single page app?

 

So what this means we have a analytics page load order:

Header code

(all datalayer elements

data.page.category.categories)

Pagebottom

 

What I often see in a scenario you described is you define a datalayer object but its contents are available too late in relation to pagebottom.(ie page bottom fires then the datalayer element is ready.)

 

This leads to evars not populating when expected.

 

 

 

 

2 replies

yuhuisg
Community Advisor
Community Advisor
March 25, 2022

What kind of data layer are you using? Is it the Adobe Client Data Layer (ACDL)? Or a W3C-compliant key-value data layer?

kddoyleAuthor
March 25, 2022

Thank you yuhuisg for your response. .. . this is our own data layer. custom to our organization. Should not all data layers work relatively the same? 

Pablo_Childe
Community Advisor
Pablo_ChildeCommunity AdvisorAccepted solution
Community Advisor
March 25, 2022

So when working with defining building your data later the main key to remember is the final availability of the datalayer element.  Also have you defined in launch the data element? Is this related to a direct call rule? Is this a SPA type of page single page app?

 

So what this means we have a analytics page load order:

Header code

(all datalayer elements

data.page.category.categories)

Pagebottom

 

What I often see in a scenario you described is you define a datalayer object but its contents are available too late in relation to pagebottom.(ie page bottom fires then the datalayer element is ready.)

 

This leads to evars not populating when expected.

 

 

 

 

Alexis_Cazes_
Level 10
March 28, 2022

Right so data layer are in fact JavaScript objects so when you write your code what you really need to do is access the property value of the object in the same way as as you would do with any JavaScript objects.

 

so if we take the example of your data layer based on what you provided I would expect the object to look as follow:

 

var data = {
    page: {
        category: {
            categories: [
                "category1",
                "category2"
            ]
        }
    }
}

I would encourage you to go to your website, open developer tools, navigate to the developer console and type data. It should return the structure or your object.

We will consider that your use the code editor in Adobe Launch (or the JavaScript variable data element option from Core extension) to retrieve your data layer code.

Each level where there is a curly bracket, it is a property of an object type so you can use a dot then the name of nested property to navigate through your object. Now if the property is followed by a square bracket then it is an array

 

So one thing to be aware of is that arrays items do not start at index 1 but 0, so in my example above "category1" is not at index 1 but at index 0. So first item of an array would be at index 0 and second item would be at index 1 etc...

 

So to access first item of our array we would use

//access first item of array category1
data.page.category.categories[0]

//access second item of array category2
data.page.category.categories[1]

//If we access an index without a value it will return undefined
data.page.category.categories[2] //undefined as in my example we only have 2 items that live from index 0 to index 1

//If we use the wrong path to the array we will see error "Uncaught TypeError: Cannot read properties of undefined"
data.page.wrongProperty.categories[0]

So to conclude you have undefined:

  • the index 1 that you access does not have a value. I am actually wondering if you want to access only first item of the array so please use index 0
  • the array in itself has no items!
  • it seems path to array is correct otherwise you will get error as above in developer console

hope this helps