Skip to content
forked from zevarito/mixpanel

Simple lib to track events in Mixpanel service. It can be used in any rack based framework.

License

Notifications You must be signed in to change notification settings

rojoko/mixpanel

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What is Mixpanel (the service) ?

Mixpanel is a real-time analytics service that helps companies understand how users interact with web applications. mixpanel.com

What does this Gem do?

  • Track events with properties directly from your backend.

  • Track events with properties through javascript using a rack middleware.

How to install?

gem install mixpanel

How to use it with a Rails application?

Add this to your environment config file or create a new initializer for it…

config.middleware.use "Mixpanel::Tracker::Middleware", "YOUR_MIXPANEL_API_TOKEN"

If you want to use the asynchronous version of Mixpanel’s javascript API

config.middleware.use "Mixpanel::Tracker::Middleware", "YOUR_MIXPANEL_API_TOKEN", :async => true

By default the scripts are inserted into the head of the html response. If you’d prefer the scripts to run after all rendering has completed you can set the insert_js_last flag and they’ll be added at the end of the body tag. This will work whether or not you opt for the aynchronous version of the API. However, when inserting js into an ajax response it will have no effect:

config.middleware.use "Mixpanel::Tracker::Middleware", "YOUR_MIXPANEL_API_TOKEN", :async => true, :insert_js_last => true

In your application_controller class add a method to instance mixpanel.

before_filter :initialize_mixpanel

def initialize_mixpanel
  @mixpanel = Mixpanel::Tracker.new("YOUR_MIXPANEL_API_TOKEN", request.env, true)
end

Then in each request you want to track some event you can use:

To track events directly from your backend…

@mixpanel.track_event("Sign in", {:some => "property"})

To track events after response with javascript…

@mixpanel.append_event("Sign in", {:some => "property"})

To execute any javascript API call

@mixpanel.append_api("register", {:some => "property"})
@mixpanel.append_api("identify", "Unique Identifier")

If you are proxying Mixpanel API requests then you can set a custom url and additionally stop the token from being sent by marking it as false if you’re going to let the proxy add it…

@mixpanel = Mixpanel::Tracker.new(false, request.env, true, 'http://localhost:8000/mixpanelproxy?data=')

Resque and Rails example

If you don’t want to use the built in Mixpanel Gem async feature bellow there is an example about how to make async calls using Resque.

Resque is a Redis-backed Ruby library for creating background jobs

 class MixpanelTrackEventJob
   @queue = :slow

   def mixpanel(request_env)
     Mixpanel.new(MIXPANEL_TOKEN, request_env)
   end

   def perform(name, params, request_env)
     mixpanel(request_env).track_event(name, params)
   end
 end

 class UsersController < ApplicationController
   def create
     @user = User.new(params[:user])

     if @user.save
       MixpanelTrackEventJob.enqueue("Sign up", {:invited => params[:invited]}, request.env)
       redirect_to user_root_path
     else
       render :new
     end
  end
end

Notes

There are two forms of async operation:

  • Using MixpanelMiddleware, events are queued via Mixpanel#append_event and inserted into a JavaScript block within the HTML response.

  • Using Mixpanel.new(…, …, true), events are sent to a subprocess via a pipe and the sub process which asynchronously send events to Mixpanel. This process uses a single thread to upload events, and may start dropping events if your application generates them at a very high rate.

Deprecation Notes

For a short term this method will be accepted but it will be deprecated soon.

Mixpanel.new

Collaborations

All collaborations are welcome to this project, please fork and make a pull request.

Collaborators and Maintainers

About

Simple lib to track events in Mixpanel service. It can be used in any rack based framework.

Resources

License

Stars

Watchers

Forks

Packages

No packages published