Expand my Community achievements bar.

SOLVED

DTM - How to use an array for a Data Element?

Avatar

Level 2

I'm trying to figure out how to utilize an array in a Data Element using DTM.  Let's say I have the following:

var datalayer = { products: [ { sku:'Product 1', quantity:7, price:9.12 },{ sku:'Product 2', quantity:5, price:4.78 } ] }

Essentially I want to setup a Data Element for the products, and then use this to set the products variable for Site Catalyst.  What is the best way to do this?

Thanks for your time!

Tyler

1 Accepted Solution

Avatar

Correct answer by
Employee

If that datalayer object exists in your page, you can just point the DTM Data Element at it. When  you set up your Data Element, you can use JS Object as the Type, and in the Path, you just type datalayer.products

You don't have to use a Data Element here, but you can. When you set your products variable, you can just reference datalayer.products[0].sku or whatever.

View solution in original post

4 Replies

Avatar

Correct answer by
Employee

If that datalayer object exists in your page, you can just point the DTM Data Element at it. When  you set up your Data Element, you can use JS Object as the Type, and in the Path, you just type datalayer.products

You don't have to use a Data Element here, but you can. When you set your products variable, you can just reference datalayer.products[0].sku or whatever.

Avatar

Level 9

DTM does not have a built-in field for populating s.products for Adobe Analytics.  

To set s.products, you will need to set it in Adobe AnalyticsCustom Page Code section of your rule. 

Here is some example code for this, based on the datalayer structure you provided. The assumption is that you put the products array into a data element called products

var products=_satellite.getVar('products'); s.products=[]; for (var p=0,l=products.length;p<l;p++) { var prod=[]; prod.push(''); prod.push(products[p].sku||''); prod.push(products[p].quantity||''); prod.push(products[p].price||''); s.products.push(prod.join(';')); } s.products=s.products.join();

 

Note that if this is supposed to be an s.tl call, you will also need to set (or append to) s.linkTrackVars.

Avatar

Level 2

I have set up datalayer.products as a data element.  (Lets say I named that element "Products")

I guess what is the best way to set the products variable?  Would I have to code it on the site itself, or can I configure the setting of s.products in DTM somehow now the array is saved in a data element?

Avatar

Employee

As far as I can tell, right now the only way to set the products variable is through code.  There is no interface that allows you to set s.products for DTM UI.

If your code above were run on the global scope, you could set your products variables by simply assigning the s.products variable to a concatenation of pieces from your datalayer object.  You would do this in the same way as @jeffchasin mentioned above.