Configure the FastStats Web Analytics core and extensions
React and JavaScript use the same core options. Pass them as props to React’s
Analytics component or to JavaScript’s init, createClient, or
WebAnalytics constructor. In Nuxt, place them under faststats.
Error tracking, Web Vitals, and session replay are separate, tree-shakeable
imports. Outbound link tracking is part of the core and needs no setup. With React or JavaScript, create each extension and add
it to the extensions array.
import { Analytics } from "@faststats/react";
import { errorTracking } from "@faststats/react/error";
import { sessionReplay } from "@faststats/react/replay";
import { webVitals } from "@faststats/react/web-vitals";
<Analytics
siteKey="your_site_key"
extensions={[
errorTracking(),
webVitals(),
sessionReplay({ recordConsole: true }),
]}
/>;
Nuxt uses a serializable extensions object and loads the selected extensions
for you.
Use true for default extension settings. errorTracking and sessionReplay
also accept an options object; webVitals has no options. See Error Tracking,
Web Vitals, and
Session Replay for their options.
init starts the JavaScript shared client immediately. createClient and the
WebAnalytics constructor create independent clients that must be started
explicitly.
An extension is an object with a name and a setup function. setup receives
a context with the client’s settings and an on method for lifecycle hooks, and
may return a cleanup function.
import type { AnalyticsExtension } from "@faststats/web";
export function diagnostics(): AnalyticsExtension {
return {
name: "diagnostics",
setup(context) {
context.on("pageChange", ({ url }) => {
if (context.debug) console.debug("page changed", url);
});
return ({ discard }) => {
// Flush buffered data, or drop it when `discard` is true.
};
},
};
}
The context exposes siteKey, baseUrl, debug, trackHash,
isCookieless(), getUserId(), and on(hook, handler). getUserId()
returns the identified user, including one identified on an earlier page load.
Hook
Payload
When it fires
pageChange
{ url }
After a client-side navigation to a new page
pageHide
{ persisted, terminal }
When the page is hidden or unloaded
pageShow
{ persisted }
When the page is shown, including from the bfcache
consentChange
{ cookieless }
When consent changes while tracking stays enabled
identityChange
{ userId }
When identify changes the user, or on logout
error
{ error }
When reportError is called
Extensions start when tracking starts and stop when the client is destroyed or
consent is denied. Cleanup receives discard: true when consent was revoked.
Errors thrown from setup or a hook are caught and do not affect analytics.