Back to home

Exploring Ahoy Analytics With Rails 7

Published: Mar 15, 2022

Last updated: Mar 15, 2022

This post will cover my setup and first look at using Ahoy with a Rails application.

Nothing more than the basics will be covered, but it will give you a look at how you can quick-start your own Ahoy-analytics application.

Source code can be found here.

Prerequisites

  1. Basic familiarity with setting up a new Rails project.

Getting started

We will use Rails to initialize the project demo-rails-ahoy:

# Create a new rails project $ rails new demo-rails-ahoy $ cd demo-rails-ahoy # Install gem $ bundler add ahoy_matey # Generate install $ bin/rails g ahoy:install $ bin/rails db:migrate # Generate a controller for testing our analytics $ bin/rails g controller home show

In the above, we add Ahoy with the gem ahoy_matey, use the Ahoy generate for the installation process and then migrate the database for the changes made.

Finally, we create a Home controller for use to setup a basic "show" route localhost:3000/home/{id} that we can create some basic analytics against.

In our example today, we will just be showing how to track an event with the track method.

Updating our routes

Update config/routes.rb for the following:

Rails.application.routes.draw do resources :home, only: [:show] end

This will enable the endpoint /home/{id} that we are aiming for.

Next is to set it up in the home controller.

Updating our controller

We will be making a very contrived example for our controller.

Update the app/controllers/home_controller.rb file to the following:

class HomeController < ApplicationController def show ahoy.track 'home#show', search_id: params[:id] render json: { message: 'ok' } end end

At this point, our application is actually prepped to record our events.

To debug and test with cURL

We will do some testing using cURL, so the last thing we need to do is update our configuration just for that.

Update the config/initializers/ahoy.rb file to disable Ahoy.quiet and enable Ahoy.track_bots:

class Ahoy::Store < Ahoy::DatabaseStore end # set to true for JavaScript tracking Ahoy.api = false Ahoy.quiet = false Ahoy.track_bots = true # set to true for geocoding (and add the geocoder gem to your Gemfile) # we recommend configuring local geocoding as well # see https://github.com/ankane/ahoy#geocoding Ahoy.geocode = false

Setting those two values will override the defaults.

  • Ahoy.quiet enables debugging information to be logged to the console.
  • Ahoy.track_bots will not skip any events from our cURL requests, enabling us to fire up the Rails console and see what is happening when an event is tracked.

Running our code

In one terminal window, run the Rails server bin/rails s.

In another, let's run some cURL commands with different routes:

$ curl localhost:3000/home/1 {"message":"ok"}% $ curl localhost:3000/home/2 {"message":"ok"}% $ curl localhost:3000/home/nicer-id {"message":"ok"}%

In the Rails server logs, you will see a number of Ahoy::Visit and Ahoy::Event creations happening, which we can explore further in the Rails console.

In another terminal, fire up the Rails console bin/rails c and run the following:

$ bin/rails c irb(main):001:0> Ahoy::Event.all.count (0.7ms) SELECT sqlite_version(*) Ahoy::Event Count (0.1ms) SELECT COUNT(*) FROM "ahoy_events" => 3 irb(main):002:0> Ahoy::Event.last Ahoy::Event Load (0.2ms) SELECT "ahoy_events".* FROM "ahoy_events" ORDER BY "ahoy_events"."id" DESC LIMIT ? [["LIMIT", 1]] => #<Ahoy::Event:0x00007fe6eb124808 id: 3, visit_id: 3, user_id: nil, name: "home#show", properties: {"search_id"=>"nicer-id"}, time: Tue, 15 Mar 2022 04:42:47.915479000 UTC +00:00> # Checking out the Ahoy::Visit values irb(main):003:0> Ahoy::Visit.all.count Ahoy::Visit Count (0.1ms) SELECT COUNT(*) FROM "ahoy_visits" => 3 irb(main):004:0> Ahoy::Visit.last Ahoy::Visit Load (0.2ms) SELECT "ahoy_visits".* FROM "ahoy_visits" ORDER BY "ahoy_visits"."id" DESC LIMIT ? [["LIMIT", 1]] => #<Ahoy::Visit:0x00007fe6ec5235c0 id: 3, visit_token: "[FILTERED]", visitor_token: "[FILTERED]", user_id: nil, ip: "::1", user_agent: "curl/7.64.1", referrer: nil, referring_domain: nil, landing_page: "http://localhost:3000/home/nicer-id", browser: "curl", os: nil, device_type: nil, country: nil, region: nil, city: nil, latitude: nil, longitude: nil, utm_source: nil, utm_medium: nil, utm_term: nil, utm_content: nil, utm_campaign: nil, app_version: nil, os_version: nil, platform: nil, started_at: Tue, 15 Mar 2022 04:42:47.904552000 UTC +00:00>

In the above, we note that each request generated an Ahoy::Visit object as well as some Ahoy::Event events (as we expected).

After checking the count, we also logged out the last entry for each to get a look at what sort of information is eligible for collection.

Summary

Today's post demonstrated how to set up Ahoy analytics and also took us through some basic examples of how that might look.

Resources and further reading

Photo credit: bfilm

Personal image

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on Visibuild.

1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.

Exploring Ahoy Analytics With Rails 7

Introduction

Share this post