百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

nuxt-gtag

> 前端框架
开源

Google Analytics 和 Google Ads 集成变得更加简单

375 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Google Analytics 和 Google Ads 集成变得更加简单

Nuxt Google Tag

Google Analytics, Google Ads and Consent Mode v2 for Nuxt, through Google's own gtag.js.

  • ✨  Release Notes

Features

  • Loads no tracking library of its own, only Google's gtag.js
  • ️ Use Google Analytics 4, Google Ads and other products
  • ️ Supports Google Consent Mode v2
  • Manually initialize a Google tag
  • Supports multiple tag IDs
  • Track events with composables
  • ️ Fully typed gtag.js API
  • SSR-ready

Setup

bash
npx nuxt module add gtag

Basic Usage

Add nuxt-gtag to the modules section of your Nuxt configuration and provide your Google tag ID (for multiple tag IDs, see below).

typescript
// `nuxt.config.ts`
export default defineNuxtConfig({
  modules: ['nuxt-gtag'],

  gtag: {
    id: 'G-XXXXXXXXXX'
  }
})

Done! The gtag.js script will be loaded and initialized client-side with your Google tag ID when the Nuxt application starts.

[!NOTE] Ensure that the Enhanced measurement feature is enabled to allow gtag.js to automatically track page changes based on browser history events in Nuxt.

To enable this feature:

  1. Go to the GA4 reporting view and click on "Admin".
  2. Select "Data Streams" under the "Property" column.
  3. Click on your web data stream.
  4. Expand the "Enhanced measurement" switch button.
  5. Ensure the "Page changes based on browser history events" switch button is enabled.

Configuration

All supported module options can be configured using the gtag key in your Nuxt configuration:

typescript
export default defineNuxtConfig({
  modules: ['nuxt-gtag'],

  gtag: {
    id: 'G-XXXXXXXXXX',
    config: {
      page_title: 'My Custom Page Title'
    }
  }
})

Conditional Enable/Disable of the Module

You may want to disable the Google tag module in certain environments, such as development or staging. To do this, set the enabled option to false.

[!NOTE] Composables like useGtag and useTrackEvent are still importable when the module is disabled. In this case, the functions are replaced with no-ops to avoid type and logic errors.

typescript
export default defineNuxtConfig({
  modules: ['nuxt-gtag'],

  gtag: {
    enabled: process.env.NODE_ENV === 'production',
    id: 'G-XXXXXXXXXX'
  }
})

Multiple Google Tags

If you want to send data to multiple destinations, you can add more than one Google tag ID to your Nuxt configuration in the tags module option. Pass a string (single tag ID) or an object (tag ID with additional configuration) to the tags array.

Here, the second tag sends to a Floodlight destination:

typescript
export default defineNuxtConfig({
  modules: ['nuxt-gtag'],

  gtag: {
    tags: [
      // Google Ads and GA4, with additional configuration
      {
        id: 'G-XXXXXXXXXX',
        config: {
          page_title: 'My Custom Page Title'
        }
      },
      // Second Google tag ID for Floodlight
      'DC-ZZZZZZZZZZ'
    ]
  }
})

Runtime Config

Instead of hard-coding your Google tag ID in your Nuxt configuration, set it in your project's .env file – Nuxt overwrites public runtime config values with matching environment variables at runtime.

ini
# Overwrites the `gtag.id` module option
NUXT_PUBLIC_GTAG_ID=G-XXXXXXXXXX

With this setup, you can omit the gtag key in your Nuxt configuration if you only intend to set the Google tag ID.

Google Consent Mode

[!TIP] Follows the Google Consent Mode v2 specification.

Set a default value for each consent type you use. Without initCommands, no consent mode values are set:

typescript
export default defineNuxtConfig({
  modules: ['nuxt-gtag'],

  gtag: {
    id: 'G-XXXXXXXXXX',
    initCommands: [
      // Set up consent mode
      ['consent', 'default', {
        ad_user_data: 'denied',
        ad_personalization: 'denied',
        ad_storage: 'denied',
        analytics_storage: 'denied',
        wait_for_update: 500,
      }]
    ]
  }
})

After a user indicates their consent choices, update relevant parameters to granted:

typescript
function allConsentGranted() {
  const { gtag } = useGtag()
  gtag('consent', 'update', {
    ad_user_data: 'granted',
    ad_personalization: 'granted',
    ad_storage: 'granted',
    analytics_storage: 'granted'
  })
}

function consentGrantedAdStorage() {
  const { gtag } = useGtag()
  gtag('consent', 'update', {
    ad_storage: 'granted'
  })
}

// Invoke the consent function when a user interacts with your banner
consentGrantedAdStorage() // Or `allConsentGranted()`

Manually Load gtag.js Script

For even more control than the consent mode, you can delay the loading of the gtag.js script until the user has granted consent to your privacy policy. Set the initMode option to manual to prevent loading the gtag.js script until you initialize it manually.

typescript
export default defineNuxtConfig({
  modules: ['nuxt-gtag'],

  gtag: {
    initMode: 'manual',
    id: 'G-XXXXXXXXXX'
  }
})

To manually load the Google tag script, e.g. after the user has accepted your privacy policy, use the initialize method destructurable from useGtag:

xml

  
    Grant Consent
  

Multi-Tenancy

Leave the Google tag ID in your Nuxt configuration blank and pass it to initialize as the first argument instead. Reach for this when each user or tenant brings their own ID.

typescript
const { initialize } = useGtag()

function acceptTracking() {
  initialize('G-TENANT-123')
  // Optionally, track the current page view
  // useTrackEvent('page_view')
}

Consent defaults do not need a static id either. Set initCommands on their own, and they run before the tenant's ID is configured:

typescript
export default defineNuxtConfig({
  modules: ['nuxt-gtag'],

  gtag: {
    initMode: 'manual',
    initCommands: [
      ['consent', 'default', {
        analytics_storage: 'denied',
        wait_for_update: 500,
      }]
    ]
  }
})

Module Options

Option Type Default Description
enabled boolean true Whether to enable the Google tag module for the current environment.
initMode 'auto' | 'manual' 'auto' Whether to initialize the Google tag script immediately after the page has loaded. Set it to 'manual' to initialize with initialize instead.
id string '' The Google tag ID to initialize.
initCommands [command, ...args][] [] Commands to run before the tag ID is configured, such as the default consent state.
config ControlParams | EventParams | ConfigParams | CustomParams {} The configuration parameters passed to the config command.
tags (string | GoogleTagOptions)[] [] Further tag IDs to initialize, for sending data to more than one destination.
loadingStrategy 'async' | 'defer' 'defer' Whether the gtag.js script is loaded with async or with defer.
url string 'https://www.googletagmanager.com/gtag/js' The URL to load the gtag.js script from.

Composables

As with other composables in the Nuxt ecosystem, they are auto-imported and can be used in your application's components.

[!NOTE] Every composable here is SSR-safe, but the gtag.js instance lives in the client only. On the server the calls have no effect.

useGtag

typescript
const { gtag, initialize, disableAnalytics, enableAnalytics } = useGtag()

Type Declarations

typescript
function useGtag(): {
  gtag: Gtag
  initialize: (id?: string) => void
  disableAnalytics: (id?: string) => void
  enableAnalytics: (id?: string) => void
}

gtag

The gtag function is the main interface to the gtag.js instance and can be used to run every gtag.js command.

Example

The following event command fires the event screen_view with two parameters: app_name and screen_name.

typescript
const { gtag } = useGtag()

gtag('event', 'screen_view', {
  app_name: 'My App',
  screen_name: 'Home'
})

Type Declarations

…

initialize

Injects the gtag.js script into the document's head and configures the tag IDs. Requires initMode: 'manual' in the module options.

The function accepts an optional ID for a Google tag that the module options do not carry. The ID joins the ones from your configuration rather than replacing them, so the call configures that tag and every tag still waiting.

A tag ID is configured once. Calling initialize again passes only the IDs it hasn't configured yet to the config command, so a consent flow may call it as often as it needs to.

Example

typescript
const { initialize } = useGtag()

// Loads the script and configures every tag ID from the module options
initialize()

Type Declarations

typescript
function initialize(id?: string): void

disableAnalytics

In some cases, it may be necessary to disable Google Analytics without removing the Google tag. For example, you might want to provide users with the option to opt out of tracking.

The gtag.js library includes a window['ga-disable-GA_MEASUREMENT_ID'] property that, when set to true, disables gtag.js from sending data to Google Analytics.

The optional ID names the tag to switch off. It covers one tag, so a setup with multiple tag IDs needs one call per tag – without an argument, only the first configured tag is affected.

Example

typescript
const { disableAnalytics } = useGtag()

disableAnalytics()

Type Declarations

typescript
function disableAnalytics(id?: string): void

enableAnalytics

The enableAnalytics method is the counterpart to disableAnalytics and can be used to re-enable Google Analytics after it has been disabled. Its id parameter selects the tag the same way.

Example

typescript
const { enableAnalytics } = useGtag()

enableAnalytics()

Type Declarations

typescript
function enableAnalytics(id?: string): void

useTrackEvent

Fires an event, optionally with a set of event parameters.

  • The name of the recommended or custom event.
  • A collection of parameters that provide additional information about the event (optional).

Example

The following fires an event called login with a parameter method:

typescript
useTrackEvent('login', {
  method: 'Google'
})

Type Declarations

typescript
function useTrackEvent(
  eventName: EventNames | (string & {}),
  eventParams?: ControlParams | EventParams | CustomParams
): void

Development

  1. Clone this repository
  2. Enable Corepack using corepack enable
  3. Install dependencies using pnpm install
  4. Run pnpm run dev:prepare
  5. Start development server using pnpm run dev

Credits

  • Konkon for his logo pixel art.
  • Junyoung Choi and Lucas Akira Uehara for their Google gtag.js API type definitions

License

MIT License © 2023-PRESENT Johann Schopplich

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptgoogle-analyticsgoogle-taggtagnuxt

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架