Quick tweaks to improve Ahoy analytics data
Ahoy is a popular analytics gem that I use to track site visits and events in my Ruby on Rails apps.
The gem is well-documented, so I’m not going to walk through the setup here. But the quality of your analytics is only as good as the quality of your data – garbage in, garbage out – so I make a few tweaks to try and ensure the data is clean and usable.
Visits
Ahoy creates a visit when someone lands on your site. By default, visits are generated server-side, which can cause a few problems.
Controller actions
Some controller actions shouldn’t be recorded as visits at all. You can skip visit tracking in specific controllers by adding:
skip_before_action :track_ahoy_visit
Bot requests, disabled cookies
Server-side visit tracking can also lead to bot requests recorded as visits, or multiple visits recorded for users with cookies disabled.
You can avoid this by setting server_side_visitsto :when_needed, which defers visit tracking to JavaScript and only create visits server-side when they are needed by events.
# Add to your Ahoy inializer
Ahoy.server_side_visits = :when_needed
Active Storage hits
Serving images or files with Active Storage can also trigger Ahoy visits. You can prevent this by stripping cookies from these requests with the rack-strip-cookies gem:
Add to your Gemfile:
gem 'rack-strip-cookies', '~> 2.0.0'
Then in application.rb:
# prevent ahoy from tracking visits on Active Storage calls
config.middleware.insert_before(
ActionDispatch::Cookies,
Rack::StripCookies,
paths: %w[
/rails/active_storage/representations/proxy
/rails/active_storage/blobs/proxy
]
)
Events
Event names
While this isn’t really a ‘tweak’, I always make sure to follow a consistent naming system for my analytics events. This makes my dashboards and reports more intuitive, and avoids a mess of duplicates like ‘cart_created’, ‘CartCreated’, ‘created_cart’.
I like to follow the object–action framework: Project Created, User Invited, Article Updated, and so on. The naming convention doesn’t need to be clever, it just needs to be consistent and predictable.
Hi, I'm David Turnbull.
I'm an independent Ruby on Rails developer with a background in growth, marketing and analytics.
Recent posts
-
Quick tweaks to improve Ahoy analytics data
Jun 16, 2025
-
A few of my favourite things: 2023
Dec 22, 2023