Sign inGet started
Tracking

Subdomains, domains and Safari

View as MarkdownOpen in ChatGPTOpen in Claude
Connect your agent
Add to CursorAdd to VS Code

Keep one visitor across your subdomains and your other domains, and stop Safari from forgetting returning visitors after 7 days.

Updated

After a visitor agrees, the tag keeps one id for them, na_id, so their visits on different days make one person. This page covers keeping that one person when they move between your subdomains and your other domains, and in Safari, which forgets a script's cookie after 7 days.

Before consent none of this applies: the tag keeps nothing on the device. See Cookieless counting and consent.

Do my subdomains share one visitor?

Yes, with nothing to set up. The tag keeps na_id in a cookie on your site's own domain, so www.shop.example, app.shop.example and shop.example all see the same visitor. A second cookie, na_d, holds that domain.

When one project spans two domains, such as a store on shop.example and checkout on pay.example, list the other domain on the tag line of each site. Both lines use the same project key.

On shop.example, the line lists pay.example:

<script async src="https://t.nextanalytica.app/t.js" data-project="na_…" data-domains="pay.example"></script>

On pay.example, it lists shop.example:

<script async src="https://t.nextanalytica.app/t.js" data-project="na_…" data-domains="shop.example"></script>

With the npm package, pass domains:

app/layout.tsx
<Analytics project="na_…" domains={["pay.example"]} />

After a yes, a link to a listed domain carries the visitor's id for 2 minutes, in a _na parameter. The tag on the other side takes it as the visitor's own id once that visitor agrees there too, and removes _na from the address before the page view is sent. Without consent, links carry nothing.

Why does Safari count returning visitors as new?

Safari keeps a cookie that a script writes for 7 days at most. A visitor who comes back after two weeks gets a new na_id and counts as someone new.

A cookie your own server sets keeps its full time. The @nextanalytica/tag/server helper renews na_id on every page your server sends, for 13 months. It only renews an id the tag already made after consent, and never sets one of its own. It also adds na_s, which tells the tag to leave the cookie to your server.

For Next.js, renew in the middleware:

middleware.ts
import { NextResponse, type NextRequest } from "next/server";
import { renew } from "@nextanalytica/tag/server";

export function middleware(req: NextRequest) {
  const res = NextResponse.next();
  renew(req, res.headers);
  return res;
}

For SvelteKit, renew in the server hook:

src/hooks.server.ts
import { renew } from "@nextanalytica/tag/server";

export const handle = async ({ event, resolve }) => {
  const res = await resolve(event);
  renew(event.request, res.headers);
  return res;
};

For any other server, renewCookies returns the Set-Cookie values to add to a page's response. Here it is in Express:

server.js
import { renewCookies } from "@nextanalytica/tag/server";

app.use((req, res, next) => {
  if (req.accepts("html")) res.append("Set-Cookie", renewCookies(req.headers.cookie, req.headers.host));
  next();
});

Next steps

Related
  • What the tag collects: Page views, clicks, forms, scroll, errors and page speed, counted without code. What never leaves the page.
  • Add the tag: Put one script line in the head of every page. Where it goes in plain HTML, site builders and each framework.
  • Cookieless counting and consent: The tag counts without cookies until a visitor agrees. How consent works, from your banner or your code.
  • Custom events: Send your own events and orders, such as sign-ups, demo requests and purchases, with one line of JavaScript each, so revenue ties to the visit.
  • Identify people: Name signed-in people with identify, so their visits on every device make one profile your agent can read. It works only after the visitor's consent.
  • Events from Google's dataLayer: The tag reads the events your site already pushes for Google Tag Manager, such as purchases and sign-ups.
Still have questions?
Was this page useful?