Data elements - JS Object seems to not allow arrays

christyw8416449

29-03-2016

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?

Accepted Solutions (1)

Accepted Solutions (1)

ParitMittal

30-03-2016

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

Answers (3)

Answers (3)

ParitMittal

03-04-2016

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

christyw8416449

04-04-2016

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