# Installing TracerKit

TracerKit manages your tracking scripts (analytics, pixels) outside your
codebase and verifies every tag actually fires. Install is one script tag;
everything else happens in the dashboard.

## Enhanced matching & dedup (loader API)

Two optional calls raise your CAPI match quality and prevent double-counting:

```js
// hash email/phone IN the browser (SHA-256) — raw values never leave the page.
// Meta's single highest-impact match parameter.
tracerkit.identify({ email: user.email, phone: user.phone });

// if you also run a browser pixel, pass the shared event id so the
// platform dedupes pixel + server into ONE conversion:
fbq("track", "Purchase", { value, currency }, { eventID: tracerkit.lastEventId });
```

Click ids (gclid / fbclid / ttclid), _fbp/_fbc, _ttp, and GA4 client/session
ids are captured automatically and forwarded server-side.

## Consent Mode v2

TracerKit understands the four Google Consent Mode v2 signals
(`ad_storage`, `analytics_storage`, `ad_user_data`,
`ad_personalization`). Wire your CMP's callback to `tracerkit.consent` —
it accepts GCMv2 signal names (`"granted"`/`"denied"` or booleans) or
TracerKit categories (`analytics` ⇔ `analytics_storage`, `marketing` ⇔
`ad_storage` + the ad_user_data/ad_personalization bundle):

```js
// from your CMP's onConsent callback — GCMv2 names pass straight through:
tracerkit.consent({
  ad_storage: "granted",
  analytics_storage: "granted",
  ad_user_data: "granted",
  ad_personalization: "denied",
});
// or the shorthand categories:
tracerkit.consent({ analytics: true, marketing: false });
```

If your CMP already drives Google tags via
`gtag('consent', 'default'|'update', {...})`, TracerKit picks that up from
`window.dataLayer` automatically (best-effort — if gtag.js loads after the
CMP AND after TracerKit, call `tracerkit.consent` explicitly).

What consent controls:

- **Client**: tags in the `analytics`/`marketing` consent category stay
  blocked until the matching signal (`analytics_storage`/`ad_storage`) is
  granted; blocked fires replay once consent arrives.
- **Server**: each event carries the consent state at fire time. Server-side
  forwarding to ad platforms (Meta, TikTok, Google Ads) requires
  `ad_storage` + `ad_user_data`; GA4 requires `analytics_storage`.
  Google Ads uploads include the GCMv2 consent fields the Data Manager API
  expects.
- Sites that never signal consent are unaffected — enforcement only applies
  when consent state is sent.

## Hand it to an AI agent

Copy the ready-made prompt from your site's dashboard page ("Copy prompt
for your AI agent") into Claude Code, Cursor, or any coding agent — it
contains your real key, the dedupe rules, and the verification steps.

## MCP server (dashboard access for agents)

Coding agents can also do the dashboard side — create the site, move tags
in, and verify beacons — over MCP (streamable HTTP):

- **Endpoint**: `https://tracerkit.com/api/mcp`
- **Auth**: `Authorization: Bearer tkapi_...` — an org owner/admin creates
  tokens under Settings → API tokens (https://tracerkit.com/dashboard/settings). The
  token is shown once; it scopes every call to that organization.
- **Tools**: `list_sites`, `create_site`, `list_tags`, `create_tag`,
  `get_site_health`, `get_install_snippet`

Example `.mcp.json` entry:

```json
{
  "mcpServers": {
    "tracerkit": {
      "type": "http",
      "url": "https://tracerkit.com/api/mcp",
      "headers": { "Authorization": "Bearer tkapi_YOUR_TOKEN" }
    }
  }
}
```

## 1. Get your site key

Sign in at https://tracerkit.com/dashboard, add your site, and copy its public key
(looks like `tk_...`) from the site page — the exact snippet is shown there.

## 2. Add the snippet

Paste into your site-wide `<head>`, ideally before other third-party
scripts:

```html
<script async src="https://tracerkit.com/t.js" data-key="tk_YOUR_SITE_KEY"></script>
```

The loader is ~2KB gzipped and `async` — it never blocks rendering.

### Framework placement

- **Plain HTML / WordPress / Shopify / Webflow**: the shared head template
  (WordPress: a header-scripts plugin or your theme's `header.php`).
- **Next.js (App Router)**: a plain `<script>` inside `<head>` in
  `app/layout.tsx`. Use `next/script` with `strategy="beforeInteractive"`
  only if a tag must beat hydration. Or render the same snippet as a typed
  component: `<TracerKit siteKey="tk_..."/>` from `@tracerkit/react`
  (https://tracerkit.com/docs/sdk.md).
- **Single-page apps**: nothing extra — the loader tracks route changes
  (history API) for route-triggered tags.
- **Migrating from Google Tag Manager**: replace the GTM snippet with the
  TracerKit snippet, then recreate your tags in the dashboard.

## 3. Move your tags into TracerKit

Add each script (GA4, Fathom, Meta Pixel, ...) as a tag in the dashboard
instead of hardcoding it. Set its load strategy, trigger, and consent
category there. Remove the old hardcoded snippets so tags don't double-fire.

## 4. Verify

- `curl https://tracerkit.com/api/c/YOUR_KEY` returns your tag config (HTTP 200)
- Load your site: the loader injects your tags and the dashboard's Health
  view starts showing per-tag fire data within a minute
- On the Verification plan, silence alerts arm automatically

## E-commerce: the TracerKit data layer

Selling online? Implement the standardized data layer — a typed event
schema (`page_view`, `view_item`, `add_to_cart`, `begin_checkout`,
`purchase`) pushed onto `window.tracerkitLayer` that feeds purchase
triggers and server-side forwarding. Spec + copy-paste snippets:
https://tracerkit.com/docs/data-layer.md (HTML: https://tracerkit.com/docs/data-layer)

Prefer typed calls? The JS/React SDK (`@tracerkit/js`,
`@tracerkit/react`) wraps the data layer and loader API with full types:
https://tracerkit.com/docs/sdk.md (HTML: https://tracerkit.com/docs/sdk)

Machine-readable version of this page: https://tracerkit.com/docs/install.md
