简体   繁体   中英

Updating Big Commerce to the new analytics.js

I'm working on a site and I updated the tracking code to the new analytics.js I transferred their site and everything is working except for conversion. It's a Big Commerce site so I can't touch server side script but this what they generate

<script type="text/javascript">
$(document).ready(function() {
 if(typeof(pageTracker) != 'undefined') {
 pageTracker._addTrans(
'358',
'Backyard Toy Company ',
'0.01',
'0.00',
'0.00',
'Lakewood',
'New Jersey',
'United States'
);
 pageTracker._addItem(
'358',
'1336',
'test',
'',
'0.01',
'1'
);
  pageTracker._trackTrans();
}
});
</script>

I updated the client side code to this

   ga('require', 'ecommerce', 'ecommerce.js');   // Load the ecommerce plug-in.

  // START CUSTOM CODE
  function old2new() {
     // define object that can route old methods to new methods
     this._addTrans = addTrans;
     this._addItem = addItem;
     this._trackTrans = trackTrans;
  }

  function addTrans(orderID,store,total,tax,shipping,city,state,country) {
    // remap _addTrans
   ga('ecommerce:addTransaction', {
      'id': orderID,
      'affiliation': store,
      'revenue': total,
      'tax': tax,
      'shipping': shipping,
   });
  }

  function addItem(orderID,sku,product,variation,price,qty) {
    // remap _addItem
  ga('ecommerce:addItem', {
    'id': orderID,
    'sku': sku,
    'name': product,
    'category': variation,
    'price': price,
    'quantity': qty
  });
 }

function trackTrans() {
  ga('send', 'ecommerce'); 
}

// instantiate converter using name of old Google tracking object
// bigcommerce code will use this and be none the wiser
var pageTracker = new old2new();
// END CUSTOM CODE

I'm sorry I'm a newbie, but I looked all over can't figure out why it's not working.

I think the send method is supposed to be something like:

ga('ecommerce:send');

If this does not work, you can do the following and see if find something else that is wrong:

Can you put an alert in all three functions and make sure that it is being called correctly?

Also, there might be a bug in the code that might be affected by the overridden this . Can you rewrite the methods like the following and see if that works:

  function addTrans(orderID,store,total,tax,shipping,city,state,country) {
   // alert("in addTrans");
   ga.call(window, 'ecommerce:addTransaction', {
      'id': orderID,
      'affiliation': store,
      'revenue': total,
      'tax': tax,
      'shipping': shipping,
   });
  }

  function addItem(orderID,sku,product,variation,price,qty) {
  // alert("in addTrans");
  ga.call(window, 'ecommerce:addItem', {
    'id': orderID,
    'sku': sku,
    'name': product,
    'category': variation,
    'price': price,
    'quantity': qty
  });
 }

function trackTrans() {
  // alert("in Trans");
  ga.call(window, 'ecommerce:send'); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM