Expand my Community achievements bar.

SOLVED

How to create a data-element based on a JSON object array?

Avatar

Level 2

 Hello all,

I'm working with the W3 dataLayer and want to trigger a rule when a certain product is on the page, but I don't know how I can create a data-element that is based on a array. So for example I have the following dataLayer and I want to trigger a rule when the product name contains laptop. How can I do this?

var digitalData = {
    "listing":
        [
        {
            "productInfo":
            {
            "productName": "tv", 
            "productId": "19381654"
            }
            ,
            "productInfo":
            {
            "productName": "laptop", 
            "productId": "19381652"
            }
            
        }
        ]
    }

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Guidov, 

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.

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 productName value from a complex Json object using Array then you will have to use-
Example of rule : 
 for this Path- digitalData.listing[0].productInfo.productName
you have to use a path like this - 
digitalData.listing.0.productInfo.productName
 
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

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

Hi Guidov, 

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.

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 productName value from a complex Json object using Array then you will have to use-
Example of rule : 
 for this Path- digitalData.listing[0].productInfo.productName
you have to use a path like this - 
digitalData.listing.0.productInfo.productName
 
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