@elbwalker/utils
Version:
Shared utils for walkerOS packages
1 lines • 7.9 kB
JavaScript
;var Utils=(()=>{var e=Object.defineProperty,t=Object.getOwnPropertyDescriptor,n=Object.getOwnPropertyNames,r=Object.prototype.hasOwnProperty,o={};function i(e){return e.language}function s(){return Intl.DateTimeFormat().resolvedOptions().timeZone}function a(e){return`${e.screen.width}x${e.screen.height}`}((t,n)=>{for(var r in n)e(t,r,{get:n[r],enumerable:!0})})(o,{elb:()=>c,getAttribute:()=>u,getHashWeb:()=>g,getLanguage:()=>i,getScreenSize:()=>a,getTimezone:()=>s,isVisible:()=>m,sendWeb:()=>b,sendWebAsBeacon:()=>v,sendWebAsFetch:()=>k,sendWebAsXhr:()=>O,sessionStart:()=>U,sessionStorage:()=>N,sessionWindow:()=>H,storageDelete:()=>I,storageRead:()=>W,storageWrite:()=>L});var c=function(){const e=window;(e.elbLayer=e.elbLayer||[]).push(arguments)};function u(e,t){return(e.getAttribute(t)||"").trim()}var d={Commands:{Action:"action",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",User:"user",Walker:"walker"},Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}};function l(e){return Array.isArray(e)}function f(e){return void 0!==e}async function g(e,t){return(await async function(e){const t=f(window)&&window.crypto?window.crypto:void 0;if(!t||!t.subtle||!TextEncoder)return;const n=(new TextEncoder).encode(e),r=await t.subtle.digest("SHA-256",n);return Array.from(new Uint8Array(r)).map((e=>e.toString(16).padStart(2,"0"))).join("")}(e)||"").slice(0,t)}function m(e){const t=getComputedStyle(e);if("none"===t.display)return!1;if("visible"!==t.visibility)return!1;if(t.opacity&&Number(t.opacity)<.1)return!1;let n;const r=window.innerHeight,o=e.getBoundingClientRect(),i=o.height,s=o.y,a=s+i,c={x:o.x+e.offsetWidth/2,y:o.y+e.offsetHeight/2};if(i<=r){if(e.offsetWidth+o.width===0||e.offsetHeight+o.height===0)return!1;if(c.x<0)return!1;if(c.x>(document.documentElement.clientWidth||window.innerWidth))return!1;if(c.y<0)return!1;if(c.y>(document.documentElement.clientHeight||window.innerHeight))return!1;n=document.elementFromPoint(c.x,c.y)}else{const e=r/2;if(s<0&&a<e)return!1;if(a>r&&s>e)return!1;n=document.elementFromPoint(c.x,r/2)}if(n)do{if(n===e)return!0}while(n=n.parentElement);return!1}var w={merge:!0,shallow:!0,extend:!0};function h(e,t={},n={}){n={...w,...n};const r=Object.entries(t).reduce(((t,[r,o])=>{const i=e[r];return n.merge&&Array.isArray(i)&&Array.isArray(o)?t[r]=o.reduce(((e,t)=>e.includes(t)?e:[...e,t]),[...i]):(n.extend||r in e)&&(t[r]=o),t}),{});return n.shallow?{...e,...r}:(Object.assign(e,r),e)}function S(e){return void 0===e?e:typeof e==typeof""?e:JSON.stringify(e)}function y(e={}){return h({"Content-Type":"application/json; charset=utf-8"},e)}function p(e,t,n){return function(...r){try{return e(...r)}catch(e){if(!t)return;return t(e)}finally{n?.()}}}function b(e,t,n={transport:"fetch"}){switch(n.transport||"fetch"){case"beacon":return v(e,t);case"xhr":return O(e,t,n);default:return k(e,t,n)}}async function k(e,t,n={}){const r=y(n.headers),o=S(t);return(i=async()=>{const t=await fetch(e,{method:n.method||"POST",headers:r,keepalive:!0,credentials:n.credentials||"same-origin",mode:n.noCors?"no-cors":"cors",body:o}),i=n.noCors?"":await t.text();return{ok:t.ok,data:i,error:t.ok?void 0:t.statusText}},s=e=>({ok:!1,error:e.message}),async function(...e){try{return await i(...e)}catch(e){if(!s)return;return await s(e)}finally{await(a?.())}})();var i,s,a}function v(e,t){const n=S(t),r=navigator.sendBeacon(e,n);return{ok:r,error:r?void 0:"Failed to send beacon"}}function O(e,t,n={}){const r=y(n.headers),o=n.method||"POST",i=S(t);return p((()=>{const t=new XMLHttpRequest;t.open(o,e,!1);for(const e in r)t.setRequestHeader(e,r[e]);t.send(i);const n=t.status>=200&&t.status<300;return{ok:n,data:p(JSON.parse,(()=>t.response))(t.response),error:n?void 0:`${t.status} ${t.statusText}`}}),(e=>({ok:!1,error:e.message})))()}function x(e=6){let t="";for(let n=36;t.length<e;)t+=(Math.random()*n|0).toString(n);return t}function U(e={}){const{cb:t,consent:n,instance:r,storage:o}=e,i=r?.push||c;if(!n)return j((o?N:H)(e),r,t);{const r=function(e,t){let n;const r=(r,o)=>{if(f(n)&&n===r?.group)return;n=r?.group;let i=()=>H(e);if(e.consent){(function(e,t={},n={}){const r={...t,...n},o={};let i=void 0===e;return Object.keys(r).forEach((t=>{r[t]&&(o[t]=!0,e&&e[t]&&(i=!0))})),!!i&&o})((l(e.consent)?e.consent:[e.consent]).reduce(((e,t)=>({...e,[t]:!0})),{}),o)&&(i=()=>N(e))}return j(i(),r,t)};return r}(e,t);i("walker on","consent",(l(n)?n:[n]).reduce(((e,t)=>({...e,[t]:r})),{}))}}function j(e,t,n){return!1===n?e:(n||(n=C),n(e,t,C))}var A,C=(e,t)=>{const n=t?.push||c,r={};return e.id&&(r.session=e.id),e.storage&&e.device&&(r.device=e.device),n("walker user",r),e.isStart&&n("session start",e),e};function I(e,t=d.Utils.Storage.Session){switch(t){case d.Utils.Storage.Cookie:L(e,"",0,t);break;case d.Utils.Storage.Local:window.localStorage.removeItem(e);break;case d.Utils.Storage.Session:window.sessionStorage.removeItem(e)}}function W(e,t=d.Utils.Storage.Session){function n(e){try{return JSON.parse(e||"")}catch(t){let n=1,r="";return e&&(n=0,r=e),{e:n,v:r}}}let r,o;switch(t){case d.Utils.Storage.Cookie:r=decodeURIComponent(document.cookie.split("; ").find((t=>t.startsWith(e+"=")))?.split("=")[1]||"");break;case d.Utils.Storage.Local:o=n(window.localStorage.getItem(e));break;case d.Utils.Storage.Session:o=n(window.sessionStorage.getItem(e))}return o&&(r=o.v,0!=o.e&&o.e<Date.now()&&(I(e,t),r="")),function(e){if("true"===e)return!0;if("false"===e)return!1;const t=Number(e);return e==t&&""!==e?t:String(e)}(r||"")}function L(e,t,n=30,r=d.Utils.Storage.Session,o){const i={e:Date.now()+6e4*n,v:String(t)},s=JSON.stringify(i);switch(r){case d.Utils.Storage.Cookie:{t="object"==typeof t?JSON.stringify(t):t;let r=`${e}=${encodeURIComponent(t)}; max-age=${60*n}; path=/; SameSite=Lax; secure`;o&&(r+="; domain="+o),document.cookie=r;break}case d.Utils.Storage.Local:window.localStorage.setItem(e,s);break;case d.Utils.Storage.Session:window.sessionStorage.setItem(e,s)}return W(e,r)}function N(e={}){const t=Date.now(),{length:n=30,deviceKey:r="elbDeviceId",deviceStorage:o="local",deviceAge:i=30,sessionKey:s="elbSessionId",sessionStorage:a="local",pulse:c=!1}=e,u=H(e);let d=!1;const l=p(((e,t,n)=>{let r=W(e,n);return r||(r=x(8),L(e,r,1440*t,n)),String(r)}))(r,i,o),f=p(((e,r)=>{const o=JSON.parse(String(W(e,r)));return c||(o.isNew=!1,u.marketing&&(Object.assign(o,u),d=!0),d||o.updated+6e4*n<t?(delete o.id,delete o.referrer,o.start=t,o.count++,o.runs=1,d=!0):o.runs++),o}),(()=>{d=!0}))(s,a)||{},g={id:x(12),start:t,isNew:!0,count:1,runs:1},m=Object.assign(g,u,f,{device:l},{isStart:d,storage:!0,updated:t},e.data);return L(s,JSON.stringify(m),2*n,a),m}function H(e={}){let t=e.isStart||!1;const n={isStart:t,storage:!1};if(!1===e.isStart)return n;if(!t){const[e]=performance.getEntriesByType("navigation");if("navigate"!==e.type)return n}const r=new URL(e.url||window.location.href),o=e.referrer||document.referrer,i=o&&new URL(o).hostname,s=function(e,t={}){const n="clickId",r={},o={utm_campaign:"campaign",utm_content:"content",utm_medium:"medium",utm_source:"source",utm_term:"term",dclid:n,fbclid:n,gclid:n,msclkid:n,ttclid:n,twclid:n,igshid:n,sclid:n};return Object.entries(h(o,t)).forEach((([t,o])=>{const i=e.searchParams.get(t);i&&(o===n&&(o=t,r[n]=t),r[o]=i)})),r}(r,e.parameters);if(Object.keys(s).length&&(s.marketing||(s.marketing=!0),t=!0),!t){const n=e.domains||[];n.push(r.hostname),t=!n.includes(i)}return t?Object.assign({isStart:t,storage:!1,start:Date.now(),id:x(12),referrer:i},s,e.data):n}return A=o,((o,i,s,a)=>{if(i&&"object"==typeof i||"function"==typeof i)for(let c of n(i))r.call(o,c)||c===s||e(o,c,{get:()=>i[c],enumerable:!(a=t(i,c))||a.enumerable});return o})(e({},"__esModule",{value:!0}),A)})();