Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion docs/content/scripts/tracking/google-tag-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,18 @@ export const GoogleTagManagerOptions = object({
id: string(),
/**
* The name of the dataLayer you want to use
* @default 'defaultGtm'
* @default 'dataLayer'
*/
dataLayerName: optional(string())
})
```

### Options types

```ts
type GoogleTagManagerInput = typeof GoogleTagManagerOptions & { onBeforeGtmStart?: ((gtag: Gtag) => void) => void }
```

## Example

Using Google Tag Manager only in production while using `dataLayer` to send a conversion event.
Expand Down Expand Up @@ -163,3 +169,9 @@ function sendConversion() {


::

## Configuring GTM before it starts

`useScriptGoogleTagManager` initialize Google Tag Manager by itself. This means it pushes the `js`, `config` and the `gtm.start` events for you.

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.
9 changes: 9 additions & 0 deletions playground/pages/third-parties/google-tag-manager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ useHead({
// composables return the underlying api as a proxy object and the script state
const { dataLayer, then, status } = useScriptGoogleTagManager({
id: 'GTM-MNJD4B',
onBeforeGtmStart(gtag) {
gtag('consent', 'default', {
ad_user_data: 'denied',
ad_personalization: 'denied',
ad_storage: 'denied',
analytics_storage: 'denied',
wait_for_update: 500,
})
},
}) // id is set via runtime config
dataLayer.push({
event: 'page_view',
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/registry/google-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface GTag {
(fn: 'config' | 'get', opt: string): void
(fn: 'event', opt: string, opt2?: Record<string, any>): void
(fn: 'set', opt: Record<string, string>): void
(fn: 'consent', opt: ConsentOptions, opt2: Record<string, string>): void
(fn: 'consent', opt: ConsentOptions, opt2: Record<string, string | number>): void
}
type DataLayer = Array<Parameters<GTag> | Record<string, unknown>>

Expand Down
9 changes: 7 additions & 2 deletions src/runtime/registry/google-tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const GoogleTagManagerOptions = object({

export type GoogleTagManagerInput = RegistryScriptInput<typeof GoogleTagManagerOptions>

export function useScriptGoogleTagManager<T extends GoogleTagManagerApi>(_options?: GoogleTagManagerInput) {
export function useScriptGoogleTagManager<T extends GoogleTagManagerApi>(_options?: GoogleTagManagerInput & { onBeforeGtmStart?: (gtag: GTag) => void }) {
return useRegistryScript<T, typeof GoogleTagManagerOptions>(_options?.key || 'googleTagManager', options => ({
scriptInput: {
src: withQuery('https://www.googletagmanager.com/gtm.js', { id: options?.id, l: options?.l }),
Expand All @@ -53,13 +53,18 @@ export function useScriptGoogleTagManager<T extends GoogleTagManagerApi>(_option
},
stub: import.meta.client ? undefined : ({ fn }) => { return fn === 'dataLayer' ? [] : void 0 },
performanceMarkFeature: 'nuxt-third-parties-gtm',
...({ tagPriority: 1 }),
tagPriority: 1,
},
clientInit: import.meta.server
? undefined
: () => {
const dataLayerName = options?.l ?? 'dataLayer';
(window as any)[dataLayerName] = (window as any)[(options?.l ?? 'dataLayer')] || []
function gtag() {
// eslint-disable-next-line prefer-rest-params
(window as any)[dataLayerName].push(arguments)
}
_options?.onBeforeGtmStart?.(gtag)
;(window as any)[dataLayerName].push({ 'gtm.start': new Date().getTime(), 'event': 'gtm.js' })
},
}), _options)
Expand Down