Google Analytics Tips & Tricks - Tracking 301 Redirects in Google Analytics
April 7, 2009 – 9:49 am by Dan CristoThere 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 Agency - Follow me on Twitter



5 Responses to “Google Analytics Tips & Tricks - Tracking 301 Redirects in Google Analytics”
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?
By seolion on Apr 7, 2009
@ 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!
By Dan Cristo on Apr 8, 2009
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?
By Marios Alexandrou on Jun 3, 2009
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?
By Nick Z on Jun 8, 2009
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?
By Dan Cristo on Jun 17, 2009