WordPress eShop Google Analytics E-commerce Tracking Script
If you have the free WordPress plugin called “eShop“, you might be wondering how to track your e-commerce transactions in Google Analytics. Well, I am here to offer up some help. In my brief experience messing with eShop so far it would seem that the Success Page (at least through Authorize.net) does not contain the session or cart data needed to send to analytics.
My Temporary Solution: The “Confirmation” screen is the last step before sending data to Authorize.net, it takes 3 clicks to get there so in theory 99% of the traffic on this page should be going through with a purchase. So I’ll send Analytics the Transaction data at this time.
Start coding
1. Open “wp-content/plugins/eshop/checkout.php”
2. On line 553 you will see it builds the array “$eshopcartarray” with all the cart data needed for line item tracking. Just FYI, dont do anything.
3. On Line 943 you will see it builds the post data arrary “$array“. Thats the array we need for the transaction data. Again, Just FYI, dont do anything.
4. Down a few lines you see “$contineproceed=’3′;” and the end brace “}“. After this paste the following…
?>
<!-- start Analytics E-commerce Tracking -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['_addTrans',
'<? echo $array['custom'] ?>', // order ID - required
'Lafayette & Rushford', // affiliation or store name
'<? echo $array['amount'] ?>', // total - required
'<? echo $array[''] ?>', // tax
'<? echo $array['shipping_1'] ?>', // shipping
'<? echo $array['ship_city'] ?>', // city
'<? echo $array['ship_state'] ?>', // state or province
'USA' // country
]);
<? foreach ($eshopcartarray as $productid => $opt){ ?>
_gaq.push(['_addItem',
'<? echo $array['custom'] ?>', // order ID - required
'<? echo $opt[postid] ?>', // SKU/code - required
'<? echo $opt[pname] ?>', // product name
'', // category or variation
'<? echo $opt[price] ?>', // unit price - required
'<? echo $opt[qty] ?>' // quantity - required
]);
<? } ?>
_gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
(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>
<?
5. Change “UA-XXXXXXXX-1” to your analytics code. Save. Upload.
6. Put some items in your cart and go to the Confirmation Screen, View the Source and see if it matches the cart. Then check your analytics E-Commerce section.
Important Template Note
eShop comes with a few templates, header, and footer files. If you put your regular analytics script in the “header.php”, you should make new header file and template file to NOT pull. For example, I copied “page-full.php”, renamed it “page-checkout.php” and had it pull “<?php include(TEMPLATEPATH . ‘/checkout-header.php’); ?>” instead of the usual “get header”.