Tracking and events
MRRocket's tracking code (the SDK, @mrrocket/sdk) counts visits, signups and events in your product, and brings what you build in MRRocket into it: feature switches, A/B tests, onboarding steps, the cancel flow, the messenger, surveys and the feedback board. The newest version is 0.11. This page is for Next.js; a website without npm (Webflow, Framer, WordPress, Shopify) uses the script tag, a React Native or Expo app @mrrocket/sdk/react-native.
Install#
The quickest way: in MRRocket open the product's Install tracking step (setup checklist, or Settings › Products › your product) and press Copy for AI agent. Paste the prompt into Claude Code, Cursor or Codex: it installs the package, adds the provider with your public key, and checks the first event.
Add the package#
npm install @mrrocket/sdk # or pnpm add / yarn addIt needs Next.js 15 or newer (App Router) and ships compiled JavaScript with types.
Set your public key#
Copy the product's public key from Settings › Products › your product. It is safe in the browser, like a PostHog project key; never put a secret key in client code.
.env.local NEXT_PUBLIC_MRROCKET_HOST=https://app.mrrocket.dev NEXT_PUBLIC_MRROCKET_KEY=mrr_pub_…Without
NEXT_PUBLIC_MRROCKET_HOSTthe SDK calls the page's own origin. ThehostandapiKeyprops override both. On Cloudflare Workers (OpenNext) set both wherenext buildruns: Next.js inlinesNEXT_PUBLIC_*at build time.Add the provider#
app/layout.tsx import { CockpitProvider } from "@mrrocket/sdk/next"; export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en"> <body> <CockpitProvider>{children}</CockpitProvider> </body> </html> ); }Check it works#
Open your site. Within a few seconds the product's tracking reads Receiving events in MRRocket.
Identify your signed-in users so feedback, chats and survey answers carry a name and email, and they show in Contacts: identify(user.id, { name, email }).
Page views and goals#
Page views are tracked on every route change. Mark any element as a goal with data-ck-goal:
<button data-ck-goal="signup_started">Start free trial</button>Your own events#
"use client";
import { useCockpit } from "@mrrocket/sdk/next";
const { track, identify } = useCockpit();
track("checkout_started", { plan: "team" });
identify(user.id, { plan: user.plan });Events are sent in small batches every two seconds and when the page is hidden. The names signup, checkout and purchase feed the signups, checkouts and purchases in Visitors; identify() creates the person in Usage › People.
Consent modes#
Set how the SDK may track with the consent prop. The default is "cookieless".
| Mode | Stored in the browser | Sent |
|---|---|---|
"cookieless" | Nothing | Events without a visitor id |
"cookies" | ck_id (1 year), ck_sid (30 minutes), dismissed surveys | Events with the ck_id visitor id |
"off" | Nothing | Nothing, and no surveys |
<CockpitProvider apiKey={key} consent="cookieless">
{children}
</CockpitProvider>Change the mode at runtime with setConsent() from useCockpit(); consent returns the mode you set. If the consent prop changes, the SDK follows it.
- Switching from
"off"to"cookieless"or"cookies"starts tracking and records the current page view right away. - Leaving
"cookies"deletes theck_idandck_sidcookies and the stored survey dismissals. Switching to"off"also drops events not sent yet.
The SDK does not remember the choice. Your consent banner stores it and calls setConsent() on every page load:
"use client";
import { useEffect, useState } from "react";
import { useCockpit } from "@mrrocket/sdk/next";
export function ConsentBanner() {
const { setConsent } = useCockpit();
const [choice, setChoice] = useState<string | null>("loading");
// Apply the saved choice on every page load.
useEffect(() => {
const saved = localStorage.getItem("consent");
setChoice(saved);
if (saved === "all") setConsent("cookies");
}, [setConsent]);
const choose = (value: "all" | "essential") => {
localStorage.setItem("consent", value);
setChoice(value);
setConsent(value === "all" ? "cookies" : "cookieless");
};
if (choice) return null;
return (
<div role="dialog" aria-label="Cookie settings">
<button onClick={() => choose("essential")}>Only essential</button>
<button onClick={() => choose("all")}>Accept all</button>
</div>
);
}Put the banner inside CockpitProvider. If you want no tracking at all before a choice, start with consent="off" and call setConsent("cookieless") for "Only essential".
Cookieless#
In the default mode the SDK writes no cookie, no localStorage and no sessionStorage. Events go without a visitor id. The server counts visitors with a hash of your key, the date, the IP address and the user agent, salted with a server secret, like Plausible. The hash changes every day, and the IP address is never stored.
What this means:
- A visitor who comes back on another day counts as a new visitor. Retention and returning visitors need
identify()orconsent="cookies". - After
identify(user.id)events carry that id, so signed-in users are followed exactly. Call it on every page load, because nothing is kept. - Experiments, surveys and feedback still need an id. The SDK uses your
distinctIdprop if you pass one, else a random id that lives in memory for the page load. Variants stay the same across client-side navigation, but a reload can show a different variant. For stable variants across reloads, useconsent="cookies", pass your owndistinctId(for example from your login), or callidentify(). - For the same reason a dismissed survey can show again after a reload, and the server cannot tell a returning voter on the feedback board.
Do Not Track#
<CockpitProvider apiKey={key} respectDnt>With respectDnt the SDK sends nothing, stores nothing and shows no surveys when the browser sends Do Not Track or Global Privacy Control, whatever the consent mode. It is off by default.
Visits (sessions)#
A session ends after 30 minutes without an event. Every event carries the session id as sessionId. With consent="cookies" it lives in the ck_sid cookie (renewed with each event, shared across tabs); otherwise in memory, so a reload or a new tab starts a new one. Visitors also groups each visitor's events with the same 30-minute rule, so a reload does not count as a new visit.
Clicks and scroll depth (click maps)#
From 0.2.0 every click sends $click (where it landed, the window and page size, a short CSS selector, the label of a link or button) and the third quick click on one spot also $rageclick; leaving a page sends $scroll with the deepest point seen. They follow consent like page views and never carry typed text. Turn them off with heatmaps={false}. See Click maps.
Session recordings#
Recordings need their own consent on top of consent: <CockpitProvider replay={allowed}> or setConsent({ replay: true }), off by default. The product must also record (Recordings › Turn on recordings). The recorder loads only for visits that record. See Recordings.
Errors#
From 0.3.0 the provider reports uncaught errors and unhandled promise rejections to Bugs, with the page and the last 20 pages and clicks, under the same consent as page views. Report an error you caught with captureError(error) from useCockpit(). Set release to your version so bugs group by release; errors={false} turns it off.
Feature switches#
From 0.3.0 the SDK reads the product's feature switches and decides per person, from their identify() properties:
"use client";
import { useFlag, useFlagVariant } from "@mrrocket/sdk/next";
const newEditor = useFlag("new-editor");
const layout = useFlagVariant("pricing-layout"); // a variant key, or nullOn the server, without flicker: getFlag(key, { config, cookies }) and getFlagVariant from @mrrocket/sdk/next/server, with the config from getCockpitConfig().
What each event carries#
| Field | Value |
|---|---|
event | The name, for example $pageview or checkout_started |
distinctId | The ck_id cookie, or the id from identify(). Missing in cookieless mode until identify() |
sessionId | The current session |
props | Your properties; page views carry path |
ts | When it happened, in milliseconds |
url | The full page URL |
referrer | document.referrer on the first page, then the previous page in your app |
utm_source, utm_medium, utm_campaign, utm_term, utm_content | From the landing URL, kept for the page load. A later URL with its own UTM parameters replaces them |
screen | { w, h }, the screen size |
The server adds the country (from your host's headers), browser, OS and device (from the user agent). Previews inside MRRocket (ck_embed=1) track nothing. The wire format is in Events and config.
Rendering variants on the server needs an id the server can read: the ck_id cookie with consent="cookies", or your own distinctId passed to both getVariant() and CockpitProvider.