Skip to content

Instantly share code, notes, and snippets.

@gerbz
Last active November 5, 2015 20:47
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 gerbz/c64167a03f38eee46104 to your computer and use it in GitHub Desktop.
Save gerbz/c64167a03f38eee46104 to your computer and use it in GitHub Desktop.
Google Analytics eCommerce Hack
/*
Add as many of the company's products as you want to the transaction_product array
and then run this code in the console for any ecommerce site which has
Google Analytics eCommerce enabled via ga('require', 'ecommerce');
*/
var transaction_count = 10;
var transaction_min = 1;
var transaction_max = 100;
var transaction_amount = 0;
var transaction_product = ['Super Cool Thing!', 'The best product ever', 'Widget #4', 'Christmas sweater'];
var transaction_product_id = 0;
for(var i = 0; i < transaction_count; i++){
transaction_amount = Math.random() * (transaction_max - transaction_min) + transaction_min;
transaction_amount = transaction_amount.toFixed(2);
transaction_id = Math.random() * (10000000 - 1000000) + 1000000;
transaction_id = transaction_id.toFixed(0);
transaction_product_id = Math.random() * transaction_product.length;
transaction_product_id = transaction_product_id.toFixed(0);
if(transaction_product_id != 0){transaction_product_id--};
var transaction = {
'id': transaction_id,
'revenue': transaction_amount
};
var item = {
'id': transaction_id,
'name': transaction_product[transaction_product_id],
'price': transaction_amount,
'quantity': '1'
};
ga('ecommerce:addTransaction', transaction);
ga('ecommerce:addItem', item);
ga('ecommerce:send');
//console.log('id: ' + transaction_id + ' amount: ' + transaction_amount + ' product_id:' + transaction_product_id + ' product: ' + transaction_product[transaction_product_id]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment