How Does Google Analytics handle 301 and 302 Redirects?

I’ve seen this question a zillion times, so please link the hell out of this so it shows up in search results and helps people (Matt Cutts, please ignore the previous statement, I’m just trying to help people). I also posted this at the Google Analytics help forums and will do a post on Search Engine Land next week, so hopefully one of these will rank and the world will be a happier place.

ONCE AND FOR ALL! LET’S DO THIS! 🙂

Google Analytics will report the ORIGINAL referrer to the ORIGINAL requested page if a server-side 301 (or 302) redirect is in place.

Let’s shake out an example.

You own mySite.com and have a page called unicorns.html. You decide that you want to make a better page for this, so you make “DOUBLE-unicorns.html”.

Then you realize that you want to remove the original unicorns.html because it is lame compared to your double unicorns page, but you want to make sure that people who linked to that old page see the new, awesome double unicorns page. Because you’re a good, SEO-conscious person, you do a 301 redirect from “/unicorns.html” to “http://www.mySite.com/DOUBLE-unicorns.html”.

OK, breathe…here we go.

Let’s say that the site unicornUniverse.com loved your original page, and had a link on it. Now that your 301 redirect is present, what happens when someone clicks on that link at unicornUniverse.com? What shows up as a referrer?

Yes: unicornUniverse.com is the referrer! And what is your landing page? DOUBLE-unicorns.html is your landing page.

This happens because the browser never actually sees the first file: the server redirects your browser and the field of the DOM that monitors your referrer isn’t updated. This field is referred to by the document.referrer variable, and a quick cheat to check what’s in it is to type the following in your location bar:

javascript:alert(document.referrer);

If you’re a browser ninja, you might have firebug installed, and you can look at the DOM map there to see the same thing.

Let’s take another example:

What if someone searches for “super awesome unicorn page” on Google, just MINUTES after you put your redirect up? Well, Google hasn’t had time to find your redirect and update the URL in their index! OH MY! What will happen?

All is well in the world (almost, see the last paragraph below for what’s not well in the world), because when someone clicks the link to http://www.mySite.com/unicorns.html in the Google search results, they will be 301 redirected to http://www.mySite.com/DOUBLE-unicorns.html. Referrer? google.com (the document.referrer will actually read: http://www.google.com/search?q=super+awesome+unicorn+page — that is how it knows your search engine keywords). And again, DOUBLE-unicorns.html will be the landing page.

So, direct is direct, search is search, referred is referred.

Exceptions:

There are times when a 301 redirect will NOT preserve referrer information, but they are rare. The most common example is when an https page links through a redirect on a non-https page. Again, drop the javascript above into your browser to audit this on your own site if this is a concern. If that script tells you the right referrer information, Google Analytics will be cool as a cucumber.

Also, if you’re trying to do a 301 that isn’t of the server-side variety (I’ve seen some people try to state 301 in HTML or through a JavaScript redirect — don’t ask), you will definitely have problems.

Google’s documentation on this refers to javascript and tracking redirects, and is MISLEADING. It is technically correct, but it is NOT talking about server-side 301/302 redirects. In the isolated case it refers to (which isn’t well explained), it is right.

Lastly, webkit-based browsers (Chrome and Safari among them) have a known problem with opening content in new tabs and windows. If a user right-clicks a link and selects Open in a New Window or Open in a New Tab, referrer data is LOST! MIND BLOWN! But if they simply hold CTRL or command (on a mac) while they click to launch in a new tab, referrer data is preserved. DOUBLE BLOWN!

KEEP IN MIND:

If you are using 301 redirects, make sure they preserve your tracking parameters (utm_source, etc., at the end of your destination URL). Commonly, parameters that may specify display, email, or paid search campaigns may be stripped. What happens here? The URL of the referring site becomes a referrer and your campaign information is lost.

For example:

1) Your email that someone reads on Gmail should be counted as source:”email” medium:”email” campaign:”super email campaign”

if you forget to keep your URL parameters, instead of this visit being attributed to the email campaign, it will count as a referral from google.com! WHAT?!? Yes!

2) You are running a paid search campaign and have the keyword “unicorn site.” You expect source:”google” medium:”cpc” campaign:”generic unicorn terms”

