Google Analytics and Rails in 60 Seconds

July 8th, 2009

This quick post will show you how to install Google Analytics into your Rails powered website in less than 60 seconds.

Before I start, I need to ask you…

  • In real terms, what is each visitor to your website worth? Do you know? Would you like to find out how you can find out?
  • What is the conversion rate of your website? How many of your visitors sign up?
  • Can you benefit from increasing your revenue per visitor? (YES!)

If you want to know more about those topics, and find the answers to those questions subscribe to the RSS feed, posts covering those areas will be coming soon!


Ok, moving forwards. Installing Google Analytics into your Ruby on Rails website is very easy. First of all, ensure you are signed up for Google Analytics if you are not already.

Once you have your Analytics account setup and you are logged in, you should be able to see the website for which you are tracking statistics, and the tracking ID (will be in the format of “UA-nnnnnn-n”. You will need this now.

Add this code to your app/helpers/application_helper.rb:

def google_analytics_js
  '<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">
  _uacct = "UA-nnnnnnn-n";
  urchinTracker();
  </script>'
end

You need to replace the “UA-nnnnnnn-n” in that code with your own tracking ID. Save and close application_helper.rb.

Rails allows you to easily keep your website design easy to manage, by allowing you to use standardized site layout templates for your entire website (application.html.erb) or for specific controllers or even actions. These files are stored in the app/views/layouts/ directory. One at a time, open each of the files in this directory, and add the following to the end of the file, just before the closing “</body>” HTML tag:

  <%= google_analytics_js %>

All done!

If you want an alternative method of inserting your Google Analytics code into your application, there are a few existing plugins that will do the work (what work?) for you. Personally, for such a trivial task, why bother with a plugin? Here is a small lesson. Using plugins all of the time is not the right approach. It creates a whole new set of maintainability problems (what if rails changes the way it works, and those plugins no longer work after upgrade? What if the Google Analytics code needs to be updated, but the plugin is no longer being maintained, but you had no idea that was the case? In every case, the decision to use plugins or not can only be decided after weighing up the pro’s and con’s of each choice. My general rule (some exceptions obviously) is, if I can do it in 5 minutes or less, a plugin is probably not a good idea.

Remember to subscribe to the RSS feed if you want to hear about the business and real money value of your visitors.

Categories: Rails, User Experience

Tags: , , Leave a comment

Like This Post?

Subscribe for more...

3 Comments

  1. David Backeus

    I would use a partial inside the layouts directory for this. No reason to pollute the application_helper since there is no actual logic to it. And html in strings doesn’t look very nice in my opinion.

  2. Jaime Iniesta

    You can also save the analytics html code on a partial, for instance on ‘app/views/shared/_analytics.html.erb’ and include it on the layouts that use it like:

    <%= render :partial => ’shared/analytics’ %>

    Also, you should only include the analytics code on the production environment, so what I prefer to do is this:

    <%= render :partial => ’shared/analytics’ if RAILS_ENV == ‘production’ %>

  3. admin

    Partial’s would also work.

    My solution for never having my own visits being tracked by Google analytics has always been to add an entry to my /etc/hosts file:

    —-
    127.0.0.1 http://www.google-analytics.com
    127.0.0.1 ssl.google-analytics.com
    —-

    That way I don’t need to worry about writing different code for different environments. This also has the advantage that my own visit’s to a production site are never recorded either. For new sites that can be statistically significant in the beginning. Sorry, I should have mentioned I do that.

Feed

http://www.mendable.com / Google Analytics and Rails in 60 Seconds