Last active
March 1, 2020 13:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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