Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

E-Commerce Modules /

Discuss about the various e-commerce modules available:
Ecommerce, SS Shop, SilverCart and SwipeStripe
Alternatively, have a look the shared mailinglist.

Moderators: martimiz, Nicolaas, Sean, Ed, frankmullenger, biapar, Willr, Ingo, Jedateach, swaiba

SwipeStripe Google Anaytics Ecommerce Tracking


Go to End


4 Posts   2818 Views

Avatar
helenclarko

Community Member, 166 Posts

11 February 2013 at 3:42pm

Edited: 15/02/2013 9:47am

Hi All,

I have been looking around for ways to add The Google Analytics Ecommerce tracking code into SwipeStripe for the last couple of days.

I have had no success, but was wondering if I was going about this the right way.

I have been editing the Order.php file in the "Swipestripe/Code/Order" Directory, as I believe this is the page displayed when the user finishes purchasing items.

Can someone please let me know if the following looks correct, or if there is anything I should be doing?

The following has been added to Order.php:

class Order_Controller extends ContentController {
public function init() {
parent::init();
Requirements::insertHeadTags("<script type='text/javascript'>
var gaJsHost = (('https:' == document.location.protocol ) ? 'https://ssl.' : 'http://www.');
document.write(unescape('%3Cscript src='' + gaJsHost + 'google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E'));
</script>
<script type='text/javascript'>
try{
var pageTracker = _gat._getTracker('UA-CODE-1');
pageTracker._trackPageview();
pageTracker._addTrans(
'#$ID', // transaction ID - required
'Company Name', // affiliation or store name
'$Total.Nice', // total - required
'6.00' // shipping
);

// 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
pageTracker._addItem(
'#$ID', // transaction ID - necessary to associate item with transaction
'#$ID', // SKU/code - required
'$Item.Object.Title', // product name
'$Item.UnitPrice.Nice', // unit price - required
'$Item.Quantity' // quantity - required
);

pageTracker._trackTrans(); //submits transaction to the Analytics servers
} catch(err) {}

</script>");
}
}

Hopefully someone can point me in the right direction.

Thanks
-helenclarko

Avatar
helenclarko

Community Member, 166 Posts

13 February 2013 at 1:53pm

Hello all,

I have achieved the effect I was after!

I simply added the following to the top of the Order.ss file:

I do suggest making a copy of the "/swipestripe/templates/Includes/Order.ss" file and putting it into your "themes/{themename}/templates/Includes" folder.

<% control Items %>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol ) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-CODE-HERE");
pageTracker._trackPageview();
pageTracker._addTrans(
"#$Order.ID", // transaction ID - required
"NAME HERE", // affiliation or store name
"$Order.Total.Nice", // total - required
"", // tax
"", // shipping
"", // city
"", // state or province
"" // country
);

// 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
pageTracker._addItem(
"#$Order.ID", // transaction ID - necessary to associate item with transaction
"$Title", // SKU/code - required
"$Object.Title", // product name
"", // category or variation
"$UnitPrice.Nice", // unit price - required
"$Quantity" // quantity - required
);

pageTracker._trackTrans(); //submits transaction to the Analytics servers
} catch(err) {}
</script>
<% end_control %>

You will need to replace *NAME HERE* and *CODE HERE* of course.

I decided not to use the Shipping or Tax, although both wouldn't be too hard to add.

Thanks,
-helenclarko

Avatar
frankmullenger

Forum Moderator, 53 Posts

14 February 2013 at 9:49am

Glad you figured this out without too much trouble - just noticed the code you have below is in the Includes/Order.ss template that is also used in the email notifications, not sure if this will have a negative effect or not.

If you wanted to run this code just on the page that the customer is returned to after payment you could just move your code to Layout/AccountPage_order.ss.

You could also check successful payment if you like using $Order.Paid which will return true if order has been successfully paid for.

Cheers,
Frank

Avatar
helenclarko

Community Member, 166 Posts

15 February 2013 at 11:20am

Hi Frank,

Thanks for your reply.

I will defiantly keep an eye on google analytics over the next week, but I believe google wont accept double of the same order.
I realize using the script this way is not good practice and google may be receiving unnecessary data, but I will be keeping a close eye on it over the next week.

I'm glad you mentioned the checking of successful payments, however the bulk of our orders are through invoice and can be left unpaid for weeks.
Running the Google tracking code as it is at the moment allows me to capture all transactions.
As a negative side effect the bulk of those orders will not have been paid on the day the order was made, leaving google analytics with false information.

Thanks again for your time,
-helenclarko