if you forget to keep your URL parameters, instead of the paid search campaign being recorded, this visit will be attributed to NON-PAID search!!! Holy shnikeys!

The last thing to keep in mind is that before 301 destination pages replace your original pages in search engines (or if you use 302 redirects, and the URLs are not replaced in that case), your actual link URL from the engines and your Entry Page will NOT be the same! In the example above, Google linked to http://www.mySite.com/unicorns.html, but the entry page was DOUBLE-unicorns.html. If you’re trying to compare traffic to entry pages with your ranking reports (tisk tisk for running ranking reports!), they will not mesh up if you marry them by URL!

Good luck!


1 Trackbacks

  1. […] Fantastic post by Evan LaPointe: […]

27 Comments

  1. You may have to set up the redirect to preserve parameters if it isn’t already. Keep in mind that document.referrer (what tells the browser what page referred to the current page) is NOT where Google Analytics (or most analytics tools like WebTrends and SiteCatalyst) get campaign data. The campaign is determined by reading a URL parameter. If that parameter isn’t present, the tool will use document.referrer. An example of where that would be bad is if someone clicked on an email ad from their Gmail account. If your campaign params are present, that visit will count as an email campaign visit. If they are missing, the referrer will be google.com.

    You can test this easily by just typing some fake parameters on the end of the page that will be redirected, like http://www.site.com/page.html?testparam=test. See if that goes to your next page and preserves the parameter “testparam”. If not, you’ll have to change your settings.

    Changing these settings depends entirely on your server (Apache, IIS, …), so you’ll just want to do a Google search for “preserve url parameters 301 redirect [servertype]”.

    Posted January 23, 2012 at 9:57 am | Permalink
  2. Lee

    Evan,
    Congrats on showing up #1 for this. I wish I could just put “so please link the hell out of this so it shows up in search results and helps people” at the top of every page and rank. 🙂

    I’ve read over all the comments and have a decent idea how to solve my problem but would appreciate some clarity.

    I have a client that wants to use a vanity URL for marketing materials and a more SEO friendly URL for search engines. They’ll be using a 301 redirect from http://www.somesite.com/vanity-url to http://www.somesite.com/seo-url.

    If I want to be able to track people that come directly to the vanity URL I will need to 301 redirect /vanity-url to /seo-url?utm_source=vanitry-url. I should get the data I need as well as not hurting any SEO (b/c Google recognizes parameters), right?

    On another related note to the above article, how is everything affected now with “secured search” in Google since it uses https, or does that even matter?

    Posted February 21, 2012 at 11:09 pm | Permalink
  3. Haha, thanks! Great strategy, huh?

    Sure, in this case you’re going to want to populate at least utm_source, utm_medium and utm_campaign. If you configure the redirect to add these three, you’ll be all set. Now, you do want to try to keep Google away from this vanity URL, because Google treats URL parameters as part of the URL. You do have the option of having GA read these campaign parameters after an anchor (#), rather than standard query params after ‘?’.

    It is rare for Google to index a page with utm parameters, but we see it all the time (check out the White House page in these results). It’s not enough to worry about: it will probably be counted as duplicate content and discarded (no, this isn’t as bad/scary as it sounds).

    Posted February 22, 2012 at 12:27 am | Permalink
  4. Forgot to answer your second question:

    Secure search will prevent the search keyword from being seen for organic searches. Feel free to email me if you have any other questions!

    Posted February 22, 2012 at 12:28 am | Permalink
  5. Lee

    So it seems as you would 301 redirect /vanity-url to /seo-url#utm_source=vanity&utm_medium=offlinemarketing&utm_campaign=offlinemarketing.

    The “#” should help with any possible duplicate content issues, I can also add these parameters into GWT and ignore them.

    Also, it seems as though your personal site redirects here now, whats your preferred email address? Feel free to reply to mine if you prefer. I definitely have questions here and there that I would love your insights on, as opposed to guessing the solution sometimes from what I can find on the big ol world wide web.

    Posted February 23, 2012 at 12:40 am | Permalink
  6. Yep, that’s right. And since this is an anchor, you shouldn’t have to do anything in GWT, as anchors are ignored.

    You will need to add _setAllowAnchor(true) to your GA page snippet to ensure GA looks in the right place. This must come before the initial call to _trackPageview.

    Posted February 23, 2012 at 11:10 am | Permalink
  7. Modi

    Hi,

    Nice post but I have a rather more complicated question which has to do with international sites.

    Let’s assume the site http://www.mysite.com consists of several subfolders, all targeting different markets. For instance:
    http://www.mysite.com/us
    http://www.mysite.com/uk

    Also imagine that 301 redirection takes place based on IP detection. A US-based user typing http://www.mysite.com will be redirected to http://www.mysite.com/us and a UK-based user will be redirected to http://www.mysite.com/uk.

    Having analytics set up for http://www.mysite.com, http://www.mysite.com/us and http://www.mysite.com/uk where are those visits going to appear and how are they going to be reported? Would the US visit only reported under http://www.mysite.com/us and the UK one only under http://www.mysite.com/uk ? Would these visits also appear in the analytics profile for http://www.mysite.com?

    Posted April 20, 2012 at 6:12 am | Permalink
  8. Do you have separate profiles tracking each translation, or do you have a single profile that is filtered into sub-profiles?

    Assuming it’s the first of the two choices, you are going to see nothing for http://www.mysite.com unless there are pages in that profile that are getting pageviews. Using an IP-based redirect, the user’s first pageview will be on the localization for their country, so that will be their landing page as long as the redirect is server-side, as it is in the scenario you’re describing.

    Does this begin to help?

    Posted April 20, 2012 at 2:35 pm | Permalink
  9. Dave

    Hi, thanks for the post.

    I am wondering if anyone has any creative ideas on how to track (capture as much information as possible) the linking out from an offline application to 3rd party websites? One idea I have had is to link to a site which automatically re-directs to the 3rd party site and do the tracking on this “middle-man” site.. The issue here is that I cannot run the analytics code on the destination site as I don’t own the domain. I also need the tracking code to run on the “middle man” site and therefore (I think) I need to do the re-direct on the page as opposed to at the server. Do you have any ideas on how to, 1: ensure the analytics code runs and 2: make sure it is as seamless/quick as possible.

    Thanks for any help/ideas

    Does anyone have any other ideas on how to do this, or on how to work around the delay?

    Posted June 19, 2012 at 5:52 am | Permalink
  10. Awesome post and amazing responses to the gazillion personal questions!

    My question is very basic. I am just migrating from an HTML static site to WordPress. I have a few pages that tend to be where people land via Google searches. What I want to know is will my server side 301 redirects for those pages continue building the page rank for the original addresses or will I be starting over with the new addresses?

    Example – original static page:
    http://roadslesstraveled.us/Budget.html

    My new WordPress page will be something like (not there yet):
    http://roadslesstraveled.us/rv-budget/

    So when I do my 301 redirect for this page from the old address to the new one, will the old address continue to increase in rank as people hit it from Google searches looking for http://roadslesstraveled.us/Budget.html, or will the effect of the page’s popularity be muted because folks clicking on my revised budget page from inside my site will be going directly to the new address http://roadslesstraveled.us/rv-budget/ instead?

    Thank you so much.

    Posted July 31, 2012 at 12:45 pm | Permalink
  11. Yep, you will accumulate page rank, however there is a dampening factor that may slightly erode the total possible credit (vs. if the links were direct). It isn’t enough to matter, in most cases, but it is there.

    Posted July 31, 2012 at 5:29 pm | Permalink
  12. Brian

    I’m working on a client’s site and notice in Google Analytics they have a default.asp page that’s receiving the most pageviews of all the pages on the site. The only thing is that this page doesn’t seem to actually exist on the site. I’m guessing it’s referring to the home page based on the traffic and title tag but there’s a 301 redirect in place to point from http://www.rootdomain.com/default.asp to http://www.rootdomain.com/

    So I’m confused as to why GA would show all of the traffic going to default.asp instead of to / since there’s a 301 redirect in place.

    Any ideas?

    Posted August 25, 2012 at 2:18 pm | Permalink
  13. Brian,

    Great question! This is likely due to a setting in Google Analytics, actually. If you go into your web property configuration, you can specify the name of a default page. That probably explains why you’re seeing this.

    Posted August 27, 2012 at 9:56 am | Permalink
  14. Great article. I am going to add it to the googleanayticstest.com website as a reference.

    Posted August 31, 2012 at 10:19 am | Permalink
  15. Brian

    @evanlapointe: Hey Evan, I really appreciate you answering my question. Would you mind clarifying where I would find that setting?

    Posted September 1, 2012 at 10:15 pm | Permalink
  16. Brian

    Okay, I figured out where that setting is. It’s in Profile Settings in the Admin. Thanks for your help and the great article Evan.

    Posted September 4, 2012 at 10:34 am | Permalink
  17. SO i got a friend who just did a 301 redirect to my t shirt shop.

    However, none of his traffic that he is sending me is showing up as his domain…

    it still shows up as a search….unless it is direct.

    this is clarifying your concept of search is search, direct is direct, right?

    any way to see what 301s gave me those search traffic hits?

    Posted September 5, 2012 at 7:47 pm | Permalink
  18. Hi there are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and set up my own.
    Do you require any html coding knowledge to make your own blog?
    Any help would be greatly appreciated!

    Posted September 24, 2012 at 12:27 am | Permalink
  19. Tom

    Hi
    Good post, still timely today. You wrote:There are times when a 301 redirect will NOT preserve referrer information, but they are rare. The most common example is when an https page links through a redirect on a non-https page.

    Is the opposite also true – a non-https page lnking to a https page through a redirect?

    Posted November 5, 2012 at 11:07 pm | Permalink
  20. @Tom

    Great question. The behavior is actually independent of whether a redirect is in place. Links from https to http drop referrer info. The other way around, that’s not the case. You can always test using the document.referrer method mentioned above, using a site that has the desired scenario in place.

    Posted November 6, 2012 at 2:24 pm | Permalink
  21. @evanlapointe: can you send me the documentation from Omniture help center regarding how 301s/302s are treated? I’d really appreciate it!

    Posted November 16, 2012 at 5:52 pm | Permalink
  22. Sorry, it’s behind a login in their help center. If you search for 301 redirects, you should be able to find it.

    Posted November 19, 2012 at 2:19 pm | Permalink
  23. Great web site. Plenty of helpful information here.
    I am sending it to a few friends ans also sharing in delicious.
    And of course, thanks for your sweat!

    Posted December 27, 2012 at 11:06 am | Permalink
  24. Great post! We are linking to this great post on our website.
    Keep up the good writing.

    Posted January 9, 2013 at 2:31 pm | Permalink
  25. Christine

    I’m having issues with parameters being stripped and loosing Analytics data. In most cases the only common denominator is a redirect but it won’t only not show the data correctly in Google Analytics it takes the user to a “oops we’re sorry” page which is a huge problem.
    For example http://www.website.com/shampoo is fine but if a param is added http://www.website.com/shampoo?utm_test=123 it will go to an error page. So essentially that’s where we’re sending traffic to for those pages-client is NOT happy.
    We have our SEO side pushing for 301 redirects (vs 302) which is fine for their efforts but on the paid side the only fix I can think of to remedy the issue is to get a list of every 301 redirect and change the URLs in our campaigns to match. Considering there are hundreds campaigns is very time consuming and not efficient.
    Any insight here or potential fixes? ANY help is appreciated!

    PS We use Marin for URL tracking not Google’s auto tagging

    Posted January 12, 2013 at 2:02 pm | Permalink
  26. Yeah, sounds like the site isn’t very friendly with URL parameters because of how it’s built. Google analytics allows you to use an anchor (#) instead of URL parameters to set the utm_ parameters, so you may want to explore that. You’ll need to allow anchor (check their documentation for most recent help on that) and then you can write your URLs in a way that GA will see but won’t upset your CMS. In this case, a 301 redirect probably won’t do anything (nor a 302 or any other kind). Not sure why they’re bringing that up, because it doesn’t sound like it has anything to do with the issue at hand.

    Posted January 14, 2013 at 1:11 pm | Permalink
  27. rob

    What if you put in AdWords url A that redirects the user, based on their searchphrase, to url B (on the same domain). How will analytics handle that kind of redirects?

    Posted February 20, 2013 at 12:06 pm | Permalink