Google Analytics in your Rails application using javascript_include_tag
A DRY, syntactically elegant way to track the traffic of your Rails application with Google Analytics
52 comments no linksHere's a (mostly) idiot-proof way to include Google Analytics tracking code in your Rails (or other Ruby-based framework) application.
First, I created a footer partial in /app/views/all/_footer.haml (yes, I use mostly HAML, except for in long forms -- the syntax is truly poetic), with the following lines at the top:
#!/app/views/all/_footer.haml = javascript_include_tag "analytics/one" = javascript_include_tag "analytics/two"
Easy enough. If you reload your app at this point, surprise surprise, you'll get an error message, because there's nothing at either of the javascript_include_tag locations. It's okay, we'll fix that now.
First create a directory called 'analytics' in '/public/javascripts/'. This saves us the hassle of having to use names like analytics_one.js and analytics_two.js, which is ugly, and redundant, and clutters up our delightful public/javascripts directory which we'll likely want to populate with all kinds of goodies later. But anyway...
Now we have to create the two javascript files, each with its respective half of the analytics code. Why two? As you'll notice, Google has included our plug-and-play analytics code inside of two separate script tags. The first being the Analytics javascript library that Google provides, and the second being the trigger for said library. While we might get away with combining them into one file on some systems, it's best practice to keep them separated into two.
That said, let's create the first:
#!/public/javascripts/analytics/one.js 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"));
Cool, there's the library. If you've created your analytics account recently (around the publish date of this article), you should be able to plug this code directly into your own file, and you'll be good to go. This is not so with the second file:
#!/public/javascripts/analytics/two.js try { var pageTracker = _gat._getTracker("UA-XXXXXXXX-X"); pageTracker._trackPageview(); } catch(err) {}
This gentleman should look primarily the same, but you will, of course, have to replace the series of nine X's "UA-XXXXXXXX-X" with your own Analytics id code that Google provides (note the hyphen before the last digit).
Good. With that out of the way, we've got our two wonderful bits of analytics javascript, and even a delightful little footer to call them so that we don't have to paste them into every single view or layout that we have, and can edit them more easily if for whatever reason we need to. Certainly very DRY.
NOW, because we've got our _footer.haml partial, all we need to do is render this at the very bottom of whatever layouts we may have employed at /app/views/layouts. If you don't have any layouts at this point, tisk-tisk. You really should. And if your application is small enough as not to warrant the use of layouts, you should be using Sinatra.
Anyway -- layouts. They're good. For the sake of argument, we'll pretend you have a layout, and that it looks something like this:
#!/app/views/layouts/application.haml !!! %html -# <head> code goes here </head> %body #content #primary = yield #secondary = render :partial => "some_partial" = render :partial => "another_partial" = render :partial => "/all/footer"
See that last render :partial => call? That's our footer. Yeah, I jumped the gun a little bit. We call it with "/all/footer" rather than just "footer" because it's in our views/all directory.
Remember, if you have additional layouts, for, say your articles controller, or users controller, you'll have to render the footer partial in the same way on those in order for their pages to be tracked accordingly.
It's also important to note (for HAML users, anyway), that the footer partial is four spaces from the left margin, making it even with the #content div and one level below the %body tag. This is because, as Google recapitulates many, many times, their code should ideally be the last thing included before the
The ghd styler comes with 1" plates. It has a black exterior with gold metal plates. This item looks very similar to the CHI hair straightener by Farouk. 'ghd' is written in red font on the top. This product is definitely at the very top of the list for GHD Styling Set. Not only does this product provide to-die-for results but it also is very reputable.cheap mbt shoes