Expand my Community achievements bar.

SOLVED

How to populate Data Elements with values from DataLayer Array?

Avatar

Level 2

Hi Community,

I am having difficulty populating specific Data Elements that are meant to be grabbing values from an array in an AppEventData datalayer. When testing in Console, I can get the right value to populate there, but when pasting the same syntax into a "Javascript Variable" Data Element, it does not pass anything.

For example, this works in Console but not in a "Javascript Variable" Data Element: "AppEventData[1].purchase.price.purchaseAmount"

I have tried the following to help resolve, but to no avail:

  • Doublecheck that the Data Element exists, is set up to feed data to the right eVar, and that the pageload-related Rule logic is delayed as much as possible (Window Loaded).
  • Have lengthened the timeout milliseconds by a fair bit, to give it even more time to process/etc.
  • Have tried swapping out the Data Element for another that is pulling from a non-array value, just to doublecheck that would populate correctly (it did).
  • Tried clearing cache/etc. and testing in multiple browsers

Note: The website/company in question is very sensitive about any potential (founded or unfounded) for "data privacy leakage", so would prefer to avoid using any third-party extensions (e.g. the Search Discovery ones).

It seems weird that it works correctly in console, but not within the Data Element, especially since the non-array stuff populates correctly within the same tag. Is there something I am missing in regards to using "Javascript Variable"-informed Data Elements when trying to get values out of arrays?

Thank you, appreciate any assistance that may spring to mind!!

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Yeah, I have noticed the same thing... I just ended up using a custom code Data Element to pull the value, which does work.

 

So instead of:

Jennifer_Dungan_0-1682374370293.png

 

 

Use Custom Code:

Jennifer_Dungan_1-1682374416551.png

 

Jennifer_Dungan_2-1682374434565.png

 

 

This is also how I process arrays of data... I pull them into a custom code data element, and use JS to process the items into a delimited list that I then "return" as my Data Element value, or I can add logic to the code as well.....

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Yeah, I have noticed the same thing... I just ended up using a custom code Data Element to pull the value, which does work.

 

So instead of:

Jennifer_Dungan_0-1682374370293.png

 

 

Use Custom Code:

Jennifer_Dungan_1-1682374416551.png

 

Jennifer_Dungan_2-1682374434565.png

 

 

This is also how I process arrays of data... I pull them into a custom code data element, and use JS to process the items into a delimited list that I then "return" as my Data Element value, or I can add logic to the code as well.....

Avatar

Community Advisor

Instead of using square brackets, use dot-notation to access the array's elements, i.e.

AppEventData.1.purchase.price.purchaseAmount

Avatar

Level 2

Both great answers and both seemingly work, thank you!! @Jennifer_Dungan got the "correct reply" for being first, but @yuhuisg 's solution also seems to work.

Avatar

Community Advisor

Generally there are multiple ways to solve any problem 

 

With my way, you can also do extra logic / processing on the array if needed... but if you always need the "first" item, then both work