[Awesome Ruby Gem] Use ahoy_matey gem to track visits and events in Ruby, JavaScript, and native apps
ahoy_matey
π₯ Simple, powerful, first-party analytics for Rails.
Track visits and events in Ruby, JavaScript, and native apps. Data is stored in your database by default, and you can customize it for any data store as you grow.
Installation
You can install it as a gem:
1 | gem install ahoy_matey |
or add it into a Gemfile (Bundler):
1 | # Gemfile |
Migration
Generate and run migraions:
1 | rails generate ahoy:install |
Usages
Restart your web server, open a page in your browser, and a visit will be created π
Track your first event from a Rails controller with:
1 | ahoy.track "My first event", language: "Ruby" |
JavaScript, Native Apps, & AMP
Enable the API in config/initializers/ahoy.rb
:
1 | Ahoy.api = true |
And restart your web server.
JavaScript
For Rails 6 / Webpacker, run:
1 | yarn add ahoy.js |
And add to app/javascript/packs/application.js
:
1 | import ahoy from "ahoy.js"; |
Track an event with:
1 | ahoy.track("My second event", {language: "JavaScript"}); |
Native Apps
Check out Ahoy iOS - https://github.com/namolnad/ahoy-ios and Ahoy Android - https://github.com/instacart/ahoy-android.
How It Works
Visits
When someone visits your website, Ahoy creates a visit with lots of useful information.
-
traffic source - referrer, referring domain, landing page
-
location - country, region, city, latitude, longitude
-
technology - browser, OS, device type
-
utm parameters - source, medium, term, content, campaign
-
Use the current_visit method to access it.
Prevent certain Rails actions from creating visits with:
1 | skip_before_action :track_ahoy_visit |
This is typically useful for APIs. If your entire Rails app is an API, you can use:
1 | Ahoy.api_only = true |
You can also defer visit tracking to JavaScript. This is useful for preventing bots (that arenβt detected by their user agent) and users with cookies disabled from creating a new visit on each request. :when_needed
will create visits server-side only when needed by events, and false will disable server-side creation completely, discarding events without a visit.
1 | Ahoy.server_side_visits = :when_needed |
Events
Each event has a name and properties. There are several ways to track events.
Ruby
1 | ahoy.track "Viewed book", title: "Hot, Flat, and Crowded" |
Track actions automatically with:
1 | class ApplicationController < ActionController::Base |
JavaScript
1 | ahoy.track("Viewed book", {title: "The World is Flat"}); |
Track events automatically with:
1 | ahoy.trackAll(); |
See Ahoy.js - https://github.com/ankane/ahoy.js for a complete list of features.
Native Apps
See the docs for Ahoy iOS - https://github.com/namolnad/ahoy-ios and Ahoy Android - https://github.com/instacart/ahoy-android.
Associated Models
Say we want to associate orders with visits. Just add visitable to the model.
1 | class Order < ApplicationRecord |
When a visitor places an order, the ahoy_visit_id
column is automatically set π
See where orders are coming from with simple joins:
1 | Order.joins(:ahoy_visit).group("referring_domain").count |
Hereβs what the migration to add the ahoy_visit_id column should look like:
1 | class AddVisitIdToOrders < ActiveRecord::Migration[6.1] |
Customize the column with:
1 | visitable :sign_up_visit |
Users
Ahoy automatically attaches the current_user
to the visit. With Devise, it attaches the user even if they sign in after the visit starts.
With other authentication frameworks, add this to the end of your sign in method:
1 | ahoy.authenticate(user) |
To see the visits for a given user, create an association:
1 | class User < ApplicationRecord |
And use:
1 | User.find(123).visits |
Custom User Method
Use a method besides current_user
1 | Ahoy.user_method = :true_user |
or use a proc
1 | Ahoy.user_method = ->(controller) { controller.true_user } |
Doorkeeper
To attach the user with Doorkeeper, be sure you have a current_resource_owner
method in ApplicationController
.
1 | class ApplicationController < ActionController::Base |
Exclusions
Bots are excluded from tracking by default. To include them, use:
1 | Ahoy.track_bots = true |
Add your own rules with:
1 | Ahoy.exclude_method = lambda do |controller, request| |
See ankane/ahoy: Simple, powerful, first-party analytics for Rails - https://github.com/ankane/ahoy to learn more Usages.
References
[1] ankane/ahoy: Simple, powerful, first-party analytics for Rails - https://github.com/ankane/ahoy
[2] ahoy_matey | RubyGems.org | your community gem host - https://rubygems.org/gems/ahoy_matey
[6] ankane/ahoy.js: Simple, powerful JavaScript analytics - https://github.com/ankane/ahoy.js