1
- // WARNING: This file is automatically generated, do not manually modify.
2
1
import { withQuery } from 'ufo'
3
2
import type { DataLayer , GTag } from 'third-party-capital'
4
3
import { useRegistryScript } from '#nuxt-scripts/utils'
@@ -9,31 +8,49 @@ export const GoogleAnalyticsOptions = object({
9
8
id : string ( ) ,
10
9
l : optional ( string ( ) ) ,
11
10
} )
11
+
12
12
export type GoogleAnalyticsInput = RegistryScriptInput < typeof GoogleAnalyticsOptions >
13
13
14
- function use ( options : GoogleAnalyticsInput ) {
15
- const gtag : GTag = function ( ...args : Parameters < GTag > ) {
16
- ( ( window as any ) [ 'gtag-' + ( options . l ?? 'dataLayer' ) ] as GTag ) ( ...args )
17
- } as GTag
18
- return { dataLayer : ( window as any ) [ options . l ?? 'dataLayer' ] as DataLayer ,
19
- gtag }
14
+ export interface GoogleAnalyticsApi {
15
+ gtag : GTag
16
+ dataLayer : DataLayer
20
17
}
21
18
22
- export function useScriptGoogleAnalytics ( _options ?: GoogleAnalyticsInput ) {
23
- return useRegistryScript < ReturnType < typeof use > , typeof GoogleAnalyticsOptions > ( _options ?. key || 'googleAnalytics' , options => ( {
19
+ export function useScriptGoogleAnalytics < T extends GoogleAnalyticsApi > ( _options ?: GoogleAnalyticsInput ) {
20
+ return useRegistryScript < T , typeof GoogleAnalyticsOptions > ( _options ?. key || 'googleAnalytics' , options => ( {
24
21
scriptInput : {
25
22
src : withQuery ( 'https://www.googletagmanager.com/gtag/js' , { id : options ?. id , l : options ?. l } ) ,
26
23
} ,
27
24
schema : import . meta. dev ? GoogleAnalyticsOptions : undefined ,
28
25
scriptOptions : {
29
- use : ( ) => use ( options ) ,
26
+ use : ( ) => {
27
+ const gtag : GTag = function ( ...args : Parameters < GTag > ) {
28
+ ( ( window as any ) [ 'gtag-' + ( options . l ?? 'dataLayer' ) ] as GTag ) ( ...args )
29
+ } as GTag
30
+ return {
31
+ dataLayer : ( window as any ) [ options . l ?? 'dataLayer' ] as DataLayer ,
32
+ gtag,
33
+ }
34
+ } ,
30
35
stub : import . meta. client ? undefined : ( { fn } ) => { return fn === 'dataLayer' ? [ ] : void 0 } ,
31
36
performanceMarkFeature : 'nuxt-third-parties-ga' ,
32
- ... ( { tagPriority : 1 } ) ,
37
+ tagPriority : 1 ,
33
38
} ,
34
- // eslint-disable-next-line
35
- // @ts -ignore
36
- // eslint-disable-next-line
37
- clientInit : import . meta. server ? undefined : ( ) => { window [ ( options ?. l ?? "dataLayer" ) ] = window [ ( options ?. l ?? "dataLayer" ) ] || [ ] ; window [ 'gtag-' + ( options ?. l ?? "dataLayer" ) ] = function ( ) { window [ ( options ?. l ?? "dataLayer" ) ] . push ( arguments ) ; } ; window [ 'gtag-' + ( options ?. l ?? "dataLayer" ) ] ( 'js' , new Date ( ) ) ; window [ 'gtag-' + ( options ?. l ?? "dataLayer" ) ] ( 'config' , ( options ?. id ) ) } ,
39
+ clientInit : import . meta. server
40
+ ? undefined
41
+ : ( ) => {
42
+ const dataLayerName = options ?. l ?? 'dataLayer'
43
+ const dataLayer = ( window as any ) [ dataLayerName ] || [ ] ;
44
+
45
+ ( window as any ) [ dataLayerName ] = dataLayer
46
+ // eslint-disable-next-line
47
+ // @ts -ignore
48
+ window [ 'gtag-' + ( dataLayerName ) ] = function ( ) {
49
+ // eslint-disable-next-line
50
+ ( window as any ) [ dataLayerName ] . push ( arguments )
51
+ }
52
+ ; ( ( window as any ) [ 'gtag-' + ( dataLayerName ) ] as GTag ) ( 'js' , new Date ( ) )
53
+ ; ( ( window as any ) [ 'gtag-' + ( dataLayerName ) ] as GTag ) ( 'config' , ( options ?. id ) )
54
+ } ,
38
55
} ) , _options )
39
56
}
0 commit comments