2

I'm doing a marketing campaign for an app where I'm targeting different schools. I want to know how many people have clicked on each link. The number of schools is pretty big, so I don't want to create a separate redirect or bit.ly for them each.

Ideally, this is how it could work:

Set up .htaccess to redirect mysite.com/download to go to the app store, then I post different links for different schools with the formats like: mysite.com/download/someschool and mysite.com/download/otherschool, and get analytics on how many people clicked on each link.

However, Google Analytics won't log this because the redirect happens before the code is run (or so I hear), and now I'm not sure what else to do. Any help would be appreciated.

2 Answers 2

1

Set up .htaccess to redirect mysite.com/download to go to the app store, then I post different links for different schools with the formats like mysite.com/download/someschool...

Assuming that you're redirecting everything after /download/ to an external site (i.e., the AppStore), this is the same as tracking Outbound links as Events.

As indicated by Google here:

Because links that lead away from your site are not automatically tracked by Google Analytics, you will need to manually tag all outbound links you want to track. To do this, you will add some custom JavaScript that uses the _trackEvent method to record these links and then modify the links you want to track.

As also highlighted there:

While it is also possible to use the _trackPageview method to record outbound links, using the _trackPageview method will cause your overall pageview count to increase. For this reason, we recommend using Event Tracking to record user activity not related to actual pageviews.

As indicated in the example provided there:

With this structure in place, you could then see Outbound Links as one of the event categories and drill down to see which particular outbound links are the most popular.

The steps for tracking outbound links covered in an example there (edited for your needs) are:

  1. Delay the outbound click by a fraction of a second (to provide the browser more time load the tracking code) by adding this JavaScript before your </head> tag:

    <script type="text/javascript">
    function trackOutboundLink(link, category, action) { 
    
    try { 
    _gaq.push(['_trackEvent', category , action]); 
    } catch(err){}
    
    setTimeout(function() {
    document.location.href = link.href;
    }, 100);
    }
    </script>
    
  2. Then you can log each click on your school links by adding an onlick event to trigger the trackOutboundLink function above:

    <a href="http://www.mysite.com/download/someschool" 
    onClick="trackOutboundLink(this, 'Outbound Links', 
    'appstore.com'); return false;">
    

As specified:

The example above uses the category label Outbound Links. This is a useful way to categorize all outbound links in the Event Tracking reports. It sets the specific name of the website as the second parameter in the call.

So change appstore.com above to whatever external site you want to attribute the link to. Also be sure to use return false; as indicated in the example.

By using the above, you'll then be able to track how many people clicked on each school link and which link is most popular.

0

Your solution sounds to be along the right lines. If you want to track the number of clicks on the links, use virtual pageviews and append an onclick event to your link:

onclick=”_gaq.push(['_trackPageview' ,'/download/someschool']);”

This way it doesn't matter if the next page redirects, the page view would be tracked before the person leaves the page that contains the link.

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.