Source: https://datafa.st/docs/cross-domain-tracking
Markdown source: https://datafa.st/docs/cross-domain-tracking.md
Description: Track visitors across multiple root domains with DataFast using cross-domain tracking.

# Track visitors across different domains

Track visitors seamlessly when they navigate between completely different root domains (e.g., from `example.com` to `myapp.io`).

> Looking to track across subdomains instead? Check out [subdomain tracking](/docs/subdomain-tracking) which works automatically without any setup.

## When to use cross-domain tracking

Use cross-domain tracking when you have multiple websites with different root domains that you want to track as a single user journey:

- Marketing site on `example.com` and app on `app.io`
- Main site on `mysite.com` and store on `shop-mysite.com`
- Multiple brand domains that share users

## Setting up cross-domain tracking

1. Go to your website settings in DataFast
2. In the **General** tab, scroll to "Additional domains"
3. Add the other domains you want to track (e.g., `myapp.io`)
4. Install the tracking script on all domains with the **same website ID**, the same `data-domain` attribute, and add the `data-allowed-hostnames` attribute with your additional domain(s):

```html
<script
  defer
  data-website-id="dfid_******" 
  data-domain="myapp.com"
  data-allowed-hostnames="myapp.io"
  src="https://datafa.st/js/script.js"
></script>
```

> Important: The `data-allowed-hostnames` attribute should be a comma-separated list of all additional domains you want to track (excluding your main domain).

## How it works

When users click a link to one of your allowed domains, DataFast automatically appends tracking parameters (`_df_vid` and `_df_sid`) to preserve their visitor and session IDs across domains.

The URL parameters are automatically cleaned from the browser address bar after being captured, keeping your URLs clean.

## Using the SDK instead of the script tag

If you're using the `datafast` npm package instead of the script tag, cross-domain tracking works through manual helpers:

```typescript
const datafast = await initDataFast({
  websiteId: 'dfid_******',
  allowedHostnames: ['myapp.io'],
});

// Build a link URL with tracking params
const url = datafast.buildCrossDomainUrl('https://myapp.io/signup');

// Or get the raw params to append yourself
const params = datafast.getTrackingParams();
```

On the receiving domain, `initDataFast` automatically picks up the `_df_vid` and `_df_sid` URL params and restores the visitor/session.
