ankane / ahoy

Simple, powerful, first-party analytics for Rails
MIT License
4.24k stars 377 forks source link

Manually setting user when creating an event #475

Closed abinoda closed 3 years ago

abinoda commented 3 years ago

I have certain controllers/actions where current_user is not set. Is there any way to manually pass in user or user_id when creating an event?

Something like this:

ahoy.track "My first event", {language: "Ruby"}, user_id: 5
abinoda commented 3 years ago

Not sure if this is officially supported but I came across https://github.com/ankane/ahoy/pull/411#discussion_r297085902

It looks like I could do:

ahoy.user = User.find(5)
ahoy.track "My first event", language: "Ruby"
ankane commented 3 years ago

Hey @abinoda, can you explain the use case a bit more?

One way to do it right now would be:

ahoy = Ahoy::Tracker.new(controller: self, user: user)
ahoy.track "My first event", language: "Ruby"
abinoda commented 3 years ago

@ankane I have certain controller actions that load users indirectly, ie. through a tokenized URL.

ankane commented 3 years ago

Makes sense.

The approach above should work for that. I've added a user_id option in the user_id_option branch, but not sure if it's worth the extra complexity.

abinoda commented 3 years ago

Cool. Yeah, I think the solution I have works. Would defer to you on whether offering the option makes sense. Thanks for your continued work on all these valuable open source projects! I previously built Pull Panda which heavily utilized groupdate and chartkick.

ankane commented 3 years ago

Thanks @abinoda

shayani commented 1 year ago

Hi @ankane. Is this working? I could not get Ahoy to set the user with

user = User.find_by(token: "my_token")
ahoy.track "User's calendar", {}, user_id: user.id
jacobperia commented 4 months ago

Hi @ankane. Is this working? I could not get Ahoy to set the user with

user = User.find_by(token: "my_token")
ahoy.track "User's calendar", {}, user_id: user.id

doesn't seem to work for me either.