# Installing the Tracking Script

Add the Mailnaptic tracking snippet to your website

Mailnaptic's site tracking lets you monitor visitor behavior on your website — page views, custom events, and purchases — and connect that data to your email contacts. To enable site tracking, you need to install a small JavaScript snippet on your website.

## Getting your tracking snippet

1. Navigate to **Organization Settings** from the sidebar.
2. Go to the **Site Tracking** section, or use the endpoint `GET /organization/site-tracking-key`.
3. If you do not have a site tracking key yet, one will be generated automatically.
4. Copy the tracking snippet code provided.

The snippet looks like this:

```
<script src="https://your-mailnaptic-domain/tracking-script.js" async></script>
<script>
  window.mnSettings = { siteKey: 'your-site-tracking-key' };
</script>
```

## Installing the snippet

### Option 1: Directly in your HTML

Paste the snippet into the `<head>` section of every page on your website. If you use a shared layout or template file, add it there so it appears on all pages automatically.

### Option 2: Tag manager

If you use Google Tag Manager or a similar tool:

1. Create a new Custom HTML tag.
2. Paste the tracking snippet code.
3. Set the trigger to fire on all pages.
4. Publish the changes.

### Option 3: Single-page applications

For SPAs built with React, Vue, Angular, or similar frameworks, add the snippet to your main `index.html` file. The tracking script automatically detects page navigation via the History API, so it works with client-side routing without additional configuration.

## Verifying the installation

After installing the snippet:

1. Visit a page on your website.
2. In Mailnaptic, navigate to **Domains** and open the domain associated with your website.
3. Check the **Tracking** tab, which shows the last received event and total event count.
4. You should see a page view event within a few minutes.

You can also check your browser's developer tools (Network tab) to verify that requests are being sent to the Mailnaptic ingest endpoint (`POST /site/ingest`).

## How tracking works

### Anonymous tracking

When a visitor arrives on your site, the tracking script creates a first-party cookie (`_mn_aid`) that identifies the browser session. This cookie persists for 365 days, allowing Mailnaptic to track the visitor's behavior across multiple visits even before they are identified.

### Identity resolution

When a contact clicks a link in a Mailnaptic email, the click tracking system appends a special parameter (`?_mn=TOKEN`) to the redirect URL. When the visitor lands on your site with this parameter, the tracking script sends an `identify_token` event that resolves the anonymous visitor to a known contact.

After identification:

- All future events from this browser are associated with the contact.
- Recent anonymous events from the same browser are retroactively attributed to the contact.

## Sending custom events

Beyond automatic page view tracking, you can send custom events from your website:

```
// Track a custom event
window.mn('track', 'product_viewed', {
  product_id: '123',
  product_name: 'Widget Pro'
});

// Track a purchase
window.mn('track', 'purchase_completed', {
  order_id: '456',
  total: 99.99
});
```

Custom events can be used as automation triggers (via the "site event" trigger type) and in segment filters.

## Regenerating your site key

If your site tracking key is compromised, you can regenerate it:

1. Go to **Organization Settings > Site Tracking**.
2. Click **Regenerate Key**.
3. Update the snippet on your website with the new key.
4. The old key will stop working immediately.

## Data privacy considerations

- The tracking cookie is first-party only and is not shared with third parties.
- No personally identifiable information is stored in the cookie — only an anonymous session ID.
- Include Mailnaptic tracking in your website's privacy policy and cookie consent mechanism.
- Site tracking data is subject to GDPR hard delete when a contact is deleted from Mailnaptic.
