Basically, they disabled javascript from running on their servers. There's a page, the NotificationURL, which you create on the web site's server but that is pulled through by WorldPay, some tags replaced and then rendered from their server. We used to put the GA code here but alas, this is no longer possible.
So... you need a workaround. Now personally, I would work around it by not using WorldPay at all but sometimes these decisions aren't ours to make. Our first step was to do what any self respecting developer would do -> Google it.
I found this article:
http://www.tatvic.com/blog/google-analytics-worldpay-ecommerce-tracking/
Its actually very good and we used it as the principle behind fixing the problem but our implementation was a little different.
Mainly:
1) They use forms to post variables whereas we build up a querystring and redirect
2) They use... ugh.... PHP! :) We use ASP.NET.
My advice would be to read the article above first, admire the pretty pictures, understand the principle, try not to feel queezy at the PHP code or the occasional poor English and then come back here to see how to do it in our world.
.....
.....
Welcome back.
OK so hopefully by now you get the idea. We're wanting to modify the last page before we go off to RBS WorldPay so that we push the GA cookies through the transaction process. Then on completion, we retrieve those cookies and send them back to Google to be tracked.
Step 1: Send the GA Cookies to World Pay
Put javascript like this at the bottom of the page before the user is taken to WorldPay's Payment Page.
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
var save_url=pageTracker._getLinkerUrl("http://www.yourdomain.com/paymentComplete.aspx?key=value");
var aPosition = save_url.indexOf("&");
document.getElementById('<%=hfGoogleData.ClientID%>').value = save_url.substring(aPosition + 1);
</script>
Presumably you don't need telling that the UA-XXXXXXX-X needs to be replaced with your own Google Analytics Tracking number and that the domain name needs changing.
The _getLinkerUrl parameter is the URL of the page that the customer comes back to after being to WorldPay. Being that WorldPay doesn't auto-redirect the customer back to your website, you may not have had a page for this before so create paymentComplete.aspx. If you did have one already, just put the URL to that in here instead.
You may also need to change this line
var aPosition = save_url.indexOf("&");
The point of save_url is to have all of the GA cookies in the returned querystring. After you call __getLinkerUrl, it will be set to something like:
...sitedomain.com/paymentComplete.aspx?key=value&__utma=XXXX__utmb=XXX
In our case, we are already passing key=value so we're looking for the first & (highlighted) so that the substring only includes the GA stuff. If you don't have a querystring, you would search for ? instead, if you have multiple of your own querystring parameters, well then I'm afraid you need to write a little more javascript.
The other important thing in that javascript is:
document.getElementById('<%=hfGoogleData.ClientID%>').value = save_url.substring(aPosition + 1);
This is referring to a ASP.NET Hidden Field control that we've added to the page, just put this line somewhere near the javascript.
<asp:HiddenField ID="hfGoogleData" runat="server" />
All we have to do now is on the Submit to WorldPay button, grab the contents of that hidden field and pass it to WorldPay.
Thankfully WorldPay provide a useful feature whereby you can pass any custom variables and they will happily carry them and pass them back. You don't need to define them anywhere in WorldPay's system, the only criteria is that they start with MC_. This is our Submit to WorldPay button code.
Private Sub lnkPlaceOrder_Click(ByVal sender as Object, ByVal e as System.Web.UI.ImageClickEvent Args) Handles lnkPlaceOrder.Click
CreateOrder()
Response.Redirect(ConfigurationManager.AppSettings("WorldPayTargetUrl") & "&MC_analytics" & HttpUtility.UrlEncode(hfGoogleData.Value.ToString))
End Sub
The only really important detail here is that you must URL encode the value in the hidden field as it itself is a querystring and we want it to pass as one parameter not several.
Step 2: Get the Customer Back
The NotificationURL is the one that WorldPay pulls off your server and processes, its defined in your Production settings in your RBS WorldPay account. If you had GA code in there before, take it out, all you really need here is a button that reads something like "Click Here to Complete".
Herein lies the weakness of this implementation, if they don't click this button then you'll never get the customer to come back to the site and therefore will not get your tracking data!! So.. make the button look important!!
Key thing to remember here is to UrlDecode the returned (posted) parameter from WorldPay, for example:
<a href="http://www.yourdomain.com/paymentComplete.aspx?key=value&<%=HttpUtility.UrlDecode(Request.Form("MC_analytics"))%>">CLICK HERE TO COMPLETE</a>
Step 3: Track It
If you're fixing an old problem, your old code was probably in the NotificationURL page so you just need to move it here instead but here's an example of what should be on this page other than a nice message saying
"Thank you for coming back so we could track your every move, much appreciated!"
You may wish to alter the wording.
Step 4: Test It
Put an order through and then sit back, have a nap and wait to see whether your GA tracking ever comes through.
Hope you found this blog useful.
Happy coding.
Ross Coombes
Technical Director
3 comments:
If the tracking code is placed on a page served by the original domain, why do we need to persist the cookie data through the payment system?
Shortly after this post we decided to move our client away from WorldPay. GavinC was absolutely right, there is no need to persist the cookie data through WorldPay.
We did it because we were also passing something else through, we didn't want to change the database structure and it just made sense to keep everything together.
If you're having issues or you don't have those constraints then I would advise that you take GavinC's approach and bypass WorldPay altogether.
Hi Ross,
Thanks for the post I have been looking for this.
I was wondering if I can use the same principle and store analytics cookie data and once a visitor returns after making the purchase input it directly to GA along with ecom data. Basically avoiding passing data through the 3rd party cart. If we can do that we can essentially use it with all hostile payment solutions paypal worldpay etc without any tinkering.
I would love to know if this code can be used to achieve this and how?
Post a Comment