{"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\ninterface DubAnalyticsWindow extends Window {\n  [key: string]: any;\n}\n\n/**\n * Injects the Dub Web Analytics script into the page head.\n */\nfunction inject(props: AnalyticsProps): void {\n  if (!isBrowser()) return;\n\n  (function (w: DubAnalyticsWindow, da: string) {\n    w[da] =\n      w[da] ||\n      function () {\n        (w[da].q = w[da].q || []).push(arguments);\n      };\n\n    ['trackClick', 'trackLead', 'trackSale', 'trackIdentity'].forEach(\n      function (m) {\n        w[da][m] = function () {\n          (w[da].q = w[da].q || []).push([m, ...Array.from(arguments)]);\n        };\n      },\n    );\n  })(window as DubAnalyticsWindow, 'dubAnalytics');\n\n  // Determine script source based on enabled features\n  const baseUrl = 'https://www.dubcdn.com/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  if (props.trackIdentityPaths) features.push('identity');\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.queryParams) {\n    script.setAttribute('data-query-params', JSON.stringify(props.queryParams));\n  }\n\n  if (props.trackIdentityPaths) {\n    script.setAttribute(\n      'data-track-identity-paths',\n      JSON.stringify(props.trackIdentityPaths),\n    );\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(`[Dub 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\": \"@dub/analytics\",\n  \"version\": \"0.0.33\",\n  \"description\": \"\",\n  \"keywords\": [\n    \"analytics\",\n    \"dub\"\n  ],\n  \"repository\": {\n    \"url\": \"github:dubinc/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      \"@dub/eslint-config\"\n    ],\n    \"rules\": {\n      \"tsdoc/syntax\": \"off\"\n    }\n  },\n  \"dependencies\": {\n    \"server-only\": \"^0.0.1\"\n  },\n  \"devDependencies\": {\n    \"@dub/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() {\n  return typeof window !== 'undefined';\n}\n\nexport function isDubAnalyticsReady() {\n  return typeof window !== 'undefined' && window.dubAnalytics;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCE,WAAQ;AACR,cAAW;;;ACFN,SAAS,YAAY;AAC1B,SAAO,OAAO,WAAW;AAC3B;;;AFSA,SAAS,OAAO,OAA6B;AAX7C;AAYE,MAAI,CAAC,UAAU;AAAG;AAElB,GAAC,SAAU,GAAuB,IAAY;AAC5C,MAAE,EAAE,IACF,EAAE,EAAE,KACJ,WAAY;AACV,OAAC,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,KAAK,CAAC,GAAG,KAAK,SAAS;AAAA,IAC1C;AAEF,KAAC,cAAc,aAAa,aAAa,eAAe,EAAE;AAAA,MACxD,SAAU,GAAG;AACX,UAAE,EAAE,EAAE,CAAC,IAAI,WAAY;AACrB,WAAC,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,KAAK,CAAC,GAAG,KAAK,CAAC,GAAG,GAAG,MAAM,KAAK,SAAS,CAAC,CAAC;AAAA,QAC9D;AAAA,MACF;AAAA,IACF;AAAA,EACF,GAAG,QAA8B,cAAc;AAG/C,QAAM,UAAU;AAChB,QAAM,W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