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

How to trigger ahoy.track from a model ? #460

Closed
leoplct opened this issue Sep 8, 2020 · 5 comments
Closed

How to trigger ahoy.track from a model ? #460

leoplct opened this issue Sep 8, 2020 · 5 comments

Comments

@leoplct
Copy link

leoplct commented Sep 8, 2020

Hi,

I have to trigger the event ahoy.track 'User Sign up' in the User.after_create callback.
I want to avoid to call it in a controller (eg. thank you page).

after_create do
  ahoy.track(user_id: self.id, name: 'User Sign up')
end 

Error: ahoy is not defined.

@ankane
Copy link
Owner

ankane commented Sep 25, 2020

Hey @leoplct, Ahoy doesn't have an official method to access the ahoy object in a model. Unofficially (which could break at any time), you can use Thread.current[:ahoy].

@ankane
Copy link
Owner

ankane commented Nov 7, 2020

Cleaning up issues

@ankane ankane closed this as completed Nov 7, 2020
ankane added a commit that referenced this issue Dec 4, 2020
@ankane
Copy link
Owner

ankane commented Dec 4, 2020

Just added an official method for this on master - Ahoy.instance . It'll only be set if the model is used in a controller context, so you'll need to make sure your code handles the case when it's nil.

after_create do
  ahoy = Ahoy.instance
  ahoy.track("User Sign up", user_id: id) if ahoy
end 

@leoplct
Copy link
Author

leoplct commented Dec 17, 2020

Thank you.
I need to trigger an event when an ActiveJob is completed. Do you think this is the best approch?

I have updated to last version and run it from console but I got nil.

Running via Spring preloader in process 6612
Loading development environment (Rails 6.0.3.4)
irb(main):001:0> Ahoy.instance
=> nil

@InsightForesight
Copy link

I have meet the same issue, and fixed like this:

add a new method to module Ahoy, it create a fake visit and track events
# config/initializers/ahoy.rb
module Ahoy
  def self.non_visit_track(event_name, visit_properties: {}, event_properties: {})
    visitor = Ahoy::Visit.create(visit_properties)
    visitor.events.create(name: event_name, time: Time.now, properties: event_properties)
  end
end

and call it anywhere

Ahoy.non_visit_track(AppEvent::MembershipTransfer,
        visit_properties: { user_agent: 'Rails Console' },
        event_properties: { from_user_id: from_user.id, to_user_id: target_user.id })

Thank you. I need to trigger an event when an ActiveJob is completed. Do you think this is the best approch?

I have updated to last version and run it from console but I got nil.

Running via Spring preloader in process 6612
Loading development environment (Rails 6.0.3.4)
irb(main):001:0> Ahoy.instance
=> nil

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

3 participants