Expand my Community achievements bar.

SOLVED

Data elements - JS Object seems to not allow arrays

Avatar

Level 3

I am trying to implement the W3C standard for a data layer but have run into a snag.  Looks like arrays are not allowed in the path for a JS object in a data element.  For example, this only returns an empty string in a data element but returns the expected value in the console:

digitalData.user[0].profile[0].profileInfo.userName

Anything  I should be doing differently?

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Christy ,

Yes, Arrays are not allowed in the path for a JS Object in a data element. For making it work Choose the Data Element Type  as "Custom Script" instead of "JS Object" and return digitalData.user[0].profile[0].profileInfo.userName  from the custom script. 

Please let us know in case of any more questions or queries.

Thanks & Regards

Parit Mittal

View solution in original post

4 Replies

Avatar

Correct answer by
Level 10

Hi Christy ,

Yes, Arrays are not allowed in the path for a JS Object in a data element. For making it work Choose the Data Element Type  as "Custom Script" instead of "JS Object" and return digitalData.user[0].profile[0].profileInfo.userName  from the custom script. 

Please let us know in case of any more questions or queries.

Thanks & Regards

Parit Mittal

Avatar

Level 3

Thank you Parit.  Any idea if engineering is working to allow arrays for a JS object in a future release?  

Avatar

Level 10

Hi Christy, 

We have talked to the engineering team and they are saying that there is a particular way in which we can represent path as an array in a JS object type for Data Elements. Please see the following content for more details.

Complex JSON object containing Arrays

Complex JSON object using Array
 
By default in JavaScript if you want to extract userName value then you would use simple javascript syntax.
In DTM it is different. If you want to create a data element to extract userName value from a complex Json object using Array  then you will have to use-

Example of rule : 

 for this Path- digitalData.user[0].profile[0].profileInfo.userName

you have to use a path like this - 

digitalData.user.0.profile.0.profileInfo.userName

 

So to be able to extract value from an array in JavaScript you would use arrayName[x] where x is the index of the value in the array.

In DTM when you use a data element based on JS path then you will need to use the following if you want to extract a value from a JavaScrtipt array : arrayName.x where x is the index of the value in the array.

 
Thanks & Regards

Parit Mittal

Avatar

Level 3

Hi Parit,

Thank you for this response, it is exactly what I was looking for.  Much more readable than having to use custom code for just pulling a value out of the data layer.

Thanks!

Christy