---
title: Visualizing Ahoy analytics in Rails
published: "2024-02-12"
publisher: Honeybadger
author: Joshua Wood
category: Ruby articles
tags:
  - Ruby
  - Rails
  - Honeybadger
description: "Honeybadger co-founder Joshua Wood explains how to graph Ahoy page views in Rails with Chartkick, with a preview of our upcoming observability tool—Insights!"
url: "https://www.honeybadger.io/blog/ahoy-rails-analytics/"
---

At Honeybadger, we use [Ahoy](https://github.com/ankane/ahoy) for first-party analytics in Rails. Ahoy is excellent for developers because it lives in your Rails application alongside your other data and code. Want to answer a specific question about your product or website visitors? It's just one `ActiveRecord` query away:

```ruby
Ahoy::Event.where_event("$view", page: "/").count
```

Ahoy also works with [Chartkick](https://www.chartkick.com/) and [Groupdate](https://github.com/ankane/groupdate) to visualize your data. For example, after installing the two libraries, you can add a chart of all page views to any Rails view:

```erb
<%= line_chart Ahoy::Event. where_event("$view", page: "/"). group_by_hour(:time). count %>
```

The above code produces a chart like this:

![A line graph with a time series on the x-axis ranging from "Feb 7, 2 PM" to "Feb 9, 8 AM". The y-axis starts at 0 and extends upward, though the upper limit isn't visible; it's at least 600 based on the graph's peak. The line plot has significant fluctuations: starting at a value around 100, it spikes to just above 600, drops back near 100, rises again slightly above 200, then drops to nearly 0 where it remains flat until a small uptick near "Feb 9, 8 AM". This could represent a metric like website visitors or system performance over time, indicating a peak in activity or usage early on, followed by a period of inactivity or normal levels. The context of the data is not provided in the image.](https://www.honeybadger.io/images/blog/posts/ahoy-rails-analytics/ahoy-chart.png)_Charting Ahoy data with Chartkick_

Ahoy and Chartkick are great for tracking analytics and building dashboards, but the downside is that you must deploy your app to make changes. If you find yourself regularly deploying to update your dashboards, you could try using [Blazer](https://github.com/ankane/blazer), which lets you explore your data and create charts and dashboards with SQL.

![A screenshot of a web-based database interface showing a SQL query result. The SQL command "SELECT * FROM "ratings" LIMIT 10" is visible, suggesting the user has queried the first 10 rows from a 'ratings' table. Displayed are columns for 'id', 'user_id', 'movie_id', 'rating', and 'rated_at'. Five rows are visible.](https://www.honeybadger.io/images/blog/posts/ahoy-rails-analytics/blazer.png)_Screenshot from Blazer README_

That said, SQL can be complex to work with. At Honeybadger, we've used the Ahoy+Chartkick+Blazer stack for years and recently augmented Ahoy's data store to send events to [Honeybadger Insights](/tour/logging-observability/) so that we can explore our web analytics and create dashboards alongside our other observability data.

## Integrating Ahoy with Honeybadger Insights

Honeybadger Insights is a new [logging and observability tool](/tour/logging-observability) that we recently added to Honeybadger. You can use our query language—_BadgerQL_—to dive into your Honeybadger data (errors, uptime events, etc.) and create charts and dashboards from BadgerQL queries. You can also ship us your application logs and custom events to have all of your data in one place.

Here's how to integrate Ahoy with Honeybadger Insights and create a similar chart to Chartkick using BadgerQL.

In Rails, if you aren't using the `honeybadger` gem yet, you'll need to install it. Follow the [Rails integration instructions](https://docs.honeybadger.io/lib/ruby/integration-guides/rails-exception-tracking/#installation) (usually takes just a few minutes).

If you're already use the `honeybadger` gem, I recommend upgrading to the latest version. First, make sure the `honeybadger` gem is pinned to version `~> 5.5` in your `Gemfile`:

```ruby
# Gemfile gem "honeybadger", "~> 5.5"
```

Then upgrade to the latest version:

```sh
bundle update honeybadger
```

Next, open `config/initializers/ahoy.rb` in a text editor, and modify the default `Ahoy::Store` to look like this:

```ruby
class Ahoy::Store < Ahoy::DatabaseStore def track_visit(data) Honeybadger.event("ahoy_visit", data) super(data) end def track_event(data) Honeybadger.event("ahoy_event", data) super(data) end def geocode(data) Honeybadger.event("ahoy_geocode", data) super(data) end def authenticate(data) Honeybadger.event("ahoy_authenticate", data) super(data) end end
```

That's it. To test in your development environment, you can run `rails server` with the `HONEYBADGER_REPORT_DATA=true` environment variable to temporarily turn off Honeybadger's development mode.

```sh
HONEYBADGER_REPORT_DATA=true rails server
```

As Ahoy collects events, you should see them on the _Insights_ tab in your Honeybadger project:

![A screenshot of an insights dashboard in Honeybadger. It shows a search interface with a query for fields 'ts' and 'preview' sorted by 'ts' (timestamp). The timeframe of "2d -> now" indicates a search for the past two days up to the current time in PST (Pacific Standard Time). Below the search bar, the results display an "Events" view selected from a dropdown that also includes a "Line" view. Each entry corresponds to a timestamped event, labeled with "@ts" for the time in PST and "@preview" which contains JSON formatted data. The JSON data includes an "event_type": "ahoy_event" and an "event_id", with each event having a unique ID. All events are tagged with the name "$view", suggesting they are page or view-related events within the application, possibly tracking user interactions. The events are listed in reverse chronological order, the most recent being at "2024-02-09 12:02:11.000" and the oldest visible at "2024-02-09 11:53:53.000". The green checkmark indicates 100 of 100 results displayed successfully.](https://www.honeybadger.io/images/blog/posts/ahoy-rails-analytics/insights-ahoy-events.png)_Ahoy events in Honeybadger Insights_

To create a chart similar to the Chartkick example above, drop the following BadgerQL into the query box and hit _Search_.

```
filter event_type::str == "ahoy_event" | filter name::str == "$view" | stats count() by bin(1h) as date | fill date step 1h | sort date
```

![A web analytics dashboard with a search query for event tracking, specifically filtering for "ahoy_event" of type "$view". The results are aggregated by the count of events per hour, sorted by date and time in PST. The table shows several rows with 'count()' and corresponding 'date' columns, with counts of 549, 296, 76, and 270 events at different hourly intervals on February 7, 2024, indicating fluctuating user engagement over time. The latest times show zero events, suggesting no activity or data collection during those periods.](https://www.honeybadger.io/images/blog/posts/ahoy-rails-analytics/insights-ahoy-query.png)_For a walkthrough of the BadgerQL in this query, [check out my screencast on YouTube](https://youtu.be/o4qo3ulxps8)_

Finally, toggle the _Line_ button to visualize the data as a line chart:

![A line chart on a web analytics dashboard displays event counts over time. A prominent spike to nearly 600 events occurs around 17:00 on February 7, with other notable activity around 06:00 on February 9. The x-axis covers a period from 18:00 on one day to 12:00 the next, while the y-axis ranges from 0 to 600. The chart is part of the Honeybadger app interface with query filters visible, set to the PST timezone.](https://www.honeybadger.io/images/blog/posts/ahoy-rails-analytics/insights-ahoy-chart.png)_Ahoy chart in Honeybadger Insights_

Sending our Ahoy data to Insights means we can query our web/product analytics and user events in the same place as our observability data. It's a significant upgrade for product-led teams like ours. If that sounds like you, check it out!

---

## Try Honeybadger for FREE

Intelligent logging, error tracking, and Just Enough APM™ in one dev-friendly platform. Find and fix problems before users notice.

[Start free trial](https://app.honeybadger.io/users/sign_up)

[See plans and pricing](https://www.honeybadger.io/plans/)
