Skip to main content
Analytics Web has three authentication strategies. Each one fits one place: a public page, a server, or a page with a signed-in user. This page lists the strategies, the headers and domains that a site must allow, and the data that a page must not send.

Authentication strategies

Grain Tag sends events without a credential. It uses the tenant alias only. Read Authentication for the setup of each strategy.
Do not use NONE with sensitive data. Anyone who holds the tenant alias can send events and read every configuration value.

Secret keys

A secret key belongs on a server. A browser bundle is public, so a key in it is public.
Rules for secrets:
  1. Read the key from an environment variable. Never write it in source.
  2. Never use SERVER_SIDE in browser JavaScript, in a mobile app, or in any code that ships to a client.
  3. Rotate the key on a schedule.
  4. Send every request over HTTPS.
  5. Keep the SDK up to date.

JWT and user IDs

With JWT, the API compares the user ID on each event with the sub claim of the token. An event with a different user ID is rejected. This stops one user from sending events as another.
Return a fresh token from getToken() on each call. Do not cache the token in your own code. The auth library refreshes it.

User ID overrides

setProperty(properties, { userId }) writes properties for a user other than the current one. Grain can block a source that writes properties for many distinct user IDs in a short time, or that sends an unusual pattern of property updates. Rules for overrides:
  1. Use setUserId() for the current user. Use the userId option only when the server acts for another user.
  2. Make sure that the caller is allowed to change that user.
  3. With JWT, the override must match the token subject.

Sensitive data

Never send a password, a government ID number, a card number, or a health record as an event property or a user property. Send a category, not the value.
Send an email address or a location as a user property only when the user consented to it. Read Privacy and consent.

Allowed origins

The Grain API checks the Origin header of a browser request against the allowed origins of the tenant. Set them in the dashboard under Settings, Allowed Origins. A request from an origin that is not on the list fails with a CORS error in the browser console.

Content Security Policy

If the site sends a Content-Security-Policy header, allow these hosts.

Configuration values and XSS

A remote configuration value is a string that anyone with dashboard access can edit. Do not insert it as HTML.
React escapes {heroText} in JSX. element.innerHTML = heroText does not escape it.