Skip to main content
This page installs Analytics Web (@grainql/analytics-web) on a server and sends events with the SERVER_SIDE strategy. It ends with Express, Fastify, AWS Lambda, and Vercel examples. Grain Tag is for browsers. For a browser install, read Install Grain.

Before you start

You need three things:
  • Your tenant alias. The dashboard at https://grainql.com/dashboard shows it. It is not the UUID.
  • A secret key from the dashboard, in the environment variable GRAIN_SECRET_KEY. Read Authentication.
  • Node.js with fetch, or a serverless runtime.
Do not commit the secret key, and do not ship it in browser code. Anyone who holds the secret key can send events as your tenant.

1. Install the package

2. Create the client

Create one client per process, at module scope, and reuse it. In a serverless function, a module-scope client survives across warm invocations.

3. Make sure that events arrive

Run this script one time with debug: true in the configuration.
The console shows the request. The dashboard shows the installation_test event within seconds. If the event does not appear, read Troubleshooting.

4. Track an event

Call track with an event name and a properties object. Analytics Web queues the event and sends the queue every 5 seconds, or when it holds 50 events, whichever comes first.
Call flush after a batch job, before the process exits. Otherwise the events in the queue are lost. Read Track an event.

5. Identify a user and set properties

identify sets the user ID for every later event. setProperty stores properties on the user.
Read Identify users and User properties.

6. Read a configuration value

getConfig returns a configuration value from the cache. Every value is a string.
Read Remote configuration.

7. Express

Track every request in a middleware, and flush on SIGTERM.

8. Fastify

Track every response in an onResponse hook, and flush in onClose.

9. AWS Lambda

Create the client outside the handler. Pass { flush: true } to every track call, so the event leaves before the function stops.

10. Vercel

A Vercel function stops after the response. Pass { flush: true } to every track call.

Next steps

Core methods

Every method of both SDKs, with signatures.

Authentication

The three strategies, secret keys, and API keys.

Export and query data

Read events out with the Query API.

Configuration options

Every option of both SDKs, with defaults.