giancy9 giancy9 - 1 month ago 13
JSON Question

JS add array in tracking code push method

I've been trying since yesterday to pass an array in a push method.

There is a tracking code called Criteo and they required to fill the following to make it work. Everything's fine except the viewBasket.

<script type="text/javascript">
window.criteo_q = window.criteo_q || [];
window.criteo_q.push(
{ event: "setAccount", account: 11111 },
{ event: "setEmail", email: "username@domain.com" },
{ event: "setSiteType", type: "d" },
{ event: "viewBasket", item: [
{ id: "product_id_1", price: price_1, quantity: quantity_1 },
{ id: "product_id_2", price: price_2, quantity: quantity_2 }
/* add a line for each item in the user's basket */
]}
);
</script>


So I created an array and populated it with the data Criteo requires (which is the product id, the price and the quantity). Although in the console I could see the correct structure, when I pass it in the code it does not work.

In the console I can see this (first part is the two lines I pushed into array, and second one is the whole array):

{ id:"20020-278", price: 119, quantity: 1},
{ id:"20009-129", price: 927, quantity: 3},

Array[2]
0: "{ id:"20020-278", price: 119, quantity: 1},"
1: "{ id:"20009-129", price: 927, quantity: 3},"
length: 2__proto__: Array[0]


Exactly as I want it but for some reason it does not work. I tried to convert it to JSON array or just pass a normal line with no variables and I still have this problem. I also escaped that symbol { ..

<script type="text/javascript">
...
...
var full_line = "\{ id:\""+pid+"\", price: "+price+", quantity: "+quantity+"\},";
//var full_line = "\{ id:20020-278, price:119, quantity:1\},";
//var full_lineJson = JSON.stringify(full_line);

console.log(full_line);
allitems.push(full_line);
</script>


I pass the 'allitems' array in Criteo code

<script type="text/javascript">
window.criteo_q = window.criteo_q || [];
window.criteo_q.push(
{ event: "setAccount", account: 11111 },
{ event: "setEmail", email: "username@domain.com" },
{ event: "setSiteType", type: "d" },
{ event: "viewBasket", item: [
allitems
]}
);
</script>


On Criteo debug page is displayed like this:

enter image description here

The result should be:

Product ID Price Quantity
20010-278 69 1


But as you can see the structure is broken somehow. I tried so many different ways and still didn't manage to fix this. There is something wrong with the structure in array but I am not sure what else I can do. Any suggestions please?

EDIT: If I pass an object

Object {product_id: "20020-278", price: "119", quantity: "1"}
Object {product_id: "20009-129", price: "927", quantity: "3"}

Array[2]
0: Object
price: "119"
product_id: "20020-278"
quantity: "1"

__proto__: Object

1: Object
price: "927"
product_id: "20009-129"
quantity: "3"

__proto__: Object

length: 2__proto__: Array[0]


Criteo site displays this error when I use objects:
Product ID info missing: "item" property is missing

Code I used for objects:

<script type="text/javascript">
...
var full_line = {};
full_line.product_id = product_id;
full_line.price = price;
full_line.quantity = quantity;
allitems.push(full_line);
...
</script>


Then I just used the allitems inside the Criteo "viewBasket", item property.

Answer

var firstLine = {
  product_id: "20020-278",
  price: "119",
  quantity: "1"
};
var secondLine = {
  product_id: "20009-129",
  price: "927",
  quantity: "3"
};

var items = [];
items.push(firstLine);
items.push(secondLine);

var myObj = {
  event: "viewBasket",
  item: items
};

console.log(myObj);

//window.criteo_q = window.criteo_q || [];
//window.criteo_q.push({
//  event: "setAccount",
//  account: 11111
//}, {
//  event: "setEmail",
//  email: "username@domain.com"
//}, {
//  event: "setSiteType",
//  type: "d"
//}, myObj);