Skip to content

Instantly share code, notes, and snippets.

@jeromedalbert
Last active March 1, 2020 13:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeromedalbert/d5448ab11ccc44c45bc0f3a14c7efd96 to your computer and use it in GitHub Desktop.
Save jeromedalbert/d5448ab11ccc44c45bc0f3a14c7efd96 to your computer and use it in GitHub Desktop.
Make the Mixpanel gem work with Rails Active Job. Should work with any background job backend.
# Make Mixpanel work with Rails Active Job.
# Should work with any background job backend.
class MixpanelAsyncTracker < Mixpanel::Tracker
def initialize
super(ENV['MIXPANEL_TOKEN']) do |*message|
SendMessage.perform_later(message.to_json)
end
end
class SendMessage < ApplicationJob
def perform(message)
args = JSON.parse(message)
Mixpanel::Consumer.new.send!(*args)
end
end
end
tracker = MixpanelAsyncTracker.new
# Tracking is the same
tracker.track('User', 'EventName')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment