🌱 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.
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_eventsEach event has a name and properties.
There are three ways to track events.
ahoy.track("Viewed book",{title: "The World is Flat"});ahoy.track"Viewed book",title: "Hot, Flat, and Crowded"Send a POST request to /ahoy/events with:
- name
- properties
- user token (depends on your authentication framework)
Ahoy-Visitheader
Requests should have Content-Type: application/json.
You choose how to store events.
Create an Ahoy::Event model to store events.
rails generate ahoy_events:active_record
rake db:migrateCreate 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.newAdd as many subscribers as you’d like.
Track all Rails actions
classApplicationController < ActionController::Baseafter_filter:track_actionprotecteddeftrack_actionahoy.track"Hit action",request.filtered_parametersendend- Ability to track JavaScript events automatically (button clicks, etc)
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features