@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/dashboardshows 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.
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 withdebug: true in the configuration.
installation_test event within
seconds. If the event does not appear, read Troubleshooting.
4. Track an event
Calltrack 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.
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.
6. Read a configuration value
getConfig returns a configuration value from the cache. Every value is a string.
7. Express
Track every request in a middleware, and flush onSIGTERM.
8. Fastify
Track every response in anonResponse 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.