---
title: "Google Analytics · Nuxt Scripts"
description: "Use Google Analytics in your Nuxt app."
canonical_url: "https://scripts.nuxt.com/scripts/v0/analytics/google-analytics"
meta:
  "og:description": "Use Google Analytics in your Nuxt app."
  "og:image": "https://scripts.nuxt.com/_og/d/c_Docs%2Ctitle_Google%2BAnalytics%2Cdescription_Use%2BGoogle%2BAnalytics%2Bin%2Byour%2BNuxt%2Bapp.%2Cheadline_Analytics%2Cp_Ii9zY3JpcHRzL3YwL2FuYWx5dGljcy9nb29nbGUtYW5hbHl0aWNzIg%2Cs_jcd5p3-GgMINxOjE.png"
  "og:image:height": "600"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "Google Analytics · Nuxt Scripts"
  "twitter:image": "https://scripts.nuxt.com/_og/d/c_Docs%2Ctitle_Google%2BAnalytics%2Cdescription_Use%2BGoogle%2BAnalytics%2Bin%2Byour%2BNuxt%2Bapp.%2Cheadline_Analytics%2Cp_Ii9zY3JpcHRzL3YwL2FuYWx5dGljcy9nb29nbGUtYW5hbHl0aWNzIg%2Cs_jcd5p3-GgMINxOjE.png"
---

