Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save peteeveleigh/94523eac6cd0876b799cd88aba252f93 to your computer and use it in GitHub Desktop.
Save peteeveleigh/94523eac6cd0876b799cd88aba252f93 to your computer and use it in GitHub Desktop.
Sample of Google Analytics transaction tracking for ExpressionEngine and Cartthrob
ga('require', 'ecommerce'); // Load the ecommerce plug-in.
ga('ecommerce:addTransaction', {
'id': '{order_id}', // Transaction ID. Required
'affiliation': '{site_name}', // Affiliation or store name
'revenue': '{exp:cartthrob:view_formatted_number number="{order_total}" prefix="" decimals="2" dec_point="." thousands_sep="" }', // Sub Total
'shipping': '{exp:cartthrob:view_formatted_number number="{order_shipping}" prefix="" decimals="2" dec_point="." thousands_sep="" }', // Shipping
'tax': '{exp:cartthrob:view_formatted_number number="{order_tax}" prefix="" decimals="2" dec_point="." thousands_sep="" }' // Tax
});
// add item might be called for every item in the shopping cart
// where your ecommerce engine loops through each item in the cart and
// prints out _addItem for each
// this is a loop that goes through each item in the order
{exp:cartthrob:order_items order_id="{order_id}"}
ga('ecommerce:addItem', {
'id': '{item:order_id}', // Transaction ID. Required
'name': '{item:title}', // Product name. Required
'sku': '{item:entry_id}', // SKU/code
'category': '', // Category or variation
'price': '{item:price_numeric}', // Unit price
'quantity': '{item:quantity}' // Quantity
});
{/exp:cartthrob:order_items}
ga('ecommerce:send'); // Send transaction and item data to Google Analytics.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment