UNPKG

@codeqr/analytics

Version:

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

113 lines (109 loc) 3.67 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name2 in all) __defProp(target, name2, { get: all[name2], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/generic.ts var generic_exports = {}; __export(generic_exports, { default: () => generic_default, inject: () => inject }); module.exports = __toCommonJS(generic_exports); // package.json var name = "@codeqr/analytics"; var version = "0.1.1"; // src/utils.tsx function isBrowser() { return typeof window !== "undefined"; } // src/generic.ts function inject(props) { var _a, _b, _c, _d; if (!isBrowser()) return; const w = window; const da = "codeqrAnalytics"; const initFn = function analyticsFunction(...args) { if (!initFn.q) { initFn.q = []; } initFn.q.push(args); }; const methods = ["trackClick", "trackLead", "trackSale"]; methods.forEach((method) => { initFn[method] = function methodFunction(...args) { initFn(method, ...args); }; }); w[da] = initFn; const baseUrl = "https://cdn.codeqr.io/analytics/script"; const features = []; if ((_a = props.domainsConfig) == null ? void 0 : _a.site) features.push("site-visit"); if ((_b = props.domainsConfig) == null ? void 0 : _b.outbound) features.push("outbound-domains"); if (props.publishableKey) features.push("conversion-tracking"); const src = ((_c = props.scriptProps) == null ? void 0 : _c.src) || (features.length > 0 ? `${baseUrl}.${features.join(".")}.js` : `${baseUrl}.js`); if (document.head.querySelector(`script[src*="${src}"]`)) return; const script = document.createElement("script"); script.src = src; script.defer = ((_d = props.scriptProps) == null ? void 0 : _d.defer) ?? true; script.setAttribute("data-sdkn", name); script.setAttribute("data-sdkv", version); if (props.apiHost) { script.setAttribute("data-api-host", props.apiHost); } if (props.publishableKey) { script.setAttribute("data-publishable-key", props.publishableKey); } if (props.domainsConfig) { script.setAttribute("data-domains", JSON.stringify(props.domainsConfig)); } if (props.shortDomain) { script.setAttribute("data-short-domain", props.shortDomain); } if (props.attributionModel) { script.setAttribute("data-attribution-model", props.attributionModel); } if (props.cookieOptions && Object.keys(props.cookieOptions).length > 0) { script.setAttribute( "data-cookie-options", JSON.stringify(props.cookieOptions) ); } if (props.queryParam) { script.setAttribute("data-query-param", props.queryParam); } if (props.scriptProps) { const { src: _, ...restProps } = props.scriptProps; Object.assign(script, restProps); } script.onerror = () => { console.log(`[CodeQR Web Analytics] failed to load script from ${src}.`); }; document.head.appendChild(script); } var generic_default = { inject }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { inject }); //# sourceMappingURL=index.cjs.map