|
| 1 | +import { GooglaAnalyticsData, GoogleAnalytics, GoogleTagManager, GoogleTagManagerData, type Output } from 'third-party-capital' |
| 2 | +import { addImports, addTemplate, useNuxt } from '@nuxt/kit' |
| 3 | +import type { RegistryScript } from '../runtime/types' |
| 4 | +import { extendTypes } from '../kit' |
| 5 | +import { generateTpcContent, generateTpcTypes, type ScriptContentOpts } from './utils' |
| 6 | + |
| 7 | +interface TpcDescriptor { |
| 8 | + input: ScriptContentOpts |
| 9 | + filename: string |
| 10 | + registry: RegistryScript |
| 11 | +} |
| 12 | + |
| 13 | +const scripts: TpcDescriptor[] = [{ |
| 14 | + input: { |
| 15 | + data: GoogleTagManagerData as Output, |
| 16 | + scriptFunctionName: 'useScriptGoogleTagManager', |
| 17 | + use: () => { |
| 18 | + return { dataLayer: window.dataLayer, google_tag_manager: window.google_tag_manager } |
| 19 | + }, |
| 20 | + stub: ({ fn }) => { |
| 21 | + return fn === 'dataLayer' ? [] : undefined |
| 22 | + }, |
| 23 | + tpcKey: 'gtm', |
| 24 | + tpcTypeImport: 'GoogleTagManagerApi', |
| 25 | + featureDetectionName: 'nuxt-third-parties-gtm', |
| 26 | + }, |
| 27 | + filename: 'nuxt-scripts-gtm', |
| 28 | + registry: { |
| 29 | + label: 'Google Tag Manager', |
| 30 | + category: 'tracking', |
| 31 | + logo: `<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 256 256"><path fill="#8AB4F8" d="m150.262 245.516l-44.437-43.331l95.433-97.454l46.007 45.091z"/><path fill="#4285F4" d="M150.45 53.938L106.176 8.731L9.36 104.629c-12.48 12.48-12.48 32.713 0 45.207l95.36 95.986l45.09-42.182l-72.654-76.407z"/><path fill="#8AB4F8" d="m246.625 105.37l-96-96c-12.494-12.494-32.756-12.494-45.25 0c-12.495 12.495-12.495 32.757 0 45.252l96 96c12.494 12.494 32.756 12.494 45.25 0c12.495-12.495 12.495-32.757 0-45.251"/><circle cx="127.265" cy="224.731" r="31.273" fill="#246FDB"/></svg>`, |
| 32 | + scriptBundling(options) { |
| 33 | + const data = GoogleTagManager(options) |
| 34 | + const mainScript = data.scripts?.find(({ key }) => key === 'gtag') |
| 35 | + |
| 36 | + if (mainScript && 'url' in mainScript && mainScript.url) |
| 37 | + return mainScript.url |
| 38 | + |
| 39 | + return false |
| 40 | + }, |
| 41 | + }, |
| 42 | +}, { |
| 43 | + input: { |
| 44 | + data: GooglaAnalyticsData as Output, |
| 45 | + scriptFunctionName: 'useScriptGoogleAnalytics', |
| 46 | + use: () => { |
| 47 | + return { dataLayer: window.dataLayer, gtag: window.gtag } |
| 48 | + }, |
| 49 | + // allow dataLayer to be accessed on the server |
| 50 | + stub: ({ fn }) => { |
| 51 | + return fn === 'dataLayer' ? [] : undefined |
| 52 | + }, |
| 53 | + tpcKey: 'gtag', |
| 54 | + tpcTypeImport: 'GoogleAnalyticsApi', |
| 55 | + featureDetectionName: 'nuxt-third-parties-ga', |
| 56 | + }, |
| 57 | + filename: 'nuxt-scripts-ga', |
| 58 | + registry: { |
| 59 | + label: 'Google Analytics', |
| 60 | + category: 'tracking', |
| 61 | + logo: `<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 256 256"><path fill="#8AB4F8" d="m150.262 245.516l-44.437-43.331l95.433-97.454l46.007 45.091z"/><path fill="#4285F4" d="M150.45 53.938L106.176 8.731L9.36 104.629c-12.48 12.48-12.48 32.713 0 45.207l95.36 95.986l45.09-42.182l-72.654-76.407z"/><path fill="#8AB4F8" d="m246.625 105.37l-96-96c-12.494-12.494-32.756-12.494-45.25 0c-12.495 12.495-12.495 32.757 0 45.252l96 96c12.494 12.494 32.756 12.494 45.25 0c12.495-12.495 12.495-32.757 0-45.251"/><circle cx="127.265" cy="224.731" r="31.273" fill="#246FDB"/></svg>`, |
| 62 | + scriptBundling(options) { |
| 63 | + const data = GoogleAnalytics(options) |
| 64 | + const mainScript = data.scripts?.find(({ key }) => key === 'gtag') |
| 65 | + |
| 66 | + if (mainScript && 'url' in mainScript && mainScript.url) |
| 67 | + return mainScript.url |
| 68 | + |
| 69 | + return false |
| 70 | + }, |
| 71 | + }, |
| 72 | +}] |
| 73 | + |
| 74 | +export function addTpc(registry: RegistryScript[]) { |
| 75 | + const nuxt = useNuxt() |
| 76 | + const fileImport = new Map<string, string>() |
| 77 | + |
| 78 | + for (const script of scripts) { |
| 79 | + extendTypes(script.filename, async () => await generateTpcTypes(script.input)) |
| 80 | + |
| 81 | + const { dst } = addTemplate({ |
| 82 | + getContents() { |
| 83 | + return generateTpcContent(script.input) |
| 84 | + }, |
| 85 | + filename: `modules/${script.filename}.ts`, |
| 86 | + }) |
| 87 | + |
| 88 | + addImports({ |
| 89 | + from: dst, |
| 90 | + name: 'useScriptGoogleAnalytics', |
| 91 | + }) |
| 92 | + |
| 93 | + script.registry.import = { |
| 94 | + from: dst, |
| 95 | + name: script.input.scriptFunctionName, |
| 96 | + } |
| 97 | + |
| 98 | + fileImport.set(script.filename, script.input.scriptFunctionName) |
| 99 | + |
| 100 | + registry.push(script.registry) |
| 101 | + } |
| 102 | + |
| 103 | + nuxt.hook('prepare:types', async ({ declarations }) => { |
| 104 | + declarations.push(` |
| 105 | +${Array.from(fileImport).map(([filename, fn]) => `import { ${fn} } from '#build/modules/${filename}'`).join('\n')} |
| 106 | +declare module '#imports' { |
| 107 | +${Array.from(fileImport).map(([_, fn]) => ` export { ${fn} }`).join('\n')} |
| 108 | +} |
| 109 | + `) |
| 110 | + }) |
| 111 | +} |
0 commit comments