Google Analytics Tips & Tricks – Tracking 301 Redirects in Google Analytics

by Dan Cristo on April 7, 2009

There are many ways to redirect a web page, but few are SEO friendly, and even fewer can be tracked through Google Analytics. Here I’m going to show you a way of tracking 301 redirects with Google Analytics.

1) About SEO Friendly Redirects
Typically a 301 redirect, the most SEO friendly type of redirect, is setup on the server level via the .htaccess file or httpd.conf file. These are easy ways of setting up 301 redirects, but they don’t allow tracking via Google Analytics. So we’re going to setup our 301 redirect at the page level using scripting.

I’m going to be using the PHP scripting language in my example, because it’s an extremely popular scripting language, but almost any scripting language will do (see 301 redirect examples in other programming languages).

2) Setting up the Redirect

There are two snippets of code that needs to be added to the page you want to redirect. 

- 301 redirect command code
So here is an example of the 301 redirect command in PHP:
<?
Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: http://www.new-url.com” );
?>

- Google Analytics Tracking Code
<script type=”text/javascript”>
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>
<script type=”text/javascript”>
try {
var pageTracker = _gat._getTracker(“UA-YOURPROFILE-ID”);
pageTracker._trackPageview();
} catch(err) {}</script>

3) Results in Google Analytics
As long as the Google Analytics tracking code is in place on BOTH of the pages involved in the redirect, redirect page itself, as well as the destination page, you’ll see the redirect page in your Top Content report in Google Anlaytics.


(an example of the page being redirected showing up in Google Analytics – just for testing purposes)

**A big shout out and thank you to Hugo Guzman for the idea behind this post, and with help in testing**

Dan Cristo is a search engine optimization specialist at Zeta Interactive AgencyFollow me on Twitter

Share

No related posts.

{ 7 comments… read them below or add one }

seolion April 7, 2009 at 4:16 pm

php usually throw an error when you try to use redirection code after sending a response to the browser. Are you sure the above method works?

Reply

Dan Cristo April 8, 2009 at 9:32 am

@ seolion – Thanks for your comment seolion. You are right, the order of the header redirect and tracking code should be switched. I’ve edited the post to reflect that. Thanks for the great catch!

Reply

Marios Alexandrou June 3, 2009 at 10:40 pm

Am I reading this right in that you’re saying that JavaScript code on a page will be executed by a browser even if the header information contains redirect directives?

Reply

Nick Z June 8, 2009 at 2:10 pm

It would be great to know if Marios’s question is correct… Can you please follow up on this?

Also, if this works with collecting data in the redirected location from the redirected file, can you give us a full example of the code for the redirecting page?

Reply

Dan Cristo June 17, 2009 at 11:06 am

Hey Nick,
Marios comment was spot on. I’ve updated the post to reflect the update, so post is now correct.

Let me make sure I understand your question correctly… You’d like to see a full example of the redirecting pages code, not just the two snippets provided correct?

Reply

Jarad September 26, 2010 at 3:57 pm

This does not work. Did you test this? Maybe I’m doing something wrong so enlighten me. One one page, let’s call it redirect.php, I put the header code first, then put the Google Analytics (GA) code. The page redirects fine but the headers are sent out BEFORE the GA code meaning the page redirects without the javascript getting triggered. To prove this I go to mypath.com/redirect.php directly and view source but see not javacript.

Second idea, change the placement of the code so put GA first and the 301 below the javascript. This leads to a “Cannot modify header information” error.

So yeah, doesn’t seem to work for me.

Reply

sidder October 6, 2010 at 7:56 am

Nice reading this right in that you’re saying that JavaScript code on a page will be executed by a browser.That make a lot of scene.

Reply

Leave a Comment

Previous post:

Next post: