Tracking worldpay transaction with Google Analytics & getting ecommerce data in most accurate manner is amongst one of the challenging project that we executed for one of our clients recently. I wanted this blog post to address what we all did to capture most accurate Google Analytics data possible into worldpay.
Several issues need to be addressed before proper e commerce tracking can be executed.
Here is the design how worldpay works:
Worldpay doesnt allow javascript to be executed on their domains. So you cannot post any Google analytics code on green boxes as describe above. So technically, nothing can be tracked post step no.1 as described above.
To make this tracking work, several things needs to happen.
- We need a thank you page after worldpay transaction finishes which is on a domain where javascript can be executed. That is, we need to have one more step after (3) occurs which pushes user to move from (3) to a new page. That is the page where we can place Google analytics ecommerce tracking and track the value of transaction. Let’s call it step no.(4)
- More importantly, we need to pass all the Google analytics cookies to this thank you page ,so that the ecommerce code that gets executed on step no.4 attributes the transaction to original cookies that were present on product page or step (1).
So essentially we need to have a page at step (3) that provides a button on Worldpay that says click here to finish the transaction. I will come back later as to what other things that button needs to do.
So till now , by having that button “click now to finish the transaction” ,one problem is solved so that we have one page where we can put GA code & execute the addtrans etc functions of Google Analytics.
Lets focus now on how we can pass on same cookies that existed on step (1) to step (4). Normal google analytics functions of linkbypage will not work as they can only work if the next page can fire Google Analytics.
This is where this gets really innovative.
You can get all the required cookies one by one. But, there is a simple way to get all GA cookies required at once. By using the method, pageTracker._getLinkerUrl of Google Analytics, which will give you all the cookies required along with the URL.
Now, there is a concept of hidden variable in Worldpay. Hidden variables can pass different values like price, currency, order type etc. You can create a new hidden variable and pass the Google Analytics cookies into these hidden variable.
Generally, the RBS World Pay form (where the RBS World Pay button is located and from where the users goes to RBS World Pay site for payment) looks like the following:
<form action="https://select.worldpay.com/wcc/transaction" method="post">
<input type="hidden" name="cartId" value="Cart ID">
<input type="hidden" name="desc" value="Product Desc">
<input type="hidden" name="currency" value="GBP">
<input type="hidden" name="amount" value="XX.XX">
<input type="hidden" name="instId" value=137379>
<input type="hidden" name="lang" value="en">
<input type="hidden" name="subst" value="yes">
<input type="submit" value="Order by credit/debit card" name="submit">
</form>
There are some changes that you need to make for this to work:
- The form needs to be given a name, if one is not present. This can be done by adding name=”worldpay” in the form element.
- We also need to have one custom variable defined in RBS World Pay system which will have the value of the cookies required, that can be accessed in Thank you page. Here, the defined custom variable is MC_gacookie. In this particular example, we also have one more defined custom variable MC_analytics which will have account id. This is required only when pages with different profile ids leads to same Thank you page.
With these changes, the above code of RBS world pay on product page will look like the following:
<form action="https://select.worldpay.com/wcc/transaction" method="post">
<input name="cartId" type="hidden" value="Cart ID" />
<input name="desc" type="hidden" value="Product Desc" />
<input name="currency" type="hidden" value="GBP" />
<input name="amount" type="hidden" value="XX.XX" />
<input name="instId" type="hidden" value="”XXXXXX”" />
<input name="lang" type="hidden" value="en" />
<input name="subst" type="hidden" value="yes" />
<input name="MC_analytics" type="hidden" value="XX-XXXXXX-X" />
<input name="MC_gacookie" type="hidden" />
<input name="submit" type="submit" value="Order by credit/debit card" />
</form>
Now, let us see what extra statements that we need to add to the normal GA tracking script on product page (or step: 1) that is generally placed at the end of the page.
The modified GA tracking script looks like the following:
<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-XXXXX-Y");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
pageTracker._trackPageview();
var save_url = pageTracker._getLinkerUrl("www.abc.com");
var aPosition = save_url.indexOf("?");
save_url = save_url.substring(aPosition + 1);
worldpay.MC_gacookie.value = save_url;
} catch(err) {}
</script>
These additional statements are aimed to accomplish the task of saving all the required cookies as the value of the custom variable MC_gacookie that is created above.
Now, we need to store this value in a custom variable, MC_gacookie, which is specially created for this purpose. This value is accessed in the format <formname>.<variablename>
.value and to this, we assign the value of required substring, save_url, obtained above. In the above example, formname is worldpay and variablename is MC_gacookie.
So by doing above, we have been able to pass the cookie values to a specific custom variable to be passed into worldpay without the need of javascript.
What is needed now is what to be done on page 3 (in worldpay’s language its called callback page). In This call back page (step: 3), we get all the variables through post methods. Just to remind that this page (or call back page or step 3) is generally a purchase completion page. Here we are changing to just have a button which says “click here to complete the transaction” button. This call back page resides on website’s server however it gets executed on worldpay.
Now this button needs to pass the values of Google analytics cookies that we had been able to pass it via a custom variable (in our example MC_gacookie) in worldpay. If you use PHP, you can use following code to get that custom variable and form a query string (don’t confuse this with GA custom variable- this is WP custom variable).
<?php $parameter = 'MC_gacookie';
if($_REQUEST[$parameter])
{
$query="";
//Get all existing parameters
foreach($_REQUEST as $k => $v){
if($k != $parameter) {
$query=$query.$k."=".$v."&";
}
else
{
$query=$query.$v."&";
}
}
$query=substr($query, 0, -1);
?>
Here $currentpage is the complete path of the thank you page. So, $currentpage can take value like http://yourwebsite.com/thankyou.html
Note: You will get a warning message when you click on the button above.
So what you have done till now is, you have passed the GA cookies till step 3 and have it as a query parameter when user clicks on the button.
The thank you page will now have the following GA e-commerce code.
<script>
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>
var pageTracker = _gat._getTracker("UA-XXXXXX-XX"); //This needs to be set if this value is different
pageTracker._setDomainName("none")
pageTracker._setAllowLinker(true);
pageTracker._setAllowHash(false);
pageTracker._trackPageview();
pageTracker._addTrans(
"", // order ID - required
"", // affiliation or store name
"", // total - required
"", // tax
"", // shipping
"", // city
"", // state or province
"" // country
);
pageTracker._addItem(
"", // order ID - required
"", // SKU code (stock keeping unit)
">?=$_GET['item_name']?>", // product name
"", // category or variation
"", // unit price - required
"" // quantity - required
);
pageTracker._trackTrans();
</script>
30 Comments. Leave new
Hi,
Thanks for this post. I’ve found it very useful.
I am stuck on the part where you have to pass the Analytics cookie back to fabparty.co.uk. Do I do this in the resultY.html page of Worldpay? You mention $currentpage, but you do not mention $currentpage in the code.
Can you help?
Thanks
Jonny
Hi,
Useful post, this potentially solve a problem I am currently having with a client.
When you say “we need to have a page at step (3) that provides a button on Worldpay that says click here to finish the transaction” can this be crossed by having your WorldPay confirmation page automatically re-direct to Step 4 without the need to click? This is how my client currently has it set up and seems to negate the criticism of ‘burdening’ the customer with an extra click.
Also, and I’m a bit of a novice (and know I shouldn’t necessarily be trying this if I don’t understand it, but situations dictate…) with some of this so I apologise if I’ve completely missed something, but is there any reason in your examples of changes to the hidden variable code why the order of the input and type in each line was swapped around? I’ve assumed not and that it doesn’t matter as you don’t mention it.
Anyway thank you for your valuable post.
Matt
Hi,
Great article. I’ve been trying to work out how to go about fixing this Worldpay Analytics issue, and your article seems to describe a pretty good way of doing things.
One question. When you say “Note: You will get a warning message when you click on the button above.” does this mean that the buyer will get a warning message?
Cheers
@Matt : Thanks for your comment . If you set up automatic redirect please make sure that you append query parameters that contain google analytics cookies to destination of redirect.
The order of variable is immaterial here, however thanks for pointing , its just done by un-experienced developer like me.
@Stephan Yes This does mean that buyer will get a warning message. However this shouldnt be a huge issue as much as I believe.
Drop me a note at ravi at tatvic dot com if you are stuck anywhere.
Thanks,
@Jonny: $currentpage refers to page on fabparty.co.uk. Yes, you redirect to $currentpage in resultY.html page of worldpay with Analytics cookie.
Hiya,
Can’t wait to try this on our clients sites – its been annoying me for ages!
Nikki Rae
Hi Ravi, great post – just not sure how to pass the $query string we have generated from page 3 to page 4.
I am using a meta refresh on page 3 – if I append the $query string to the URL in the refresh code, how are the utm variables collected by google? Can it read the URL string automatically or do we need to specifically tell it to get the utm variables from the URL?
Hi Ashley, whenever first time the page loads, you need to make sure that the Google Analytics script doesn’t get executed. This you can make sure by checking url for one or some of the utm variables that we append. You need not do anything specific to get the utm variables from URL. The existing GA script on this page will take care of that.
Hi, I am a little confused on this as I am not a developer or familiar with PHP, however I have the job of this.
I have passed the GA cookie from my site to Worldpay.
On my resultY.html Worldpay page I have added the code you wrote above, but this is just being printed on the confirmation page. Am I doing something wrong?
There is no mention of $currentpage above anymore. I would like to use a refresh with the query string in so that the customer does not have to make a click. Do I just copy and paste the code above to do this eg…
<meta http-equiv="refresh" content="0;url=http://example.com? $v){
if($k != $parameter) {
$query=$query.$k.”=”.$v.”&”;
}
else
{
$query=$query.$v.”&”;
}
}
$query=substr($query, 0, -1);
?>” />
Hello,
This post seems to make sense, but is far too technical for me to set up unfortunately. I am looking for someone to help us track our conversions with google adwords. On our website consumers purchase products either through worldpay or paypal.
Does anyone know of someone who could provide a quote for this job?
Thanks
Well we have had issues with Worldpay and Google Analytics, and also Adsense when you talk about conversion tracking. I see this solution a nice work-around for something annoying and I am pushing my tech guys to work on it!
As someone concern about UI, it may not be the best flow, but anyway the user already paid so it’s OK 😀
Just an update that may be helpful, a customer reported today that the link on page 3 “click here to complete transaction” has had the href=”” stripped out (presumably by worldpay).
I have a meta refresh which works for most cases (i’m not sure why it didn’t work for this particular customer) but it’s a bit annoying to have the fallback mechanism ‘broken’ by worldpay.
So is there no way of automatically redirecting customers back to your website, without asking them to click to review order or to finish the transaction?
Hi Soraya,
Thanks for reaching out.
I am not aware of any redirection mechanism that can be allowed on worldpay servers !! If you can find out alternative, I’d appreciate if you can update the comments sections for everyone’s benefit !
Hi Ashley,
Yes that can happen, We have had that problem as well. If you can find out what browser was that, could potentially research more.
Thanks for connecting
Hi, i’ve just had another customer report this happening (meta refresh not working and URL removed from manual link) – I don’t think this is a browser issue, it seems to be server side.
Meta refresh isn’t disallowed by WorldPay’s whitelist however setting it to instant can cause problems (IE seems to follow it as a relative link sometimes and ends up on an error page on WorldPay’s domain) – 3+ seconds seems to work fine.
There’s no code to remove hyperlinks however the filtering in place which removes javascript does also remove invalid html attributes or tags so if you have a link with something like brackets / spaces / url encoded script etc then it’s quite possible this will be removed automatically.
Best way to verify any code is acceptable is to put it in a new html file and upload it via WorldPay’s “Payment Page Editor” which will give notes on any content which is forbidden upon upload, if it’s safe there then it should be safe on your results page.
Hi Ravi
I already have a sort of “Click here to complete” page. It executes an HTTP POST. If I just add the parameters in MC_gacookie as HIDDEN variables in the http post then will Google Analytics still pick them up?
Owen – I’ve set the meta refresh to 3 seconds rather than 2 on your advice
You say: There’s no code to remove hyperlinks however the filtering in place which removes javascript does also remove invalid html attributes or tags so if you have a link with something like brackets / spaces / url encoded script etc then it’s quite possible this will be removed automatically.
This link includes the Urchin variables which could possibly make the URL flag as javascript – is there any way to handle this safely?
@Ashley : sorry I havent come across a solution to avoid flag as of now. But if I do , I will definitely post it here
@Jon : Yes It will pick up value provided your last thank you page has code as provided in the blog post.
Hi Ravi,
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.
Ravi
thanks for this post which I realise was done quite some time ago. I am struggling to overcome this same issue on our site. I have carefully read your method and think I understand it but the problem is that our site uses the newer google code snippet in each product page HEADER opposed to the script in your method that goes in the BODY. I don’t know how to modify that newer code snippet to make your solution work. Any help would be much appreciated.
This is a ridiculously hard procedure to follow when you consider it just works on SagePay!
Hi,
Yes it may be, but since worldpay doesnt allow execution of javascript, you got to get work around. Is there a specific problem that you are facing that we can help you with ?
Hi Ravi,
Can I please clarify the meaning of ‘worldpay doesnt allow execution of javascript’ – is it
1) if you have javascript on your Worldpay page it will just be ignored ie not executed
or
2) Worldpay require you to remove any javascript from your Worldpay page source code before they will approve your website?
Thanks
Ju
Hi Ju,
I am referring to 1) if you have javascript on your Worldpay page it will just be ignored ie not executed
Hi,
Please can you let me know what method I need
to use instead of pageTracker._getLinkerUrl as we are using the
new async code. Please can you also confirm if any of the other tracking code
and implementation needs to change because of this new tracking code?
Thanks
Hi
You can use following code:
—–
_gaq.push(function() {
var pageTracker = _gat._getTrackerByName(); // Gets the default tracker. var linkerUrl = pageTracker._getLinkerUrl(‘ http://www.my-example-iframecontent.com/‘);
});
—–
Refer: https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._getLinkerUrl
For any additional requirements you can let me know your website URL via submitting your query through contact us : https://tatvic.com/contact/.
Thanks.
Hi,
Thanks for the detailed solution. Is there a reason that adding utm_nooverride=1 to the thank you page URL won’t work (before I create the detailed solution)?
Thanks
Peter
A great thank for your for the informative article. I am SEO beginner and give more time to setup eCommerce tracking code to our business but fail. But your post relay helps me. I will implement it to our business.
Again Thanks