WordPress eShop Google Analytics E-commerce Tracking Script

Posted on by

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”.

  • L Maarschalkerweerd

    Thanks for the code! Works perfectly! Is there any way to send the transaction to GA when it actually is completed? Maybe by using the code on the ‘Thank you’ page, or in paypay.php where the transaction actually is done?

  • http://www.stpetersburgwebdesigner.com dubROD

    This is my first involvement with eShop so Im doing this as I need to. According to everything Ive read on eShop no session data is sent to the Thank You Page, at least not through Authorize.net. My client isn’t using PayPal so I’m not sure. (in other words, not doing it for free =) . Furthermore, I despise PayPal and will never do anything to help them.

  • http://www.facebook.com/profile.php?id=650170602 Laurens Maarschalkerweerd

    PayPal was just an exemple. I’m using PayPal and iDeal. The script on the pages that handle these payments are similar. I was wondering whether it would be possible to use your script on those pages, after an transactions has been confirmed. It definitely needs some tweaking to work on those pages.

  • Peter Armenti

    Doesn’t work for PayPal unfortunately.. I tried it

  • http://www.stpetersburgwebdesigner.com dubROD

    Peter, it works no matter what merchant you use b/c it sends GA data on the confirm screen. Try re-reading it again.

  • Gagi

    Should there be a quote marks on $opt[postid] and on other $opt array keys?

  • Kara Lumsden

    Thank you so much for posting this! When testing the source in the confirmation screen as you suggested I noticed the store name was: ‘Lafayette & Rushford’, // affiliation or store name. Do you know why this is?

    Kara

  • http://www.stpetersburgwebdesigner.com dubROD

    Ya, you have to change that part to your Store Name. L&R was my client.

  • Kara

    Sorry I noticed that was your store name after I commented.

    I’m not sure about your last 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 “” instead of the usual “get header”.

    EPN is my merchant and I don’t see anything showing up with the tweaks above. Do I need to edit the above?

  • http://www.stpetersburgwebdesigner.com dubROD

    You dont want to call GA tracking code twice so if you have a global header like most wordpress themes the checkout page needs a header with no GA Tracking code.

    GA doesnt show stats on same day. You’d have to wait a day to see data in your ecommerce section.

  • Kara

    Ok sorry I’m new to all this SEO stuff. Sigh. Hope it gets easier as I go.. :)

    Thanks for all your help :)

    I’m a native to Dunedin and i REALLY liked the website you did for L&R!!!! The way you customized eshop is terrific!

  • Kara

    I have one more question. I placed my regular analytics code within the footer.php file. I’m not sure I understand how to remove this from the checkout page? Do you have any suggestions?