Expand my Community achievements bar.

SOLVED

Need help in creating a DataElement

Avatar

Level 1

I'm trying to create a dataelement that will read an object called "transactionProds", and its properties. in my site code this is what i have:

var transactionProds = {

                  "sku": "SM-T280NZKAXAC",

                  "name": "Galaxy Tab A (7.0)",

                  "category": "",

                  "price": "199.99",

                  "quantity": "1",

                  "discount": "0.0"

              };

              digitalData.ecommerce.transactionProducts.transactionProds;

             

              var transactionProds = {

                  "sku": "SM-G955WZKAXAC",

                  "name": "Galaxy S8+",

                  "category": "",

                  "price": "959.99",

                  "quantity": "1",

                  "discount": "0.0"

              };

              digitalData.ecommerce.transactionProducts.push(transactionProds);

it could be a single product or multiple products. how do get the dataelement to read the multiple objects and its properties. I have tried this - where i used digitalData.ecommerce.transactionProducts in the path - but the result is: [object%20Object]

Edit Data Element

1 Accepted Solution

Avatar

Correct answer by
Level 2

You're trying to push to an Object instead of an Array.

Your code should be in this order using an array of objects:

- Declare your array

var digitalData = {};

    digitalData.ecommerce = {};

    digitalData.ecommerce.transactionProds = [];

- Push to it.

var aProduct = {

    'sku':'12345',
    'name':'Product Name'

};

digitalData.ecommerce.transactionProds.push(aProduct);

Accessing all of the products and using them how you need them might require some custom code to do; it really depends on what you're trying to do with your data.

View solution in original post

4 Replies

Avatar

Correct answer by
Level 2

You're trying to push to an Object instead of an Array.

Your code should be in this order using an array of objects:

- Declare your array

var digitalData = {};

    digitalData.ecommerce = {};

    digitalData.ecommerce.transactionProds = [];

- Push to it.

var aProduct = {

    'sku':'12345',
    'name':'Product Name'

};

digitalData.ecommerce.transactionProds.push(aProduct);

Accessing all of the products and using them how you need them might require some custom code to do; it really depends on what you're trying to do with your data.

Avatar

Level 1

Thanks for the reply - the code that i mentioned above is automatically generated by our CMS, i should have made it clear. i pasted it there just for reference. what im trying to say is that how do i display the objects properties in my tag, for example my object is "trransactionProds, and its properties are "SKU, NAME...etc" i need to display the Sku in my rules tag.

Avatar

Employee Advisor

Try this maybe?

digitalData.ecommerce.transactionProducts.sku.toString();

Avatar

Community Advisor

To rephrase your request... You have this:

Which from the console yields this:

You want a data element that will return your sku(s) and another that will return your quantity(s), etc.

The problem is really that DTM data elements can hold anything but the DTM UI (for Adobe Analytics and other tools) resolves data elements into strings (and will choke on complex data types that cannot be coerced into strings (thus [object%20Object]).

You CAN access individual array indexes in the Path field of a JS data element, so you could get at the 1st, 2nd, 3rd sku with three data elements using:

...but this is dumb (and probably not what you are looking for).

If you need to iterate through the items of transactionProducts you'll need to do it in custom JS.

What is your end goal?  Are you trying to get this data into Adobe Analytics?  Into Google Analytics? Into 3rd party tags? Other?