For consistent behavioral and event tracking across Rails applications. Supports Mixpanel out of the box, and allows for implementing custom tracker plugins for additional providers.
Add this line to your application's Gemfile:
gem'event_tracker',git: 'https://www.github.com/returnly/event_tracker'And then execute:
$ bundle
Instantiate a tracker:
tracker=EventTracker::Tracker.new(doer_id,properties)where properties is a hash with common event properties, e.g. session information. Then, call tracker.track(event_name, event_label, properties):
tracker.track('return.refunded.merchant','Merchant Refunded Return',event_context)where event_context is a hash with extra information to be merged with the original properties, e.g. a tag that identifies which button triggered the action.
By default, it will use the Development tracker, which simply prints events to the console.
Add event_tracker.rb to your config/initialiers directory:
EventTracker.configuredo |config|
config.mixpanel_project_token='yourTokenHere'endRegister your tracker
defmixpanel_tracker@mixpanel_tracker ||= begintracker=EventTracker::Trackers::Mixpanel.new(EventTracker.configuration.mixpanel_project_token)tracker.register_tracker(EventTracker::Trackers::Mixpanel)trackerendendCreate a Tracker and corresponding Job in your project like so:
my_tracker.rb
moduleEventTrackermoduleTrackersclassMyTracker < EventTracker::Trackers::Basedeftrack(event_name,event_label,properties)EventTracker::Jobs::MyJob.perform_later(@doer_id,event_name,event_label,properties)endendendendmy_job.rb
moduleEventTrackermoduleJobsclassMyJob < EventTracker::Jobs::TrackerJobdefperform(doer_id,event_name,event_label,properties)track(doer_id,event_name,event_label,properties)endprivatedeftrack(*args)# Connect to your custom event-tracking provider here :)endendendendAnd to register your new tracker in your project:
defevent_tracker@event_tracker ||= begintracker=EventTracker::Tracker.new(current_user_id,session.to_hash)tracker.register_tracker(EventTracker::Trackers::MyTracker)trackerendendAfter checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/returnly/event_tracker :)