Docs · Guide

Identifying users

Out of the box Statlark counts anonymous visitors — one id per browser. Call identify once you know who someone is, and Statlark ties that browser to a durable person: their activity and revenue follow them across devices and sessions, and they show up on your People card with a lifetime value and a full cross-device journey.

Using an AI coding tool?Hand it the prompt below, or point it at this page as markdown: identifying-users.md

Identify a user

Once the tracker snippet is installed, call statlark.identify() from your app the moment a visitor becomes known — typically right after login or signup:

// Call this once your user is known — right after login or signup.
// The script is deferred, so guard for it.
window.statlark?.identify({
  user_id: "user_123",          // your app's stable id (set once, never reused)
  user_email: "jo@example.com", // lets Stripe payments stitch to this person
  user_name: "Jo Rivera",
  traits: { plan: "pro" },
});

Every field is optional. The ones that matter:

  • user_id — your app’s stable id for the person. This is the anchor for cross-device merge, so pick something that never changes and is never reused (a database id, not an email or username). It’s write-once: once a person has a user_id, later calls won’t reassign it.
  • user_email — stored lower-cased. Unlike user_id it can change over time (latest wins). Sending it also lets payments that carry only a customer email — like Stripe charges — attach their revenue to this person.
  • user_name and user_image — a display name and avatar URL, shown on the People card.
  • traits — any extra key/values you want to keep on the person (plan, company, signup source…). Capped at 20 keys with primitive values, same as goal properties.

Calls are a merge, not a replace: only the fields you pass are updated, so a later call carrying just new traits won’t wipe an email or name set earlier.

// Only the fields you pass are updated — this won't clear the email or name
// set by an earlier call. Good for keeping traits fresh as things change.
window.statlark?.identify({ traits: { plan: "enterprise" } });

How cross-device merge works

A person is keyed by their user_id and/or their user_email. When the same user_id shows up on a second device — your user logs in on their phone as well as their laptop — the two browsers roll up into one person, and their sessions, revenue and timeline combine. The People card shows how many devices are merged into each person.

Merging also reaches back through email. A payment that carries only a customer email (the usual case for Stripe and other providers) is stitched to the matching person even if that browser never called identify — so revenue lands on the right person whether it came from the client or a server-side webhook. A payment with no visitor id is also credited to that person’s first visit, so its source gets the sale. If someone else signs in later on the same browser, the sale stays with its buyer. If a user_id and an email each already point at different people, Statlark merges them into one, keeping the older record.

Lifetime value

A person’s lifetime value is the total revenue they have generated, net of refunds, across every device they’ve been seen on. Statlark recomputes it from the payments themselves rather than incrementing a counter, so it can never drift out of step with your Overview revenue. Payments in a currency other than your site’s are recorded but excluded from the total — the same rule the rest of Statlark applies when it sums revenue.

Identifying from your server

You can do the same thing server-side with POST /api/v1/identify. It’s the ad-blocker-immune twin of statlark.identify() — pass the person’s visitor_id plus the same identity fields. It has the same merge semantics: only the fields you send are updated.

Signing out

identify has a counterpart: window.statlark?.reset?.(). Call it when someone signs out and the browser forgets its visitor and session, so the next person on that device starts as a fresh anonymous visitor instead of inheriting the previous user’s identity and first-touch attribution. It takes effect immediately, with no page reload.

This is the other half of the merge above. A shared device keeps one visitor id, so without a reset the next identify re-points that device — and every payment recorded on it — onto the new person: one profile ends up carrying two people’s sessions and revenue. In cookieless mode there is no id on the device to clear, so sign-out isn’t an identity boundary there — reset() starts a new session only.

Hand this to your AI

Paste this into Claude Code, Cursor or another AI coding tool, in your project. It never needs your API key.

Connect signed-in users in this project to Statlark, so their visits and payments follow them across devices.

Follow this guide for the exact code: https://statlark.com/docs/identifying-users.md
If that link doesn't open, try the same address without .md. If neither opens, stop and tell me. Don't write this from memory.

1. Check how the site loads Statlark: the script tag, consent.js, @statlark/web or a first-party proxy. If you can't find it in this code, stop and ask me how it's added, for example through Google Tag Manager. Don't install it as part of this.
2. Call identify in the browser once, right after a user signs up or logs in, not on every page load: each call counts toward my plan's event allowance. Guard it, as in window.statlark?.identify({ ... }), so nothing breaks when the script is blocked. A call made before the script loads is skipped, so if sign-in ends with a full page load, or no browser code runs after it, ask me where to put it. If the project uses @statlark/web, call identify on its handle instead. If the project uses TypeScript and window.statlark doesn't type-check, add the declaration from https://statlark.com/docs/goals-conversions.md once: that's the only other change allowed.
3. Send user_id: the user's stable id from this app's database, never an email or username, and never reused for someone else. Ask me before also sending user_email, user_name, user_image or traits. The email is what ties Stripe payments to the person.
4. Call reset when a signed-in user signs out, guarded the same way: window.statlark?.reset?.(). Put it where the app learns of the sign-out, so every open tab resets. Make sure it doesn't also run on sign-in, on a token refresh, or when someone who was never signed in loads a page: each reset forgets the visitor.
5. Don't change anything else. Finish by telling me what you changed and where. Remind me to sign in on the live site and check that I appear on the People card, because visits from localhost aren't counted by default.

The People card

Identified people appear under the People card, ranked by lifetime value — each row showing whether they’re a customer, the channel that first brought them in, their sessions, when you last saw them, and how many devices are merged. Open anyone to see their full cross-device journey: every pageview, goal and payment in order, wherever it happened.

Identity is entirely opt-in — Statlark only knows what you choose to send. If you track cookielessly or need to honor a deletion request, nothing here changes that: don’t call identify for visitors you shouldn’t, and personal data stays out.

Last reviewed