Skip to content

Instantly share code, notes, and snippets.

@clnmcgrw
Last active August 5, 2022 16:37
Show Gist options
  • Save clnmcgrw/9b0316aa15dbc6b6a47a822703e1d6cc to your computer and use it in GitHub Desktop.
Save clnmcgrw/9b0316aa15dbc6b6a47a822703e1d6cc to your computer and use it in GitHub Desktop.
Bigcommerce Conversion Tracking - Get Product Data
(function() {
//Bigcommerce doesn't provide variables for individual product info,
//so this is a hacky way to read it from the GA ecommerce script
var products = [];
var scripts = document.getElementsByTagName('script');
for (var i = 0; i < scripts.length; i++) {
if (scripts[i].innerHTML.match(/pageTracker\._addItem/)) {
var stuff = scripts[i].innerHTML.match(/_addItem\(([^\)]+)/g);
for (var c = 0; c < stuff.length; c++) {
var txt = stuff[c];
txt = txt.replace('_addItem(', "");
txt = txt.replace(/'|\n/g, "");
txt = txt.replace(/,\s+/g, ',');
txt = txt.trim();
var fields = txt.split(',');
products.push({
orderId: fields[0],
sku: fields[1],
name: fields[2],
category: fields[3],
price : fields[4],
quantity : fields[5]
});
}
break;
}
}
//now product info is available to pass to a conversion tracking script
//example use w/ avant metrics
var _AvantMetrics = _AvantMetrics || [];
_AvantMetrics.push(['order',{
order_id:'%%ORDER_ID%%',
amount:'%%ORDER_AMOUNT%%',
state:'[BILLING_STATE]',
country:'[BILLING_COUNTRY]'
}]);
for (var i=0; i < products.length; i++) {
_AvantMetrics.push(['item',{
order_id:'%%ORDER_ID%%',
parent_sku: products[i].category,
variant_sku: products[i].sku,
price: products[i].price,
qty: products[i].quantity
}]);
}
})();
@ShoesOnTheWeb
Copy link

This is perfect for what I needed for Criteo Conversion... Thanks!

@xeeshan79
Copy link

Boss You are genius..HERO...Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment