1

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.

Fabien Benoit-Koch
  • 2,784
  • 2
  • 21
  • 33
giancy9
  • 31
  • 10

1 Answers1

0

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);
Weedoze
  • 13,683
  • 1
  • 33
  • 63
  • Hello, thanks for the help. Will give it a try now and let you know – giancy9 Oct 26 '16 at 13:28
  • I tried it but it prints the same error "Product ID info missing: "item" property is missing". Maybe it has something to do with the object structure again? not sure exactly :( I changed my code as you suggested but still not working. – giancy9 Oct 26 '16 at 13:36
  • Show me the final object – Weedoze Oct 26 '16 at 13:37
  • You should check what you have to send to criterio. You are certainly missing some info – Weedoze Oct 26 '16 at 13:44
  • You can see in the first code I posted in my post what they need. And it is exactly what we are passing in there. But they do have a **comma** sign in the end of each line. Would that be the problem maybe? Template from Criteo: **{ id: "product_id_1", price: price_1, quantity: quantity_1 },** – giancy9 Oct 26 '16 at 13:45
  • Hello again, I finally found my silly mistake. I was using "product_id: 20020-278" instead of "id: 20020-278". Your answer helped me a lot. Thank you – giancy9 Oct 26 '16 at 15:31