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

- [Migrate self-hosted Next.js and containers from AWS to Vercel](/kb/guide/migrate-containers-from-aws-to-vercel): Migrate Next.js apps and HTTP containers from AWS to Vercel. Configure AWS access, assess background workers, and plan testing, DNS cutover, and rollback.
- [Migrate a Next.js app from Webflow Cloud to Vercel](/kb/guide/migrate-a-next-js-app-from-webflow-cloud-to-vercel): Move your Next.js app from Webflow Cloud to Vercel: remove the OpenNext Cloudflare adapter, drop the base path, map storage bindings to Vercel Blob, Redis, and Postgres, and deploy with Git or the vercel CLI.
- [How to revalidate Sitecore pages on publish with Next.js](/kb/guide/update-sitecore-content-manually): Learn how to use a Sitecore Experience Edge webhook and Next.js revalidatePath to refresh cached Sitecore Content SDK pages after each publish.