My client is looking to set up a complex mixpanel tracking system on their Rails 3.2 app that contains extensive backbone.js functionality.
I want to abstract the mixpanel functionality into some sort of module (as opposed to having the calls sprinkled throughout the existing codebase).
One of the requirements is that the mixpanel calls don't get fired until a successful action, so I don't think I can attach the mixpanel calls to DOM elements.
My additional thought was to create some kind of mediator object in javascript that subscribes to the various backbone events that are needing to be tracked. However, only about half of the events that I need to track are in backbone, which makes me feel like I don't think I can use mixpanel in js at all.
Maybe a lib class that listens to the rails models?
Any thoughts or advice?
Thanks!
We ran into this same problem about six months ago, and decided to build an open source javascript library that would abstract Mixpanel's functionality across all the analytics services we use, including KISSmetrics, Google Analytics, Chartbeat, etc.
You can check it out at http://segmentio.github.com/analytics.js
The result is a simple API where you can call analytics.track() or analytics.identify() in one line of code, and send data to all the different providers.
The library also has helper functions like analytics.trackLink() and analytics.trackForm() to make it easier to track outbound clicks and form submissions, which usually cause the page to reload before the events can get sent. Those functions introduce slight delays to let the events get out.
You could certainly use one of the Ruby libraries in an ActiveRecord callback or Observer, doing identify()
with the user's ID to associate it with them.
Another option is to do the actions via AJAX on the client, then trigger the mixpanel event in a success
callback. If you need to send them to a different page, you can set window.location
after a timeout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With