0

Im setting up Ahoy gem into my application, but there are some options that I don't understand where to add.

This is what I have done currently:

1) Added gem "ahoy_matey" to my Gem File and ran Bundle install & restarted my server

2) Added //= require jquery & //= require ahoy to my application.js

3) Ran rails generate ahoy:stores:active_record and added new tables to my database by running rake db:migrate

I also have ahoy.rb in my initializers folder.

At this point I can see that ahoy is added in my application, but not sure how to do some stuff.

I want to add Ahoy.cookie_domain = :all & Ahoy.visit_duration = 1.minute, But I don't know where to add them

AND one other issue I have is that whatever page I go to is added to my Visits table (PS: I haven't added any JS or Ruby code for tracking in any pages or controller) and as I see it ,ahoy gem tracks all events/visits in my application. I only want to track events/visits on one particular action in my whole application, posts#show action and rest don't need to be added at all.

How can I achieve this?

1 Answers1

2

I want to add Ahoy.cookie_domain = :all & Ahoy.visit_duration = 1.minute, But I don't know where to add them

You add them in the initializer that you created (./config/initializers/ahoy.rb). This is precisely the purpose of files in that folder: Setting configuration values for the application to use.

whatever page I go to is added to my Visits table [...] I only want to track events/visits on one particular action in my whole application

The Gem's README says:

There are three ways to track events. [...] See Ahoy.js for a complete list of features.

So start there, and take a look at ahoy.js if you need some more advanced configuration. The gem is very flexible - you just need to read the documentation and configure it as needed.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • Thanks @TomeLord. `Visit_duration` works great, and I now manage to only track `posts#show` in my `ahoy_events`, but I still see `ahoy_events` are added and same with `visits`, but in my `ahoy_events`, it's `visit_id` is `nil`. This is what I added to my view: `- ahoy.track "Viewed post", id: @post.id`. What Im doing wrong here? –  May 03 '17 at 11:05
  • Are you still using `ahoy.trackAll();`? (Don't.) Have you restarted the server? (Do.) Is that view only being rendered by the `Posts#show` action? (You could also consider logging the event [in ruby](https://github.com/ankane/ahoy/blob/master/README.md#ruby), not JS.) – Tom Lord May 03 '17 at 11:25
  • Thanks for reply @TomLord, In my `Posts#show`, I haven't added any JS, only `Ruby`: `<% ahoy.track "Viewed post", id: @post.id %>`. And yeah I have restarted my server and yes, that view is only rendered by `Posts#show` action. As I can see from my tables, `Ahoy_events` works as expected and only tracks from `Posts#show`, **BUT** my `visits` table, track all visits on all views in my application, like *homepage, dashboard, posts#new etc* –  May 03 '17 at 12:14
  • Ahh right, sorry - I'm with you now. Again, please just check the documentation -- at a glance, it looks like you could for example place [`skip_before_action :track_ahoy_visit`](https://github.com/ankane/ahoy/blob/master/README.md#track-visits-immediately) in the `ApplicationController`. – Tom Lord May 03 '17 at 12:38
  • Thanks @TomLord. Just to double check and to see if its correct practice, Adding `skip_before_action :track_ahoy_visit` into `ApplicationController` will prevent all actions to not track visits, which means no visits will be tracked, But what I did was to add `skip_before_action :track_ahoy_visit` to all controllers **BUT** in my `PostsController` I did: skip_before_action `:track_ahoy_visit, except: [:show]`, and can see that visits only for this action is added to `visits` table. Can I do this? Thanks again :) –  May 03 '17 at 12:58
  • Yes, that would work. Or, you could just place `skip_before_action :track_ahoy_visit` in the `ApplicationController` and `before_action :track_ahoy_visit, only: :show` in the `PostsController`. However, can I please request that any further issues be raised as new questions in StackOverflow rather than us having a continued discussion thread in these comments? :) – Tom Lord May 03 '17 at 13:22
  • (Also, if you're ONLY using the `Ahoy` gem for this one tiny use case, then I'd seriously consider just writing your own implementation in that controller action rather than include the third party dependancy!) – Tom Lord May 03 '17 at 13:24
  • Thanks a bunch @TomLord. I was thinking about the same thing but I need GEO and UTM tracking that `ahoy` does. You'r the man :) –  May 03 '17 at 13:25