diff --git a/package.json b/package.json index e27f2c8b..990d269b 100644 --- a/package.json +++ b/package.json @@ -88,7 +88,7 @@ "shiki": "1.24.0", "sirv": "^3.0.0", "std-env": "^3.8.0", - "third-party-capital": "2.3.0", + "third-party-capital": "3.0.0", "ufo": "^1.5.4", "unimport": "^3.14.4", "unplugin": "^2.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 059de0cf..99a45dde 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -90,8 +90,8 @@ importers: specifier: ^3.8.0 version: 3.8.0 third-party-capital: - specifier: 2.3.0 - version: 2.3.0 + specifier: 3.0.0 + version: 3.0.0 ufo: specifier: ^1.5.4 version: 1.5.4 @@ -5911,8 +5911,8 @@ packages: thenify@3.3.1: resolution: {integrity: sha512-RVZSIV5IG10Hk3enotrhvz0T9em6cyHBLkH/YAZuKqd8hRkKhSfCGIcP2KUY0EPxndzANBmNllzWPwak+bheSw==} - third-party-capital@2.3.0: - resolution: {integrity: sha512-p4rGOF4JCkI18HH3a1Vfd89Mg37TLlAsGmePBt20MA52frviYSBE6ToGOmTpqRC4EIlS5/4Owv6TpegFJUSkOg==} + third-party-capital@3.0.0: + resolution: {integrity: sha512-zCrYkb4lnFTpsz0cyZ7c3q9aqQ5zX4po1Gy4utqOf8UkIV9Jx45P8ty6kpF1gLVvfkfNB36nEKv3isQhJv7Ftw==} tiny-inflate@1.0.3: resolution: {integrity: sha512-pkY1fj1cKHb2seWDy0B16HeWyczlJA9/WW3u3c4z/NiWDsO3DOU5D7nhTLE9CF0yXv/QZFY7sEJmj24dK+Rrqw==} @@ -15006,7 +15006,7 @@ snapshots: dependencies: any-promise: 1.3.0 - third-party-capital@2.3.0: + third-party-capital@3.0.0: dependencies: semver: 7.6.3 diff --git a/scripts/utils.ts b/scripts/utils.ts index 9be534f0..b3bdb4cd 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -35,7 +35,7 @@ export async function generateTpcContent(input: TpcDescriptor) { chunks.push(` declare global { - type Window = ${input.tpcTypeAugmentation} + interface Window extends ${input.tpcTypeAugmentation} {} }`) } @@ -58,12 +58,12 @@ export async function generateTpcContent(input: TpcDescriptor) { const optionalParams = [...new Set(input.tpcData.scripts?.map(s => Object.keys(s.optionalParams) || []).flat() || [])] if (params.length || optionalParams.length) { - const validatorImports = new Set(['object', 'string']) + const validatorImports = new Set(['object', 'any']) if (optionalParams.length) { validatorImports.add('optional') } - const properties = params.filter(p => !optionalParams.includes(p)).map(p => `${p}: string()`).concat(optionalParams.map(o => `${o}: optional(string())`)) + const properties = params.filter(p => !optionalParams.includes(p)).map(p => `${p}: any()`).concat(optionalParams.map(o => `${o}: optional(any())`)) // need schema validation from tpc chunks.push(`export const ${titleKey}Options = object({ ${properties.join(',\n')} @@ -131,7 +131,13 @@ ${functionBody.join('\n')} } function replaceTokenToRuntime(code: string, defaultValues?: Record) { - return code.split(';').map(c => c.replaceAll(/'?\{\{(.*?)\}\}'?/g, (_, token) => { + // replace all {{#token}}...{{/token}} with if(options?.token){...} + return code.replace( + /\{\{#([^{}]+)\}\}(.*)\{\{\/\1\}\}/g, + (_match, name, innerCode) => { + return `if(options?.${name}){${replaceTokenToRuntime(innerCode, defaultValues)}};` + }, + ).split(';').map(c => c.replaceAll(/'?\{\{(.*?)\}\}'?/g, (_, token) => { return `(options?.${token} ${defaultValues?.[token] ? `?? ${JSON.stringify(defaultValues?.[token])}` : ''})` })).join(';') } diff --git a/src/runtime/registry/google-analytics.ts b/src/runtime/registry/google-analytics.ts index 6b95821b..5057be93 100644 --- a/src/runtime/registry/google-analytics.ts +++ b/src/runtime/registry/google-analytics.ts @@ -3,11 +3,13 @@ import { withQuery } from 'ufo' import type { DataLayer, GTag } from 'third-party-capital' import { useRegistryScript } from '#nuxt-scripts/utils' import type { RegistryScriptInput } from '#nuxt-scripts/types' -import { object, string, optional } from '#nuxt-scripts-validator' +import { object, any, optional } from '#nuxt-scripts-validator' export const GoogleAnalyticsOptions = object({ - id: string(), - l: optional(string()), + id: any(), + l: optional(any()), + consentType: optional(any()), + consentValues: optional(any()), }) export type GoogleAnalyticsInput = RegistryScriptInput @@ -34,6 +36,6 @@ export function useScriptGoogleAnalytics(_options?: GoogleAnalyticsInput) { // eslint-disable-next-line // @ts-ignore // eslint-disable-next-line - 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 ))}, + 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);};if(options?.consentValues){window['gtag-'+(options?.l ?? "dataLayer")]('consent', (options?.consentType ?? "default"), (options?.consentValues ));};window['gtag-'+(options?.l ?? "dataLayer")]('js',new Date());window['gtag-'+(options?.l ?? "dataLayer")]('config',(options?.id ))}, }), _options) } diff --git a/src/runtime/registry/google-tag-manager.ts b/src/runtime/registry/google-tag-manager.ts index 41322b62..945430f0 100644 --- a/src/runtime/registry/google-tag-manager.ts +++ b/src/runtime/registry/google-tag-manager.ts @@ -3,14 +3,16 @@ import { withQuery } from 'ufo' import type { GoogleTagManagerApi, DataLayer } from 'third-party-capital' import { useRegistryScript } from '#nuxt-scripts/utils' import type { RegistryScriptInput } from '#nuxt-scripts/types' -import { object, string, optional } from '#nuxt-scripts-validator' +import { object, any, optional } from '#nuxt-scripts-validator' declare global { interface Window extends GoogleTagManagerApi {} } export const GoogleTagManagerOptions = object({ - id: string(), - l: optional(string()), + id: any(), + l: optional(any()), + consentType: optional(any()), + consentValues: optional(any()), }) export type GoogleTagManagerInput = RegistryScriptInput @@ -33,6 +35,6 @@ export function useScriptGoogleTagManager(_options?: GoogleTagManagerInput) { // eslint-disable-next-line // @ts-ignore // eslint-disable-next-line - clientInit: import.meta.server ? undefined : () => {window[(options?.l ?? "dataLayer")]=window[(options?.l ?? "dataLayer")]||[];window[(options?.l ?? "dataLayer")].push({'gtm.start':new Date().getTime(),event:'gtm.js'});}, + clientInit: import.meta.server ? undefined : () => {window[(options?.l ?? "dataLayer")]=window[(options?.l ?? "dataLayer")]||[];if(options?.consentValues){(function () {window[(options?.l ?? "dataLayer")].push(arguments)})('consent', (options?.consentType ?? "default"), (options?.consentValues ));};window[(options?.l ?? "dataLayer")].push({'gtm.start':new Date().getTime(),event:'gtm.js'});}, }), _options) }