Ahoy.js

0.4.5 · active · verified Sun Apr 19

Ahoy.js is a client-side JavaScript analytics library designed for simple and powerful visit and event tracking. It currently stands at version 0.4.5 and is actively maintained, though a specific release cadence isn't defined. The library's core function is to capture user interactions, such as unique visits, page views, clicks, submits, and custom events, sending this raw data via `POST` requests to a configurable backend endpoint. A key differentiator is its backend-agnostic design, allowing developers to integrate it with any server-side technology, including a dedicated Ruby gem for Rails applications. It automatically manages visit and visitor tokens (expiring after 4 hours and 2 years, respectively) and provides comprehensive data points like referrer, landing page, and event properties, which can be further enriched on the server with IP, user agent, and authentication details. The library also offers robust configuration options for URL endpoints, cookie management, cross-domain tracking, and debug logging.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure Ahoy.js for various tracking scenarios, including custom event tracking, page views, clicks, and form submissions, along with essential cross-domain and debugging settings.

ahoy.configure({
  cookieDomain: "example.com", // Use for tracking across subdomains (e.g., app.example.com, blog.example.com)
  withCredentials: true,       // Essential for sending cookies across different domains/subdomains for authentication
  visitsUrl: "/api/analytics/visits", // Customize endpoint for visit tracking
  eventsUrl: "/api/analytics/events"  // Customize endpoint for event tracking
});

// Enable debug logging in the browser console
ahoy.debug();

// Track a custom event with properties
ahoy.track("Product Viewed", {
  productId: "SKU123",
  category: "Electronics",
  price: 499.99
});

// Track a page view, automatically capturing URL and title
ahoy.trackView();

// Track clicks on specific CSS selectors
ahoy.trackClicks(".add-to-cart-button, #checkout-link");

// Track form submissions on specific CSS selectors
ahoy.trackSubmits("#newsletter-signup-form");

// To force a new visit for testing (requires page reload)
// ahoy.reset();
// window.location.reload();

view raw JSON →