mixpanel_typhoeus

This is a Mixpanel gem that allows you to make parallel API calls easily, thanks to being built on top of Typhoeus.

Usage

To track a datapoint, the API is similar to the JavaScript one.

MixpanelTyphoeus.track(event, properties = {})

There are two requirements for calling track().

  • You must set your API token first. MixpanelTyphoeus.token = 'mytoken'.
  • You must include either the ip or distinct_id parameters in the properties hash of each call.

The time parameter will automatically be calculated and sent with every datapoint, so you don't have to worry about it.

Serial (blocking) API calls

require 'mixpanel_typhoeus'
MixpanelTyphoeus.token = 'mytoken'

user_id = 10
result = MixpanelTyphoeus.track(:my_datapoint,
                                :distinct_id => user_id)
if result
  puts "yay!"
else
  puts "awwww :("
end

Parallel API calls

It's easy to make parallel API calls, since this library is built on Typhoeus/monster_mash.

require 'mixpanel_typhoeus'
MixpanelTyphoeus.token = 'mytoken'

user_ids = [1, 2, 3, 4, 5, 6, 7]

hydra = Typhoeus::Hydra.new
mixpanel = MixpanelTyphoeus.new(hydra)

completed_successfully = 0
failed = 0
user_ids.each do |id|
  # Queue each HTTP request to be fired when we call hydra.run
  mixpanel.track(:my_datapoint, :distinct_id => id) do |result, error|
    if result
      completed_successfully += 1
    else
      failed += 1
    end
  end
end

hydra.run # fire all requests, block until all complete.

puts "completed successfuly: #{completed_successfully}"
puts "failed: #{failed}"
puts ":)" if failed == 0

Contributing to mixpanel_typhoeus

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright (c) 2011 David Balatero / MediaPiston. See LICENSE.txt for further details.