Hosting · 3 min setup

How to use VisitTrack analytics on Vercel

To use VisitTrack analytics on Vercel, add the script tag in your framework's root layout (Vercel has no setting that injects scripts for you), render it only when VERCEL_ENV is production, and save your production domain under VisitTrack's allowed hostnames so preview deployments never count.

Updated

Vercel hosts whatever framework you deploy — Next.js, SvelteKit, Nuxt, Astro, Remix or a static site — so the install itself follows that framework's guide. What's Vercel-specific is environments: every pull request gets a preview deployment on its own *.vercel.app URL, and you don't want reviewers' clicks in your production numbers.

VisitTrack works the same on any host, which is the main difference from Vercel Web Analytics; for a detailed comparison see VisitTrack vs Vercel Analytics.

Vercel at a glance

Where the tag goes
Your framework's root layout (see its guide)
Production only
Render when process.env.VERCEL_ENV === "production"
Preview URLs
Dropped once you save Allowed hostnames
CSP
headers in vercel.json or your framework config
AI crawlers
Next.js proxy.ts / middleware → docs

How to install VisitTrack on Vercel

  1. Step 1: Install the tag in your framework

    Follow your framework's guide: Next.js, SvelteKit, Nuxt, Astro, Remix, or the plain snippet in your static index.html.

    <head>
    <script defer data-site="SITE_ID" src="https://visitrack.app/tracker.js"></script>
  2. Step 2: Render it in production only

    Vercel sets VERCEL_ENV to production, preview or development. Server-rendered layouts can read it directly; this is the Next.js App Router version.

    app/layout.tsx
    import Script from "next/script";
    
    const isProd = process.env.VERCEL_ENV === "production";
    
    export default function RootLayout({ children }: { children: React.ReactNode }) {
      return (
        <html lang="en">
          <body>
            {children}
            {isProd && <Script defer data-site="SITE_ID" src="https://visitrack.app/tracker.js" />}
          </body>
        </html>
      );
    }
  3. Step 3: Save your allowed hostnames

    In VisitTrack, open Settings → General → Allowed hostnames and save your production domain (and *.yourdomain.com for subdomains). Hits from any other host — *.vercel.app previews included — are dropped before they're stored. Blocked hosts are listed for 7 days with an Allow button, so nothing disappears silently.

  4. Step 4: Allow VisitTrack in your CSP headers

    If you send a Content-Security-Policy from vercel.json, add the origin to script-src and connect-src. Merge it into your existing policy rather than replacing it.

    vercel.json
    {
      "headers": [
        {
          "source": "/(.*)",
          "headers": [
            {
              "key": "Content-Security-Policy",
              "value": "default-src 'self'; script-src 'self' https://visitrack.app; connect-src 'self' https://visitrack.app;"
            }
          ]
        }
      ]
    }

How to check VisitTrack is working on Vercel

  1. Open a preview deployment — nothing should arrive (no tag, or the host is dropped).
  2. Open the production domain and browse two pages; VisitTrack's live view shows you within seconds.
  3. If a host was dropped by mistake, it appears under Blocked hostnames in Settings → General.

Navigation tracking depends on your framework, not on Vercel: client-side routers (Next.js, SvelteKit, Nuxt, Remix) are followed through the History API, and static sites count one pageview per page load. Vercel's edge caching and ISR don't affect it — the script runs in the visitor's browser.

How to track custom events and goals in Vercel

Custom events are framework code: window.visitrack("signup") in the browser, or POST /api/track from a Vercel Function or server action with a secret API key stored in your Vercel project's environment variables (never with a NEXT_PUBLIC_ prefix).

app/api/signup/route.ts
// A Vercel Function / route handler
await fetch("https://visitrack.app/api/track", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.VISITRACK_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ type: "event", visitorId, name: "signup" }),
});

See custom events and server-side events.

Track AI crawlers on Vercel

Crawlers like GPTBot and ClaudeBot never run JavaScript. On a Next.js app on Vercel, report them from middleware (proxy.ts on Next.js 16) — the AI crawler tracking docs include the snippet. Make sure your matcher doesn't skip robots.txt and sitemap.xml, which crawlers fetch first.

Script options you might need on Vercel

All optional — add them to the same tag. Full reference: script configuration.

AttributeWhat it does
data-cookielessStore nothing in the browser; the server derives a daily-rotating id. See cookieless mode.
data-exclude="/app/*"Never track these paths (comma-separated, * wildcard). Checked on every navigation.
data-allow-localTrack on localhost and *.local — for testing an install only.
data-debugLog every event the script sends, and why it skipped one, to the console.
data-auto="false"Turn off automatic events (downloads, contact clicks, form submits, rage clicks, JS errors, 404s).

Vercel troubleshooting

Preview deployments show up in your stats

Save Allowed hostnames, or render the tag only when VERCEL_ENV === "production".

Production custom domain is being blocked

It isn't in Allowed hostnames. Add it (or use *.yourdomain.com) — it's listed under Blocked hostnames with a one-click Allow.

CSP blocks the tracker after adding vercel.json headers

Both script-src and connect-src need https://visitrack.app. A framework-level CSP (Next.js headers or proxy.ts) can also override it — check the final response header in DevTools.

Server events fail with 401

The VISITRACK_API_KEY env var isn't set for the Production environment in Vercel, or the deployment predates it. Redeploy after adding it.

Vercel analytics FAQ

Can I add VisitTrack on Vercel without changing code?

No. Vercel doesn't inject scripts into your pages, so the tag goes into your framework's layout or index.html. It's a one-time, one-line change.

How do I stop Vercel preview deployments from being tracked?

Save your production domain under Settings → General → Allowed hostnames in VisitTrack, which drops hits from other hosts, or render the tag only when VERCEL_ENV is production.

Is VisitTrack an alternative to Vercel Web Analytics?

Yes. It isn't tied to Vercel hosting, and adds revenue attribution, funnels, journeys, session replays and an API. See VisitTrack vs Vercel Analytics for the trade-offs.

Does Vercel's caching affect VisitTrack?

No. The script runs in the visitor's browser and sends events to visitrack.app, so cached and ISR pages are counted like any other.

Where do I store the VisitTrack API key on Vercel?

In your project's Environment Variables, without a NEXT_PUBLIC_ prefix, so it's only available to server code.

Keep going

Add VisitTrack to your Vercel site

Cookie-free analytics with revenue attribution, live visitors, funnels and session replays. One script tag, every feature on every plan. 14 days free, no card required.

Not on Vercel? See all 29 integration guides.