@zywave/zywave-api-toolkit-bundle
Version:
2 lines (1 loc) • 8.16 kB
JavaScript
import{i as t,n as e,a as i,x as a}from"./_baebe903.js";const n=new Set;const s=t`:host{position:absolute;display:none;width:0;height:0;border:0}`;function r(t,e){return new Promise(((i,a)=>{let n;if(n="string"==typeof t?new URL(t):t,e?.preconfigureUrl&&(n=e.preconfigureUrl(n)),document.querySelector(`script[src="${n}"]`))i();else{const t=document.createElement("script");t.type="text/javascript",t.async=!0,t.defer=!0,t.src=n.toString(),e?.preconfigureScriptElement&&e.preconfigureScriptElement(t),document.head.appendChild(t),t.addEventListener("load",(()=>i())),t.addEventListener("error",(()=>a()))}}))}function o(t,e,i){Event.prototype.composedPath&&t.addEventListener(e,(t=>{const e=t.composedPath();e&&e.length&&e[0]!==t.target&&e[0]instanceof Element&&i(t,e[0])}))}async function c(t,e,i,a,n){a??={};const s=new URL("cdn.heapanalytics.com/js/",n);function c(t,e){return{"Target ID":e.id,"Target Class":e.className,"Target Tag":e.tagName,"Target Text":e.textContent,"Target Name":e.getAttribute("name"),"Target Type":e.getAttribute("type"),"Target Href":e.getAttribute("href"),"Target Action":e.getAttribute("action")}}function d(){window.heap.track("View")}try{return await r(s,{preconfigureUrl:n=>{const s=window.heap||[];window.heap=s,s.appid=t,s.config={},e&&s.push(["identify",e]);const r=a?.isImpersonating??!1;if(i){if("userPrincipalId"in i&&(i["User Principal ID"]=i.userPrincipalId,delete i.userPrincipalId),!r){const t={prefersDarkMode:window.matchMedia?.("(prefers-color-scheme: dark)").matches??!1};i={...t,...i}}s.push(["addUserProperties",i])}const o=function(){if(!window.navigator.connection)return;const t="unknown"===window.navigator.connection.type?void 0:window.navigator.connection.type;return{networkReducedData:window.navigator.connection.saveData??!1,networkConnectionType:t,networkRtt:window.navigator.connection.rtt,networkDownlink:window.navigator.connection.downlink,networkDownlinkMax:window.navigator.connection.downlinkMax}}(),c={screenWidth:window.screen.width,screenHeight:window.screen.height,screenResolution:`${window.screen.width}x${window.screen.height}`,...o,...a};return"userPrincipalId"in c&&(c["User Principal ID"]=c.userPrincipalId,delete c.userPrincipalId),"isImpersonating"in c&&(c["User Profile Impersonated"]=(c.isImpersonating??!1).toString(),delete c.isImpersonating),s.clearEventProperties?.(),s.push(["addEventProperties",c]),s.track=(t,...e)=>{e.unshift(t,"track"),s.push(e)},n=new URL(`heap-${t}.js`,n)}}),window.heap&&(o(window.document,"click",((t,e)=>{window.heap.track("Composed click",c(0,e))})),o(window.document,"submit",((t,e)=>{window.heap.track("Composed submit",c(0,e))})),o(window.document,"change",((t,e)=>{window.heap.track("Composed change",c(0,e))})),window.addEventListener("zapiReplaceState",d),window.addEventListener("zapiPushState",d)),{track(t,e){window.heap?.track?.(t,e)},trackerId:"Heap"}}catch{return}}const d=class{static get instance(){return this.#t??=new this}static#t;#e=[];constructor(){}track(t,e){for(const i of this.#e)i.then((i=>i?.track(t,e)))}registerTracker(t){t&&this.#e.push(Promise.resolve(t))}}.instance;var p=function(t,e,i,a){var n,s=arguments.length,r=s<3?e:null===a?a=Object.getOwnPropertyDescriptor(e,i):a;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(t,e,i,a);else for(var o=t.length-1;o>=0;o--)(n=t[o])&&(r=(s<3?n(r):s>3?n(e,i,r):n(e,i))||r);return s>3&&r&&Object.defineProperty(e,i,r),r};let l=new URL(import.meta.url).origin;l.includes("unpkg.com")&&(l="https://cdn.zywave.com",console.warn("Loading the Zywave API Toolkit from unpkg.com is deprecated. Please use cdn.zywave.com instead. Your application is likely to break."));class h extends i{constructor(){super(...arguments),this.heapAppId=null,this.appcuesAccountId=null,this.identity=null,this.userProperties=null,this.defer=!1,this.cdnHost=l,this.contextPath=null,this.#i=!1}static get styles(){return[s]}#a;get#n(){return!!(this.heapAppId||this.appcuesAccountId||this.identity)}get _userProperties(){const t=this.#s?.userProperties,e="object"==typeof this.userProperties?this.userProperties:void 0,i={languages:navigator.languages.map((t=>`[${t.toLowerCase()}]`)).join(";"),...t,...e};return this.#r&&(i.email=this.#r.email||i.email,i.givenName=this.#r.given_name||i.givenName,i.familyName=this.#r.family_name||i.familyName),i}#s;#r;#i;connectedCallback(){super.connectedCallback(),function(){const t=function(t,e){if(n.has(t))return window.history[t];const i=window.history[t];return n.add(t),function(){const a=i.apply(this,arguments),n=new CustomEvent(e??t,{detail:{args:arguments}});return window.dispatchEvent(n),a}};history.pushState=t("pushState","zapiPushState"),history.replaceState=t("replaceState","zapiReplaceState")}()}disconnectedCallback(){super.disconnectedCallback(),this.#a&&clearInterval(this.#a),this.#a=void 0}async firstUpdated(t){super.firstUpdated(t),this.#n||await this._apiClientReady,await Promise.allSettled([this.#o(),this.#c()]),this.#i&&(this.defer&&"complete"!==document.readyState?document.addEventListener("readystatechange",(t=>{"complete"===t.target.readyState&&this.#d()})):this.#d())}render(){return a``}#d(){this.#p();const{heapAppId:t,appcuesAccountId:e,identity:i,eventProperties:a}=this.#s??{},n=this.cdnHost??"https://cdn.zywave.com";let s,o;t&&(s=c(t,i,this._userProperties,a,n)),e&&(o=async function(t,e,i,a,n){function s(){window.Appcues.page()}const o=new URL("fast.appcues.com/",n);try{if(await r(o,{preconfigureUrl:e=>(window.AppcuesSettings=Object.assign({},window.AppcuesSettings,{skipAMD:!0}),new URL(`${t}.js`,e))}),window.Appcues){if(import("./_e149c9d8.js").then((t=>{t.style.styleSheet&&(document.adoptedStyleSheets=[...document.adoptedStyleSheets,t.style.styleSheet])})),e&&!a?.isImpersonating){let t;i&&(t={...i,first_name:i.givenName,last_name:i.familyName},delete t.givenName,delete t.familyName),window.Appcues.identify(e,t)}window.addEventListener("zapiReplaceState",s),window.addEventListener("zapiPushState",s)}return{track(t,e){},trackerId:"Appcues"}}catch{return}}(e,i,this._userProperties,a,n)),d.registerTracker(s),d.registerTracker(o),Promise.allSettled([s,o]).then((()=>{this.dispatchEvent(new CustomEvent("load"))}))}track(t,e){d.track(t,e)}async#o(){if(this.#n)this.#s={heapAppId:this.heapAppId||"",appcuesAccountId:this.appcuesAccountId||"",identity:this.identity||"",contextPath:this.contextPath||""},this.#i=!0;else if(this._authorized&&this._apiClient){const t=new URL("shell/v2.0/analyticsinfo",this.apiUrl);t.searchParams.set("host",window.location.hostname);const e=await this._apiClient.get(t);if(e instanceof Response&&e.ok){const t=await e.json();this.#i=!0,this.#s=t,this.#s.contextPath??=this.contextPath}}else this.#i=!0}async#c(){if(this._authorized&&this._apiClient){const t=new URL("userinfo",this.apiUrl),e=await this._apiClient.fetch(t);if(e instanceof Response&&e.ok){const t=await e.json();this.#r=t}}}async#p(){if(!this.#s?.contextPath)return;const{ActivityTracker:t}=await import("./_4d5e4721.js");await t.connect({isImpersonated:this.#s.isImpersonating??!1,cookieDomain:this.#s.cookieDomain}),this.#a=window.setInterval((async()=>{await this.#l(t)}),this.#s?.activityTrackingInterval??2e3),window.addEventListener("visibilitychange",(async()=>{"hidden"===document.visibilityState&&await this.#l(t)}))}async#l(t){const e=await t.retrieveActivity();if(!this.#s?.contextPath||!e.length)return;const i=new URL(`v3/shell/v1.0${this.#s.contextPath}/a/e`,this.apiUrl);this._apiClient?.fetch(i,{method:"POST",body:JSON.stringify(e),headers:{"Content-Type":"application/json"},keepalive:!0})}}p([e({type:String,attribute:"heap-app-id"})],h.prototype,"heapAppId",void 0),p([e({type:String,attribute:"appcues-account-id"})],h.prototype,"appcuesAccountId",void 0),p([e({type:String})],h.prototype,"identity",void 0),p([e({type:Object,attribute:"user-properties"})],h.prototype,"userProperties",void 0),p([e({type:Boolean})],h.prototype,"defer",void 0),p([e({type:String,attribute:"cdn-host"})],h.prototype,"cdnHost",void 0),p([e({type:String,attribute:"context-path"})],h.prototype,"contextPath",void 0),window.customElements.define("zywave-analytics",h);export{d as A};