Posted by graeme
I wrote some god awful code recently to provide Google Analytics functionality for en e-commerce site we've been working on. For those of you are unaware of Google Analytic's e-commerce extended features, you can basically add transactions and line items to orders and record them in Analytics for conversion monitoring purposes.
As I mentioned I wasn't very happy with the way the code had turned out, so I had immediately decided that it needed refactoring. Ideally I wanted to simply add helpers to the view to generate the analytics code to insert. As we currently manage a number of ecommerce sites I decided I might as well make it re-usable. So I decided to write my first Ruby Gem.
SFAnalytics is available on rubygems.org, and the source code is of course on GitHub.
The gem provides three helpers:
- analytics_generate('UA-1234567-8') - Generates the analytics Javascript to be included in the page. It uses the new asyncronous version provided by analytics.
- analytics_add_item(item_hash) - Adds a line item to the Analytics request. Should be called before analytics_generate.
- analytics_add_transaction(transaction_hash) - Adds the transaction details to the Analytics request. Again this should be called before generate.
I'd imagine that there are a number of things I can do with this gem in the future, so if you have any ideas please let me know, or even better fork and a submit merge request!




