UNPKG

@elbwalker/utils

Version:

Shared utils for walkerOS packages

1 lines 7.92 kB
"use strict";var e,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,o=Object.prototype.hasOwnProperty,i={};function s(e){return e.language}function a(){return Intl.DateTimeFormat().resolvedOptions().timeZone}function c(e){return`${e.screen.width}x${e.screen.height}`}((e,n)=>{for(var r in n)t(e,r,{get:n[r],enumerable:!0})})(i,{elb:()=>u,getAttribute:()=>d,getHashWeb:()=>m,getLanguage:()=>s,getScreenSize:()=>c,getTimezone:()=>a,isVisible:()=>w,sendWeb:()=>k,sendWebAsBeacon:()=>O,sendWebAsFetch:()=>v,sendWebAsXhr:()=>x,sessionStart:()=>j,sessionStorage:()=>N,sessionWindow:()=>H,storageDelete:()=>I,storageRead:()=>W,storageWrite:()=>L}),module.exports=(e=i,((e,i,s,a)=>{if(i&&"object"==typeof i||"function"==typeof i)for(let c of r(i))o.call(e,c)||c===s||t(e,c,{get:()=>i[c],enumerable:!(a=n(i,c))||a.enumerable});return e})(t({},"__esModule",{value:!0}),e));var u=function(){const e=window;(e.elbLayer=e.elbLayer||[]).push(arguments)};function d(e,t){return(e.getAttribute(t)||"").trim()}var l={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 f(e){return Array.isArray(e)}function g(e){return void 0!==e}async function m(e,t){return(await async function(e){const t=g(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 w(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 h={merge:!0,shallow:!0,extend:!0};function S(e,t={},n={}){n={...h,...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 y(e){return void 0===e?e:typeof e==typeof""?e:JSON.stringify(e)}function p(e={}){return S({"Content-Type":"application/json; charset=utf-8"},e)}function b(e,t,n){return function(...r){try{return e(...r)}catch(e){if(!t)return;return t(e)}finally{n?.()}}}function k(e,t,n={transport:"fetch"}){switch(n.transport||"fetch"){case"beacon":return O(e,t);case"xhr":return x(e,t,n);default:return v(e,t,n)}}async function v(e,t,n={}){const r=p(n.headers),o=y(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 O(e,t){const n=y(t),r=navigator.sendBeacon(e,n);return{ok:r,error:r?void 0:"Failed to send beacon"}}function x(e,t,n={}){const r=p(n.headers),o=n.method||"POST",i=y(t);return b((()=>{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:b(JSON.parse,(()=>t.response))(t.response),error:n?void 0:`${t.status} ${t.statusText}`}}),(e=>({ok:!1,error:e.message})))()}function U(e=6){let t="";for(let n=36;t.length<e;)t+=(Math.random()*n|0).toString(n);return t}function j(e={}){const{cb:t,consent:n,instance:r,storage:o}=e,i=r?.push||u;if(!n)return A((o?N:H)(e),r,t);{const r=function(e,t){let n;const r=(r,o)=>{if(g(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})((f(e.consent)?e.consent:[e.consent]).reduce(((e,t)=>({...e,[t]:!0})),{}),o)&&(i=()=>N(e))}return A(i(),r,t)};return r}(e,t);i("walker on","consent",(f(n)?n:[n]).reduce(((e,t)=>({...e,[t]:r})),{}))}}function A(e,t,n){return!1===n?e:(n||(n=C),n(e,t,C))}var C=(e,t)=>{const n=t?.push||u,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=l.Utils.Storage.Session){switch(t){case l.Utils.Storage.Cookie:L(e,"",0,t);break;case l.Utils.Storage.Local:window.localStorage.removeItem(e);break;case l.Utils.Storage.Session:window.sessionStorage.removeItem(e)}}function W(e,t=l.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 l.Utils.Storage.Cookie:r=decodeURIComponent(document.cookie.split("; ").find((t=>t.startsWith(e+"=")))?.split("=")[1]||"");break;case l.Utils.Storage.Local:o=n(window.localStorage.getItem(e));break;case l.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=l.Utils.Storage.Session,o){const i={e:Date.now()+6e4*n,v:String(t)},s=JSON.stringify(i);switch(r){case l.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 l.Utils.Storage.Local:window.localStorage.setItem(e,s);break;case l.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=b(((e,t,n)=>{let r=W(e,n);return r||(r=U(8),L(e,r,1440*t,n)),String(r)}))(r,i,o),f=b(((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:U(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(S(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:U(12),referrer:i},s,e.data):n}//# sourceMappingURL=web.js.map