Skip to content

Commit 8b46f30

Browse files
committed
doc(googleTagManager): add guide for sending page events
1 parent d269066 commit 8b46f30

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

docs/content/scripts/tracking/google-tag-manager.md

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@ Nuxt Scripts provides many features you can easily
2020
implement within your Nuxt app. If you're using GTM for Google Tag Manager, you can use the `useScriptGoogleAnalytics` composable instead.
2121
::
2222

23-
### Nuxt Config Setup
23+
## Loading Globally
2424

25-
The simplest way to load Google Tag Manager globally in your Nuxt App is to use Nuxt config. Alternatively you can directly
26-
use the [useScriptGoogleTagManager](#useScriptGoogleTagManager) composable.
27-
28-
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
29-
disable the script in development.
25+
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.
3026

3127
::code-group
3228

@@ -35,7 +31,7 @@ export default defineNuxtConfig({
3531
scripts: {
3632
registry: {
3733
googleTagManager: {
38-
id: 'YOUR_ID'
34+
id: '<YOUR_ID>'
3935
}
4036
}
4137
}
@@ -48,21 +44,15 @@ export default defineNuxtConfig({
4844
scripts: {
4945
registry: {
5046
googleTagManager: {
51-
token: 'YOUR_TOKEN_ID',
47+
id: '<YOUR_ID>',
5248
}
5349
}
5450
}
5551
}
5652
})
5753
```
5854

59-
::
60-
61-
#### With Environment Variables
62-
63-
If you prefer to configure your id using environment variables.
64-
65-
```ts [nuxt.config.ts]
55+
```ts [Environment Variables]
6656
export default defineNuxtConfig({
6757
scripts: {
6858
registry: {
@@ -74,31 +64,52 @@ export default defineNuxtConfig({
7464
public: {
7565
scripts: {
7666
googleTagManager: {
77-
id: '', // NUXT_PUBLIC_SCRIPTS_GOOGLE_TAG_MANAGER_ID
67+
// .env
68+
// NUXT_PUBLIC_SCRIPTS_GOOGLE_TAG_MANAGER_ID=<your-id>
69+
id: '',
7870
},
7971
},
8072
},
8173
},
8274
})
8375
```
8476

85-
```text [.env]
86-
NUXT_PUBLIC_SCRIPTS_GOOGLE_TAG_MANAGER_ID=<YOUR_ID>
87-
```
77+
::
8878

8979
## useScriptGoogleTagManager
9080

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

83+
9384
```ts
9485
const { proxy } = useScriptGoogleTagManager({
95-
id: 'YOUR_ID'
86+
id: 'YOUR_ID' // id is only needed if you haven't configured globally
9687
})
9788
// example
9889
proxy.dataLayer.push({ event: 'conversion', value: 1 })
9990
```
10091

101-
Please follow the [Registry Scripts](/docs/guides/registry-scripts) guide to learn more about advanced usage.
92+
Please follow the [Registry Scripts](/docs/guides/registry-scripts) guide to learn more about `proxy`.
93+
94+
### Guide: Sending Page Events
95+
96+
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.
97+
This composable will trigger the provided function on route change after the page title has been updated.
98+
99+
```ts
100+
const { proxy } = useScriptGoogleTagManager({
101+
id: 'YOUR_ID' // id is only needed if you haven't configured globally
102+
})
103+
104+
useScriptEventPage((title, path) => {
105+
// triggered on route change after title is updated
106+
proxy.dataLayer.push({
107+
event: 'pageview',
108+
title,
109+
path
110+
})
111+
})
112+
```
102113

103114
### GoogleTagManagerApi
104115

0 commit comments

Comments
 (0)