OneDollarStats is a lightweight, zero-dependency analytics library for client applications that automatically tracks pageviews, UTM
parameters, and custom events with minimal setup.
It supports hash-based navigation, automatically collects UTM parameters, tracks clicks on elements with data-s-event attributes, and
integrates effortlessly.
npm i onedollarstats
⚠️ Initialize analytics on every page for static sites, or at the root layout (app entrypoint) in SPA apps. Callingvieworeventbeforeconfigurewill automatically initialize the tracker with the default configuration.
import { configure } from "onedollarstats";
// Configure analytics
configure({
collectorUrl: "https://collector.onedollarstats.com/events",
autocollect: true, // automatically tracks pageviews & clicks
hashRouting: true // track SPA hash route changes
});Note: Any path or properties you pass to
vieworeventtake priority over values found on the page (likedata-s-path,data-s-view-props, or meta tags).
Track Pageviews
By default, pageviews are tracked automatically. If you want to track them manually (for example, with autocollect: false), you can use the view function:
import { view } from "onedollarstats";
// Simple pageview
view("/homepage");
// Pageview with extra properties
view("/checkout", { step: 2, plan: "pro" });Track Custom Events
The event function can accept different types of arguments depending on your needs:
import { event } from "onedollarstats";
// Simple event
event("Purchase");
// Event with a path
event("Purchase", "/product");
// Event with properties
event("Purchase", { amount: 1, color: "green" });
// Event with path + properties
event("Purchase", "/product", { amount: 1, color: "green" });Config Options:
| Option | Type | Default | Description |
|---|---|---|---|
collectorUrl |
string |
https://collector.onedollarstats.com/events |
URL to send analytics events |
trackLocalhostAs |
string | null |
null |
Deprecated. Use hostname and devmode |
hostname |
string | null |
null |
Override event hostname(for server-side or desktop runtimes) Required for devmode |
devmode |
boolean |
false |
For dev testing, requires hostname |
hashRouting |
boolean |
false |
Track hash route changes as pageviews |
autocollect |
boolean |
true |
Automatically track pageviews & clicks |
excludePages |
string[] |
[] |
Pages to ignore for automatic tracking |
includePages |
string[] |
[] |
Pages to explicitly include for tracking |
Notes:
- Manual calls of
vieworeventignoreexcludePages/includePages.- By default, events from
localhostare ignored. Use thehostnameanddevmodeoptions to simulate a hostname for local development.
view(pathOrProps?: string | Record<string, string>, props?: Record<string, string>) sends a pageview event.
Parameters:
pathOrProps– Optional, string represents the path, object represents custom properties.props– Optional, properties if the first argument is a path string.
event(eventName: string, pathOrProps?: string | Record<string, string>, props?: Record<string, string>) sends a custom event.
Parameters:
eventName– Name of the event.pathOrProps– Optional, string represents the path, object represents custom properties.props– Optional, properties if the second argument is a path string.
onedollarstats/expo is a dedicated entry point for Expo apps using expo-router. It auto-collects pageviews on route change and when the app returns from the background (not for system overlays like Face ID or permission prompts), supports dynamic-route templates (/profile/[id] instead of /profile/abc123), and sends events natively on iOS/Android and via image beacon + sendBeacon on web. Native requests carry a User-Agent such as Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X; Apple) onedollarstats-expo, so the dashboard shows the OS (iOS or Android) and the device type (phone or tablet). React Native's default UA (okhttp or CFNetwork) would show Android traffic as unknown and every iPad as a phone.
// app/_layout.tsx
import { Stack } from 'expo-router';
import { OneDollarStatsProvider } from 'onedollarstats/expo';
export default function RootLayout() {
return (
<OneDollarStatsProvider config={{ hostname: 'example.com' }}>
<Stack screenOptions={{ headerShown: false }} />
</OneDollarStatsProvider>
);
}Fire custom events or manual pageviews from any component with useAnalytics():
import { useAnalytics } from 'onedollarstats/expo';
const { event, view } = useAnalytics();
event('signup', { plan: 'pro' }); // event with current route
event('signup', '/landing'); // event with explicit path
view({ campaign: 'spring' }); // pageview with just props
view('/landing', { campaign: 'spring' }); // pageview with explicit pathAs on the web, a view() of the page that was just counted is skipped, whether autocollect or an earlier view() counted it. Paths are compared by the URL they produce, so '/landing', 'landing/' and '/landing?ref=ad' are the same page. That means view({ campaign: 'spring' }) on a screen autocollect already counted sends nothing. If you want to send props with each screen's pageview, set autocollect: false and call view() yourself. event() is never deduplicated.
Expo-specific config options:
| Option | Type | Default | Description |
|---|---|---|---|
collapseDynamicRoutes |
boolean |
true |
Use useSegments() to record routes as templates (/profile/[id]) instead of concrete paths. Group segments like (tabs) are stripped. |
hostname is required. collectorUrl and autocollect work as in the web tracker above. devmode is covered under Development builds below. trackLocalhostAs and hashRouting aren't supported.
excludePages / includePages work differently from the web tracker:
- Entries match a path and everything under it, with no wildcards.
'/admin'matches/adminand/admin/users, but not/administrator. - When
includePagesis set,excludePagesis ignored. The web tracker applies both. includePages: ['/']matches every screen, because every path is under/.- With
collapseDynamicRouteson (the default), match against the template, for example'/profile/[id]', not'/profile/abc123'. - As on the web, manual
view()andevent()calls ignore both lists.
Development builds: events are ignored in development, like localhost on the web. That covers dev bundles (__DEV__: Expo Go, dev clients, npx expo start) on iOS, Android and web, plus Expo web served from localhost. Set devmode: true to send them as debug events. In release builds and on your real domain, devmode is ignored, so leaving it on can't turn production traffic into debug events.
Page view events
List of attributes for tags that allow modifying the sent page view:
-
data-s-path– Optional. Specifies the path representing the page where the event occurred. This attribute should be set on the<body>tag. -
data-s-view-props– Optional. Defines additional properties to include with the page view event. All properties from elements on the page with this attribute will be collected and sent together.
Click events
Automatically capture clicks on elements using these HTML attributes:
data-s-event– Name of the eventdata-s-event-pathOptional, the path representing the page where the event occurreddata-s-event-props– Optional, properties to send with the event
See full onedollarstats documentation.