Skip to content

Instantly share code, notes, and snippets.

@justinboyle
Created October 30, 2013 20:33
Show Gist options
  • Save justinboyle/7239745 to your computer and use it in GitHub Desktop.
Save justinboyle/7239745 to your computer and use it in GitHub Desktop.
override this as analytics.js
(function() {
Checkout.ApplicationController.reopen({
// Virtual Pageviews
routeChanged: function() {
Ember.run.next(function() {
analytics.pageview('/checkout/cc/#/' + this.currentPath);
}.bind(this));
}.observes('currentPath')
});
Checkout.AnalyticsController = Ember.Controller.extend({
actions: {
trackTransaction: function(callback) {
// Gather all data necessary to track the transaction from the Receipt.
// All required data *must* be present in the Receipt.
var receipt = Checkout.get('Receipt')
, affiliation = ''
, orderNum = receipt.order_number
, revenue = receipt.summary.subtotal
, tax = receipt.summary.tax || Checkout.log('error', 'Missing tax cost.')
, shipping = receipt.summary.shipping || Checkout.log('error', 'Missing shipping cost.')
, currency = receipt.summary.currency || 'USD'
, promos = receipt.summary.promos
, products = receipt.products
, _location = Checkout.config.locationToTrack
, location = _location ? Checkout.get('BillingAddress') : Checkout.get('ShippingAddress')
, accounts = Checkout.get('config.analyticsAccounts');
if (!receipt.isLoaded) {
Checkout.log('error', 'Receipt has not yet loaded. '
+ 'Transaction will not be tracked. '
+ 'Receipt must be loaded before attemping to log transaction data.');
if (typeof callback === 'function') {
callback();
}
}
if (!(orderNum && revenue && products && products.length)) {
var warning = '⛔ WARNING! ⛔\nReceipt data incomplete.\n\n'
+ (!orderNum && 'Missing order number.\n')
+ (!revenue && 'Missing subtotal.\n')
+ ((!products || !products.length) && 'Missing products.\n')
+ '\nAnalytics will fail!';
if (App.env === 'development') {
while(true) {
alert(warning);
}
} else {
console.warn(warning);
}
if (typeof callback === 'function') {
callback();
}
return;
}
// Check for Free Shipping promo
if (promos) {
$.each(promos, function(i, promo) {
if (/free shipping/gi.test(promo.description)) {
shipping = '0.00';
}
});
}
// You can config which Google Analytics accounts to send the ecommerce
// tracking data to in `config.js` for this checkout. It's an array of the
// keys for the `trackingId`s configured in the frontend `config/analytics.json`
Ember.$.each(accounts, function(i, account) {
// Add Transaction
_gaq.push([account + '._addTrans',
orderNum,
affiliation,
revenue,
tax,
shipping,
location.city,
location.region,
location.country
]);
// Add Items
Ember.$.each(products, function(i, product) {
var options = product.options && $.map(product.options, function(value, key) { return value; });
_gaq.push([account + '._addItem',
orderNum,
product.id || Checkout.log('error', 'Missing product id.'),
product.title || Checkout.log('error', 'Missing product title.'),
options && options.join(', '),
product.price && product.price.per_item || Checkout.log('error', 'Missing product unit price.'),
product.quantity || Checkout.log('error', 'Missing product quantity.')
]);
});
// Set currency
_gaq.push([account + '._set', 'currencyCode', currency]);
// Track Transaction
_gaq.push([account + '._trackTrans']);
});
if (typeof callback === 'function') {
callback();
}
}
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment