> ## Documentation Index
> Fetch the complete documentation index at: https://doc.trackrev.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Install the tracking pixel

> Add one script tag to your site so TrackRev can tie browser sessions to clicks and join them to revenue.

The TrackRev pixel (`p.js`) is a dependency-free script — under 3 KB — that connects a
browser session to the visitor TrackRev is following. It records pageviews, lets you attach
an email to a visitor with `identify()`, and hands the visitor's identity through to your
checkout so a purchase can be joined back to the click that produced it.

<Note>
  You only need the pixel for **revenue attribution** and **pageview analytics**. Click
  tracking on your short links works without it — clicks are recorded server-side at the
  redirect. Install the pixel on the destination site (your marketing site *and* your app),
  not on the link.
</Note>

## Get your snippet

<Steps>
  <Step title="Open the Integrations tab">
    In your workspace, go to **Settings → Integrations** (the top-level **Integrations** link
    redirects here). Your install snippet is shown under **"1 · Install the pixel"**, pre-filled
    with your workspace ID.
  </Step>

  <Step title="Copy the script tag">
    It looks like this — `data-id` is your workspace UUID:

    ```html theme={null}
    <script async src="https://trackrev.io/p.js" data-id="YOUR_WORKSPACE_ID"></script>
    ```
  </Step>

  <Step title="Paste it into your site head">
    Add it to every page you want to track. On most sites that means the shared layout,
    template, or `<head>` partial so it loads everywhere at once.
  </Step>

  <Step title="Verify it's live">
    Back on the Integrations tab, click **Verify pixel**. TrackRev loads your site and
    confirms the script is firing. See [Verify your install](#verify-your-install) below.
  </Step>
</Steps>

## Where to add it, by stack

<CodeGroup>
  ```html HTML theme={null}
  <!-- In your <head>, on every page -->
  <script async src="https://trackrev.io/p.js" data-id="YOUR_WORKSPACE_ID"></script>
  ```

  ```jsx Next.js (App Router) theme={null}
  // app/layout.tsx
  import Script from "next/script";

  export default function RootLayout({ children }) {
    return (
      <html lang="en">
        <body>
          {children}
          <Script
            src="https://trackrev.io/p.js"
            data-id="YOUR_WORKSPACE_ID"
            strategy="afterInteractive"
          />
        </body>
      </html>
    );
  }
  ```

  ```jsx React (Vite / CRA) theme={null}
  // index.html — add inside <head>
  <script async src="https://trackrev.io/p.js" data-id="YOUR_WORKSPACE_ID"></script>
  ```

  ```html Google Tag Manager theme={null}
  <!-- New tag → Custom HTML -->
  <script async src="https://trackrev.io/p.js" data-id="YOUR_WORKSPACE_ID"></script>
  <!-- Trigger: All Pages -->
  ```

  ```html Webflow / Framer / Squarespace theme={null}
  <!-- Site settings → Custom code → Head -->
  <script async src="https://trackrev.io/p.js" data-id="YOUR_WORKSPACE_ID"></script>
  ```
</CodeGroup>

## What the pixel does automatically

Once loaded, the pixel:

* **Resolves a visitor id (`vid`).** If the visitor arrived through a TrackRev link, the
  redirect handed off the id in the URL (`?_vid=…`) and that always wins, so the
  server-side click and this browser session share one identity. Otherwise it reads (or
  creates) a first-party `vid` cookie.
* **Writes the `vid` cookie on your registrable domain** (e.g. `.example.com`), so one
  identity spans your marketing site, app and checkout subdomains. The cookie is
  first-party and lasts one year.
* **Sends the first pageview**, then exposes `window.trk` for everything else.

<Info>
  Because the cookie is set from **your** domain, not a third-party tracker, it is not
  blocked by Safari ITP the way third-party cookies are, and it is invisible to most
  ad-blockers. This is what "first-party tracking" means in TrackRev.
</Info>

## The `window.trk` API

After the script loads, these methods are available on `window.trk`:

<ParamField path="window.trk.vid" type="string">
  The current visitor id. Pass this to your checkout so a purchase can be joined back to the
  click — see [Connect your revenue](/revenue-attribution/connect-stripe).
</ParamField>

<ParamField path="window.trk.identify(email, props?)" type="function">
  Attach an email (and optional properties) to the current visitor. Call it the moment you
  learn who someone is — on sign-up, login, or newsletter subscribe. Email is TrackRev's
  fallback for joining a payment to a visitor when no checkout reference is present.
</ParamField>

<ParamField path="window.trk.track(event, props?)" type="function">
  Record a custom event (e.g. `"signup"`, `"trial_started"`) with arbitrary properties.
</ParamField>

<ParamField path="window.trk.pageview()" type="function">
  Manually record a pageview. Useful in single-page apps where route changes don't reload
  the document — call it on every client-side navigation.
</ParamField>

### Identify a visitor

Call `identify()` as soon as you know the visitor's email:

```js theme={null}
// After a successful sign-up or login
window.trk?.identify("customer@acme.com");

// With extra properties
window.trk?.identify("customer@acme.com", { plan: "pro", name: "Dana" });
```

### Track a custom event

```js theme={null}
window.trk?.track("trial_started", { plan: "pro" });
```

### Record pageviews in a single-page app

The pixel fires one pageview on load. In an SPA, fire one on each route change:

```jsx theme={null}
// e.g. in a Next.js route-change effect or React Router listener
useEffect(() => {
  window.trk?.pageview();
}, [pathname]);
```

## Options

Set these as attributes on the script tag.

<ParamField path="data-id" type="string" required>
  Your workspace UUID. Found on the Integrations tab. The pixel does nothing without it.
</ParamField>

<ParamField path="data-cookie-domain" type="string">
  Force the cookie's `Domain` explicitly, e.g. `.example.com`. By default the pixel detects
  your registrable domain automatically; set this only if auto-detection picks the wrong
  scope (rare — mostly needed on unusual multi-domain setups).
</ParamField>

```html theme={null}
<script
  async
  src="https://trackrev.io/p.js"
  data-id="YOUR_WORKSPACE_ID"
  data-cookie-domain=".example.com"
></script>
```

## Verify your install

<Steps>
  <Step title="Run the built-in check">
    On the **Integrations** tab, click **Verify pixel**. TrackRev fetches a page from your
    site and confirms the script is present and firing. A green tick means you're done.
  </Step>

  <Step title="Or check it yourself in the console">
    Open your site, open the browser dev tools console, and type:

    ```js theme={null}
    window.trk
    ```

    You should see an object with `identify`, `track`, `pageview` and a `vid` string. If it's
    `undefined`, the script hasn't loaded — check that the tag is in the `<head>` and that no
    content blocker is stripping it.
  </Step>

  <Step title="Confirm the cookie">
    In dev tools → Application → Cookies, look for a `vid` cookie on your domain. Its presence
    confirms the pixel is identifying the visitor.
  </Step>
</Steps>

<Warning>
  If `window.trk` is defined but revenue never attributes, the usual cause is that the `vid`
  isn't reaching your checkout. Make sure you pass `window.trk.vid` as the checkout reference —
  see [Pass the visitor id to checkout](/revenue-attribution/pass-vid-to-checkout).
</Warning>

## Next steps

<CardGroup cols={2}>
  <Card title="Connect your revenue" icon="stripe" href="/revenue-attribution/connect-stripe">
    Link your payment processor so clicks become attributed revenue.
  </Card>

  <Card title="Pass the visitor id to checkout" icon="arrow-right-to-bracket" href="/revenue-attribution/pass-vid-to-checkout">
    The one line that joins a purchase back to the click that earned it.
  </Card>
</CardGroup>
