Ahoy Documentation

repository·master·Indexed 26 days ago

https://github.com/ankane/ahoy

A simple and powerful analytics engine for Ruby on Rails applications. Ahoy enables developers to track visits and custom events across Ruby, JavaScript, and native mobile apps, storing data directly in their own database or custom stores like Kafka, RabbitMQ, Fluentd, NATS, NSQ, and Amazon Kinesis Firehose. It includes features for geocoding, GDPR compliance via IP masking, and API support for non-browser clients.

Tokens
5.4K
Snippets
22
Records
36
Agent score
89%

What's inside Ahoy

  1. Configure GDPR Compliance and IP Masking

    master

    To comply with GDPR, you can mask IP addresses, disable cookies in favor of anonymity sets, and disable automatic linking of visits and users.

    # config/initializers/ahoy.rb
    class Ahoy::Store < Ahoy::DatabaseStore
      def authenticate(data)
        # disables automatic linking of visits and users
      end
    end
    
    Ahoy.mask_ips = true
    Ahoy.cookies = :none
    // Client-side JS
    ahoy.configure({cookies: false});
  2. Setup Local Geocoding (City or Country level)

    master

    For privacy and performance, use local geocoding via maxminddb (for city-level) or geoip (for country-level).

    # City-level with MaxMind
    # Gemfile: gem "maxminddb"
    Geocoder.configure(
      ip_lookup: :geoip2,
      geoip2: {
        file: "path/to/GeoLite2-City.mmdb"
      }
    )
    
    # Country-level with GeoIP
    # Gemfile: gem "geoip"
    Geocoder.configure(
      ip_lookup: :maxmind_local,
      maxmind_local: {
        file: "/usr/share/GeoIP/GeoIP.dat",
        package: :country
      }
    )
  3. Delete old Ahoy data

    master

    To comply with data retention policies, you can delete old visits and their associated events in batches. You can also use Rollup to aggregate data before deletion.

    # Delete visits older than 2 years and their events
    Ahoy::Visit.where("started_at < ?", 2.years.ago).find_in_batches do |visits|
      visit_ids = visits.map(&:id)
      Ahoy::Event.where(visit_id: visit_ids).delete_all
      Ahoy::Visit.where(id: visit_ids).delete_all
    end
    
    # Aggregate data before deleting
    Ahoy::Visit.rollup("Visits", interval: "hour")
  4. Debug Ahoy in development

    master

    Use these commands in your browser console or Ruby environment to debug tracking behavior.

    JavaScript (Browser Console):

    • ahoy.reset(): Forces a new visit (reload the page after calling).
    • ahoy.debug(): Enables logging.
    • ahoy.debug(false): Disables logging.
    • ahoy.configure({visitParams: {key: value}}): Passes additional data from JS to the visit.

    Ruby:

    • Ahoy.quiet = false: Enables debugging of API requests.
    ahoy.reset(); // then reload the page
    ahoy.debug();
    ahoy.debug(false);
    ahoy.configure({visitParams: {referral_code: 123}});
  5. Configure JavaScript tracking with Bun, esbuild, rollup.js, or Webpack

    master

    Install the ahoy.js package via your preferred package manager and import it in your application entry point.

    bun add ahoy.js
    # or
    yarn add ahoy.js
    // app/javascript/application.js
    import ahoy from "ahoy.js"
  6. Install Ahoy in a Rails application

    master

    To install Ahoy, add the ahoy_matey gem to your Gemfile and run the installation commands to generate the necessary files and migrations.

    gem "ahoy_matey"
    bundle install
    rails generate ahoy:install
    rails db:migrate
  7. Delete data for a specific user

    master

    To remove all tracking data associated with a specific user ID, delete their events and visits, then delete any events explicitly tied to their user ID.

    user_id = 123
    visit_ids = Ahoy::Visit.where(user_id: user_id).pluck(:id)
    Ahoy::Event.where(visit_id: visit_ids).delete_all
    Ahoy::Visit.where(id: visit_ids).delete_all
    Ahoy::Event.where(user_id: user_id).delete_all