(function(){let e=`https://p.conversion.ai/api`,t={HeadingPromotion:`H1_PROMOTION`,HeadingDemotion:`H1_DEMOTION`,AnchorTarget:`ADD_LINK_TARGET`,AnchorTitle:`ADD_LINK_TITLE`,MetaDescription:`OPTIMIZE_DESCRIPTION`,DocumentTitle:`OPTIMIZE_TITLE`,MetaOpenGraph:`ADD_OG_SITE_TAG`,MetaViewport:`META_VIEWPORT`,LinkCanonical:`LINK_CANONICAL`,ImageAlternateText:`ADD_ALT_TAG`},n={Active:`ACTIVE`,Modified:`EDITED`,Disabled:`REJECTED`};var r=e=>{let t=[];for(;e;){if(e.id){t.unshift(`/*[@id="${e.id}"]`);break}let n=e.parentNode?.childNodes??[],r=1;for(let i=0;ie===a.Error?`#fb2c36`:e===a.Warning?`#fe9a00`:e===a.Success?`#00bc7d`:`#2b7fff`;function s(e,t,...n){console.log(`%c[Conversion]`,`color: ${o(e)};`,t,...n),window.postMessage({id:`conversion`,type:`log`,data:{level:e,message:t,data:n}})}var c=`https://seonaapi.conversion.ai/api/v3/plugin-code-changes`,l=async()=>{try{let{success:e,data:t,error:n}=await(await fetch(`${c}?url=${encodeURIComponent(window.location.origin+window.location.pathname)}`)).json();if(!e)throw Error(n);let r=t.codeChanges.filter((e,t,n)=>n.findIndex(({xpath:t,type:n})=>t===e.xpath&&n===e.type)===t).map(e=>({...e,data:JSON.parse(e.data)}));return s(a.Success,`Successfully fetched code changes`,r),r}catch{return s(a.Error,`Failed to fetch code changes`),[]}},u=e=>document.evaluate(e,document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue,d={apply:e=>{let{data:{href:t},status:r,xpath:a,id:o}=e,{initialState:s}=i,c=u(a);if(!c||c.href!==t)return;let l=`_blank`;r===n.Disabled&&(l=s[o]?.target),s[o]||(s[o]={target:c.target}),c.target=l},property:`target`},f={apply:e=>{let{data:{href:t,title:r},status:a,xpath:o,id:s}=e,{initialState:c}=i,l=u(o);if(!l||l.href!==t)return;let d=r;a===n.Disabled&&(d=c[s]?.title),c[s]||(c[s]={title:l.title}),l.title=d},property:`title`},p={apply:e=>{let{data:{textContent:t},status:r,xpath:a,id:o}=e,{initialState:s}=i,c=u(a),l=t;if(r===n.Disabled){if(!c)return;if(!s[o]?.textContent){c.remove();return}l=s[o]?.textContent}if(c){s[o]||(s[o]={textContent:c.textContent}),c.textContent=l;return}let d=document.createElement(`title`);d.textContent=l,document.head.appendChild(d)},verify:e=>e.tagName.toLowerCase()===`title`},m=(e,t)=>{t.innerHTML=e.innerHTML;for(let n of e.attributes)t.setAttribute(n.name,n.value);e.parentElement?.insertBefore(t,e);let n=window.getComputedStyle(e);for(let r in n){let i=n.getPropertyValue(r);i&&(e.style.setProperty(r,`unset`),i!==n.getPropertyValue(r)&&(t.style[r]=i))}},h={apply:({xpath:e,status:t})=>{let a=t===n.Disabled,{modifiedXPaths:o}=i,s=u(a?o[e]:e);if(!s)return;let c=document.createElement(a?`h1`:`h2`);if(m(s,c),s.remove(),a){delete o[e];return}o[e]=r(c)},verify:(e,t)=>e.tagName.toLowerCase()===`h1`&&r(e)===t.xpath},g={apply:e=>{let{data:{tagName:t},status:a,xpath:o}=e,s=a===n.Disabled,{modifiedXPaths:c}=i,l=u(s?c[o]:o);if(!l)return;let d=document.createElement(s?t:`h1`);if(m(l,d),l.remove(),s){delete c[o];return}c[o]=r(d)},verify:(e,t)=>e.tagName.toLowerCase()!==`h1`&&r(e)===t.xpath},_={apply:e=>{let{data:{alt:t,src:r},status:a,xpath:o,id:s}=e,{initialState:c}=i,l=u(o);if(!l||l.src!==r)return;let d=t;a===n.Disabled&&(d=c[s]?.alt),c[s]||(c[s]={alt:l.alt}),l.alt=d},property:`alt`},v={apply:e=>{let{data:{href:t},status:r,xpath:a,id:o}=e,{initialState:s}=i,c=u(a),l=t;if(r===n.Disabled){if(!c)return;if(!s[o]?.href){c.remove();return}l=s[o]?.href}if(c){s[o]||(s[o]={href:c.href}),c.href=l;return}let d=document.createElement(`link`);d.rel=`canonical`,d.href=l,document.head.appendChild(d)},verify:e=>e.tagName.toLowerCase()===`link`&&e.rel===`canonical`,property:`href`},ee={apply:e=>{let{data:{content:t},status:r,xpath:a,id:o}=e,{initialState:s}=i,c=u(a),l=t;if(r===n.Disabled){if(!c)return;if(!s[o]?.content){c.remove();return}l=s[o]?.content}if(c){s[o]||(s[o]={content:c.content}),c.content=l;return}let d=document.createElement(`meta`);d.name=`description`,d.content=l,document.head.appendChild(d)},verify:e=>e.tagName.toLowerCase()===`meta`&&e.name===`description`,property:`content`},te={apply:e=>{let{data:{content:t,name:r,property:a},status:o,xpath:s,id:c}=e,{initialState:l}=i,d=u(s),f=t;if(o===n.Disabled){if(!d)return;if(!l[c]?.content){d.remove();return}f=l[c]?.content}if(d){l[c]||(l[c]={content:d.content}),d.content=f;return}let p=document.createElement(`meta`);r&&p.setAttribute(`name`,r),a&&p.setAttribute(`property`,a),p.content=f,document.head.appendChild(p)},verify:(e,t)=>{if(e.tagName.toLowerCase()!==`meta`)return!1;let{data:{name:n,property:r}}=t;return n?e.name===n:e.getAttribute(`property`)===r},property:`content`},y={apply:e=>{let{data:{content:t},status:r,xpath:a,id:o}=e,{initialState:s}=i,c=u(a),l=t;if(r===n.Disabled){if(!c)return;if(!s[o]?.content){c.remove();return}l=s[o]?.content}if(c){s[o]||(s[o]={content:c.content}),c.content=l;return}let d=document.createElement(`meta`);d.name=`viewport`,d.content=l,document.head.appendChild(d)},verify:e=>e.tagName.toLowerCase()===`meta`&&e.name===`viewport`,property:`content`},b={[t.HeadingDemotion]:h,[t.HeadingPromotion]:g,[t.ImageAlternateText]:_,[t.AnchorTarget]:d,[t.AnchorTitle]:f,[t.MetaDescription]:ee,[t.MetaOpenGraph]:te,[t.DocumentTitle]:p,[t.MetaViewport]:y,[t.LinkCanonical]:v},x=[],S=null,C={},ne=8,w=(e,n)=>{if(e instanceof HTMLElement&&[`title`,`h1`,`h2`,`h3`,`h4`,`h5`,`h6`,`meta`,`link`,`a`,`img`].includes(e.tagName.toLowerCase()))for(let i of n){if(C[i.id]===ne)continue;let{verify:n,property:a,apply:o}=b[i.type];if(!(n?n(e,i):r(e)===i.xpath)||i.type===t.AnchorTarget&&e.target===`_blank`||a&&e[a]===i.data[a])continue;let s=C[i.id]||0;C[i.id]=s+1,o(i)}},T=new MutationObserver(e=>{for(let{target:t,addedNodes:n}of e){w(t,x);for(let e of n)w(e,x)}}),E=e=>{if(e)try{return new URL(e,window.location.href).pathname}catch{return}},D=e=>{let t=E(e);if(!t||t===S||(T?.disconnect(),!x))return;let r=x.filter(({status:e})=>e!==n.Disabled).map(e=>({...e,status:n.Disabled}));for(let e of r)b[e.type].apply(e);i.initialState={}},O=async e=>{let t=E(e);if(!(!t||t===S)){S=t,C={},x=await l();for(let e of x)b[e.type].apply(e);T?.observe(document,{childList:!0,subtree:!0,attributes:!0,attributeFilter:[`alt`,`property`,`content`,`title`,`target`,`href`]})}},re=history.pushState;history.pushState=function(...e){D(e[2]),re.apply(history,e),O(e[2])};var ie=history.replaceState;history.replaceState=function(...e){D(e[2]),ie.apply(history,e),O(e[2])};var k=`_s__t`,A=`_s__s`,j=`_s__s__set`,M=1e3*60*60*24*365;let N=()=>document.cookie.split(`; `).find(e=>e.startsWith(`${A}=`))?.split(`=`)[1],P=e=>{let t=N();if(t&&sessionStorage.getItem(j)){s(a.Debug,`Continuing existing session`,t);return}if(t&&document.referrer)try{if(new URL(document.referrer).hostname.endsWith(e)){s(a.Debug,`Ignoring referrer URL`,document.referrer),sessionStorage.setItem(j,`true`);return}}catch(e){s(a.Error,`Failed to parse referrer URL`,e);return}let n=crypto.randomUUID(),r=new Date;r.setTime(r.getTime()+M),document.cookie=`${A}=${n}; path=/; domain=${e}; expires=${r.toUTCString()}`,sessionStorage.setItem(j,`true`),s(a.Debug,`Set session ID`,n)},F=()=>document.cookie.split(`; `).find(e=>e.startsWith(`${k}=`))?.split(`=`)[1],I=e=>{let t=F();if(t){s(a.Debug,`Found tracking ID`,t);return}let n=crypto.randomUUID(),r=new Date;r.setTime(r.getTime()+M),document.cookie=`${k}=${n}; path=/; domain=${e}; expires=${r.toUTCString()}`,s(a.Debug,`Set tracking ID`,n)};var L=async(e,t,n=!0)=>{let r=F(),i=N();if(!n&&!r&&(s(a.Warning,`Unable to find tracking ID, generating a fallback ID`),r=crypto.randomUUID()),!n&&!i&&(s(a.Warning,`Unable to find session ID, generating a fallback ID`),i=crypto.randomUUID()),!r)return s(a.Error,`Failed to send request`,`No tracking ID found`),!1;try{return await fetch(e,{...t,keepalive:!0,method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({pageUrl:window.location.origin+window.location.pathname,trackingId:r,sessionId:i,referrer:document.referrer,...t?.body})}),!0}catch{return!1}};let R={PageVisit:`PAGE_VISIT`,FormSubmission:`FORM_SUBMISSION`},z={Custom:`CUSTOM`};async function B(t,n){let r=await L(`${e}/v1/e/t`,{body:{data:n,type:t}},t===R.FormSubmission);return r?s(a.Success,`Successfully tracked engagement`,{type:t,data:n}):s(a.Error,`Failed to track engagement`,{type:t,data:n}),r}var V=e=>{if(e.id)return[e.id];if(e.name)return[e.name];let t=Array.from(e.classList);if(t.length>0)return t;let n=e.getAttribute(`action`);if(n)return[n]},H=e=>{let t=e.target;return t.id?.startsWith(`hsForm_`)||t.action?.includes(`hsforms.com`)?!0:window.HubSpotFormsV4?(window.HubSpotFormsV4.getForms?.())?.some(e=>t.id.includes(e.getInstanceId?.())||t.id.includes(e.getFormId?.()))??!1:!1},U=e=>{s(a.Debug,`Detected a form submission`);let t=e.target,n=new FormData(t);if(H(e)){s(a.Debug,`Skipping HubSpot form submission tracking`,{formId:t.id});return}let r=Array.from(n.entries()).reduce((e,[n,r])=>{if(r instanceof File||typeof r==`string`&&r.trim().length===0)return e;let i=t.querySelector(`[name="${n}"]`);return n.toLowerCase().includes(`password`)||i&&i.type===`password`?(s(a.Debug,`Detected a sensitive form field`,{name:n}),e):(e.push({name:n,value:r}),e)},[]),i=V(t);if(!i){s(a.Warning,`Did not track form submission for a form without a key`,{fields:r});return}B(R.FormSubmission,{type:z.Custom,key:i,name:t.name,submission:{fields:r}})},W=()=>{document.addEventListener(`submit`,U,!0)},G=async()=>{try{let t=await fetch(`${e}/v1/bd?url=${encodeURIComponent(window.location.origin+window.location.pathname)}`);if(!t.ok)return;let{data:n}=await t.json();return n}catch{s(a.Error,`Failed to fetch base domain`,window.location.href)}},K=e=>{if(!window.HubSpotFormsV4)return;let t=window.HubSpotFormsV4.getFormFromEvent(e);t&&B(R.FormSubmission,{name:``,type:z.Custom,key:[t.getFormId()],submission:{fields:t.getFormFieldValues().reduce((e,{name:t,value:n})=>{if(n===void 0)return e;let r=typeof n==`object`?JSON.stringify(n):n.toString();return r.trim().length===0||e.push({name:t,value:r}),e},[])}})},q=()=>{window.addEventListener(`hs-form-event:on-submission:success`,K)},J={OnBeforeFormInit:`onBeforeFormInit`,OnFormReady:`onFormReady`,OnBeforeFormSubmit:`onBeforeFormSubmit`,OnFormSubmit:`onFormSubmit`,OnFormSubmitted:`onFormSubmitted`},ae=e=>{if(e.data?.type!==`hsFormCallback`)return;let t=e.data;t.eventName===J.OnBeforeFormSubmit&&B(R.FormSubmission,{name:``,type:z.Custom,key:[t.id],submission:{fields:t.data.filter(({value:e})=>typeof e==`string`&&e.trim().length>0)}})},oe=()=>{window.addEventListener(`message`,ae)},Y=`https://forms.conversion.ai`,se=()=>{let e=F();if(!e)return;let t=N();if(!t)return;let n=document.referrer,r=window.location.origin+window.location.pathname;document.querySelectorAll(`iframe`).forEach(e=>{e.src.startsWith(Y)&&e.contentWindow?.postMessage({type:`conversion-forms-ping`},Y)}),window.addEventListener(`message`,i=>{if(i.origin===Y){if(i.data.type===`conversion-forms-height-adjustment`&&i.data.data)try{let{height:e}=i.data.data;window.requestAnimationFrame(()=>{document.querySelectorAll(`iframe`).forEach(t=>{t.contentWindow===i.source&&(t.style.height=`${e}px`)})})}catch(e){console.warn(`Invalid message from child iframe`,i,e)}if(i.data.type===`conversion-forms-ping`&&i.source?.postMessage({type:`conversion-forms-pong`,data:{trackingId:e,sessionId:t,referrer:n,pageUrl:r}},{targetOrigin:Y}),i.data.type===`conversion-forms`)try{let e=i.data;e.data.eventName===`submitted`&&e.data.redirectUrl&&(window.location.href=e.data.redirectUrl)}catch(e){console.warn(`Invalid message from child iframe`,i,e)}}})},ce=()=>{let e=new URLSearchParams(window.location.search);return Array.from(e).reduce((e,[t,n])=>((t.startsWith(`utm_`)||t.startsWith(`cai_`))&&(e[t]=n),e),{})},X=!1,Z=null,le=async()=>{if(X)return!0;let e=await G();return e?(X=!0,P(e),I(e),q(),oe(),W(),se(),!0):!1},Q=async()=>{window.location.pathname!==Z&&(Z=window.location.pathname,await le()&&B(R.PageVisit,{utmFields:ce()}))},ue=history.pushState;history.pushState=function(...e){ue.apply(history,e),Q()};var de=history.replaceState;history.replaceState=function(...e){de.apply(history,e),Q()};var $=()=>{s(a.Debug,`Initializing pixel`),O(window.location.href),Q()};(()=>{if(s(a.Debug,`Loading pixel`),window.self!==window.parent||window.location.href===`about:srcdoc`){s(a.Warning,`Injecting pixel in parent window`);try{if(window.parent.__conversion_loaded){s(a.Warning,`Pixel already loaded in the parent window`);return}let e=window.parent.document.createElement(`script`);e.src=`https://p.conversion.ai`,e.defer=!0,window.parent.document.head.appendChild(e),s(a.Debug,`Injected pixel in parent window`)}catch(e){s(a.Error,`Failed to inject pixel in parent window`,e)}return}if(window.__conversion_loaded){s(a.Warning,`Pixel already loaded`);return}if(window.__conversion_loaded=!0,s(a.Success,`Pixel loaded`),document.readyState===`loading`){document.addEventListener(`DOMContentLoaded`,$);return}$()})()})();