(()=>{function f(i){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var n in e)i[n]=e[n]}return i}var T={read:function(i){return i[0]==='"'&&(i=i.slice(1,-1)),i.replace(/(%[\dA-F]{2})+/gi,decodeURIComponent)},write:function(i){return encodeURIComponent(i).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,decodeURIComponent)}};function w(i,t){function e(r,o,s){if(typeof document!="undefined"){s=f({},t,s),typeof s.expires=="number"&&(s.expires=new Date(Date.now()+s.expires*864e5)),s.expires&&(s.expires=s.expires.toUTCString()),r=encodeURIComponent(r).replace(/%(2[346B]|5E|60|7C)/g,decodeURIComponent).replace(/[()]/g,escape);var a="";for(var c in s)!s[c]||(a+="; "+c,s[c]!==!0&&(a+="="+s[c].split(";")[0]));return document.cookie=r+"="+i.write(o,r)+a}}function n(r){if(!(typeof document=="undefined"||arguments.length&&!r)){for(var o=document.cookie?document.cookie.split("; "):[],s={},a=0;a<o.length;a++){var c=o[a].split("="),h=c.slice(1).join("=");try{var u=decodeURIComponent(c[0]);if(s[u]=i.read(h,u),r===u)break}catch(l){}}return r?s[r]:s}}return Object.create({set:e,get:n,remove:function(r,o){e(r,"",f({},o,{expires:-1}))},withAttributes:function(r){return w(this.converter,f({},this.attributes,r))},withConverter:function(r){return w(f({},this.converter,r),this.attributes)}},{attributes:{value:Object.freeze(t)},converter:{value:Object.freeze(i)}})}var L=w(T,{path:"/"}),g=L;var v=(i,t)=>{let e=g.get(i)||R();return g.set(i,e,t),e},m=i=>{let t=[];if(i instanceof Element){for(;i.nodeType===Node.ELEMENT_NODE;){let e=i.nodeName.toLowerCase();if(i.id){e+=`#${i.id}`,t.unshift(e);break}else{let n=i,r=1;for(;n=n.previousElementSibling;)n.nodeName.toLowerCase()==e&&r++;r!=1&&(e+=`:nth-of-type(${r})`)}t.unshift(e),i=i.parentNode}return t.join(">")}},_=i=>{let t=i.getAttribute("tw-ref"),e=i.getAttribute("href"),n=i.innerText;if(t)return t;if(n&&e)return`[${n}](${e})`;if(n)return n;if(e)return e;console.log({element:i,ref:t,text:n,href:e})};function R(){return"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx".replace(/[xy]/g,i=>{let t=Math.random()*16|0;return(i==="x"?t:t&3|8).toString(16)})}var E=()=>v("unique_id",{expires:365}),y=()=>v("session_id"),k=()=>{try{let i=g.get("twilight-user"),t=decodeURIComponent(i);return JSON.parse(t)}catch(i){return null}},S=i=>i.split("&").reduce((t,e)=>{let[n,r]=e.split("=").map(o=>decodeURIComponent(o));return n?{...t,[n]:r}:t},{}),C=i=>{let e=encodeURIComponent(JSON.stringify(i)).replace(/%([0-9A-F]{2})/g,(r,o)=>String.fromCharCode(Number(`0x${o}`))),n=btoa(e);return`data=${encodeURIComponent(n)}`},A=()=>{let{search:i}=window.location,t=S(i.slice(1)),e=Object.keys(t),n=/^(tt|utm)_(.*)$/i;return e.reduce((r,o)=>n.exec(o)||[]?{...r,[o]:t[o]}:r,{})};window.Element&&!Element.prototype.closest&&(Element.prototype.closest=function(i){var t=(this.document||this.ownerDocument).querySelectorAll(i),e,n=this;do for(e=t.length;--e>=0&&t.item(e)!==n;);while(e<0&&(n=n.parentElement));return n});var P=class{type=null;ref=null;selector=null;analytics=null;callback=()=>null;constructor(t,e,n,r,o,s){this.type=e,this.ref=[r,o],this.selector=n,this.analytics=t,typeof s=="function"&&(this.callback=s)}getExtraParams(t){let{ref:e}=this,[n,r]=e;return typeof n=="function"?{...n(t)}:{action:n,action_ref:r}}buildEventPayload(t){let{analytics:e}=this;try{let{action:n,...r}=this.getExtraParams(t),o=m(t);return e.buildEventPayload(n,{...r,element_path:o})}catch(n){return e.buildErrorPayload(t,n)}}},p=class{dataLayer=[["config","auto_track_outbound_links",!0],["config","add_attribute_helpers",!0],["config","debug",!1]];watchers={};config={debug:!1,auto_track_outbound_links:!1,add_attribute_helpers:!1};environment="production";client_app=null;client_id=null;user_id=null;target="marketing";language=document.documentElement.lang;endpoint="https://spade.twitch.tv/track";get baseEventProperties(){let t=A(),e=document.referrer||t.utm_referrer||null,n=e?new URL(e).host:null;return{device_id:E(),session_id:y(),analytics_version:"c208de32aa6f06d44dbad163d30eb28cdeef69d7",client_time:Date.now()/1e3,host:this.host,url:window.location.href,browser:window.navigator.userAgent,preferred_language:window.navigator.language,referrer_url:e,referrer_host:n,...t}}get userParams(){let{user_id:t}=this,{id:e}={...k()};return{user_id:t||e}}get globalEventProperties(){return this.configurableGlobalMetrics.reduce((t,e)=>({...t,[e]:this[e]}),{})}get host(){return window.location.host}constructor(t){this.bindEventHandlers(),this.pushQueue(t||[["page_view"]])}log(t,...e){this.config.debug&&console[t]("[DEBUG]",...e)}bindEventHandlers(){this.handleClick=this.handleClick.bind(this),this.handleOutboundLink=this.handleOutboundLink.bind(this),this.handleAttributedElement=this.handleAttributedElement.bind(this),document.addEventListener("click",this.handleClick,!0)}handleClick(t){let{watchers:e}=this,{type:n,target:r,metaKey:o}=t,s=e[n]||[],{tagName:a}=r,c=[],h=[],u=r.href;for(let l=0,U=s.length;l<U;l++){let x=s[l],{selector:I}=x,d=r.closest(I);if(d)try{let{tagName:b}=d,O=x.buildEventPayload(d);c.push(O),h.push(x.callback),(b==="A"||a==="A")&&(u=u||d.href,o||r.target||t.preventDefault())}catch(b){c.push(this.buildErrorPayload(d,b))}}this.sendEvents(c,()=>{h.forEach(l=>l()),u&&t.defaultPrevented&&(window.location=u)})}buildErrorPayload(t,e){let n=m(t);return this.buildEventPayload("error",{element_path:n,action_ref:e.message})}handleOutboundLink(t){let e=window.location.host,n=t.getAttribute("href");if(n&&e!==n.host)return{action:"outbound_link",action_ref:_(t)}}getTwitchAttributes(t){let e=/^tw-(.+)$/;if(t)try{return[...t.attributes].reduce((n,{name:r,value:o})=>{let s=e.exec(r);if(s){let[a,c]=s;return{...n,[c]:o}}return n},{})}catch(n){let r=t.getAttribute("tw-action");return this.log("error","getTwitchAttributes",n,t),{action:r}}}handleAttributedElement(t){let e=this.getTwitchAttributes(t),{event:n,action:r,...o}=e,s=r||n;if(s===n&&this.log("warn","You are using a deprecated `tw-event` attribute. Rename `tw-event` to `tw-action`.",{value:n,element:t}),s)return{...o,action:s,action_ref:_(t)}}pushQueue(t){t instanceof Array?t.forEach(e=>this.push(e)):this.dataLayer.forEach(e=>this.push(e))}on(t,e,n,r,o){let{watchers:s}=this,a=s[t]||[];this.watchers={...s,[t]:[...a,new P(this,t,e,n,r,o)]}}off(t,e,n,r){let{watchers:o}=this,s=o[t]||[];this.watchers={...o,[t]:s.filter(a=>a.selector===e&&a.ref[0]===n&&a.ref[0]===r)}}autoTrackOutboundLinks(t){!this.config.auto_track_outbound_links&&t&&this.on("click","a[href]:not([tw-action])",this.handleOutboundLink),t||this.off("click","a[href]:not([tw-action])",this.handleOutboundLink),this.config.auto_track_outbound_links=t}addAttributeHelpers(t){!this.config.add_attribute_helpers&&t&&this.on("click","[tw-action],[tw-event]",this.handleAttributedElement),t||this.off("click","[tw-action],[tw-event]",this.handleAttributedElement),this.config.add_attribute_helpers=t}pageView(t={},e){this.event("page_view",t,e)}async sendEvents(t,e){let{endpoint:n}=this;try{let r=t.filter(o=>o&&o.event&&o.properties&&o.properties.action);if(r.length>0){let o=new Blob([C(r)],{type:"application/x-www-form-urlencoded; charset=UTF-8"});this.log("info","sendEvents",r.length,r),await fetch(n,{method:"POST",body:o})}}catch(r){this.log("error","sendEvents",r,t)}typeof e=="function"&&e()}buildEventPayload(t,e={}){let{target:n,...r}={...this.globalEventProperties,...e,...this.baseEventProperties,...this.userParams,action:t};return n&&t?{event:n,properties:r}:null}async event(t,e={},n){let r=this.buildEventPayload(t,e);return this.sendEvents([r],n)}get configurableGlobalMetrics(){return["client_app","client_id","environment","language","target","user_id"]}get configurable(){return{auto_track_outbound_links:this.autoTrackOutboundLinks,add_attribute_helpers:this.addAttributeHelpers,debug:t=>this.config.debug=t}}get pushableMethods(){return{config:this.pushConfig,event:this.event,page_view:this.pageView,on:this.on}}maybeCallFunction(t,e,...n){typeof t[e]=="function"&&t[e].call(this,...n)}pushConfig(t,e){this.configurableGlobalMetrics.includes(t)?this[t]=e:this.maybeCallFunction(this.configurable,t,e)}push(t){return this.maybeCallFunction(this.pushableMethods,...t)}};var D=new p(window._tw);window._tw={push(i){return D.push(i),null}};})();
/*! js-cookie v3.0.1 | MIT */
