Skip to content

Instantly share code, notes, and snippets.

@ptz0n
Created September 29, 2016 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptz0n/92e213fdfbade716ea32361b907d06b8 to your computer and use it in GitHub Desktop.
Save ptz0n/92e213fdfbade716ea32361b907d06b8 to your computer and use it in GitHub Desktop.
Enjoy Analytics
// Load GA
(function(G,o,O,g,l){
// Mock function
G.GoogleAnalyticsObject=O;
G[O]||(G[O]=function(){
(G[O].q=G[O].q||[]).push(arguments)
});
G[O].l=+new Date;
// Async load library
// could be replaced by TT.loadScript('//www.google-analytics.com/analytics.js');
g=o.createElement('script'),
l=o.scripts[0];
g.src='//www.google-analytics.com/analytics.js';
l.parentNode.insertBefore(g,l)
}(this,document,'ga'));
// Init
ga('create','%s','auto',{'name':'ea'});
// Enable Display Features
ga('require', 'displayfeatures');
// Standard Tracking
ga('ea.send','pageview');
// Ecommerce Tracking
(function(window, TT, store, order, localStorage, localStorageKey) {
TT=window.TT;
if(!TT || typeof TT.getOrder !== 'function') return;
store=TT.getStore();
order=TT.getOrder();
localStorage=window.localStorage;
localStorageKey='ea.'+order.id;
if(localStorage && localStorage.getItem(localStorageKey)) return;
ga('ea.require', 'ecommerce');
ga('ea.ecommerce:addTransaction', {
'id': order.id,
'affiliation': store.name,
'revenue': order.price,
'shipping': order.shipping_price,
'tax': order.vat.price,
'currency': store.currency
});
for(var i = 0; i < order.items.length; i++) {
var item = order.items[i],
product = item.product,
sku = product.id+'-'+product.variation.id,
name = product.title;
if(product.variation.title)
name += ' - '+product.variation.title;
ga('ea.ecommerce:addItem', {
'id': order.id,
'name': name,
'sku': sku,
'price': product.price,
'quantity': item.quantity
});
}
ga('ea.ecommerce:send');
try {
if(localStorage) localStorage.setItem(localStorageKey, 1);
}
catch(e) {}
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment