Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex tracking implementation with mixpanel on Rails and Backbone

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!

like image 646
goddamnyouryan Avatar asked Feb 18 '23 19:02

goddamnyouryan


2 Answers

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.

like image 174
reinpk Avatar answered Feb 27 '23 11:02

reinpk


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.

like image 21
Aidan Feldman Avatar answered Feb 27 '23 09:02

Aidan Feldman