Skip to content

Commit

Permalink
fix(tpc): custom feature detection flags (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
huang-julien committed Jul 12, 2024
1 parent 62963b5 commit 4c35f73
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/runtime/composables/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useScript<T extends Record<string | symbol, any>>(input: UseScri
if (!nuxtApp._scripts?.[id]) {
performance?.mark?.('mark_feature_usage', {
detail: {
feature: `nuxt-scripts:${id}`,
feature: options.performanceMarkFeature ?? `nuxt-scripts:${id}`,
},
})
}
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export type NuxtUseScriptOptions<T = any> = Omit<UseScriptOptions<T>, 'trigger'>
* loading the actual script and not getting warnings.
*/
skipValidation?: boolean
/**
* @internal
*/
performanceMarkFeature?: string
}

export type NuxtUseScriptOptionsSerializable = Omit<NuxtUseScriptOptions, 'use' | 'skipValidation' | 'stub' | 'trigger' | 'eventContext' | 'beforeInit'> & { trigger?: 'client' | 'server' | 'onNuxtReady' }
Expand Down
1 change: 1 addition & 0 deletions src/tpc/google-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default function googleAnalitycsRegistry(scripts: RegistryScript[]) {
},
tpcKey: 'gtag',
tpcTypeImport: 'GoogleAnalyticsApi',
featureDetectionName: 'nuxt-third-parties-ga',
})
},
filename: 'nuxt-scripts/tpc/google-analytics.ts',
Expand Down
1 change: 1 addition & 0 deletions src/tpc/google-tag-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default function googleTagManagerRegistry(scripts: RegistryScript[]) {
},
tpcKey: 'gtm',
tpcTypeImport: 'GoogleTagManagerApi',
featureDetectionName: 'nuxt-third-parties-gtm',
})
},
filename: 'nuxt-scripts/tpc/google-tag-manager.ts',
Expand Down
2 changes: 2 additions & 0 deletions src/tpc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface ScriptContentOpts {
* This will be stringified. The function must be pure.
*/
stub: (params: { fn: string }) => any
featureDetectionName?: string
}

const HEAD_VAR = '__head'
Expand Down Expand Up @@ -100,6 +101,7 @@ ${functionBody.join('\n')}
scriptOptions: {
use: ${input.use.toString()},
stub: import.meta.client ? undefined : ${input.stub.toString()},
${input.featureDetectionName ? `performanceMarkFeature: ${JSON.stringify(input.featureDetectionName)},` : ''}
${mainScriptOptions ? `...(${JSON.stringify(mainScriptOptions)})` : ''}
},
${clientInitCode.length ? `clientInit: import.meta.server ? undefined : () => {${clientInitCode.join('\n')}},` : ''}
Expand Down

0 comments on commit 4c35f73

Please sign in to comment.