Create a simple JavaScript redirect that is trackable with Google Analytics

At work, we sometimes use a homegrown, .NET-based CMS for client websites. The CMS, while powerful and flexible, does have some limitations. For example, it has ugly URLs. Often, clients want pretty URLs to use in marketing materials or promotions. The easy solution is to create a vanity URL & redirect using IIS, but what if it needs to be tracked? For that, we use a simple JavaScript re-direct & Google Analytics event tracking:

[code]





[/code]

To set up this re-direct:

Let’s pretend we’re setting up yoursite.com/awesome.

  1. Create a directory in the site’s root called awesome.
  2. Create a new file in your new directory. You can use whatever file extension you want. We usually go with default.aspx or index.html.
  3. Note: Double check the default document in IIS on your directory. Make sure it’s set to whatever document you created. This allows yoursite.com/awesome to work without specifying the document.
  4. Edit the file:
  5. Change the title tag if you like
  6. Change the Google Analytics ID to match yours
  7. Set the category, action, and label that you want to call the event
  8. Change the URL to whatever you want it to go to
  9. Navigate to yoursite.com/awesome and verify it’s working.
  10. The data will show up in event tracking in Google, and will not increment your regular page views.

That’s pretty much it. Since the Google JS calls are asynchronous, we add a little delay to force the tracking to increment before the page redirects.

This redirect method is useful only if you plan to track these redirects as events; if not, it’s easier to set them up in IIS alone.

Andrew Block

I (Andrew Block) am the Director of Analytics and Insights for MSP-C, an awesome brand journalism company in Minneapolis, MN. Insights are everywhere - and this blog explores that premise. One part geek and one part analyst, I like to blog about techy and insightful things. Or insightful techy things...or....

Andrew Block wrote 119 posts

Post navigation

  • Alice

    EXACTLY what I was looking for. Thank you!

    I have ads on my site and I want to have mini versions of the ads in my feed. With this scenario, I can combine the counts for the category (Ads) and labels (AdvertiserABC, AdvertiserDEF, etc). I created a new aciton called “FeedClick”. So I can see that I had 400 total Ads clicks, that AdvertiserABC got 50 of them (20 from FeedClicks and 30 from Outbound). Woohoo!