Nimble Industries

Dump Google Analytics and use Ahoy to track your Rails app traffic

D

StatusGator is built on Ruby on Rails, a popular choice for rapid web application development for more than a decade. One of the many benefits of Rails is its rich ecosystem of open source gems which can provide massive value quickly. We discovered one such gem, Ahoy, on our recent quest to understand how our users find out about StatusGator.

StatusGator gets about 3,000 visitors and 20 sign ups every month, but it’s not always clear where those are coming from. We do use Google Analytics but many people justifiably block Google Analytics with ad blockers, so they can avoid being stalked around the web by Google. In fact, only 4 out of every 10 user sign ups on StatusGator are recorded by Google Analytics, suggesting 60% of StatusGator users are using ad blockers. 

60% of StatusGator users are using ad blockers

Ultimately, we would love to move away from Google Analytics. At a minimum, we wanted have an alternative data source. Our goal is not to play a cat and mouse game around tracking blockers. If users want to block Google from following them or even just block referral data from their requests, then we encourage that. But relying on a third party means we don’t own the data. Even our most basic data, such as how many people visit StatusGator each day, is locked into someone else’s platform.

How, then, could we easily get this basic traffic data into our own app and own database? Ahoy was the answer and it could not have been easier.

Installing Ahoy

First, we added the Ahoy gem to our Gemfile:

gem 'ahoy_matey'

Then, ran the supplied generator which creates the requisite data migration, and we ran that migration:

rails generate ahoy:install
rails db:migrate

That’s it! With a quick push to Heroku, our visitor tracking was live. Visitor and referral data flows into a table called ahoy_visits in our Postgres database. We were thrilled to have access to basic visitor counts and metrics without being tied to Google.

Tracking Ad Campaigns with UTM Codes

Upon inspecting the data, we noticed that Ahoy includes fields for UTM codes:

utm_source
utm_medium
utm_term
utm_content
utm_campaign

These codes are a universally accepted way to track the success of marketing campaigns. (“UTM” stands for “Urchin Tracking Module”, which references the original name of Google Analytics.)

Next, we updated our marketing campaigns in Twitter to utilize these attributes as query parameters in the links to StatusGator. This process is a bit tedious as it requires creating specific links in every promoted tweet. We used a spreadsheet to help concatenate the parameters to the proper link.

Once our ads were updated, new traffic quickly found its way to StatusGator via our Twitter ads and user signups were miraculously being tracked with Ahoy. Lastly, we needed a simple way to expose this data to ourselves. Brilliantly, the Ahoy gem automatically integrates with Devise and associates the anonymous visitor data with an authenticated user retroactively once they sign up.  The most basic and useful information for us is knowing how each converted signup found out about StatusGator. To do this, we added a simple method to our User model that looks up the visit associated with the user’s conversion:

has_one :first_visit, ->(u) { order(:started_at).where("started_at < ?", u.created_at) }, class_name: 'Ahoy::Visit'

StatusGator’s admin backend is built with ActiveAdmin, which made adding this data to our app very easy. Inside of our User’s show page block, we added:

panel "First Visit" do
  attributes_table_for user.first_visit do
    row(:landing_page) { |v| link_to(v.landing_page, v.landing_page) if v.landing_page }
    row(:referrer) { |v| link_to(v.referrer, v.referrer) if v.referrer }
    row('Time to Signup') {|v| distance_of_time_in_words(v.user.created_at, v.started_at) }
    row(:location)
    row(:technology)
    row(:utm_source)
    row(:utm_medium)
    row(:utm_term)
    row(:utm_content)
    row(:utm_campaign)
  end
end

To get those convenient, location and technology strings from Ahoy’s data, we enriched the Ahoy::Visit model with a couple of instance methods:

class Ahoy::Visit < ActiveRecord::Base
  self.table_name = "ahoy_visits"

  has_many :events, class_name: "Ahoy::Event"
  belongs_to :user

  def technology
    [browser, os, device_type].map(&:presence).compact.join(', ')
  end

  def location
    [city, region, country].map(&:presence).compact.join(', ')
  end
end

When a user signs up, we can now see how they found StatusGator and adjust our marketing strategy accordingly:

Rails visit tracking with Ahoy displayed in ActiveAdmin

Recording our visits with the Ahoy gem turned out to be incredibly simple and very satisfying. In the coming months, we will expand our analysis of this data and aim to remove Google Analytics entirely.

About the author

Colin Bartlett

Colin Bartlett is co-founder of Nimble Industries, creators of StatusGator, VimTricks, and many more. He has been building web applications, managing software development projects, and leading engineering teams for 22 years.

Nimble Industries