---
title: Load Google Analytics script based on user location in Next.js
description: Learn how you can conditionally load a Google Analytics script based on your user's location
url: "https://vercel.com/kb/guide/geolocation-script"
published: 2025-11-03
last_updated: 2025-11-10
authors: Steven Tey
install_vercel_plugin: npx plugins add vercel/vercel-plugin
search_with_vercel_cli: npx vercel@latest kb search "<query>"
read_with_vercel_cli: "npx vercel@latest kb inspect geolocation-script"
---

When you build a site, you might want to load a script based on your users' location.

For example, you may want to only load Google Analytics in certain countries. This could be due to privacy laws amongst other reasons. Being able to load scripts based on conditions can make compliance easier.

On Vercel, you can use the [`x-vercel-ip-country ` header](https://vercel.com/docs/edge-network/headers#x-vercel-ip-country) with a lazy loaded server component to conditionally load a script.

## Example

In this example you will see how to only load a Google Analytics script for countries outside of the European Union.

```tsx
import { headers } from 'next/headers'
import Script from 'next/script'
import { EU_COUNTRY_CODES } from '@/lib/constants' // list of EU country codes in ISO 3166-1 format

export default function Analytics() {
  const countryCode = headers().get('x-vercel-ip-country') || 'US'

  if (EU_COUNTRY_CODES.includes(countryCode)) {
    return null
  }

  return (
    <Script src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID" />
  )
}
```

In the code above, we use the `headers()` [function](https://nextjs.org/docs/app/api-reference/functions/headers) from [Next.js](https://nextjs.org/) to read the `x-vercel-ip-country` header of the incoming request, determine the user's location, and render the GA script only if the user is not located in an EU country using Next.js' [Script component](https://nextjs.org/docs/app/api-reference/components/script).

You can then import this component using `Suspense` in your root layout:

```tsx
import { Suspense } from 'react'
import Analytics from './analytics'

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body>
        <Suspense>
          <Analytics />
        </Suspense>
        {children}
      </body>
    </html>
  )
}
```

This will ensure the `<Analytics/>` component is imported dynamically without blocking the render of the main layout.

Check out the demo here: [https://geolocation-script.vercel.app/](https://geolocation-script.vercel.app/)

![CleanShot\_2023-10-24\_at\_13.16.26\_2x.png](https://images.ctfassets.net/hjgychtc108g/UiZMrCMHABGTF954GW4To/de79cdb8ae386bebf7af2bb4ed788966/CleanShot_2023-10-24_at_13.16.26_2x.png)

If you are located in an EU country, the script will not be loaded. This is communicated via a banner in the UI.

## Related Documentation

- [Next.js](/docs/frameworks/full-stack/nextjs)

## More Next.js guides

- [Vercel vs Webflow Cloud](/kb/guide/vercel-vs-webflow-cloud): Compare Vercel and Webflow Cloud for deploying Next.js and Astro apps, including runtime, framework support, storage, pricing, and where your code runs.
- [Next.js on Vercel vs Webflow Cloud](/kb/guide/next-js-on-vercel-vs-webflow-cloud): Compare running Next.js on Vercel Functions with Fluid compute against Webflow Cloud on Cloudflare Workers. Learn how Next.js feature support, runtimes, storage, pricing, and developer experience differ so you can choose the right platform.
- [Build an AI Chat Agent with Weather API Tool Calling](/kb/guide/build-ai-agent-weather-api): Build an intelligent conversational agent that fetches real-time weather data using the AI SDK, tool calling, and a backend weather API powered by Express, FastAPI or Nitro

## Reference Architecture

Choose a stack and work through the decisions behind it, with curated documentation, guides, templates, and courses to help you build.

- [Agent stack](/kb/architecture/agents): Build and operate agents with models, tools, and durable workflows.
- [Web applications](/kb/architecture/web): Choose rendering, data, authentication, and caching approaches.
- [Backends](/kb/architecture/backends): Choose a runtime, connect services, and run background work.

[Browse all stacks](/kb/architecture)

## Build with prompts

Build an app, extend an agent, or improve your project with ready-to-use prompts for your coding agent.

[Browse the prompt library](/kb/prompts) · [Read the Markdown index](/kb/prompts.md)

## Search guides from your terminal

Search the Knowledge Base and read guides as Markdown with the Vercel CLI. No login required.

```bash
# Find guides by keyword
npx vercel@latest kb search "rewrites and redirects"
npx vercel@latest kb search "AI SDK streaming"

# Show the latest guides
npx vercel@latest kb

# Print a guide as Markdown, by slug or URL
npx vercel@latest kb inspect <guide>
```
