Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events tracked via Resque never appear #14

Closed
shipstar opened this issue Aug 1, 2013 · 3 comments
Closed

Events tracked via Resque never appear #14

shipstar opened this issue Aug 1, 2013 · 3 comments

Comments

@shipstar
Copy link

shipstar commented Aug 1, 2013

I have a tracking job that I'm enqueueing via Resque. It's a fairly simple passthrough to Analytics.track:

# in an after_filter
def track_event
  Resque.enqueue MyAnalytics::Event,
    user_id: current_user.id,
    event: 'Created a thing',
    properties: { company_id: current_user.company_id }
end

module MyAnalytics
  class Event
    @queue = 'my_analytics'

    def self.perform(opts)
      Analytics.track opts.with_indifferent_access
    end
  end
end

However, I'm never seeing these statistics show up in Segment.io or any of the connected accounts.

Per the documentation, I also tried with

def self.after_perform(opts)
  Analytics.flush
end

but that just sits there for about a minute, then appears to timeout.

If I call Analytics.track directly in a console or in that after_filter, it works as expected.

Any thoughts? Am I doing something horribly wrong?

@calvinfo
Copy link
Contributor

calvinfo commented Aug 1, 2013

I'm less familiar with Resque, but do your workers run in new processes or threads? If that's the case, you'll have to call Analytics.init before running any sort of track commands since it spawns a separate thread to process the messages.

Analytics.flush shouldn't work for a minute in that case though, it should just exit immediately if there aren't any messages to be processed

Here's the code another one of our users have implemented:

module Segment
  module Tracking

    def self.perform(action, data)
      data.symbolize_keys!
      data.values.each { |v| v.symbolize_keys! if v.is_a? Hash }

      Analytics.init secret: APP_CONFIG[:segment_secret]
      Analytics.send action, data
      Analytics.flush
    end

    def track_event(name, properties)
      data = { user_id: current_user.id.to_s, event: name, properties: properties }
      Resque.enqueue Segment::Tracking, 'track', data
    end

    def identify_user(event)
      traits = { email: current_user.email,
                   name: current_user.name }

      data = { user_id: current_user.id.to_s, traits: traits }
      Resque.enqueue Segment::Tracking, 'identify', data
    end

  end
end

@shipstar
Copy link
Author

shipstar commented Aug 4, 2013

Calling Analytics.init does solve this issue. I'll go ahead and close it.

For the record, whichever user wrote this code would probably benefit from the changes I made last night. It should prevent them from needing to symbolize keys. They're symbolizing recursively, which I didn't account for, but I think analytics-ruby only looks one level deep in the hash for its keys anyway.

Also, if they're using ActiveSupport (which it seems like they are based on the presence of symbolize_keys!), they should look into Hash#with_indifferent_access.

@shipstar shipstar closed this as completed Aug 4, 2013
@shipstar
Copy link
Author

shipstar commented Aug 4, 2013

It might be worth adding a small snippet somewhere in the documentation that specifically highlights the need to call Analytics.init when a new process/thread spins up. It's covered in the Passenger + Unicorn sections, but it didn't jump out at me with regard to Resque (although it's obvious in hindsight).

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants