Step 2 of 7. Send pageviews and typed custom events from the dashboard into its own project with @spoar/sdk.
The dashboard tracks itself into the project it reads, so its own visits are the first data it shows. This step adds the SDK client and a type for every custom event the page will send.
4 files, 2 new
1234567891011121314151617181920212223242526import { createAnalytics } from "@spoar/sdk";import type { Analytics } from "@spoar/sdk";import { clicks } from "@spoar/sdk/plugins";
import type { Events } from "./events";import type { Settings } from "./settings";import { trimEndpoint } from "./settings";
/** * @name createTracking * @description The dashboard's own `@spoar/sdk` client, so the example tracks itself into the * project it reads. Without a public key the SDK runs in development mode and logs instead. * * @example * const analytics = createTracking(settings); * analytics.track("period_changed", { period: "30d" }); */export function createTracking(settings: Settings): Analytics<Events> { return createAnalytics<Events>({ project: settings.project, key: settings.publicKey, endpoint: `${trimEndpoint(settings.endpoint)}/v2/events`, mode: settings.publicKey ? "production" : "development", plugins: [clicks()], });}The browser client posts to endpoint exactly as given, so it gets the full /v2/events URL.
Without a public key the client runs in development mode and logs events to the console instead of sending them.
Pageviews are on by default. clicks() adds a click event for every element with a data-spoar-click attribute.
123456789101112131411 unmodified linesimport { useEffect, useMemo } from "react";
import { createTracking } from "./analytics";import { settings } from "./settings";
export function App() { const analytics = useMemo(() => createTracking(settings), []);
useEffect(() => () => void analytics.shutdown(), [analytics]);
return ( <div className="shell"> <main className="main"> {settings.project === "" ? (11 unmodified linesshutdown() sends what is still queued, then removes the client's listeners.
123456789import type { NoProps } from "@spoar/sdk";
export type Events = { period_changed: { period: string }; traffic_changed: { traffic: string }; filter_added: { dimension: string }; filter_removed: { dimension: string }; settings_saved: NoProps;};Every custom event name and its properties. A track call with an unknown name or a wrong property fails to compile.
9 unmodified lines1011121314151617181920212223249 unmodified lines project: process.env.BUN_PUBLIC_DASHBOARD_PROJECT ?? "", token: process.env.BUN_PUBLIC_DASHBOARD_TOKEN ?? "", publicKey: process.env.BUN_PUBLIC_DASHBOARD_PUBLIC_KEY ?? "",};
/** * @name trimEndpoint * @description Strips trailing slashes so paths can be appended without doubling them. * * @example * trimEndpoint("https://api.example.com/"); // "https://api.example.com" */export function trimEndpoint(endpoint: string) { return endpoint.replace(/\/+$/, "");}The client sends a pageview on load and on every pushState navigation. The events in Events are sent from step 5, once the page has controls to click.
Reload http://localhost:3301, then open the project in the dashboard and pick Realtime. The pageview shows up within a few seconds.
If nothing arrives, check the browser's network tab for the POST to /v2/events. A 403 means http://localhost:3301 is missing from the project's allowed origins. Events do not arrive covers the other causes.
On a real site, send events through your own domain with the proxy, so ad blockers that block third-party hosts do not drop them.
Next, read the overview.