@nuxt/scripts 0.11.2 → 0.11.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,65 @@
1
1
  import type { RegistryScriptInput } from '#nuxt-scripts/types';
2
- type ConsentOptions = 'default' | 'update';
2
+ export type GtagCustomParams = Record<string, any>;
3
+ export type ConsentStatus = 'granted' | 'denied';
4
+ export interface ConsentOptions {
5
+ ad_user_data?: ConsentStatus;
6
+ ad_personalization?: ConsentStatus;
7
+ ad_storage?: ConsentStatus;
8
+ analytics_storage?: ConsentStatus;
9
+ functionality_storage?: ConsentStatus;
10
+ personalization_storage?: ConsentStatus;
11
+ security_storage?: ConsentStatus;
12
+ wait_for_update?: number;
13
+ region?: string[];
14
+ }
15
+ export interface ConfigParams extends GtagCustomParams {
16
+ send_page_view?: boolean;
17
+ transport_url?: string;
18
+ cookie_domain?: string;
19
+ cookie_prefix?: string;
20
+ cookie_expires?: number;
21
+ cookie_update?: boolean;
22
+ cookie_flags?: string;
23
+ user_id?: string;
24
+ }
25
+ export interface EventParameters extends GtagCustomParams {
26
+ value?: number;
27
+ currency?: string;
28
+ transaction_id?: string;
29
+ items?: Array<{
30
+ item_id?: string;
31
+ item_name?: string;
32
+ item_category?: string;
33
+ item_variant?: string;
34
+ price?: number;
35
+ quantity?: number;
36
+ [key: string]: any;
37
+ }>;
38
+ [key: string]: any;
39
+ }
40
+ export type DefaultEventName = 'add_payment_info' | 'add_shipping_info' | 'add_to_cart' | 'add_to_wishlist' | 'begin_checkout' | 'purchase' | 'refund' | 'remove_from_cart' | 'select_item' | 'select_promotion' | 'view_cart' | 'view_item' | 'view_item_list' | 'view_promotion' | 'login' | 'sign_up' | 'search' | 'page_view' | 'screen_view' | string;
3
41
  export interface GTag {
4
- (fn: 'js', opt: Date): void;
5
- (fn: 'config' | 'get', opt: string): void;
6
- (fn: 'event', opt: string, opt2?: Record<string, any>): void;
7
- (fn: 'set', opt: Record<string, string>): void;
8
- (fn: 'consent', opt: ConsentOptions, opt2: Record<string, string | number>): void;
42
+ (command: 'js', value: Date): void;
43
+ (command: 'config', targetId: string, configParams?: ConfigParams): void;
44
+ (command: 'get', targetId: string, fieldName: string, callback?: (field: any) => void): void;
45
+ (command: 'event', eventName: DefaultEventName, eventParams?: EventParameters): void;
46
+ (command: 'set', params: GtagCustomParams): void;
47
+ (command: 'consent', consentArg: 'default' | 'update', consentParams: ConsentOptions): void;
48
+ }
49
+ export interface DataLayerObject {
50
+ event?: string;
51
+ [key: string]: any;
52
+ }
53
+ export type DataLayer = Array<DataLayerObject>;
54
+ export interface GoogleAnalyticsApi {
55
+ gtag: GTag;
56
+ dataLayer: DataLayer;
9
57
  }
10
- type DataLayer = Array<Parameters<GTag> | Record<string, unknown>>;
11
58
  export declare const GoogleAnalyticsOptions: import("valibot").ObjectSchema<{
12
59
  readonly id: import("valibot").StringSchema<undefined>;
13
60
  readonly l: import("valibot").OptionalSchema<import("valibot").StringSchema<undefined>, undefined>;
14
61
  }, undefined>;
15
62
  export type GoogleAnalyticsInput = RegistryScriptInput<typeof GoogleAnalyticsOptions>;
