UNPKG

auto-roi-calc-web-events-tracking

Version:

A wrapper for the AutoROICalc custom web events tracking JavaScript module.

1 lines 6.39 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.AutoRoiCalc=t():e.AutoRoiCalc=t()}(self,(()=>(()=>{"use strict";var e={};return(()=>{var t=e;const o=["google","bing","yahoo","duckduckgo","yandex","baidu","ecosia","startpage","ask","naver","aol","seznam"],i=["gclid","fbclid","yclid","yclid_v2","yclid_fingerprint","cpc_","sklik","etarget"];let r=new class{constructor(){this.trackingId="",this.config={debug:!1,maxSources:6,cookieNameSources:"arc_sources",storageNameTimeout:"arc_timeout",storageNameTimeoutSent:"arc_timeout_sent",storageNameSources:"arc_sources",timeout:60,ignoreUtmCampaign:!1,ignoreUtmSource:!1,useSourceMedium:!0},this.urlParams=new URLSearchParams(window.location.search)}debugLog(e){this.config.debug&&console.log(e)}setTrackingId(e){this.debugLog(`Setting tracking id: ${e}`),this.trackingId=e}setConfig(e,t){return this.debugLog(`Setting config value ${t} for key ${e}`),!(!(e in this.config)||("debug"==e&&"boolean"==typeof t?(this.config.debug=t,0):"maxSources"==e&&"number"==typeof t&&t>0&&t<7?(this.config.maxSources=t,0):"cookieNameSources"==e&&"string"==typeof t?(this.config.cookieNameSources=t,0):"storageNameTimeout"==e&&"string"==typeof t?(this.config.storageNameTimeout=t,0):"storageNameTimeoutSent"==e&&"string"==typeof t?(this.config.storageNameTimeoutSent=t,0):"storageNameSources"==e&&"string"==typeof t?(this.config.storageNameSources=t,0):"timeout"==e&&"number"==typeof t&&t>0?(this.config.timeout=t,0):"ignoreUtmCampaign"==e&&"boolean"==typeof t?(this.config.ignoreUtmCampaign=t,0):"ignoreUtmSource"==e&&"boolean"==typeof t?(this.config.ignoreUtmSource=t,0):"useSourceMedium"!=e||"boolean"!=typeof t||(this.config.useSourceMedium=t,0)))}getConfig(){return this.debugLog("Getting config."),this.config}getArcSource(){return this.debugLog(`Getting arc_source url parameter: ${this.urlParams.get("arc_source")}`),this.urlParams.get("arc_source")}getCampaign(){return this.config.ignoreUtmCampaign?(this.debugLog("Getting utm_campaign is set to be ignored."),null):(this.debugLog(`Getting utm_campaign url parameter: ${this.urlParams.get("utm_campaign")}`),this.urlParams.get("utm_campaign"))}getSource(){let e=this.urlParams.get("utm_source");return this.config.ignoreUtmSource||!e?(this.debugLog("Getting utm_source is set to be ignored or no utm_source value found."),this.getReferrer()):(this.debugLog(`Getting utm_source url parameter: ${e}`),e)}getMedium(){this.debugLog("Getting medium...");let e=this.urlParams.get("utm_medium");if(e)return this.debugLog(`Medium found in utm_medium url param: ${e}`),e;this.debugLog("Checking if is cpc...");let t=document.referrer;if(t)for(let e of o)if(t.includes(e))for(let e of i)if(t.includes(e))return this.debugLog("Is cpc..."),"cpc";this.debugLog("Checking if is organic...");for(let e of o)if(t.includes(e))return this.debugLog("Is organic..."),"organic";return this.debugLog("Checking if is (none)..."),"(direct)"==this.getReferrer()?(this.debugLog("Is (none)..."),"(none)"):(this.debugLog("Resolving as referral."),"referral")}getSourceMedium(){let e=this.getSource(),t=this.getMedium();return this.debugLog("Getting source / medium: "+e+" / "+t),e+" / "+t}getReferrer(){let e=document.referrer;if(e){let t=new URL(e);t.search="",t.pathname="";let i=t.toString();if(i.endsWith("/")&&(i=i.slice(0,-1)),i=i.replace(/^(https?:\/\/)?(www\.)?/,"").replace(/\/$/,""),this.debugLog(`Got referrer from document.referrer: ${i}`),window.location.origin.includes(i))return this.debugLog(`Location origin includes: ${i}. Falling back to (direct)`),"(direct)";for(let e of o)if(i.includes(e))return this.debugLog(`Is search engine: ${e}`),e;return i}return this.debugLog("document.referrer not set, giving: (direct)"),"(direct)"}getTheSource(){this.debugLog("Getting the AutoROICalc source...");let e=this.getArcSource();return e||(this.getCampaign()||(this.config.useSourceMedium?this.getSourceMedium():this.getSource()))}isTimeoutExpired(e){this.debugLog("Timeout check is set.");let t=parseInt(localStorage.getItem(e));return!(t&&(new Date).getTime()-t<=60*this.config.timeout*1e3&&(this.debugLog("Not expired, aborting."),1))}appendNewSource(e="",t=!0,o=""){let i=o||this.config.storageNameTimeout;if(this.debugLog("Appending new source..."),t&&!this.isTimeoutExpired(i))return!1;let r=this.getSources();if(e)this.debugLog(`Pushing custom source: ${e}`),r.includes(e)||r.push(e);else{let e=this.getTheSource();this.debugLog(`Pushing source determined by session: ${e}`),r.includes(e)||r.push(e)}return r.length>this.config.maxSources&&(this.debugLog("Removing residual sources..."),r=r.slice(-this.config.maxSources)),localStorage.setItem(this.config.storageNameSources,r.join("|")),localStorage.setItem(i,(new Date).getTime().toString()),!0}getSources(e=!0){this.debugLog("Getting previous sources from the local storage...");let t=[],o=localStorage.getItem(this.config.storageNameSources);return o&&(t=o.split("|")),e&&(t=[...new Set(t)]),t}send(e,t,o=1,i=!0,r="",n=""){let s=r||this.config.storageNameTimeoutSent;if(this.debugLog("Starting to send the event..."),!this.trackingId||!e||!t)return this.debugLog("Tracking ID, Record Type, or Description is not set. Aborting."),!1;if(i&&!this.isTimeoutExpired(s))return!1;let g="https://api.autoroicalc.com/record/collect_web_event";return g+="?trackingId="+encodeURIComponent(this.trackingId),g+="&type="+encodeURIComponent(e),g+="&desc="+encodeURIComponent(t),g+="&sources="+encodeURIComponent(n||this.getSources().join("|")),g+="&value="+encodeURIComponent(o),this.debugLog(`Sending the event to ${g}`),fetch(g,{mode:"no-cors"}).then((()=>{this.config.debug&&console.log("Event sent successfully."),localStorage.setItem(s,(new Date).getTime().toString())})).catch((e=>{this.config.debug&&console.error("Fetch error:",e)})),!0}setCookie(){document.cookie=this.config.cookieNameSources+"="+this.getSources().join("|")+"; path=/"}};t.appendNewSource=function(e="",t=!0,o=""){return r.appendNewSource(e,t,o)},t.getSources=function(e=!0){return r.getSources(e)},t.setTrackingId=function(e){r.setTrackingId(e)},t.setConfig=function(e,t){return r.setConfig(e,t)},t.getConfig=function(){return r.getConfig()},t.sendEvent=function(e,t,o=1,i=!0,n="",s=""){return r.send(e,t,o,i,n,s)},t.setSourcesCookie=function(){r.setCookie()}})(),e})()));