Subdomains, domains and Safari
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.
How do I link my other domains?
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:
<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:
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:
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:
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
- Identify people to join a signed-in person's devices.
- Tag JavaScript API for every attribute of the tag line.