Ahoy GEM Introduction

If you are wondering how to use Ahoy gem in Ruby on Rails, then we have got you covered. It is useful in Rails to track user activity for different products. Rails Ahoy gem provides various aspects to track visits and events in Ruby. How is it used with Google Analytics? We will look at that in detail.

Google analytics provides very good options for tracking usage data – visits, views, clicks and much more. In one of the e-commerce projects we are working on, the client wanted to track products which user has liked/disliked the most and the highest number of products visited by the vendors.

There could be other such use-cases where Ruby on Rails developers need to use the data available from such third-party services and acquire some new parameters. So, how can they do that?

There is no point reinventing the wheel, writing a whole new business-logic component without looking at the available gems from the vast ruby community. And we found ‘Ahoy Gem’!

Checkout 10 best Ruby on Rails Gems for Web Development

Presenting Ahoy Gem

Ahoy is a word for greetings. But in the realm of Rails Ahoy is a gem which we can use for tracking various events and visits as we want. It has saved a lot of time and effort for us!

Here is the short description from gem documentation:

Track visits and events in Ruby, JavaScript, and native apps. Data is stored in your database by default so you can easily combine it with other data.

Now, lets jump straight into the installation and coding part. Installation is as simple as adding line to gemfile and running some generators.

Add this line to gemfile and hit bundle command as below:

gem 'ahoy_matey'
bundle install

This will install various gems which will help to track user actions. Some of them are as below:

browser, device_detector, geocoder

After bundle, run below generator:

rails generate ahoy:install

This will generate ahoy.rb file in initializers directory, two model files visit.rb and event.rb in app/models/ahoy directory and one migration which will add two tables named as ahoy_events and ahoy_visits.

Run rails:db:migrate to create tables.

Now, installation is done and let’s see it in action. Restart your web server, open a page in your browser, and a visit will be created.

Fire up rails c and check Ahoy::Visit.first and you can see new event.

Ahoy automatically attaches the current_user to the visit. With Devise, it attaches the user even if he or she signs in after the visit starts.

If you are using any other gem then add below line at the end of your sign in method.

ahoy.authenticate(user)

To see the visits for a given user, create an association:

class User < ApplicationRecord
has_many :visits, class_name: "Ahoy::Visit"
end

And you can see all visits of that user.

User.find(123).visits

By default, a new visit is created after 4 hours of inactivity. You can change this in ahoy.rb file in initializers.

Ahoy.visit_duration = 30.minutes

How It Works?

When someone visits your website, Ahoy events and visitors gem creates a visit with lots of useful information.

  • traffic source – referrer, referring domain, landing page, search keyword
  • location – country, region, and city
  • technology – browser, OS, and 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:

skip_before_action :track_ahoy_visit

Events.

Each event has a name and properties.
There are three ways to track events.

JavaScript.

ahoy.track("Viewed the product", {title: "i-phone 8 plus"});

or track events automatically with:

ahoy.trackAll();

See Ahoy.js for a complete list of features.

Ruby.

ahoy.track "Viewed the product", title: "Redmi 3s Prime"

or track actions automatically with:

class ApplicationController < ActionController::Base
after_action :track_action
protected
def track_action
ahoy.track "Action Name", title: 'Some awesome information'
end
end

Native Apps.

For Android, check out Ahoy Android. For other platforms, see the API spec.

Associated Models.

You can associate your models with visit as you want. For example if you want to associate orders with visits. Just add visitable to the model.

class Order < ApplicationRecord
visitable
end

And create the migration to add visit_id to orders table.

class AddVisitIdToOrders < ActiveRecord::Migration[5.2]
def change
add_column :orders, :visit_id, :bigint
end
end

When a visitor places an order, the visit_id column is automatically set and you can have very useful information like where orders are coming from with simple joins:

Order.joins(:visit).group("referring_domain").count
Order.joins(:visit).group("city").count
Order.joins(:visit).group("device_type").count

Track Additional Data.

You can track additional data by adding below methods to ahoy.rb file

There are many other useful options available in the Ahoy gem. The Rails Ahoy dashboard enables Ruby on Rails developers to use all the functions and features of the gem.

There are many other useful option available in the gem. It provides a number of options to help with GDPR compliance too. That’s it for now.

Thank You for reading the blog!

Consulting is free – let us help you grow!