0

I am quite new to Javascript so sorry if my question is too basic. I am trying to implement "Criteo tag" in a page and I need to pass three IDs in it.

The provided code from Criteo is

<script type="text/javascript">
window.criteo_q = window.criteo_q || [];
window.criteo_q.push(
{ event: "viewList", item:[ "product_id_1" , "product_id_2" , "product_id_3" ]}
);
</script>

They basically tell you to pass the first three products in a page, within that item element.

I have an array with all product IDs, but I am not sure how to pass it in JS and somehow use a foreach loop and get first product, second product, third product and use it within event - viewlist - item element.

What I'm trying to succeed is something like:

<script type="text/javascript">
var getproducts = array_slice($allproducts, 0, 3);

window.criteo_q = window.criteo_q || [];
window.criteo_q.push(
{ event: "viewList", item:[ getproducts[0] , getproducts[1], getproducts[2] ]}
);
</script>

The above code is probably wrong... Can anyone please lead me to the right way?

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

1 Answers1

0

Call .slice() chained to $allproducts array

window.criteo_q.push(
 { event: "viewList", item: $allproducts.slice(0, 3) }
);
guest271314
  • 1
  • 15
  • 104
  • 177
  • Hello, thanks a lot for the answer... but Criteo site set the item in the following structure==> item:[ "product_id_1" , "product_id_2" , "product_id_3" ]. Don't I need to have the same punctuations like comma etc? – giancy9 Oct 24 '16 at 11:08
  • `.slice()` returns a new array containing elements from `start` to `end`, here `0` to `3` of array which the method is call on. – guest271314 Oct 24 '16 at 11:09
  • ok thanks, and by just using the name of the original array in javascript it will be fine? do I have to define anything? or I can simply pass $allproducts array from php by just using the same name in javascript? thank you – giancy9 Oct 24 '16 at 11:11
  • If `getproducts` is a `javascript` array, use that array. – guest271314 Oct 24 '16 at 11:13
  • my main question is how to pass that PHP array in JS :) I defined getproducts but I passed in there the PHP array (both php code and js code is in the same script btw) – giancy9 Oct 24 '16 at 11:15
  • Is `getproducts` defined at `javascript`? – guest271314 Oct 24 '16 at 11:16
  • I actually added it there but no. The original array let's say, the array with all products, is defined in PHP. And I am trying to find a way to just pass it in JS. – giancy9 Oct 24 '16 at 11:18
  • See [How to embed php in javascript?](http://stackoverflow.com/questions/3352576/how-to-embed-php-in-javascript) . You can request the array from `php` using `XMLHttpRequest()` or `fetch()` if you want to retrieve the array after `document` has loaded. – guest271314 Oct 24 '16 at 11:20
  • @giancy9 Did the linked Question and Answers answer your current Question? – guest271314 Oct 24 '16 at 11:34
  • Hello, sorry for not answering. It just takes me quite a lot of time to understand this. Your answer is completely fine for my second part of question. Just to mention it again, that javascript code is INSIDE php, it needs to be there anyway in order to track the products. And I am just not sure about the array and how to pass it (the link didn't help that much to be honest). Thanks a lot for the help – giancy9 Oct 24 '16 at 11:46
  • I have the array with all products, i know how to get the first three products (by using the slice function)... but these 3 products need to be passed inside the Criteo javascript method. That is where I'm having hard time – giancy9 Oct 24 '16 at 11:48
  • _"I have the array with all products"_ You successfully passed the array from `php` to `javascript`? – guest271314 Oct 24 '16 at 17:06