I have a handful of subdomains set up as redirects because we are using them for QR codes.

I want to be able to track the QR code redirects (which are already set up and printed so no changing them at this point) and see the effectiveness of each.

Here's two examples: http://qr.glorkianwarrior.com and http://ad.glorkianwarrior.com are set up to forward to our iTunes page (later on this year it may forward to Google Play or a specific landing page), is there any way on my server to track the redirect from the subdomain to iTunes and see where traffic is coming from first?

I have the redirects set up through cPanel presently using subdomains.

Edit: From the research I've seen I can't track a 301 directly. If I redirect to an internal page and then do a timed redirect to the iTunes link, how long will it take for the tracking script to track a hit?

2 Answers 2


You can do this with PHP and Google Analytics.

For each subdomain run a index.php file that will send data to Google Analytics and then give a header redirect response to send the visitor to itunes. Google will record the events in analytics and the user won't see anything different.

Here is an example for a PHP redirect:


Here is the documentation for Google's Measurement Protocol API:


You can get details on the API here:


There is a PHP library on GitHub for this:


  • According to a question on stack overflow, your method of sending events to the Google Analytics from a server side script is not supported. It says that now with Universal Analytics you could use Measurement Protocol Commented Jun 6, 2014 at 16:35
  • @StephenOstermiller Yes, you are correct. I referred to the old "ga" API and Google is switching to the new Universal which requires Measurement Protocol. I'll update my answer, thanks.
    – Reactgular
    Commented Jun 6, 2014 at 17:02

If I redirect to an internal page and then do a timed redirect to the iTunes link, how long will it take for the tracking script to track a hit?

There is a better way of doing this than just waiting for a specific amount of time. Google Analytics event tracking supports a "call back" function that you can pass it. You can create such a call back function that changes the URL and it executes as soon as Google Analytics tracking has finished.

I implemented something like this and use the following function which wraps around Universal Analytics:

function logeventga(category, action, label, value, callback, nonAction){
    var event = {
        'eventCategory': category,
        'eventAction': action
    if (label) event['eventLabel'] = label;
    if (value) event['eventValue'] = value;
    if (callback) event['hitCallback'] = callback;
    if (nonAction) event['nonInteraction'] = 1;
    if (typeof ga == 'function' && ga.hasOwnProperty('loaded') && ga.loaded === true) {
        ga('send', 'event', event);
    } else if (callback) {

This method checks to see if Google Analytics is loaded so that the callback function gets executed even if the analytics js gets blocked by AdBlock.

You could use it something like this called from the onload event:

logeventga('redirect', 'redirect', href, null, function(){
    window.location.href = href;

You would probably also want to set your Google Analytics snippet NOT to track a "pageview" on this particular page.

The disadvantage of using this approach would be that the redirect won't work for non-JavaScript enabled browsers and web crawlers. To make it work better for those browsers that would do a meta refresh after a few seconds if the JavaScript didn't execute for some reason. Google Analytics tracking usually executes within a few hundred milliseconds.

<meta http-equiv="refresh" content="5; url=http://example.com/">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.