UNPKG

@codeqr/analytics

Version:

`@codeqr/analytics` allows you to track leads and sales conversions for CodeQR.

1 lines 7.3 kB
{"version":3,"sources":["../src/generic.ts","../package.json","../src/utils.tsx"],"sourcesContent":["import { name, version } from '../package.json';\nimport type { AnalyticsProps } from './types';\nimport { isBrowser } from './utils';\n\n// Definimos uma interface para a função de analytics para garantir a tipagem correta.\ninterface AnalyticsFunction {\n (...args: unknown[]): void;\n q?: unknown[];\n trackClick: (...args: unknown[]) => void;\n trackLead: (...args: unknown[]) => void;\n trackSale: (...args: unknown[]) => void;\n}\n\n// O tipo de janela agora inclui a função de analytics.\ntype CodeQRAnalyticsWindow = Window & {\n codeqrAnalytics?: AnalyticsFunction;\n};\n\n/**\n * Injects the CodeQR Web Analytics script into the page head.\n */\nfunction inject(props: AnalyticsProps): void {\n if (!isBrowser()) return;\n\n const w = window as CodeQRAnalyticsWindow;\n const da = 'codeqrAnalytics';\n\n // Initialize analytics function\n const initFn = function analyticsFunction(...args: unknown[]): void {\n if (!initFn.q) {\n initFn.q = [];\n }\n initFn.q.push(args);\n } as AnalyticsFunction;\n\n // Define methods for the analytics function\n const methods: (keyof Pick<\n AnalyticsFunction,\n 'trackClick' | 'trackLead' | 'trackSale'\n >)[] = ['trackClick', 'trackLead', 'trackSale'];\n\n methods.forEach((method) => {\n initFn[method] = function methodFunction(...args: unknown[]): void {\n initFn(method, ...args);\n };\n });\n\n w[da] = initFn;\n\n // Determine script source based on enabled features\n const baseUrl = 'https://cdn.codeqr.io/analytics/script';\n const features = [];\n\n if (props.domainsConfig?.site) features.push('site-visit');\n if (props.domainsConfig?.outbound) features.push('outbound-domains');\n if (props.publishableKey) features.push('conversion-tracking');\n\n const src =\n props.scriptProps?.src ||\n (features.length > 0\n ? `${baseUrl}.${features.join('.')}.js`\n : `${baseUrl}.js`);\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = props.scriptProps?.defer ?? true;\n script.setAttribute('data-sdkn', name);\n script.setAttribute('data-sdkv', version);\n\n if (props.apiHost) {\n script.setAttribute('data-api-host', props.apiHost);\n }\n\n if (props.publishableKey) {\n script.setAttribute('data-publishable-key', props.publishableKey);\n }\n\n if (props.domainsConfig) {\n script.setAttribute('data-domains', JSON.stringify(props.domainsConfig));\n }\n\n if (props.shortDomain) {\n script.setAttribute('data-short-domain', props.shortDomain);\n }\n\n if (props.attributionModel) {\n script.setAttribute('data-attribution-model', props.attributionModel);\n }\n\n if (props.cookieOptions && Object.keys(props.cookieOptions).length > 0) {\n script.setAttribute(\n 'data-cookie-options',\n JSON.stringify(props.cookieOptions),\n );\n }\n\n if (props.queryParam) {\n script.setAttribute('data-query-param', props.queryParam);\n }\n\n if (props.scriptProps) {\n const { src: _, ...restProps } = props.scriptProps; // we already set the src above\n Object.assign(script, restProps);\n }\n\n script.onerror = (): void => {\n // eslint-disable-next-line no-console -- Logging to console is intentional\n console.log(`[CodeQR Web Analytics] failed to load script from ${src}.`);\n };\n\n document.head.appendChild(script);\n}\n\nexport { inject };\nexport type { AnalyticsProps };\n\n// eslint-disable-next-line import/no-default-export -- Default export is intentional\nexport default {\n inject,\n};\n","{\n \"name\": \"@codeqr/analytics\",\n \"version\": \"0.1.1\",\n \"description\": \"\",\n \"keywords\": [\n \"analytics\",\n \"codeqr\"\n ],\n \"repository\": {\n \"url\": \"github:codeqr-io/analytics\",\n \"directory\": \"packages/web\"\n },\n \"license\": \"MPL-2.0\",\n \"type\": \"module\",\n \"exports\": {\n \"./package.json\": \"./package.json\",\n \".\": {\n \"browser\": \"./dist/index.js\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./react\": {\n \"browser\": \"./dist/react/index.js\",\n \"import\": \"./dist/react/index.js\",\n \"require\": \"./dist/react/index.cjs\"\n }\n },\n \"main\": \"dist/index.js\",\n \"types\": \"dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"*\": [\n \"dist/index.d.ts\"\n ],\n \"react\": [\n \"dist/react/index.d.ts\"\n ]\n }\n },\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"tsup --watch\",\n \"lint\": \"eslint .\",\n \"lint-fix\": \"eslint . --fix\",\n \"type-check\": \"tsc --noEmit\"\n },\n \"eslintConfig\": {\n \"extends\": [\n \"@codeqr/eslint-config\"\n ],\n \"rules\": {\n \"tsdoc/syntax\": \"off\"\n }\n },\n \"dependencies\": {\n \"server-only\": \"^0.0.1\"\n },\n \"devDependencies\": {\n \"@codeqr/eslint-config\": \"workspace:0.0.0\",\n \"@swc/core\": \"^1.3.66\",\n \"@types/node\": \"^20.3.1\",\n \"@types/react\": \"^18.2.14\",\n \"react\": \"^18.2.0\",\n \"react-dom\": \"^18.2.0\",\n \"tsup\": \"7.1.0\"\n }\n}","export function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nexport function isCodeQRAnalyticsReady(): boolean {\n return typeof window !== 'undefined' && Boolean(window.CodeQRAnalytics);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCE,WAAQ;AACR,cAAW;;;ACFN,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;;;AFmBA,SAAS,OAAO,OAA6B;AArB7C;AAsBE,MAAI,CAAC,UAAU;AAAG;AAElB,QAAM,IAAI;AACV,QAAM,KAAK;AAGX,QAAM,SAAS,SAAS,qBAAqB,MAAuB;AAClE,QAAI,CAAC,OAAO,GAAG;AACb,aAAO,IAAI,CAAC;AAAA,IACd;AACA,WAAO,EAAE,KAAK,IAAI;AAAA,EACpB;AAGA,QAAM,UAGC,CAAC,cAAc,aAAa,WAAW;AAE9C,UAAQ,QAAQ,CAAC,WAAW;AAC1B,WAAO,MAAM,IAAI,SAAS,kBAAkB,MAAuB;AACjE,aAAO,QAAQ,GAAG,IAAI;AAAA,IACxB;AAAA,EACF,CAAC;AAED,IAAE,EAAE,IAAI;AAGR,QAAM,UAAU;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