Skip to content

Instantly share code, notes, and snippets.

@khash
Created January 1, 2012 12:41
Show Gist options
  • Save khash/1547236 to your computer and use it in GitHub Desktop.
Save khash/1547236 to your computer and use it in GitHub Desktop.
Mixpanel Resque Job
require 'net/http'
class MixpanelTrackEventJob
@queue = :slow
def self.perform(name, params, user_id, user_ip = nil)
user = User.find(user_id)
params.merge!({ :distinct_id => user.id, :mp_name_tag => user.email }) if !user.nil?
params.merge!({ :ip => user_ip }) if !user_ip.nil?
track_event(name, params)
end
private
def self.parse_response(response)
response == "1" ? true : false
end
def self.request(params)
payload = JSON.generate(params)
data = Base64.encode64(payload).gsub(/\n/,'')
url = "http://api.mixpanel.com/track/?data=#{data}"
Net::HTTP.get(URI.parse(url))
end
def self.append_event(event, properties = {})
append_api('track', event, properties)
end
def self.append_api(type, *args)
queue << [type, args.map {|arg| arg.to_json}]
end
def self.build_event(event, properties)
{ :event => event, :properties => properties }
end
def self.track_event(event, properties = {})
options = { :token => AppSettings.api_keys.mixpanel, :time => Time.now.utc.to_i }
options.merge!(properties)
params = build_event(event, options)
parse_response request(params)
end
end
@mikhailov
Copy link

made it a bit smaller, 18 lines of code, mixpanel/mixpanel-ruby#13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment