-
Notifications
You must be signed in to change notification settings - Fork 3
rails google analytics
Last updated 20 June 2012
Guide to using Google Analytics with Rails. Current best practices for Rails 3.2. Google Analytics is a free service that provides detailed data about website traffic and usage.
This is a guide for developers using the example apps from the RailsApps repository. The RailsApps project provides open source apps and detailed tutorials for Rails developers. Next time you build an app, check the RailsApps project to see if we can save you some time with a starter app.
Follow @rails_apps on Twitter for updates and timely Rails tips.
If you don’t track usage of your web app and the source of traffic, you won’t have data to improve the site and you won’t know which traffic-building strategies are worthwhile. Tracking data closes the communication loop with your users; your website puts out a message and Google Analytics data shows how visitors respond. There are other services that help you track visitors and website usage but Google Analytics is free, easy to use, and familiar to most web developers.
To collect usage and traffic data, every web page must contain a snippet of JavaScript code, referred to as the Google Analytics Tracking Code. The tracking code snippet includes a unique website identifier named the Google Analytics Tracking ID, which looks like this: UA-XXXXXXX-XX
. You will obtain the JavaScript snippet and Tracking ID from the Google Analytics website when you set up an account for your website. You can set up an account for your website at any time. It’s advisable to set up the account before you deploy your app so you can obtain the Tracking ID and set up your app to use it. You’ll check your Google Analytics account later to verify that Google is collecting data.
The tracking code snippet loads a larger Javascript file (an 18KB file named ga.js) from the Google webserver that serves as a web beacon. The ga.js file is downloaded once and cached for the remainder of the session; often, it is already cached and doesn’t need to be downloaded because a visitor has (very likely) visited another website that cached the file.
Before December 2009, Google’s Javascript code could slow the loading of a page because page rendering could be blocked until the Google code finished downloading. Google introduced asynchronous JavaScript code to improve page performance. Now, the ga.js file downloads in parallel with other page assets. Page rendering can begin before the ga.js file is delivered. In practice, the ga.js file is often already cached.
Google recommended placing the original (synchronous JavaScript) snippet immediately before the final