Skip to content
snagr
Snagr docs

SDK reference

Browser and server methods in the Snagr SDK.

Browser

Load the script with your publishable key (see the quickstart), then call methods on window.snagr.

Script tag attributes

AttributeWhat it does
data-keyYour publishable key (snagr_pk_…). Required for auto-start.
data-nudges="off"Don't draw nudges; read them from onState and draw your own.
data-hostSend events somewhere other than https://www.snagr.sh (for a proxy).

Leave out data-key and call snagr.init("snagr_pk_…", { nudges: false }) yourself if you'd rather start the SDK in code.

Methods

MethodWhat it does
snagr.identify({ email, userId, emailHash })Says who the visitor is. emailHash comes from your server and lets Snagr email them.
snagr.track(event, properties)Records an event, such as checkout_started. Include url for the link back and revenue in major units.
snagr.reset()Forgets the visitor, for example on sign-out.
snagr.flag(key, fallback)Reads a flag set by a flow.
snagr.onState(callback)Called with { flags, nudges } whenever they load. Returns a function that unsubscribes.
snagr.flush()Sends any queued events now. Events also go out automatically when the page is hidden.

Verifying the email

import { createHmac } from "node:crypto";

const emailHash = createHmac("sha256", process.env.SNAGR_SECRET_KEY)
  .update(email.trim().toLowerCase())
  .digest("hex");

Server

From your server, send events to the HTTP API with your secret key. Server events identify the person by email directly.

Reserved names

Events starting with snagr: are reserved for Snagr's own use.