Skip to main content
This guide works with Vue, Svelte, Angular, or plain JavaScript — either via script tag or npm. Using React or Next.js? Try the React Quick Start or Next.js Quick Start instead.
Add the Grain Tag script to your HTML:
Replace your-tenant-id with your tenant identifier from your dashboard. Then get the SDK instance in your JavaScript:

Option 2: npm Package

Install via your package manager:
npm
yarn
pnpm
Then import and initialize:
With the npm package, you can use the top-level exports (track, identify) directly, or get the instance with getInstance().

Automatic Tracking

Grain Tag automatically tracks the following with zero configuration:
  • Page views — including SPA navigation via the History API
  • Heatmap clicks — click positions with element identification
  • Scroll depth — how far users scroll on each page
  • DOM snapshots — visual page captures for heatmap overlays
  • Sessions — session start/end with duration
  • Device, browser, and OS — automatically detected
  • UTM parameters and referrer — captured on first visit
No manual page_viewed calls are needed. Grain Tag hooks into pushState and replaceState automatically.

Track Custom Events

Call track() with an event name and optional properties:
Events are automatically batched and sent. You don’t need to manually flush.

Identify Users

Associate events with a specific user:
Grain is cookieless by default with daily rotating IDs. When you need explicit consent handling:

Complete Example

Here’s a small app that puts it all together:

Vue.js Example

Using Grain Tag in a Vue component: Script tag approach (add <script src="https://tag.grainql.com/v4/your-tenant-id.js"></script> to your index.html):
npm approach:
Initialize Grain once in your app entry point (main.ts):
Page views are tracked automatically — Grain Tag hooks into Vue Router’s History API navigation.

Svelte Example

Using Grain Tag in a Svelte component: Script tag approach (add <script src="https://tag.grainql.com/v4/your-tenant-id.js"></script> to your app.html):
npm approach:
Initialize Grain once in your app entry point:
Page views are tracked automatically — Grain Tag hooks into SvelteKit’s History API navigation.

What’s Next?

Core API Reference

See all available methods and options

User Identification

Learn about tracking users across sessions

Trackers

Track element clicks without writing code

Event Best Practices

Learn what to track and how to structure events
Need remote configuration or feature flags? See @grainql/analytics-web for remote config support, React hooks, and more.