-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
module.ts
133 lines (121 loc) · 3.28 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { defu } from 'defu'
import { addImports, addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
import { name, version } from '../package.json'
import type { GoogleTagOptions } from './runtime/types'
export interface ModuleOptions {
/**
* Whether to initialize the Google tag script immediately after the page has loaded.
*
* @remarks
* Set this to `false` to delay the initialization until you call the `enable` function manually.
*
* @default true
*/
enabled?: boolean
/**
* The Google tag ID to initialize.
*
* @default undefined
*/
id?: string
/**
* Additional commands to be executed before the Google tag ID is initialized.
*
* @remarks
* Useful to set the default consent state. Only applies when `id` is set. For multiple tags, use the `tags` option instead.
*
* @example
* ```ts
* commands: [
* ['consent', 'default', {
* ad_storage: 'denied',
* ad_user_data: 'denied',
* ad_personalization: 'denied',
* analytics_storage: 'denied'
* }]
* ]
* ```
*
* @default []
*/
initCommands: GoogleTagOptions['initCommands']
/**
* Additional configuration for the Google tag ID, to be set during initialization of the tag ID with the `config' command.
*
* @remarks
* Only applies when `id` is set. For multiple tags, use the `tags` option instead.
*
* @default {}
*/
config?: GoogleTagOptions['config']
/**
* The Google tags to initialize.
*
* @remarks
* Each item can be a string or an object with `id` and `config` properties. The latter is useful especially when you want to set additional configuration for the Google tag ID.
*
* @default []
*/
tags?: string[] | GoogleTagOptions[]
/**
* Whether to load the Google tag ID script asynchronously or defer its loading.
*
* @remarks
* If set to `async`, the script will be loaded asynchronously.
* If set to `defer`, the script will be loaded with the `defer` attribute.
*
* @default 'defer'
*/
loadingStrategy?: 'async' | 'defer'
/**
* The URL to load the Google tag script from.
*
* @remarks
* Useful if you want to proxy the script through your own server.
*
* @default 'https://www.googletagmanager.com/gtag/js'
*/
url?: string
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name,
version,
configKey: 'gtag',
compatibility: {
nuxt: '^3.7',
},
},
defaults: {
enabled: true,
id: '',
initCommands: [],
config: {},
tags: [],
loadingStrategy: 'defer',
url: 'https://www.googletagmanager.com/gtag/js',
},
setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)
// Add module options to public runtime config
// @ts-expect-error: Loosely typed runtime config
nuxt.options.runtimeConfig.public.gtag = defu(
nuxt.options.runtimeConfig.public.gtag as Required<ModuleOptions>,
options,
)
// Transpile runtime
nuxt.options.build.transpile.push(resolve('runtime'))
addImports([
'useGtag',
'useTrackEvent',
].map(name => ({
name,
as: name,
from: resolve(`runtime/composables/${name}`),
})))
addPlugin({
src: resolve('runtime/plugin.client'),
mode: 'client',
})
},
})