Posts Tagged ‘Referrer information’

How to use Google Analytics Tracking through RBS WorldPay

Monday, July 27th, 2009

A very technical post this time, so that others with the same problem don’t have to struggle as hard as we did:

What’s the Problem?

We encountered that Google Analytics shows the wrong referrer information when tracking through the payment pages of RBS WorldPay (Business Gateway). The payment pages are served by WorldPay, and Google Analytics shows WorldPay as the referrer, instead of the original referrer (e.g. an organic google search or an Adwords ad). The original referral/visitor information is lost. This in particular annoying for the tracking of e-commerce transactions on the payment response pages, which will all show WorldPay as the referrer.

All code snippets are in PHP and JavaScript. It shouldn’t be a problem to translate the snippets into other languages. This solution should also work with the announced changes to the WorldPay payment pages (which will forbid any JavaScript).

[Jump to solution summary]

What doesn’t work and why?

The Analytics help explains that you should add linker methods to your Analytics code for cross domain tracking between different servers (your server and the WorldPay server in this case):

var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setDomainName("none")
pageTracker._setAllowLinker(true);

on the second (linked) website and

onSubmit="pageTracker._linkByPost(‘https://select.wp3.rbsworldpay.com/wcc/purchase’);"

or

<a href="https://select.wp3.rbsworldpay.com/wcc/purchase" onclick="pageTracker._link(this.href); return false;">go to WorldPay</a>

on the first (linking) website.

These linker methods represent a workaround for the cookie based tracking that Analytics uses by default. Since a cookie can only be accessed by the web server placing the cookie, another website cannot use the same cookie to ‘continue’ tracking a user coming from a different server. The linker methods modify the target-URL of the link. On the target website, they also instruct Analytics to use the GET parameters (from the linked URL) instead of the (non-existing) cookie to retrieve the referral information, and to place a cookie with that information for the new web server. Hence, the linker methods ‘copy’ the cookie from the first (linking) server to the second (linked) server.

However, since you are reading this, you probably know already that this solution does NOT work with WorldPay.

We believe it is due to the internal re-directions on the WorldPay pages: WorldPay automatically redirects the user through different subdomains on their servers during the payment process, and there is no way to evoke the above mentioned linker methods for these redirects.

In addition, with the announced changes to the WorldPay payment pages (which will forbid any JavaScript), tracking on the payment pages wouldn’t work any longer anyway.

So what’s the solution?

The answer is to bypass tracking on the WorldPay pages altogether, and to track only on the payment response page . The payment response page is served by WorldPay, but you can instruct WorldPay to fetch the code from your server (WorldPay only asks you to include a special banner-tag, so that they can show some payment related information). While you cannot track the correct referrer information on the payment response page directly (because it is served by WorldPay, and thus doesn’t have access to the original cookie from your server with the referrer information), you can include an iframe from your server on the payment response pages . In this iframe (which may be hidden), you can include your tracking code for page tracking and for e-commerce transactions. The iframe is served from your server, so it has access to the original cookie and can read the original (and correct) referrer information. In the example below, we pass on our own order ID to track it within the track-transaction.php file.

On the payment response page, include:

<iframe id="wp_frame" src="www.your-server.com/track-transaction.php?id=<?php echo $order_id; ?>" style="border:none; display:none"></iframe>

That solves the problem. Well, almost. It solves the problem for all browsers except for… you guessed it… Internet Explorer (6 and higher). Internet Explorer with standard privacy settings does not accept third-party cookies. And your iframe is considered third-party content, because it is not served by the same host as the payment response page.

While pageviews and transactions are tracked with the correct referrer in FireFox, Chrome, Opera, and the other browsers, the pageview and transaction is not tracked for visitors using Internet Explorer.

A bit of internet research will tell you to include a P3P privacy policy using

header(‘P3P: CP=\"CAO PSA OUR\"’);

in the beginning of your code to overcome this problem. Placing this code on the page that serves the iframe content will instruct Internet Explorer to trust your site, and hence allow access to the cookie.

When you include the privacy policy in your code, the pageview and transaction tracking actually takes place, and you can see it in Google Analytics. But… You are back at square one (at least for IE), because it shows the WorldPay server as the referrer. For some unknown reason, the Analytics code is not able to read the correct referrer information from the cookie, and replaces it with a new referrer, just as if there hadn’t been any cookie at all.

To overcome this you have to use the linker methods mentioned above: On the page that links to the WorldPay payment pages (i.e., your cart or checkout page), you need to invoke the _getLinkerUrl method to prepare the correct URL:

var save_url=pageTracker._getLinkerUrl("www.your-server.com/track-transaction.php?order_id=");

Save that URL somewhere on your server (e.g. in the orders table of your database) or pass it through WorldPay using their hidden fields. Then, upon reaching the payment response page, retrieve this information and use it as the URL for the iframe:

<iframe id="wp_frame" src="<?php echo $save_url ; ?>" style="border:none; display:none"></iframe>

Finally, on the page containing the iframe content, you need to include the following statement to instruct Google Analytics to read the referrer from the GET parameters in the URL instead of from the cookie:

var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();

In addition, you also need to include the P3P privacy policy as mentioned above, because Analytics still needs to write the cookie back to the system.

And that’s it. A small step for mankind…

What it doesn’t do

The mentioned solution does NOT track on the WorldPay pages itself. It merely enables you to link the tracking before and after a user visits the WorldPay pages, so that you get the original referrer information for e-commerce transaction on the payment response pages. With the announced changes on the WorldPay payment pages (which will forbid all JavaScript), tracking wouldn’t be possible on these pages anyway. If you really need to track that a user has visited the WorldPay pages, you might try using an additional iframe in the header or footer of these pages, or calling an Ajax request on your own server upon linking to WorldPay. We haven’t tried that, though.

Solution Summary

Here is a page-by-page summary for those who don’t care how and why it works.

1) On your checkout page (the page that links to WorldPay, e.g. www.your-domain.com/cart.php or www.your-domain.com/checkout.php)

Execute the following statement just before transferring to WorldPay:

var save_url=pageTracker._getLinkerUrl("www.your-server.com/track-transaction.php?order_id=");

Save the "save_url" variable in your database or pass it through WorldPay’s hidden field. You need to access it on the payment response page.

2) On the payment response page (served by WorldPay, but fetched from your server)

Somewhere in the bottom, add:

<iframe id="wp_frame" src="<?php echo $save_url ; ?>" style="border:none; display:none"></iframe>

where $save_url is the URL string created using _getLinkerUrl in step 1.

3) In the iframe content page (track-transaction.php)

a) Add a P3P policy:

<?php
session_start();
header(‘P3P: CP=\"CAO PSA OUR\"’);

(click here for examples in other programming languages)

b) Add the _setAllowLinker method to your Google Analytics code:

var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();

and track the transaction using the _addTrans, _addItem and _trackTrans methods as explained in the Google Analytics help.

That’s it. Cheers!