Skip to content

Commit 5e7d9b4

Browse files
authored
feat(gtm): add onBeforeGtmStart (#392)
1 parent cf3937c commit 5e7d9b4

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,18 @@ export const GoogleTagManagerOptions = object({
128128
id: string(),
129129
/**
130130
* The name of the dataLayer you want to use
131-
* @default 'defaultGtm'
131+
* @default 'dataLayer'
132132
*/
133133
dataLayerName: optional(string())
134134
})
135135
```
136136

137+
### Options types
138+
139+
```ts
140+
type GoogleTagManagerInput = typeof GoogleTagManagerOptions & { onBeforeGtmStart?: ((gtag: Gtag) => void) => void }
141+
```
142+
137143
## Example
138144
139145
Using Google Tag Manager only in production while using `dataLayer` to send a conversion event.
@@ -163,3 +169,9 @@ function sendConversion() {
163169

164170

165171
::
172+
173+
## Configuring GTM before it starts
174+
175+
`useScriptGoogleTagManager` initialize Google Tag Manager by itself. This means it pushes the `js`, `config` and the `gtm.start` events for you.
176+
177+
If you need to configure GTM before it starts. For example, (setting the consent mode)[https://developers.google.com/tag-platform/security/guides/consent?hl=fr&consentmode=basic]. You can use the `onBeforeGtmStart` hook which is run right before we push the `gtm.start` event into the dataLayer.

playground/pages/third-parties/google-tag-manager.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ useHead({
88
// composables return the underlying api as a proxy object and the script state
99
const { dataLayer, then, status } = useScriptGoogleTagManager({
1010
id: 'GTM-MNJD4B',
11+
onBeforeGtmStart(gtag) {
12+
gtag('consent', 'default', {
13+
ad_user_data: 'denied',
14+
ad_personalization: 'denied',
15+
ad_storage: 'denied',
16+
analytics_storage: 'denied',
17+
wait_for_update: 500,
18+
})
19+
},
1120
}) // id is set via runtime config
1221
dataLayer.push({
1322
event: 'page_view',

src/runtime/registry/google-analytics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface GTag {
1010
(fn: 'config' | 'get', opt: string): void
1111
(fn: 'event', opt: string, opt2?: Record<string, any>): void
1212
(fn: 'set', opt: Record<string, string>): void
13-
(fn: 'consent', opt: ConsentOptions, opt2: Record<string, string>): void
13+
(fn: 'consent', opt: ConsentOptions, opt2: Record<string, string | number>): void
1414
}
1515
type DataLayer = Array<Parameters<GTag> | Record<string, unknown>>
1616

src/runtime/registry/google-tag-manager.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const GoogleTagManagerOptions = object({
4141

4242
export type GoogleTagManagerInput = RegistryScriptInput<typeof GoogleTagManagerOptions>
4343

44-
export function useScriptGoogleTagManager<T extends GoogleTagManagerApi>(_options?: GoogleTagManagerInput) {
44+
export function useScriptGoogleTagManager<T extends GoogleTagManagerApi>(_options?: GoogleTagManagerInput & { onBeforeGtmStart?: (gtag: GTag) => void }) {
4545
return useRegistryScript<T, typeof GoogleTagManagerOptions>(_options?.key || 'googleTagManager', options => ({
4646
scriptInput: {
4747
src: withQuery('https://www.googletagmanager.com/gtm.js', { id: options?.id, l: options?.l }),
@@ -53,13 +53,18 @@ export function useScriptGoogleTagManager<T extends GoogleTagManagerApi>(_option
5353
},
5454
stub: import.meta.client ? undefined : ({ fn }) => { return fn === 'dataLayer' ? [] : void 0 },
5555
performanceMarkFeature: 'nuxt-third-parties-gtm',
56-
...({ tagPriority: 1 }),
56+
tagPriority: 1,
5757
},
5858
clientInit: import.meta.server
5959
? undefined
6060
: () => {
6161
const dataLayerName = options?.l ?? 'dataLayer';
6262
(window as any)[dataLayerName] = (window as any)[(options?.l ?? 'dataLayer')] || []
63+
function gtag() {
64+
// eslint-disable-next-line prefer-rest-params
65+
(window as any)[dataLayerName].push(arguments)
66+
}
67+
_options?.onBeforeGtmStart?.(gtag)
6368
;(window as any)[dataLayerName].push({ 'gtm.start': new Date().getTime(), 'event': 'gtm.js' })
6469
},
6570
}), _options)

0 commit comments

Comments
 (0)