Home > Web Development > Tracking 301 Redirects with Google Analytics

Tracking 301 Redirects with Google Analytics

Post image for Tracking 301 Redirects with Google Analytics

by Lane on September 2, 2011

If you are anything like me you have several domains that are related pointing, or redirecting, to your primary domain name. The extra domain names can be helpful in protecting your personal or business brand. Of course you are careful to have 301 redirects in place pointing to your main site in an effort to keep Google and the other search engines happy. However, how do you know how many visitors to your site are there because of your related domain names? My intention this morning was to find out.

For the sake of this example we will use my primary domain name of lanevance.com and a secondary domain of lanevance.me.

Initially I started crafting a solution that was composed of funneling all traffic for lanevance.me to a simple PHP script that would load Google Analytics javascript to the browser before doing a meta refresh to another PHP script that would have the 301 redirect to lanevance.com. I quickly realized that this would give me data in Google Analytics but would negatively effect any possible benefits of the secondary domains in SEO. Rather than page rank flowing down to the primary domain, it would be stopping at the tracker PHP script. This just won’t do so it was time to start thinking creatively.

After a few minutes it came to me. Why can’t I treat each secondary domain like a campaign and add the URL tracking data to the redirects? At first I was not sure it would work but a quick test proved it is quite effective at doing the 301 redirect and showing the redirect in Google Analytics under the Campaigns section. Now I can see how frequently my other domains send me traffic and understand how visitors to that domain differ from visitors to my primary domain.

This solution is amazingly quick to set up! First, go to the Google Campaign URL Builder tool page. You can get as complicated as you’d like with this but I stuck to just the required fields:

Click to embiggen.

First, you want to enter your primary domain name in Step 1. Next, set the campaign attributes you want to track in a way that will be meaningful to you. I am simply calling my source “redirects” so I can lump all the redirects in to a single reporting location. I am differentiating the sources with the secondary domain name as the medium. Since it is a required field I labeled the campaign name as 301_Redirects. Notice I included an underscore (_) rather than using a space to avoid any issues with URL string character encoding. Use values that make sense to you. Once you have filled in the form click the “Generate URL” button and lastly copy the URL from the box in step 4.

The hard part is now done! To implement this, simply open your .htaccess file for this secondary domain name and pasted this URL in with just a bit more code. Obviously you want to replace my campaign URL with your own:

RewriteEngine On
RewriteRule ^$ http://www.lanevance.com/?utm_source=redirects&utm_medium=lanevance.me&utm_campaign=301_Redirects [R=301,L]

If you have only a single .htaccess file with all your domains pointing to the same site then you would add a condition to the redirect to only do so if the secondary domain name is used:

RewriteEngine On
RewriteCond %{HTTP_HOST} lanevance\.me
RewriteRule ^$ http://www.lanevance.com/?utm_source=redirects&utm_medium=lanevance.me&utm_campaign=301_Redirects [R=301,L]

Once you have some traffic to your domain and data collecting in Google Analytics you can find your reports at:

You will now see your new campaign showing up in the Google Analytics campaigns list:

Click to embiggen.

And to see the details on which of your secondary domains brought the traffic to your site click in to the “301_Redirects” campaign:

Click to embiggen.

There you have it, a simple way to implement and track traffic from your secondary domain names. Have you found another way to track your domains? Let me know in the comments section below!