> Index of every page on this site: [llms.txt](https://scripts.nuxt.com/llms.txt)

**Analytics**

# **Google Analytics**

[Copy for LLMs](https://scripts.nuxt.com/scripts/v0/analytics/google-analytics.md)

[**Google Analytics**](https://marketingplatform.google.com/about/analytics/) is an analytics solution for Nuxt Apps.

It provides detailed insights into how your website is performing, how users are interacting with your content, and how they are navigating through your site.

The simplest way to load Google Analytics globally in your Nuxt App is to use Nuxt config. Alternatively you can directly use the **useScriptGoogleAnalytics** composable.

### Loading Globally

If you don't plan to send custom events you can use the [**Environment overrides**](https://nuxt.com/docs/getting-started/configuration#environment-overrides) to disable the script in development.

```ts
export default defineNuxtConfig({
  scripts: {
    registry: {
      googleAnalytics: {
        id: 'YOUR_ID',
      }
    }
  }
})
```

## useScriptGoogleAnalytics

The `**useScriptGoogleAnalytics**` composable lets you have fine-grain control over when and how Google Analytics is loaded on your site.

```ts
const googleAnalytics = useScriptGoogleAnalytics({
  id: 'YOUR_ID'
})
```

Please follow the [**Registry Scripts**](https://scripts.nuxt.com/docs/v0/guides/registry-scripts) guide to learn more about advanced usage.

### Usage

To interact with the Google Analytics API, it's recommended to use script [**proxy**](https://scripts.nuxt.com/docs/v0/guides/key-concepts#understanding-proxied-functions).

```ts
const { proxy } = useScriptGoogleAnalytics()

proxy.gtag('event', 'page_view')
```

The proxy exposes the `**gtag**` and `**dataLayer**` properties, and you should use them following Google Analytics best practices.

### GoogleAnalyticsApi

```ts
export interface GTag {
  // Initialize gtag.js with timestamp
  (command: 'js', value: Date): void

  // Configure a GA4 property
  (command: 'config', targetId: string, configParams?: ConfigParams): void

  // Get a value from gtag
  (command: 'get', targetId: string, fieldName: string, callback?: (field: any) => void): void

  // Send an event to GA4
  (command: 'event', eventName: DefaultEventName, eventParams?: EventParameters): void

  // Set default parameters for all subsequent events
  (command: 'set', params: GtagCustomParams): void

  // Update consent state
  (command: 'consent', consentArg: 'default' | 'update', consentParams: ConsentOptions): void
}

interface GoogleAnalyticsApi {
  dataLayer: Record<string, any>[]
  gtag: GTag
}
```

### Config Schema

You must provide the options when setting up the script for the first time.

```ts
export const GoogleAnalyticsOptions = object({
  /**
   * The Google Analytics ID. Optional - allows loading gtag.js without initial configuration.
   */
  id: optional(string()),
  /**
   * The datalayer's name you want it to be associated with
   */
  l: optional(string())
})
```

### Customer/Consumer ID Tracking

For e-commerce or multi-tenant applications where you need to track customer-specific analytics alongside your main tracking:

ProductPage.vue

```vue
<script setup lang="ts">
// Product page with customer-specific tracking
const route = useRoute()
const pageData = await $fetch(`/api/product/${route.params.id}`)

// Load gtag with a custom dataLayer name for customer tracking
const { proxy: customerGtag, load } = useScriptGoogleAnalytics({
  key: 'gtag-customer',
  l: 'customerDataLayer', // Custom dataLayer name
})

// Configure customer's tracking ID when available
const consumerGtagId = computed(() => pageData?.gtag)

if (consumerGtagId.value) {
  // Configure the customer's GA4 property
  customerGtag.gtag('config', consumerGtagId.value)

  // Send customer-specific events
  customerGtag.gtag('event', 'product_view', {
    item_id: pageData.id,
    customer_id: pageData.customerId,
    value: pageData.price
  })
}
</script>
```

## Example

Using Google Analytics only in production while using `**gtag**` to send a conversion event.

```vue
<script setup lang="ts">
const { proxy } = useScriptGoogleAnalytics()

// noop in development, ssr
// just works in production, client
proxy.gtag('event', 'conversion-test')
function sendConversion() {
  proxy.gtag('event', 'conversion')
}
</script>

<template>
  <div>
    <button @click="sendConversion">
      Send Conversion
    </button>
  </div>
</template>
```

[Edit this page](https://github.com/nuxt/scripts/edit/0.x/docs/content/analytics/google-analytics.md)

[Markdown For LLMs](https://scripts.nuxt.com/scripts/v0/analytics/google-analytics.md)

[**Fathom Analytics** Use Fathom Analytics in your Nuxt app.](https://scripts.nuxt.com/scripts/v0/analytics/fathom-analytics) [**Matomo Analytics** Use Matomo Analytics in your Nuxt app.](https://scripts.nuxt.com/scripts/v0/analytics/matomo-analytics)

**On this page **

- [Loading Globally](#loading-globally)
- [useScriptGoogleAnalytics](#usescriptgoogleanalytics)
- [Example](#example)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://scripts.nuxt.com/#website","@type":"WebSite","description":"Nuxt Scripts lets you load third-party scripts with better performance, privacy, security and DX. It includes many popular third-parties out of the box.","name":"Nuxt Scripts","url":"https://scripts.nuxt.com/","publisher":{"@id":"https://scripts.nuxt.com/#identity"}},{"@id":"https://scripts.nuxt.com/scripts/v0/analytics/google-analytics#webpage","@type":"WebPage","description":"Use Google Analytics in your Nuxt app.","name":"Google Analytics","url":"https://scripts.nuxt.com/scripts/v0/analytics/google-analytics","breadcrumb":{"@id":"https://scripts.nuxt.com/scripts/v0/analytics/google-analytics#breadcrumb"},"isPartOf":{"@id":"https://scripts.nuxt.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://scripts.nuxt.com/scripts/v0/analytics/google-analytics"]}],"primaryImageOfPage":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/#identity","@type":"Organization","name":"Nuxt Scripts","url":"https://scripts.nuxt.com","logo":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/scripts/v0/analytics/google-analytics#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Nuxt Scripts","item":"https://scripts.nuxt.com/","position":1},{"@type":"ListItem","name":"Scripts","item":"https://scripts.nuxt.com/scripts","position":2},{"@type":"ListItem","name":"V0","item":"https://scripts.nuxt.com/scripts/v0","position":3},{"@type":"ListItem","name":"Analytics","item":"https://scripts.nuxt.com/scripts/v0/analytics","position":4},{"@type":"ListItem","name":"Google Analytics","item":"https://scripts.nuxt.com/scripts/v0/analytics/google-analytics","position":5}]},{"@id":"https://scripts.nuxt.com/#logo","@type":"ImageObject","caption":"Nuxt Scripts","contentUrl":"https://scripts.nuxt.com/logo.svg","inLanguage":"en","url":"https://scripts.nuxt.com/logo.svg"}]}
```

## Sitemap

See the full [sitemap](https://scripts.nuxt.com/sitemap.md) for all pages.
