Adding Google Analytics script to Sprockets
Published: January 30, 2014 (almost 4 years ago)
Updated: almost 3 years ago
The Google Analytics Script
((i, s, o, g, r, a, m) -> i["GoogleAnalyticsObject"] = r i[r] = i[r] or -> (i[r].q = i[r].q or ).push arguments_ i[r].l = 1 * new Date() a = s.createElement(o) m = s.getElementsByTagName(o) a.async = 1 a.src = g m.parentNode.insertBefore a, m ) window, document, "script", "//www.google-analytics.com/analytics.js", "ga" ga "create", "UA-123456789-1", "example.com" ga "send", "pageview"
Including in your view
In the above, the last line is the line we just added and the “if production?” on that line is a simple helper method in the app/helpers/application_helper.rb file:
We’re not done, yet!
If you were to deploy with just the above, you’d find out very quickly, you’re not quite done as the views would render this:
Notice how the Google Analytics script isn’t prefixed with the assets path. If you were to check it in development mode (after removing the if production? guard), you’d see this:
In seeing the output above, you would think that because everything’s served from the assets folder, it should likewise be served from the assets folder on production. However, this isn’t the case, and its one of sprockets greatest mistakes, I believe. Code that is tested and functioning in development should be expected to behave correctly when deployed to production. I am a big believer in failing fast and loud rather than later and silently. Luckily, there’s a gem out there called ‘sprockets_better_errors’ that makes Sprockets behave just like this and give you fair warning that your code’s going to fail in production. The only downfall this gem has is that it surfaces all other gems that have trouble with their own assets pipelines – Active Admin, currently being one of those! However, its a great asset to finding assets related problems early on in development. Back to the problem at hand, it turns out that if sprockets is configured to pull in application.js.coffee and anything in its manifest, but nothing else – which means, when you take out the “require_tree .” line, our new-fangled google_analytics script won’t get pre-compiled by sprockets. The solution is to modify config/environments/production.rb and add the following line inside the config block:
ComingSoon::Application.configure do # Other lines omitted for brevity config.assets.precompile += ['google_analytics.js'] end
Notice that the file name is specified without the coffee extension! With this solution in place, you should notice that rake assets:precompile now generates the fingerprinted version of google_analytics.js file into your public/assets folder. Your view will now render with something like the following:
<script src="/assets/application-f15feb4200e0d1db4963a00b28fbe9ca.js"></script> <script src="/assets/google_analytics-df845574fb2ba0fcbf892ecbdeae5107.js"></script>
Rails 4’s new assets pipeline processing has definitely stumped more than a few seasoned Rails developers, but sprockets does a comprehensive job of resolving many long-standing issues with having fully-expanded, individually included resources at development time that are seamlessly rolled up into one compressed/minified resource at production time. Hopefully, this brief article puts you on the right track to mastering sprockets on a more granular level.
a.k.a. Code Connoisseur
- [email protected]
- ICQ ‐ 25239620
- AIM ‐ mwlang88
- Yahoo! ‐ mwlang88
- Google ‐ mwlang
- Twitter ‐ @mwlang88
EducationBachelor of Science
Information and Computer Science
- On Hiring Good People
- Week Five in the Gym
- The True Power of the Internet
- Rekindling a desire to workout consistently
- I'd Rather Eat my Britches than Do This
- Mold Killer Recipe
- Gonna be Starting Something New
- Pitch Camp, what is it good for?
- Less communication can be more
- Let the Musings Begin
- Working on a Referral Pre-Launch Site
- Making Commitments, Reaching Out
- Preparing for Countdown
- Ground Zero
- A Reflection of the Technologies Built Things With
- Dynamic Routing in Rails Revisited
- Creating Dynamic Routes at runtime in Rails 4
- Adding Google Analytics script to Sprockets
- Gems you should consider for every Rails projects
- Weak Password will get you Hacked!
- Status updating...