Photo Credit: Bull3t
  • Thir

    Is there a way to not have it show the full url 9http://www.lanevance.com/?utm_source=redirects&utm_medium=lanevance.me&utm_campaign=301_Redirects) in the browser address bar?

    • http://twitter.com/matthegriz MattheGriz

      replied on comment thread

      • Terence Watson

        is there any way to do this, if the redirected url is on the same domain as the destiation?

        like domain.com/page.html goes to domain.com/page2.html

        its the same domain so I can’t really edit the htaccess, or can I?

        • lanevance

          @aab071baf3a19f4704d3c0dbd0f92c1f:disqus Do you just want to redirect page.html to page2.html? Do you care about tracking this in a campaign in Google Analytics?

          Depending on your web host and the platform your site runs on, you should have the ability to modify the .htaccess file for your site. It is a very common feature for servers running Linux/FreeBSD and Apache.

          • http://www.facebook.com/profile.php?id=100003000538210 Siddhu Arevindh

            i moved to new domain i want the path also what to du ?

      • lanevance

        Posted a reply to your other reply in the comment thread. :-)

  • http://twitter.com/matthegriz MattheGriz

    THIR on landing domain add this:
    RewriteCond %{QUERY_STRING} ^utm_source=(.*)&utm_medium=(.*)&utm_campaign=(.*) [NC]
    RewriteRule /* http://www.domain.com/? [R=301,L]

    • lanevance

      Thanks for the suggestion MattheGriz!

      By doing this you will be removing the Google variables (utm_*) from the query string. The unintended side effect will be when your web page loads the Google Analytics javascript will not have access to those variables and thus the action will not be recorded in your GA. You have to let the query string stay to be able to track the action.

      I suppose you could add some additional javascript to your target site after the GA code loads to redirect the page back to just the base domain. However, you will see 2 issues with this. First, that visitor will be counted as 2 visits rather than one, throwing off your stats. Secondly, your visitor may have already started reading or navigating your site when you force the refresh which may lead to a poor user experience. To me, the user experience is less impacted by just leaving the query string in place.

  • Domenic1

    I followed all the steps however my campaign is not showing up at all in my Google Analytics account. What did I do wrong?

    • lanevance

      @Domenic1, can you give us the domains you are trying this on? Sometimes it may take a day for the info to start showing up in Google Analytics.

  • Steve

    This worked, I just have a bunch of sub-domain landing pages… ie. register.mydomain.com, photos.mydomain.com, etc. So the only difference was that I created the campaign and just stuck that directly into my DownDoggy.com SUB-DOMAIN record. I’m not interested in the SEO traffic. It worked perfectly, now I can see all my referring 301 sub-domains.

    • lanevance

      Thanks for the additional suggestion on how to implement this! Glad this helped solve your problem!

  • Robert

    Hi,

    I am new at this so please bear with me.

    Do you have to add url generated by the Google URL Builder to the htaccess file or can it be used as the redirect itself? We have purchased a number of URLs to track direct mail campaigns from Go Daddy, and they offer a redirect screen instead of having to host each of these URLs separately. You are supposed to use their form to enter the URL you want to be redirected to, and then they automatically take care of updating DNS settings. So would it work to simply put the entire URL generated by the URL Builder here, or would this not work?

    Thanks!

  • http://twitter.com/messagemaker Robert

    Hey Lane, just to double check. This will allow you to know who got to your website via a certain domain… but you won’t know how they got to the domain right?

  • http://sightreadingmastery.com/ Evan R. Murphy

    It’s a great technique, but it won’t work for the case I need right now – tracking redirects to a hosted PDF.

  • http://reltrek.com/ Peter Hrbacik

    For those who need to track domains which are redirected to sites where is unable to place a tracking code, or just don’t want to use GA, we have created a simple service http://reltrek.com/domain-forwarding which provides bulk domain forwarding (with hosted DNS) and quite detailed analytics. (Sorry for self-promo)

  • Pingback: How to use Google Analytics to Track Printed Media Marketing Campaigns. | Showcase creativeShowcase creative

  • Roman Losev

    I solved the problem with javascript help. Wrote guide how to do it: http://pilotaz.blogspot.com/2013/06/tracking-redirects-with-google-analytics.html

  • Robert Gilbreath

    Nice post – thanks – quick question:

    Scenario: User goes to google and performs a search – one of my old pages shows in the results (a page I have just created a 301 quick redirect for using an app within wordpress) – They click on my result – are redirected to the new page.

    Question: Will this user show-up in Google analytics as a direct visitor? If not, how would they show as far as their traffic source?

  • Jonathan Berry

    Hey Lane, just found your post here. I was using a javascript to redirect after page load. That way the GA code ran and then the redirect. First issue, the problem of non-JS users not getting the redirect. So I added a regular “click here” link on the page. Second, I did not know if there were any SEO penalties for this method. Have you learned of any other techniques recently?

  • Simon

    Hi im new to this.
    if i add this in my
    .htaccess file

    RewriteEngine On

    Redirect 301 /index.php http://www.mydomain.com/webpagefolder/index.php

    RewriteCond %{HTTP_HOST} name

    RewriteRule ^$ http://mydomain.com/?utm_source=redirects&utm_medium=name&utm_campaign=301_redirects

    will this make Google analytics dashbord work?

  • http://nsiteful.com Jeff Cohan

    Perfect solution for a need I had. I’ve been using Google Campaigns a lot of late, for various purposes.

    Recently, I set up some 301 redirects in a site’s .htaccess file to accommodate my client’s direct mail campaigns. In those campaigns, they printed short “alias URLs” to redirect to pages whose actual URLs are too long (and too easy to either misspell
    or give up on).

    For example:
    http://example.com/widgets
    redirects to
    http://example.com/our-products/our-widgets.php

    But when I checked Google Analytics, I realized (duhhh) that the 301 redirects cannot be detected.

    I just implemented your solution, and voila!

    Thanks much.

  • Manikanta Reddy Doragacharla

    Hi,

    I have created as per your process but me it’s not working here is the code
    RewriteEngine On
    RewriteCond %{HTTP_HOST} motorheadmarketing.com
    RewriteCond %{HTTP_HOST} rloconnor.com
    RewriteRule ^$ http://www.autorepairmarketing.com/?utm_source=motorheadmarketing.com&utm_medium=redirect1&utm_campaign=motorheadmarketing
    RewriteRule ^$ http://www.autorepairmarketing.com/?utm_source=rloconnor.com&utm_medium=redirect2&utm_campaign=rloconnor

    The resulted URL home pare URL is comming like this(Search bar)

    http://www.autorepairmarketing.com/?utm_source=rloconnor.com&utm_medium=redirect2&utm_campaign=rloconnor

    If i entered my home page URL http://www.autorepairmarketing.com

    In this case also the URL is comming like this

    http://www.autorepairmarketing.com/?utm_source=rloconnor.com&utm_medium=redirect2&utm_campaign=rloconnor

    If you don’t mine please let me know any syntex changes for this code

    Thanks & Regards

    Manikanta

Previous post:

Next post: