Skip to content

Commit

Permalink
doc(googleTagManager): add guide for sending page events
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Sep 15, 2024
1 parent d269066 commit 8b46f30
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions docs/content/scripts/tracking/google-tag-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ Nuxt Scripts provides many features you can easily
implement within your Nuxt app. If you're using GTM for Google Tag Manager, you can use the `useScriptGoogleAnalytics` composable instead.
::

### Nuxt Config Setup
## Loading Globally

The simplest way to load Google Tag Manager globally in your Nuxt App is to use Nuxt config. Alternatively you can directly
use the [useScriptGoogleTagManager](#useScriptGoogleTagManager) composable.

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.
If you'd like to avoid loading the analytics in development, you can use the [Environment overrides](https://nuxt.com/docs/getting-started/configuration#environment-overrides) in your Nuxt config.

::code-group

Expand All @@ -35,7 +31,7 @@ export default defineNuxtConfig({
scripts: {
registry: {
googleTagManager: {
id: 'YOUR_ID'
id: '<YOUR_ID>'
}
}
}
Expand All @@ -48,21 +44,15 @@ export default defineNuxtConfig({
scripts: {
registry: {
googleTagManager: {
token: 'YOUR_TOKEN_ID',
id: '<YOUR_ID>',
}
}
}
}
})
```

::

#### With Environment Variables

If you prefer to configure your id using environment variables.

```ts [nuxt.config.ts]
```ts [Environment Variables]
export default defineNuxtConfig({
scripts: {
registry: {
Expand All @@ -74,31 +64,52 @@ export default defineNuxtConfig({
public: {
scripts: {
googleTagManager: {
id: '', // NUXT_PUBLIC_SCRIPTS_GOOGLE_TAG_MANAGER_ID
// .env
// NUXT_PUBLIC_SCRIPTS_GOOGLE_TAG_MANAGER_ID=<your-id>
id: '',
},
},
},
},
})
```

```text [.env]
NUXT_PUBLIC_SCRIPTS_GOOGLE_TAG_MANAGER_ID=<YOUR_ID>
```
::

## useScriptGoogleTagManager

The `useScriptGoogleTagManager` composable lets you have fine-grain control over when and how Google Tag Manager is loaded on your site.


```ts
const { proxy } = useScriptGoogleTagManager({
id: 'YOUR_ID'
id: 'YOUR_ID' // id is only needed if you haven't configured globally
})
// example
proxy.dataLayer.push({ event: 'conversion', value: 1 })
```

Please follow the [Registry Scripts](/docs/guides/registry-scripts) guide to learn more about advanced usage.
Please follow the [Registry Scripts](/docs/guides/registry-scripts) guide to learn more about `proxy`.

### Guide: Sending Page Events

If you'd like to manually send page events to Google Tag Manager, you can use the `proxy` with the [useScriptEventPage](/docs/api/use-script-event-tag) composable.
This composable will trigger the provided function on route change after the page title has been updated.

```ts
const { proxy } = useScriptGoogleTagManager({
id: 'YOUR_ID' // id is only needed if you haven't configured globally
})

useScriptEventPage((title, path) => {
// triggered on route change after title is updated
proxy.dataLayer.push({
event: 'pageview',
title,
path
})
})
```

### GoogleTagManagerApi

Expand Down

0 comments on commit 8b46f30

Please sign in to comment.