16
- export interface GoogleAnalyticsApi {
17
- gtag: GTag;
18
- dataLayer: DataLayer;
19
- }
20
- export declare function useScriptGoogleAnalytics<T extends GoogleAnalyticsApi>(_options?: GoogleAnalyticsInput): import("#nuxt-scripts/types").UseScriptContext<T>;
21
- export {};
63
+ export declare function useScriptGoogleAnalytics<T extends GoogleAnalyticsApi>(_options?: GoogleAnalyticsInput & {
64
+ onBeforeGtagStart?: (gtag: GTag) => void;
65
+ }): import("#nuxt-scripts/types").UseScriptContext<T>;
@@ -3,36 +3,38 @@ import { useRegistryScript } from "#nuxt-scripts/utils";
3
3
  import { object, string, optional } from "#nuxt-scripts-validator";
4
4
  export const GoogleAnalyticsOptions = object({
5
5
  id: string(),
6
+ // The GA4 measurement ID (format: G-XXXXXXXX)
6
7
  l: optional(string())
8
+ // Optional global name for dataLayer (defaults to 'dataLayer')
7
9
  });
8
10
  export function useScriptGoogleAnalytics(_options) {
9
- return useRegistryScript(_options?.key || "googleAnalytics", (options) => ({
10
- scriptInput: {
11
- src: withQuery("https://www.googletagmanager.com/gtag/js", { id: options?.id, l: options?.l })
12
- },
13
- schema: import.meta.dev ? GoogleAnalyticsOptions : void 0,
14
- scriptOptions: {
15
- use: () => {
16
- const gtag = function(...args) {
17
- window["gtag-" + (options.l ?? "dataLayer")](...args);
18
- };
19
- return {
20
- dataLayer: window[options.l ?? "dataLayer"],
21
- gtag
22
- };
11
+ return useRegistryScript(_options?.key || "googleAnalytics", (options) => {
12
+ const dataLayerName = options?.l ?? "dataLayer";
13
+ const w = import.meta.client ? window : {};
14
+ return {
15
+ scriptInput: {
16
+ src: withQuery("https://www.googletagmanager.com/gtag/js", { id: options?.id, l: options?.l })
23
17
  },
24
- performanceMarkFeature: "nuxt-third-parties-ga",
25
- tagPriority: 1
26
- },
27
- clientInit: import.meta.server ? void 0 : () => {
28
- const dataLayerName = options?.l ?? "dataLayer";
29
- const dataLayer = window[dataLayerName] || [];
30
- window[dataLayerName] = dataLayer;
31
- window["gtag-" + dataLayerName] = function() {
32
- window[dataLayerName].push(arguments);
33
- };
34
- window["gtag-" + dataLayerName]("js", /* @__PURE__ */ new Date());
35
- window["gtag-" + dataLayerName]("config", options?.id);
36
- }
37
- }), _options);
18
+ schema: import.meta.dev ? GoogleAnalyticsOptions : void 0,
19
+ scriptOptions: {
20
+ use: () => {
21
+ return {
22
+ dataLayer: w[dataLayerName],
23
+ gtag: w.gtag
24
+ };
25
+ },
26
+ performanceMarkFeature: "nuxt-third-parties-ga",
27
+ tagPriority: 1
28
+ },
29
+ clientInit: import.meta.server ? void 0 : () => {
30
+ w[dataLayerName] = w[dataLayerName] || [];
31
+ w.gtag = function() {
32
+ w[dataLayerName].push(arguments);
33
+ };
34
+ _options?.onBeforeGtagStart?.(w.gtag);
35
+ w.gtag("js", /* @__PURE__ */ new Date());
36
+ w.gtag("config", options?.id);
37
+ }
38
+ };
39
+ }, _options);
38
40
  }
@@ -1,41 +1,103 @@
1
1
  import type { GTag } from './google-analytics.js';
2
- import type { RegistryScriptInput } from '#nuxt-scripts/types';
3
- type DataLayer = Array<Parameters<GTag> | Record<string, unknown>>;
4
- interface GoogleTagManagerDataLayerApi {
5
- name: 'dataLayer';
6
- set: (opt: {
7
- [key: string]: string;
8
- }) => void;
9
- get: (key: string) => void;
2
+ import type { NuxtUseScriptOptions, RegistryScriptInput, UseFunctionType, UseScriptContext } from '#nuxt-scripts/types';
3
+ /**
4
+ * Improved DataLayer type that better reflects GTM's capabilities
5
+ * Can contain either gtag event parameters or custom data objects
6
+ */
7
+ export type DataLayerItem = Parameters<GTag> | Record<string, unknown>;
8
+ export type DataLayer = Array<DataLayerItem>;
9
+ /**
10
+ * DataLayer push function type
11
+ */
12
+ export interface DataLayerPush {
13
+ (...args: Parameters<GTag>): void;
14
+ (obj: Record<string, unknown>): void;
15
+ }
16
+ /**
17
+ * Improved DataLayer API type with more precise methods
18
+ */
19
+ export interface GoogleTagManagerDataLayerApi {
20
+ name: string;
21
+ push: DataLayerPush;
22
+ set: (config: Record<string, unknown>) => void;
23
+ get: <T = unknown>(key: string) => T;
10
24
  reset: () => void;
25
+ listeners: Array<() => void>;
11
26
  }
12
- interface GoogleTagManagerDataLayerStatus {
27
+ /**
28
+ * DataLayer status information
29
+ */
30
+ export interface GoogleTagManagerDataLayerStatus {
13
31
  dataLayer: {
14
32
  gtmDom: boolean;
15
33
  gtmLoad: boolean;
16
34
  subscribers: number;
35
+ [key: string]: unknown;
17
36
  };
18
37
  }
19
- type GoogleTagManagerInstance = GoogleTagManagerDataLayerStatus & {
20
- [key: string]: {
21
- callback: () => void;
22
- dataLayer: GoogleTagManagerDataLayerApi;
23
- };
24
- };
25
- interface GoogleTagManagerApi {
38
+ /**
39
+ * Container instance type
40
+ */
41
+ export interface GoogleTagManagerContainer {
42
+ callback: () => void;
43
+ dataLayer: GoogleTagManagerDataLayerApi;
44
+ state: Record<string, unknown>;
45
+ }
46
+ /**
47
+ * Complete GTM instance object
48
+ */
49
+ export interface GoogleTagManagerInstance extends GoogleTagManagerDataLayerStatus {
50
+ [containerId: string]: GoogleTagManagerContainer | any;
51
+ }
52
+ /**
53
+ * Complete Google Tag Manager API accessible via window
54
+ */
55
+ export interface GoogleTagManagerApi {
26
56
  google_tag_manager: GoogleTagManagerInstance;
27
- dataLayer: DataLayer;
57
+ dataLayer: DataLayer & {
58
+ push: DataLayerPush;
59
+ };
28
60
  }
61
+ /**
62
+ * Enhanced window type with GTM
63
+ */
29
64
  declare global {
30
65
  interface Window extends GoogleTagManagerApi {
31
66
  }
32
67
  }
68
+ /**
69
+ * GTM configuration options with improved documentation
70
+ */
33
71
  export declare const GoogleTagManagerOptions: import("valibot").ObjectSchema<{
72
+ /** GTM container ID (format: GTM-XXXXXX) */
34
73
  readonly id: import("valibot").StringSchema<undefined>;
74
+ /** Optional dataLayer variable name */
35
75
  readonly l: import("valibot").OptionalSchema<import("valibot").StringSchema<undefined>, undefined>;
76
+ /** Authentication token for environment-specific container versions */
77
+ readonly auth: import("valibot").OptionalSchema<import("valibot").StringSchema<undefined>, undefined>;
78
+ /** Preview environment name */
79
+ readonly preview: import("valibot").OptionalSchema<import("valibot").StringSchema<undefined>, undefined>;
80
+ /** Forces GTM cookies to take precedence when true */
81
+ readonly cookiesWin: import("valibot").OptionalSchema<import("valibot").UnionSchema<[import("valibot").BooleanSchema<undefined>, import("valibot").LiteralSchema<"x", undefined>], undefined>, undefined>;
82
+ /** Enables debug mode when true */
83
+ readonly debug: import("valibot").OptionalSchema<import("valibot").UnionSchema<[import("valibot").BooleanSchema<undefined>, import("valibot").LiteralSchema<"x", undefined>], undefined>, undefined>;
84
+ /** No Personal Advertising - disables advertising features when true */
85
+ readonly npa: import("valibot").OptionalSchema<import("valibot").UnionSchema<[import("valibot").BooleanSchema<undefined>, import("valibot").LiteralSchema<"1", undefined>], undefined>, undefined>;
86
+ /** Custom dataLayer name (alternative to "l" property) */
87
+ readonly dataLayer: import("valibot").OptionalSchema<import("valibot").StringSchema<undefined>, undefined>;
88
+ /** Environment name for environment-specific container */
89
+ readonly envName: import("valibot").OptionalSchema<import("valibot").StringSchema<undefined>, undefined>;
90
+ /** Referrer policy for analytics requests */
91
+ readonly authReferrerPolicy: import("valibot").OptionalSchema<import("valibot").StringSchema<undefined>, undefined>;
36
92
  }, undefined>;
37
93
  export type GoogleTagManagerInput = RegistryScriptInput<typeof GoogleTagManagerOptions>;
38
- export declare function useScriptGoogleTagManager<T extends GoogleTagManagerApi>(_options?: GoogleTagManagerInput & {
39
- onBeforeGtmStart?: (gtag: GTag) => void;
40
- }): import("#nuxt-scripts/types").UseScriptContext<T>;
41
- export {};
94
+ /**
95
+ * Hook to use Google Tag Manager in Nuxt applications
96
+ */
97
+ export declare function useScriptGoogleTagManager<T extends GoogleTagManagerApi>(options?: GoogleTagManagerInput & {
98
+ /**
99
+ * Optional callback that runs before GTM starts
100
+ * Allows for custom initialization or configuration
101
+ */
102
+ onBeforeGtmStart?: (gtag: DataLayerPush) => void;
103
+ }): UseScriptContext<UseFunctionType<NuxtUseScriptOptions<T>, T>>;
@@ -1,31 +1,72 @@
1
1
  import { withQuery } from "ufo";
2
2
  import { useRegistryScript } from "#nuxt-scripts/utils";
3
- import { object, string, optional } from "#nuxt-scripts-validator";
3
+ import { object, string, optional, boolean, union, literal } from "#nuxt-scripts-validator";
4
4
  export const GoogleTagManagerOptions = object({
5
+ /** GTM container ID (format: GTM-XXXXXX) */
5
6
  id: string(),
6
- l: optional(string())
7
+ /** Optional dataLayer variable name */
8
+ l: optional(string()),
9
+ /** Authentication token for environment-specific container versions */
10
+ auth: optional(string()),
11
+ /** Preview environment name */
12
+ preview: optional(string()),
13
+ /** Forces GTM cookies to take precedence when true */
14
+ cookiesWin: optional(union([boolean(), literal("x")])),
15
+ /** Enables debug mode when true */
16
+ debug: optional(union([boolean(), literal("x")])),
17
+ /** No Personal Advertising - disables advertising features when true */
18
+ npa: optional(union([boolean(), literal("1")])),
19
+ /** Custom dataLayer name (alternative to "l" property) */
20
+ dataLayer: optional(string()),
21
+ /** Environment name for environment-specific container */
22
+ envName: optional(string()),
23
+ /** Referrer policy for analytics requests */
24
+ authReferrerPolicy: optional(string())
7
25
  });
8
- export function useScriptGoogleTagManager(_options) {
9
- return useRegistryScript(_options?.key || "googleTagManager", (options) => ({
10
- scriptInput: {
11
- src: withQuery("https://www.googletagmanager.com/gtm.js", { id: options?.id, l: options?.l })
26
+ export function useScriptGoogleTagManager(options) {
27
+ return useRegistryScript(
28
+ options?.key || "googleTagManager",
29
+ (opts) => {
30
+ const dataLayerName = opts?.l ?? opts?.dataLayer ?? "dataLayer";
31
+ return {
32
+ scriptInput: {
33
+ src: withQuery("https://www.googletagmanager.com/gtm.js", {
34
+ id: opts.id,
35
+ l: opts.l,
36
+ gtm_auth: opts.auth,
37
+ gtm_preview: opts.preview,
38
+ gtm_cookies_win: opts.cookiesWin ? "x" : void 0,
39
+ gtm_debug: opts.debug ? "x" : void 0,
40
+ gtm_npa: opts.npa ? "1" : void 0,
41
+ gtm_data_layer: opts.dataLayer,
42
+ gtm_env: opts.envName,
43
+ gtm_auth_referrer_policy: opts.authReferrerPolicy
44
+ })
45
+ },
46
+ schema: import.meta.dev ? GoogleTagManagerOptions : void 0,
47
+ scriptOptions: {
48
+ use: () => {
49
+ return {
50
+ dataLayer: window[dataLayerName],
51
+ google_tag_manager: window.google_tag_manager
52
+ };
53
+ },
54
+ performanceMarkFeature: "nuxt-third-parties-gtm",
55
+ tagPriority: 1
56
+ },
57
+ clientInit: import.meta.server ? void 0 : () => {
58
+ window[dataLayerName] = window[dataLayerName] || [];
59
+ function gtag(...args) {
60
+ window[dataLayerName].push(args);
61
+ }
62
+ options?.onBeforeGtmStart?.(gtag);
63
+ window[dataLayerName].push({
64
+ "gtm.start": (/* @__PURE__ */ new Date()).getTime(),
65
+ "event": "gtm.js"
66
+ });
67
+ }
68
+ };
12
69
  },
13
- schema: import.meta.dev ? GoogleTagManagerOptions : void 0,
14
- scriptOptions: {
15
- use: () => {
16
- return { dataLayer: window[options.l ?? "dataLayer"], google_tag_manager: window.google_tag_manager };
17
- },
18
- performanceMarkFeature: "nuxt-third-parties-gtm",
19
- tagPriority: 1
20
- },
21
- clientInit: import.meta.server ? void 0 : () => {
22
- const dataLayerName = options?.l ?? "dataLayer";
23
- window[dataLayerName] = window[options?.l ?? "dataLayer"] || [];
24
- function gtag() {
25
- window[dataLayerName].push(arguments);
26
- }
27
- _options?.onBeforeGtmStart?.(gtag);
28
- window[dataLayerName].push({ "gtm.start": (/* @__PURE__ */ new Date()).getTime(), "event": "gtm.js" });
29
- }
30
- }), _options);
70
+ options
71
+ );
31
72
  }
@@ -9,6 +9,6 @@ type OptionsFn<O> = (options: InferIfSchema<O>) => ({
9
9
  clientInit?: () => void;
10
10
  });
11
11
  export declare function scriptRuntimeConfig<T extends keyof ScriptRegistry>(key: T): ScriptRegistry[T];
12
- export declare function useRegistryScript<T extends Record<string | symbol, any>, O = EmptyOptionsSchema, U = {}>(registryKey: keyof ScriptRegistry | string, optionsFn: OptionsFn<O>, _userOptions?: RegistryScriptInput<O>): UseScriptContext<UseFunctionType<NuxtUseScriptOptions<T>, T>>;
12
+ export declare function useRegistryScript<T extends Record<string | symbol, any>, O = EmptyOptionsSchema>(registryKey: keyof ScriptRegistry | string, optionsFn: OptionsFn<O>, _userOptions?: RegistryScriptInput<O>): UseScriptContext<UseFunctionType<NuxtUseScriptOptions<T>, T>>;
13
13
  export declare function pick(obj: Record<string, any>, keys: string[]): Record<string, any>;
14
14
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nuxt/scripts",
3
3
  "type": "module",
4
- "version": "0.11.2",
4
+ "version": "0.11.4",
5
5
  "description": "Load third-party scripts with better performance, privacy and DX in Nuxt Apps.",
6
6
  "author": {
7
7
  "website": "https://harlanzw.com",
@@ -71,9 +71,9 @@
71
71
  }
72
72
  },
73
73
  "dependencies": {
74
- "@nuxt/kit": "^3.16.0",
74
+ "@nuxt/kit": "^3.16.1",
75
75
  "@vueuse/core": "^13.0.0",
76
- "consola": "^3.4.0",
76
+ "consola": "^3.4.2",
77
77
  "defu": "^6.1.4",
78
78
  "h3": "^1.15.1",
79
79
  "magic-string": "^0.30.17",
@@ -84,39 +84,40 @@
84
84
  "sirv": "^3.0.1",
85
85
  "std-env": "^3.8.1",
86
86
  "ufo": "^1.5.4",
87
- "unplugin": "^2.2.0",
87
+ "unplugin": "^2.2.2",
88
88
  "unstorage": "^1.15.0",
89
- "valibot": "^1.0.0-rc.4"
89
+ "valibot": "^1.0.0"
90
90
  },
91
91
  "devDependencies": {
92
- "@nuxt/devtools-kit": "^2.3.0",
93
- "@nuxt/devtools-ui-kit": "^2.3.0",
92
+ "@nuxt/devtools-kit": "^2.3.1",
93
+ "@nuxt/devtools-ui-kit": "^2.3.1",
94
94
  "@nuxt/eslint-config": "^1.2.0",
95
95
  "@nuxt/module-builder": "^0.8.4",
96
- "@nuxt/test-utils": "3.17.1",
96
+ "@nuxt/test-utils": "3.17.2",
97
97
  "@types/semver": "^7.5.8",
98
- "@typescript-eslint/typescript-estree": "^8.26.1",
98
+ "@typescript-eslint/typescript-estree": "^8.28.0",
99
99
  "acorn-loose": "^8.4.0",
100
100
  "bumpp": "^10.1.0",
101
101
  "changelogen": "^0.6.1",
102
- "eslint": "9.21.0",
102
+ "eslint": "9.23.0",
103
103
  "eslint-plugin-n": "^17.16.2",
104
104
  "happy-dom": "^17.4.4",
105
105
  "knitwork": "^1.2.0",
106
- "nuxt": "^3.16.0",
107
- "playwright-core": "^1.51.0",
106
+ "nuxt": "^3.16.1",
107
+ "playwright-core": "^1.51.1",
108
108
  "shiki": "2.5.0",
109
109
  "typescript": "5.8.2",
110
- "vitest": "^3.0.8",
110
+ "vitest": "^3.0.9",
111
111
  "vue": "^3.5.13",
112
112
  "vue-router": "^4.5.0",
113
113
  "vue-tsc": "^2.2.8",
114
- "@nuxt/scripts": "0.11.2"
114
+ "@nuxt/scripts": "0.11.4"
115
115
  },
116
116
  "resolutions": {
117
117
  "@nuxt/schema": "catalog:",
118
118
  "@nuxt/scripts": "workspace:*",
119
- "@unhead/vue": "2.0.0-rc.12",
119
+ "@unhead/vue": "2.0.1",
120
+ "unhead": "2.0.1",
120
121
  "nuxt": "catalog:",
121
122
  "typescript": "5.8.2",
122
123
  "vue": "^3.5.13",