Ahoy.js for browser visits and first-party events
Simple, powerful JavaScript analytics
At a glance
- What is it?
- Ahoy.js sends page views and custom events to an application’s own collector. Its separate visit and visitor tokens are useful when the backend defines how browser records join to accounts.
- Who is it for?
- Ahoy.js fits teams that already have a collector or can own both endpoint handlers. Teams that require a hosted dashboard from the installation should choose a full analytics service.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 154 days ago.
- What is it written in?
- Mainly JavaScript, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on October 20, 2026, and from our analysis. They are not legal advice.
Editorial analysis
Two identifiers for the same browser
Ahoy.js gives a web application a way to associate browser events with visits without designing a new client protocol. The client assigns a visit token and a visitor token when a visitor lands on the site. The default visit duration is four hours and the default visitor duration is two years. That difference matters when defining reports. A visitor who comes back after the first visit expires can produce another visit while retaining the same visitor identifier. A chart based on the count of visit tokens can therefore move independently of a chart based on distinct visitor tokens.
The identifiers serve a different purpose from account IDs. The README describes how a server can associate visits and events with an authenticated user. For an application with both signed-in and anonymous pages, that association belongs in the backend, where the application can check its own authentication state. A report that treats browser identifiers as account identifiers would mix two different definitions of a user.
What the visit and event endpoints receive
The default visit endpoint is `/ahoy/visits`. The browser sends the two tokens, the referrer and the landing page in a POST request. An application that wants campaign attribution can derive UTM fields from the landing page on the server. The README also lists the IP address and user agent as inputs the server can use. Those enrichments are choices for the backend. A JavaScript installation alone does not build them into a storage system.
The separate `/ahoy/events` endpoint receives event names, properties and times. The README describes recovering the visit token from cookies and associating the authenticated user on the server. It also suggests rejecting event times older than one minute or later than the server's current time. If you implement that check, test how the collector handles a browser clock that runs fast. Even a newly generated event can fall outside the suggested window if the server and browser disagree on the time.
Install Ahoy.js and trace a page view
In an application that uses Yarn, add the package from the application directory. This makes the browser library available to the frontend build.
yarn add ahoy.jsImport the client in the browser code for a page with a matching collector configured. The documented page-view call emits an event named `$view`.
import ahoy from 'ahoy.js';
ahoy.trackView();Inspect the browser network requests and check that the event reaches `/ahoy/events` under the default configuration. The README lists URL, title and page as properties on this event. The page property defaults to the path. If a frontend routes multiple views through the same path, decide whether that default gives analysts a usable grouping before writing a report that aggregates it. The `page` setting is available when the application needs to supply a label. On a fresh browser session, also inspect the visit request so the backend can demonstrate how it relates the two records.
Cookie scope and what the browser sends
For visits shared across subdomains, the README documents the `cookieDomain` setting. When a JavaScript client sends requests to a collector on a different origin and authentication uses cookies, the documentation points to `withCredentials`. The base configuration leaves it disabled. Verify the collector's origin and credential settings before treating a missing user association as a reporting error. The visit token can be present even when the collector has no authenticated account to attach to the event.
The documented click helper accepts a selector and collects properties including the matching element's text and destination. On a screen with account-specific labels, inspect the actual payload before binding the click helper to a broad selector. The landing page URL and referrer also deserve review because their query strings may contain values the analytics store has no reason to keep.
Choose where the analytics system lives
A Rails application can pair this client with the Ahoy gem, the backend option linked by the JavaScript README. A team on another stack can implement the documented visits and events endpoints itself. That approach gives the application control over which fields it stores and which events it joins to accounts, while making the team responsible for request validation and for keeping the collector compatible with the browser payload. For a small set of business events that originate as successful server transactions, logging those transactions directly is another approach. It keeps the success definition beside the transaction and leaves browser-only signals such as clicked links and page views outside that data set.
The repository manifest identifies version `0.4.5`, declares the MIT license and exposes distribution files for both import and require. It also defines a Rollup build script. When evaluating an upgrade, compare the repository changelog with the version resolved by the application and retest the two endpoint requests against the real collector. The client and collector can be deployed on different schedules, so a browser bundle update is only one half of that check.
Editorial conclusion
Ahoy.js fits teams that already have a collector or can own both endpoint handlers. Teams that require a hosted dashboard from the installation should choose a full analytics service. Start by following a fresh visit and one page view through the collector and confirming that their visit tokens join.
Frequently asked questions
Does Ahoy.js require Rails?
The README describes using any backend and links to the Ahoy gem for Rails applications.
How long does an Ahoy.js visit last by default?
The default visit duration is four hours. The separate visitor identifier lasts two years.
Can Ahoy.js skip creating a visit?
Setting trackVisits to false skips client-side visit creation. The README says the server is then expected to return the visit and visitor cookies.
Official sources
Was this useful?