Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

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

guidov61672237
Level 2
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
ParitMittal
Correct answer by
Level 10
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
ParitMittal
Correct answer by
Level 10
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