Mobile app version of vmapp.org
Login or Join
Cugini213

: How to keep track of how many hits on a redirect page with Google Analytics I've written a simple redirect script in PHP that takes a resource ID and then redirects the user to the appropriate

@Cugini213

Posted in: #GoogleAnalytics #Redirects

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
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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Cugini213

2 Comments

Sorted by latest first Latest Oldest Best

 

@Heady270

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: 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:


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


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

10% popularity Vote Up Vote Down


 

@Annie201

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
developers.google.com/analytics/devguides/collection/analyticsjs/events

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme