window.criteo_q.push not executing after pageload after checkout

652 Views Asked by At

I moved this code from index body into my html editor for the conversion/thankyou page. This Criteo Javascript should push order details to their database. What is going wrong?

<!-- Conversionpage htmlblock in js test -->

<body onload="fireConversion()">

<script type="text/javascript" src="//static.criteo.net/js/ld/ld.js" async="true"></script>

<script type="text/javascript">
function fireConversion() {
 var order_info = JSON.parse(ORDER_JSON).order_rows;
 var basketproducts = [];
  for(var i=0;i<order_info.length;i++) {
    basketproducts.push( { id: order_info[i].product_id, price: order_info[i].price, quantity: order_info[i].count});
  }
 var orderid = '[OrderNumber]';
 window.criteo_q = window.criteo_q || [];
 var deviceType = /iPad/.test(navigator.userAgent) ? "t" : /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test(navigator.userAgent) ? "m" : "d";

 window.criteo_q.push( { event: "setAccount", account: 52999 },  { event: "setSiteType", type: deviceType },  { event: "setEmail", email: "" } , { event: "trackTransaction", id: orderid , item: basketproducts });
</script>
}
2

There are 2 best solutions below

4
On

I think you swapped the last two lines. Try this:

<!-- Conversionpage htmlblock in js test -->

<body onload="fireConversion()">

<script type="text/javascript" src="//static.criteo.net/js/ld/ld.js" async="true"></script>

<script type="text/javascript">
function fireConversion() {
    var order_info = JSON.parse(ORDER_JSON).order_rows;
    var basketproducts = [];
    for (var i = 0; i < order_info.length; i++) {
        basketproducts.push({ id: order_info[i].product_id, price: order_info[i].price, quantity: order_info[i].count });
    }
    var orderid = '[OrderNumber]';
    window.criteo_q = window.criteo_q || [];
    var deviceType = /iPad/.test(navigator.userAgent) ? 't' : /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test(
        navigator.userAgent) ? 'm' : 'd';

    window.criteo_q.push({ event: 'setAccount', account: 52999 }, { event: 'setSiteType', type: deviceType },
        { event: 'setEmail', email: '' }, { event: 'trackTransaction', id: orderid, item: basketproducts });
}
</script>
0
On

I resolved this issue in the meanwhile.

I put the following code in index body with the condition for the conversion page.

The definition of variable "orderid" is defined by small script in conversion page body html editor.

Code in index body html:

<script type="text/javascript" src="//static.criteo.net/js/ld/ld.js" async="true"></script>
<script type="text/javascript">
if (document.body.className == "OrderPage ps-German")
{
  var order_info = JSON.parse(ORDER_JSON).order_rows;
  var basketproducts = [];
  for (var i = 0; i < order_info.length; i++) {
     basketproducts.push({ id: order_info[i].product_id, price: order_info[i].price, quantity: order_info[i].count });
  }
  window.criteo_q = window.criteo_q || [];
  var deviceType = /iPad/.test(navigator.userAgent) ? 't' : /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test(
    navigator.userAgent) ? 'm' : 'd';
  window.criteo_q.push({ event: 'setAccount', account: 59999 }, { event: 'setSiteType', type: deviceType },
    { event: 'setEmail', email: '' }, { event: 'trackTransaction', id: orderid, item: basketproducts });
}
</script>

Code in conversion page:

<script>
var orderid = '[OrderNumber]';
</script>