Google Analytics Worldpay Ecommerce Tracking
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" name="worldpay">
<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>
<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>






















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
[...] does appear that there are ways around this, and perhaps even ways of getting the cookie passed through Worldpay and back to your conversion page. However they require more investment than can be justified by the benefit provided to the client [...]
@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