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

Avatar

Avatar
Validate 1
Level 1
guidov61672237
Level 1

Like

1 like

Total Posts

15 posts

Correct reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 1
guidov61672237
Level 1

Like

1 like

Total Posts

15 posts

Correct reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile
guidov61672237
Level 1

11-08-2016

 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"
            }
            
        }
        ]
    }

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Validate 10
Level 10
ParitMittal
Level 10

Likes

93 likes

Total Posts

817 posts

Correct reply

349 solutions
Top badges earned
Validate 10
Validate 1
Boost 50
Boost 5
Boost 3
View profile

Avatar
Validate 10
Level 10
ParitMittal
Level 10

Likes

93 likes

Total Posts

817 posts

Correct reply

349 solutions
Top badges earned
Validate 10
Validate 1
Boost 50
Boost 5
Boost 3
View profile
ParitMittal
Level 10

12-08-2016

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

Answers (0)