Skip to main content
Zite connects two product analytics tools: Amplitude (the amplitude integration) and Google Analytics 4 (the googleAnalytics integration). Both track from the browser, with the key injected into the app as an import.meta.env.VITE_* variable, so there’s no server function or fetch involved in tracking. Pages render on the server first, so the tracking code runs in effects in the root route’s component, where window exists. Connect the integration first with setup_integration (see Secrets and integrations).

Amplitude

The Amplitude API key is injected as import.meta.env.VITE_AMPLITUDE_API_KEY. All tracking happens client-side through @amplitude/analytics-browser, which is already a dependency. Tracking must only run on the live app. The editor preview serves the same bundle, so without a guard your own clicking around while building is recorded as real usage, and Amplitude data is immutable once ingested: there is no cleaning it up afterwards. The platform tells the page which one it is through window.__ziteAnalytics. Step 1 reads that, and everything else keys off it. Don’t add mode checks anywhere else. Unlike Google Analytics, you don’t hand-wire page views: the browser SDK autocaptures page views, sessions, form interactions, file downloads and marketing attribution by default. You mostly initialise it once and add the custom events you care about.

Step 1: Initialise once, at app startup

Create src/lib/analytics.ts:

Step 2: Call it once from the root route

Call it once from an effect in the root route’s component, in src/routes/__root.tsx. Effects run only in the browser, and amplitude needs window.
Pass it as the root route’s component (the starter’s root route has component: Outlet), and leave the head, the shellComponent and createRootRouteWithContext<ZiteRouterContext>() as they are. Don’t call amplitude.init inside a component body or an effect that can re-run. A second init on the same page is wasted work and can double-count sessions.

Step 3: Track the actions that matter

Name events as Title Case verb phrases, consistent across the app: 'Checkout Started', 'Invoice Sent'. Amplitude groups by exact string, so 'checkout started' and 'Checkout Started' become two unrelated events.

What not to do

  • Don’t send events from a server function. Tracking is the browser SDK’s job. The write key is injected into the app build as VITE_AMPLITUDE_API_KEY, and it is not the same thing as the read credentials below.
  • Don’t drop the window.__ziteAnalytics?.enabled check from initAnalytics, and don’t call amplitude.track (or any other amplitude.* method) directly from a component. Going around the helpers puts preview noise into production data.
  • Don’t track secrets, passwords, full emails or payment details as event properties. They land in a third-party analytics tool permanently.
  • Don’t add an event to every click by default. Instrument the handful of actions that answer the question you’re asking, and let autocapture handle the rest.
  • Don’t answer “how is the app doing” from memory or from the app’s own database. The numbers live in Amplitude, and reading them is covered below.

Reading the data back

The integration reads as well as writes. Amplitude’s Dashboard API is reachable from server code with two credentials, both from await integrations.get('<name>') (import { integrations } from 'zitejs/integrations'), where <name> is the entry’s key in zite.config.json:
  • accessToken is the secret key.
  • apiKey is the project API key.
Get them inside the handler that uses them, and authenticate with HTTP Basic:
To answer a one-off question about the data, run the query as a read-only script with the run_one_off_script tool (see Build tools). Take the event names to query from /2/events/list, and send only names that appear there. An event_type Amplitude has never ingested fails the whole request with a 400 (Invalid chart definition) instead of returning an empty series. An event the app’s code sends but nobody has triggered yet is exactly that, so a freshly connected project rejects its own instrumentation. The secret key is optional when you connect, since instrumenting an app doesn’t need it. If it’s missing, add it to the Amplitude connection.

Google Analytics

The GA4 Measurement ID is injected as import.meta.env.VITE_GA_MEASUREMENT_ID (format: G-XXXXXXXXXX). All tracking happens client-side through gtag.js. Google Analytics gives an app nothing until the app names its own property: one property tracks one site, so each app sets a measurementId in its integrationSettings, under the connection’s name (see App config):
apps/<app>/zite.config.json

Step 1: Create a tracking hook

Create src/hooks/useGoogleAnalytics.ts, or add it to an existing hooks file. It loads the gtag.js script once on mount and exposes helpers for page views and custom events.

Step 2: Load gtag in the root route and track route changes

Call useGoogleAnalytics() once in the root route’s component (src/routes/__root.tsx), and fire trackPageView whenever the router’s location changes, so client-side navigations get counted. Both run in effects, so nothing touches window while the page renders on the server.
As with Amplitude, pass it as the root route’s component and leave the rest of __root.tsx as it is.

Step 3: Track custom events where they matter

Pick events from what you want to learn about the app’s users, not from a list of everything that can be clicked:
  • how many people use it (traffic and page views)
  • which features get used most
  • where users drop off
  • whether they complete a specific action (sign up, purchase)
Page-view tracking plus 2–4 events taken from the app’s obvious user actions (an item created, completed or deleted, a form submitted, a key call-to-action clicked) is a safe default. Wherever a meaningful user action happens, fire a named event. GA4 events use snake_case names and free-form params:
You can invent your own names, but prefer GA4’s standard ones for standard funnels:

Defensive behavior

  • If Google Analytics isn’t connected, VITE_GA_MEASUREMENT_ID is undefined and every helper is a no-op, so it’s safe to call trackEvent even when GA isn’t set up.
  • Never hardcode a Measurement ID. Always read import.meta.env.VITE_GA_MEASUREMENT_ID, so the app picks up the connected property.
  • Don’t load the gtag script more than once. The scriptLoaded guard handles this. In strict mode React runs the effect twice, which is fine.
  • gtag.js loads asynchronously. Events fired before it arrives are queued on window.dataLayer and sent once the script finishes loading, so early calls aren’t lost.