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've written a simple redirect script in PHP that takes a resource ID and then redirects the user to the appropriate page.

The main motivation was if all of my links were to break because the service provider decided to disable them for whatever reason, I can update all links easily since they're in a single location.

Now that I actually have a redirect page up, I'm interested in knowing which resources are most popular.

I have looked at Google Analytics and it seems promising, but I am not sure how to set it up to capture what I want. My redirect page takes a set of parameters as such

http://mySite.com/redirect.php?type=RESOURCE_TYPE&id=RES_ID

I would like to keep track of each parameter (type, id) and the values. How can I accomplish this? Will there be a problem if the page does not load up completely before my redirect script kicks in and sends them off?

share|improve this question

2 Answers 2

On your redirect page use PHP to capture the parameters posted type and id, then fire off a custom event to track using those parameter values

https://developers.google.com/analytics/devguides/collection/analyticsjs/events

share|improve this answer

Google Analytics uses a JavaScript snippet to do all the tracking. No JavaScript runs on redirect requests. That means that Google Analytics can't track redirects directly. There are two approaches I use for this type of tracking:

Campaigns

If these redirects are tracking clicks from other sites, then you probably want to use Google Analytics campaign tracking features for them. To do so, have the redirect script append additional utm parameters to the redirect URL. Use the Google Analtyics URL builder to determine what the parameters should actually be. You could put the type and id into the utm_content or utm_source parameter, for example.

I use this method to track alternate domain names. When somebody types in my alternate domain alternate.example.com they get redirected to my real site with these types of parameters: www.example.com/?utm_campaign=domainnames&utm_medium=domain&utm_source=alternate.example.com

The data that I'm looking for then appears in Google Analytics under "Acquisition" → "Campaigns" → "domainnames".

You wouldn't want to use this type of tracking if these redirects are from your own site and you are planning to track other external campains. A user can only be attributed to one campaign at a time.

Cookies and events

Another way is to have the the redirector store the type and id to a cookie. Then the next pageview can report these items to Google Analytics as events.

You would have to include some JavaScript on every page that:

  1. Looks for the cookie with type and id.
  2. Uses JavaScript like this to send an event: ga('send', 'event', 'trackedredirect', type + " " + id, {'nonInteraction': 1});
  3. Deletes the cookie.

Once you did that, it would appear in Google Analytics under "Behavior" → "Events" → "Top Events" → "trackedredirect".

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.