Skip to main content
This page installs Grain Tag on a website with one script tag. Grain Tag collects page views, clicks, scroll depth, heatmaps, and DOM snapshots without further code. The page ends with a custom event, an identified user, and a consent call.

Before you start

You need three things:
  • Your tenant alias. The dashboard at https://grainql.com/dashboard shows it. It is not the UUID.
  • Write access to the HTML of your site, or to a tag manager. For Google Tag Manager, read Google Tag Manager.
  • A browser with developer tools, to make sure that events arrive.

1. Add the script

Put this tag in the <head> of every page. Replace your-tenant-id with your tenant alias.
The script initializes itself when it loads. It exposes the global GrainTag object. Add the tag one time per page. A second tag on the same page is not necessary.

2. Make sure that events arrive

Open the page in a browser. In the developer console, run:
The dashboard shows the installation_test event within 30 seconds. If the event does not appear, read Troubleshooting.

3. Track a custom event

Call track with an event name and an optional properties object.
Grain Tag batches events and sends them in groups. A manual flush is not necessary. GrainTag.track and GrainTag.identify work before initialization completes. Grain Tag queues the calls and replays them after initialization. Read Track an event and Name events.

4. Identify a user

After a login, pass the user ID to identify. Every later event holds this user ID.
Read Identify users. Grain Tag is cookieless by default and uses daily rotating IDs. When your site asks for consent, call the consent methods on the instance.
grant enables persistent tracking. Grain Tag stores the persistent ID in localStorage, not in a cookie. status returns the current ConsentState, or null. Read Privacy and consent.

6. Single-page applications

Grain Tag tracks the first page view and every navigation through the History API (pushState and replaceState). A router that uses the History API needs no extra code. If your router does not use the History API, track the navigation yourself:

Next steps

Core methods

Every method of both SDKs, with signatures.

Trackers

Track element clicks from the dashboard, without code.

Plain JavaScript, Vue, and Svelte

Grain Tag through npm in any framework.

Automatic tracking

What Grain Tag collects without code.