Skip to content

Repository files navigation

Ahoy Events

🌱 Simple, powerful event tracking for Rails

Track events in:

  • JavaScript
  • Ruby
  • Native apps

And store them wherever you’d like - your database, logs, external services, or all of them.

Installation

First, add Ahoy.

Next, add this line to your application’s Gemfile:

gem'ahoy_events'

Lastly, include the javascript file in app/assets/javascripts/application.js after Ahoy.

//= require ahoy//= require ahoy_events

How It Works

Each event has a name and properties.

There are three ways to track events.

JavaScript

ahoy.track("Viewed book",{title: "The World is Flat"});

Ruby

ahoy.track"Viewed book",title: "Hot, Flat, and Crowded"

Native Apps

Send a POST request to /ahoy/events with:

  • name
  • properties
  • user token (depends on your authentication framework)
  • Ahoy-Visit header

Requests should have Content-Type: application/json.

Storing Events

You choose how to store events.

ActiveRecord

Create an Ahoy::Event model to store events.

rails generate ahoy_events:active_record
rake db:migrate

Custom

Create your own subscribers in config/initializers/ahoy.rb.

classLogSubscriberdeftrack(name,properties,options={})data={name: name,properties: properties,time: options[:time].to_i,visit_id: options[:visit].try(:id),user_id: options[:user].try(:id),ip: options[:controller].try(:request).try(:remote_ip)}Rails.logger.infodata.to_jsonendend# and add itAhoy.subscribers << LogSubscriber.new

Add as many subscribers as you’d like.

Reference

Track all Rails actions

classApplicationController < ActionController::Baseafter_filter:track_actionprotecteddeftrack_actionahoy.track"Hit action",request.filtered_parametersendend

TODO

  • Ability to track JavaScript events automatically (button clicks, etc)

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

About

Simple, powerful event tracking for Rails

Resources

Stars

1 star

Watchers

4 watching

Forks

Releases

Packages

Contributors

Languages