overcentric
Version:
Overcentric watches your website, product, and users - and tells you what matters and what to do about it.
1 lines • 12.1 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.overcentric=t():e.overcentric=t()}(this,(()=>(()=>{"use strict";var e,t,i={},o={};function r(e){var t=o[e];if(void 0!==t)return t.exports;var n=o[e]={exports:{}};return i[e](n,n.exports,r),n.exports}r.m=i,r.d=(e,t)=>{for(var i in t)r.o(t,i)&&!r.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},r.f={},r.e=e=>Promise.all(Object.keys(r.f).reduce(((t,i)=>(r.f[i](e,t),t)),[])),r.u=e=>"overcentric."+({399:"errorCapture",462:"dock",956:"autoCapture"}[e]||e)+"."+{399:"59bd4aca6b75cb46dc03",462:"48948a2c49153494fa13",617:"8db7c070041ae6617687",775:"b8c891ff5376dd59938a",956:"f67befa1c8071a073815"}[e]+".js",r.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),e={},t="overcentric:",r.l=(i,o,n,a)=>{if(e[i])e[i].push(o);else{var c,d;if(void 0!==n)for(var s=document.getElementsByTagName("script"),u=0;u<s.length;u++){var l=s[u];if(l.getAttribute("src")==i||l.getAttribute("data-webpack")==t+n){c=l;break}}c||(d=!0,(c=document.createElement("script")).charset="utf-8",c.timeout=120,r.nc&&c.setAttribute("nonce",r.nc),c.setAttribute("data-webpack",t+n),c.src=i),e[i]=[o];var p=(t,o)=>{c.onerror=c.onload=null,clearTimeout(f);var r=e[i];if(delete e[i],c.parentNode&&c.parentNode.removeChild(c),r&&r.forEach((e=>e(o))),t)return t(o)},f=setTimeout(p.bind(null,void 0,{type:"timeout",target:c}),12e4);c.onerror=p.bind(null,c.onerror),c.onload=p.bind(null,c.onload),d&&document.head.appendChild(c)}},r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{var e;r.g.importScripts&&(e=r.g.location+"");var t=r.g.document;if(!e&&t&&(t.currentScript&&"SCRIPT"===t.currentScript.tagName.toUpperCase()&&(e=t.currentScript.src),!e)){var i=t.getElementsByTagName("script");if(i.length)for(var o=i.length-1;o>-1&&(!e||!/^http(s?):/.test(e));)e=i[o--].src}if(!e)throw new Error("Automatic publicPath is not supported in this browser");e=e.replace(/^blob:/,"").replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),r.p=e})(),(()=>{var e={792:0};r.f.j=(t,i)=>{var o=r.o(e,t)?e[t]:void 0;if(0!==o)if(o)i.push(o[2]);else{var n=new Promise(((i,r)=>o=e[t]=[i,r]));i.push(o[2]=n);var a=r.p+r.u(t),c=new Error;r.l(a,(i=>{if(r.o(e,t)&&(0!==(o=e[t])&&(e[t]=void 0),o)){var n=i&&("load"===i.type?"missing":i.type),a=i&&i.target&&i.target.src;c.message="Loading chunk "+t+" failed.\n("+n+": "+a+")",c.name="ChunkLoadError",c.type=n,c.request=a,o[1](c)}}),"chunk-"+t,t)}};var t=(t,i)=>{var o,n,[a,c,d]=i,s=0;if(a.some((t=>0!==e[t]))){for(o in c)r.o(c,o)&&(r.m[o]=c[o]);if(d)d(r)}for(t&&t(i);s<a.length;s++)n=a[s],r.o(e,n)&&e[n]&&e[n][0](),e[n]=0},i=this.webpackChunkovercentric=this.webpackChunkovercentric||[];i.forEach(t.bind(null,0)),i.push=t.bind(null,i.push.bind(i))})();var n={};r.d(n,{default:()=>E});const a={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};let c;const d=new Uint8Array(16);function s(){if(!c){if("undefined"==typeof crypto||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");c=crypto.getRandomValues.bind(crypto)}return c(d)}const u=[];for(let e=0;e<256;++e)u.push((e+256).toString(16).slice(1));function l(e,t=0){return(u[e[t+0]]+u[e[t+1]]+u[e[t+2]]+u[e[t+3]]+"-"+u[e[t+4]]+u[e[t+5]]+"-"+u[e[t+6]]+u[e[t+7]]+"-"+u[e[t+8]]+u[e[t+9]]+"-"+u[e[t+10]]+u[e[t+11]]+u[e[t+12]]+u[e[t+13]]+u[e[t+14]]+u[e[t+15]]).toLowerCase()}const p=function(e,t,i){if(a.randomUUID&&!t&&!e)return a.randomUUID();const o=(e=e||{}).random||(e.rng||s)();if(o[6]=15&o[6]|64,o[8]=63&o[8]|128,t){i=i||0;for(let e=0;e<16;++e)t[i+e]=o[e];return t}return l(o)},f=()=>"undefined"!=typeof window;function g(){return p()}const v="undefined"!=typeof window?window.location.hostname.replace(/^www\./,""):"";const m="overcentric_session",h=12e4;const w={autoCapture:{loading:!1,loaded:!1},dock:{loading:!1,loaded:!1},errorCapture:{loading:!1,loaded:!1}},b={basePath:"https://app.overcentric.com/api",debugMode:!1,isDockEnabled:!1,autoCapture:{captureClick:!0,captureScroll:!1,captureFormSubmit:!1,captureInputFocus:!1,captureHistoryChange:!0,captureVisibilityChange:!1},initDefaults:{basePath:"",debugMode:!1,enableDock:!1,enableAutoCapture:!0,enableErrorCapture:!1}},y=e=>{b.debugMode},S=(e,t)=>{console.error(`Overcentric: ${e}`,t)},C=e=>{console.warn(`Overcentric: ${e}`)};function I(){const e=Array.from(document.getElementsByTagName("script")).find((e=>e.hasAttribute("data-project-id")));if(e)try{!function(e){var t,i;const o=e.hasAttribute("data-debug-mode");if(!e.hasAttribute("data-project-id"))return void S("No project ID found in script tag 2");const r=null===(t=e.getAttribute("data-project-id"))||void 0===t?void 0:t.trim();if(!r)return void S("Empty project ID in script tag");if(window.overcentricInitialized)return void C("Already initialized, skipping duplicate initialization");if(!e.hasAttribute("data-context"))return void S("No context found in script tag");const n=null===(i=e.getAttribute("data-context"))||void 0===i?void 0:i.trim();if(!n)return void S("Empty context in script tag");y("Initializing from script tag"),k(r,{context:n,debugMode:o}),window.overcentricInitialized=!0}(e)}catch(e){S("Error during auto-initialization:",e)}}async function k(e,t){if("undefined"!=typeof window)if(t.context)if(e)try{window.overcentricProjectId=e,window.overcentricContext=t.context;const i=t.basePath||b.basePath;if(!i)return void S("Base path is required");const o=await async function(e,t){try{const i=`${t}/config/${e}`,o=await fetch(i);o.ok||S(`HTTP error fetching configuration: ${o.status}`);const r=(await o.json()).data;return{debugMode:r.debug_mode,enableAutoCapture:r.auto_capture,enableErrorCapture:r.error_capture,enableDock:r.dock}}catch(e){return S(`Failed to fetch configuration: ${e}. Using defaults.`),{}}}(e,i),n={...b.initDefaults,...o,...t};window.overcentricProjectId=e,window.overcentricInitialized=!0,b.basePath=n.basePath||b.basePath,b.debugMode=n.debugMode||b.debugMode,b.isDockEnabled=n.enableDock||b.isDockEnabled;const a=function(){var e,t;if(!f())return null;try{return(null===(t=null===(e=document.cookie.split("; ").find((e=>e.startsWith("overcentric_device_id="))))||void 0===e?void 0:e.split("="))||void 0===t?void 0:t[1])||null}catch(e){return console.warn("Failed to get device ID:",e),null}}();if(a)window.overcentricDeviceId=a;else{const e=g();!function(e){if(f())try{const t=window.location.hostname.split(".").slice(-2).join(".");document.cookie=`overcentric_device_id=${e}; domain=.${t}; path=/; max-age=31536000; SameSite=Strict; Secure`}catch(e){console.warn("Failed to store device ID:",e)}}(e),window.overcentricDeviceId=e}if(n.enableAutoCapture&&!w.autoCapture.loading){w.autoCapture.loading=!0;const e={...b.autoCapture,...n.autoCaptureConfig};r.e(956).then(r.bind(r,277)).then((async({initAutoCapture:t})=>{try{await t(D,e),w.autoCapture.loaded=!0,y("Auto-capture feature loaded and initialized")}catch(e){throw S("Failed to initialize auto-capture:",e),e}})).catch((e=>{S("Failed to load auto-capture:",e),w.autoCapture.loaded=!1})).finally((()=>{w.autoCapture.loading=!1}))}n.enableErrorCapture&&!w.errorCapture.loading&&(w.errorCapture.loading=!0,r.e(399).then(r.bind(r,188)).then((({initErrorCaptureFeature:e})=>{e(D),w.errorCapture.loaded=!0})).catch((e=>{S("Failed to load error capture:",e)})).finally((()=>{w.errorCapture.loading=!1}))),b.isDockEnabled&&!w.dock.loading&&(w.dock.loading=!0,r.e(462).then(r.bind(r,347)).then((({initDockFeature:t})=>{t(e),w.dock.loaded=!0})).catch((e=>{S("Failed to load dock:",e)})).finally((()=>{w.dock.loading=!1}))),document.addEventListener("visibilitychange",(()=>{const e=localStorage.getItem(m);if(e)try{const t=JSON.parse(e);if("hidden"===document.visibilityState)t.lastVisibilityChange=Date.now(),localStorage.setItem(m,JSON.stringify(t));else if(Date.now()-(t.lastVisibilityChange||0)>=h){const e={id:g(),lastActivity:Date.now()};localStorage.setItem(m,JSON.stringify(e))}else delete t.lastVisibilityChange,t.lastActivity=Date.now(),localStorage.setItem(m,JSON.stringify(t))}catch(e){console.error("Error handling visibility change:",e)}})),function(){const e="overcentric_initial_visit_sent";if(document.cookie.split("; ").find((t=>t.startsWith(e))))return;const t=function(){const e=["utm_source","utm_medium","utm_campaign","utm_term","utm_content"],t=new URLSearchParams(window.location.search),i={};return e.forEach((e=>{const o=t.get(e);o&&(i[e]=o)})),i}(),i={initial_referrer:document.referrer||null,initial_landing_page:window.location.href,...t,timestamp:(new Date).toISOString()},o=window.location.hostname.split(".").slice(-2).join(".");D("$initial_visit",i,(()=>{document.cookie=`${e}=true; domain=.${o}; path=/; max-age=31536000; SameSite=Strict; Secure`,y("Initial visit data sent")}))}()}catch(e){S("Overcentric: Failed to initialize:",e)}else S("Project ID is required");else S('Context must be specified as either "website" or "product"');else C("Not initializing in non-browser environment")}function D(e,t={},i){const o=window.overcentricProjectId;if(!o)return void S("Library not initialized with project id");const r=window.overcentricContext;if(!r)return void S("Library not initialized with context");const n=function(){const e=Date.now(),t=localStorage.getItem(m);if(t)try{const i=JSON.parse(t),o=e-i.lastActivity,r=i.lastVisibilityChange?e-i.lastVisibilityChange:0;if(o<18e5&&r<h)return i.lastActivity=e,localStorage.setItem(m,JSON.stringify(i)),i.id}catch(e){console.error("Error parsing stored session:",e)}const i={id:g(),lastActivity:e};return localStorage.setItem(m,JSON.stringify(i)),i.id}();!function(){const e=localStorage.getItem(m);if(e)try{const t=JSON.parse(e);t.lastActivity=Date.now(),localStorage.setItem(m,JSON.stringify(t))}catch(e){console.error("Error updating session activity:",e)}}();const a=window.overcentricDeviceId,c=window.overcentricUserIdentity,d={eventName:e,properties:t,deviceId:a,sessionId:n,context:r};(function(e,t,i,o){return new Promise(((r,n)=>{const a=new XMLHttpRequest;a.open("POST",`${t}/events`,!0),a.setRequestHeader("Content-Type","application/json"),a.onload=()=>{a.status>=200&&a.status<300?r():n(new Error(`HTTP Error: ${a.status} ${a.statusText}`))},a.onerror=()=>n(new Error("Network Error"));const c={event:{name:i.eventName,properties:i.properties,project_id:e,device_id:i.deviceId,session_id:i.sessionId,context:i.context,url:window.location.href,hostname:v,...document.referrer&&{referrer:document.referrer},screen_width:window.screen.width,screen_height:window.screen.height},...o&&o.uniqueIdentifier&&{identity:{project_id:e,unique_identifier:o.uniqueIdentifier,email:o.email,name:o.name}}};a.send(JSON.stringify(c))}))})(o,b.basePath,d,c).then((()=>{i&&i()})).catch((e=>{y(`Error sending event: ${e}`)}))}"undefined"!=typeof window&&void 0!==(null===document||void 0===document?void 0:document.readyState)&&("loading"===document.readyState?document.addEventListener("DOMContentLoaded",I):I());const E={test:function(){return"Overcentric: Test ran successfully"},init:k,identify:function(e,t={}){if(!window.overcentricInitialized)return void S("Not initialized");if(!e)return void S("User ID is required");const i={uniqueIdentifier:e,...t};window.overcentricUserIdentity=i,y(`Identified user: ${e}, info: ${JSON.stringify(t)}`),b.isDockEnabled&&(w.dock.loaded?r.e(462).then(r.bind(r,347)).then((({updateDockUser:t})=>t(e))).catch((e=>S("Failed to update dock user:",e))):C("Dock feature not loaded, skipping user update")),D("$identify",i)},trackEvent:D,setContext:function(e){window.overcentricContext=e},isInitialized:function(){return window.overcentricInitialized||!1}};return n=n.default})()));