Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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:

http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php

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

https://developers.google.com/analytics/devguides/collection/protocol/v1/

You can get details on the API here:

https://developers.google.com/analytics/devguides/collection/protocol/v1/reference

There is a PHP library on GitHub for this:

https://github.com/google/google-api-php-client

share|improve this answer
    
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 –  Stephen Ostermiller Jun 6 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. –  Mathew Foscarini Jun 6 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) {
        callback.call();
    }
}

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/">
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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