Expand my Community achievements bar.

Join us at Adobe Summit 2024 for the Coffee Break Q&A Live series, a unique opportunity to network with and learn from expert users, the Adobe product team, and Adobe partners in a small group, 30 minute AMA conversations.
SOLVED

Data Layer Value Produces No Results when Part of An Array

Avatar

Level 1

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? 

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

 

 

 

 

View solution in original post

7 Replies

Avatar

Community Advisor

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

Avatar

Level 1

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? 

Avatar

Correct answer by
Community Advisor

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.

 

 

 

 

Avatar

Level 1

Hi 

Thanks for your kind response. I don't believe this to be my scenario. It is definitely defined above the Pagebottom and i am able to retrieve other elements from the same general 'area' as long as they are not part of an array.. e.g. data.page.category.pageType

Avatar

Community Advisor

ok then make sure is there an actual data element built and published in Launch?

Once there is make sure what ever rule then is picking it up has it defined to populate

 

%rule_name%  is mapped in evar section of rule fired.

 

Also code wise lets say you are using angular:

 

'array' is defined by your page code. Then in your page code make sure the[1] it then defined to an object matching your datalayer object data.page.category.categories.

 

This then needs to be defined in launch as a data element.

Depending on your set up you have a pagelaod rule, in the rule itself it then needs to be mapped to the evar section evar63 =%rule_name% (evar number up to you.)

Avatar

Community Advisor

@kddoyle can you provide an example of page code that contains your data layer?

"Data layer" is a general term to mean a collection of data, usually for analytics purposes. That collection can take several forms, usually a key-value object or an array. When an array, it is usually used in conjunction with another script or tool to make it easy to read the data layer's values, e.g. ACDL or GTM's built-in methods in its gtm.js script.

Avatar

Community Advisor

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