Skip to content

Instantly share code, notes, and snippets.

@matherton
Last active December 23, 2015 19:29
Show Gist options
  • Save matherton/6682657 to your computer and use it in GitHub Desktop.
Save matherton/6682657 to your computer and use it in GitHub Desktop.
GA E-commerce PHP tracking code
public function getGoogleAnalytics ()
{
$googleid = Config::getConfig()->getPreference('googleAnalyticsCode');
$basketWidget = $this->getWidget('basket');
$clientData = $this->getClientDataForOrderReview($clientID);
$orderData = $this->getOrderData($basketWidget,$clientData);
$transID = 999999;
//loop for picking up all ordered items
foreach($orderData['product'] as $product){
$items .= "_gaq.push(['_addItem',
'".$transID."', // transaction ID - required
'".$product['id']."', // SKU/code - required
'".$product['name']."', // product name
'".$product['variant']['name'][0]."', // category or variation
'".$product['price']."', // unit price - required
'".$product['quantity']."' // quantity - required
]);";
}
$orderTotal = $orderData['totals']['grandTotal'];
$content = "<script type=\"text/javascript\">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '".$googleid."']);
_gaq.push(['_trackPageview']);
_gaq.push(['_addtrans,
'1', // transaction ID - required
'postalheritage.org.uk', // affiliation or store name
'".$orderTotal."', // total - required
'".$orderData['totals']['grandTotalVAT']."', // tax
'".$orderData['totals']['shipping']."', // shipping
'".$address['billing_city']."', // city
'".$address['billing_county']."', // state or province
'".$address['billing_country']."' // country
']);
".$items." //displays all items from loop
_gaq.push(['_trackTrans']); //submit transactionm to Analytics server
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>";
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment