---
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
---

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

- [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
- [Human-in-the-Loop with Chat SDK and Workflow SDK](/kb/guide/human-in-the-loop-with-chat-sdk-and-workflow-sdk): Combine Chat SDK and Workflow SDK to suspend workflows on approval cards in a chat platform, then resume on click via createWebhook.
- [Build with Vercel Blob on Next.js](/kb/guide/vercel-blob-nextjs): Deploy the Vercel Blob Next.js Starter and learn how client uploads store images securely in a private Blob store.

## 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)
