-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathgoogle-analytics.ts
67 lines (60 loc) · 2.48 KB
/
google-analytics.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
import { withQuery } from 'ufo'
import { useRegistryScript } from '#nuxt-scripts/utils'
import type { RegistryScriptInput } from '#nuxt-scripts/types'
import { object, string, optional } from '#nuxt-scripts-validator'
type ConsentOptions = 'default' | 'update'
export interface GTag {
(fn: 'js', opt: Date): void
(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 | number>): void
}
type DataLayer = Array<Parameters<GTag> | Record<string, unknown>>
export const GoogleAnalyticsOptions = object({
id: string(),
l: optional(string()),
src: optional(string()),
})
export type GoogleAnalyticsInput = RegistryScriptInput<typeof GoogleAnalyticsOptions>
export interface GoogleAnalyticsApi {
gtag: GTag
dataLayer: DataLayer
}
export function useScriptGoogleAnalytics<T extends GoogleAnalyticsApi>(_options?: GoogleAnalyticsInput) {
return useRegistryScript<T, typeof GoogleAnalyticsOptions>(_options?.key || 'googleAnalytics', options => ({
scriptInput: {
src: withQuery(options?.src || 'https://www.googletagmanager.com/gtag/js', { id: options?.id, l: options?.l }),
},
schema: import.meta.dev ? GoogleAnalyticsOptions : undefined,
scriptOptions: {
use: () => {
const gtag: GTag = function (...args: Parameters<GTag>) {
((window as any)['gtag-' + (options.l ?? 'dataLayer')] as GTag)(...args)
} as GTag
return {
dataLayer: (window as any)[options.l ?? 'dataLayer'] as DataLayer,
gtag,
}
},
stub: import.meta.client ? undefined : ({ fn }) => { return fn === 'dataLayer' ? [] : void 0 },
performanceMarkFeature: 'nuxt-third-parties-ga',
tagPriority: 1,
},
clientInit: import.meta.server
? undefined
: () => {
const dataLayerName = options?.l ?? 'dataLayer'
const dataLayer = (window as any)[dataLayerName] || [];
(window as any)[dataLayerName] = dataLayer
// eslint-disable-next-line
// @ts-ignore
window['gtag-' + (dataLayerName)] = function () {
// eslint-disable-next-line
(window as any)[dataLayerName].push(arguments)
}
; ((window as any)['gtag-' + (dataLayerName)] as GTag)('js', new Date())
; ((window as any)['gtag-' + (dataLayerName)] as GTag)('config', (options?.id))
},
}), _options)
}