Search code examples
ruby-on-railsahoy

Tracking Ahoy custom event in Rails model


I'm not sure how to use custom event tracking in ahoy. I followed the documentation and added a

ahoy.track "Viewed book", title: "Hot, Flat, and Crowded"

in one of my models, but I get

undefined local variable or method `ahoy'

Are controllers the only place where I can track ahoy events?


Solution

  • The problem is that ahoy is part of the controller/view layer, as is logs about user behaviour, like request information, browser, etc. The book-model should not know that somebody viewed the book.

    So even when you fix it somehow that you access ahoy from the model, it is not the right thing to do.

    It is better to do something like this in a controller:

    ahoy.track "Viewed book", title: @book.title