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