@schibsted/sourcepoint
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
2 lines (1 loc) • 12.3 kB
JavaScript
!function(){const e=["jsSdkVersion","id","realm","clientId","pulseTracker","referrer","state","pulseObjectName","showInWebview","identityObject","identityObjectName","disableSentry","disableNativeConsentCheck","enableUserCentric","groupId"];function n(e,n){var t,s;if(null!=(t=e.psi)&&t.identityObject)return e.psi.identityObject;if(null!=n&&n.identityObject)return n.identityObject;const o=(null==(s=e.psi)?void 0:s.identityObjectName)||(null==n?void 0:n.identityObjectName)||"Identity";return e[o]?e[o]:console.warn("@schibsted/sourcepoint: No Schibsted Account integration detected.")}function t(e){const t=Array.from({length:20},(()=>String.fromCharCode(Math.floor(25*Math.random()+97)))).join(""),s=btoa(JSON.stringify(t));n(e)&&n(e).login({state:s})}const s=["Mixed","PSI","SCC","TCF","PMC"],o="default",r="cmp",i="cache",c="accepted",a="rejected",u="unknown",l=Object.freeze({purposes:{CMP_ANALYTICS:{optIn:!1,status:u},CMP_MARKETING:{optIn:!1,status:u},CMP_ADVERTISING:{optIn:!1,status:u},CMP_PERSONALIZATION:{optIn:!1,status:u}},source:o});function p(e,n,t,s){b("Pulse: "+t+"(actor, data):",n,s),d(e,(e=>{e&&"function"==typeof e.track&&e.track(t,{schema:"http://schema.schibsted.com/events/tracker-event.json/314.json",object:Object.assign({},s,p.pulseCommonData),provider:{component:"CMP"},actor:n})}))}function d(e,n){var t,s;if(null!=(t=e.psi)&&t.pulseTracker)return n(e.psi.pulseTracker);const o=(null==(s=e.psi)?void 0:s.pulseObjectName)||"pulse";return"function"!=typeof e[o]?console.error("No Pulse integration detected. Aborting."):e[o]((e=>{"function"==typeof n&&n(e)}))}function f(){var e;const n=null==(e=p.pulseCommonData)?void 0:e.messageName;if(n)return s.find((e=>n.includes(e)))}function g(e){return"CMP_"+("personalisation"===e?"personalization":e).toUpperCase()}function m(e,n,t){const s=Object.keys(y(e)).filter((n=>y(e)[n])),o=Object.keys(y(e)).filter((n=>!y(e)[n]));return s.map((e=>{const t=n.purposes[g(e)];t.optIn=!0,t.status=c})),o.map((e=>{n.purposes[g(e)].status=a})),n.source=t,n}function y(e){const n=_((e._scc_||e._tcf_).getPermissionSync("CMP:advertising")),t=_((e._scc_||e._tcf_).getPermissionSync("CMP:analytics")),s=_((e._scc_||e._tcf_).getPermissionSync("CMP:marketing")),o=_((e._scc_||e._tcf_).getPermissionSync("CMP:personalisation"));var r;return r={advertising:n,analytics:t,marketing:s,personalisation:o},Object.entries(r).filter((e=>{let[n,t]=e;return null!=t})).reduce(((e,n)=>{let[t,s]=n;return Object.assign({},e,{[t]:s})}),{})}function v(e,n){const t=n.performance.now();return e?Math.round((t-e)/1e3):null}function b(){"undefined"!=typeof document&&document.location.href.indexOf("sp_debug=1")>0&&console.debug("[CMP]",...arguments)}function h(e,n,t,s,o){const r=new URL(e);r.searchParams.append("utm_source","cmp"),r.searchParams.append("utm_medium","psi"),r.searchParams.append("referrer",s||window.location.href),t&&r.searchParams.append("client_id",t),n&&r.searchParams.append("environment_id",n),o&&r.searchParams.append("state",o),window.location.href=r.href}function _(e){return null===e?null:Boolean(parseInt(e))}const C=(e,n)=>Object.keys(n).filter((n=>void 0!==e[n])).forEach((t=>"object"==typeof n[t]?Object.keys(n[t]).forEach((s=>e[t][s]=n[t][s])):e[t]=n[t]));function w(e,n,t){e.targetingParams=e.targetingParams||{},e.targetingParams[n]=t}const P=function(e){if("number"==typeof e||void 0===e)return config.userId=e;throw new Error("Id value "+e+" is invalid")},I=function(e){return(e=>{try{return Boolean(new URL(e))}catch(e){return!1}})(e)?window.psi.isLoggedInUser?h(e,null,window.psi.clientId,window.psi.referrer,window.psi.state):d(window,(n=>{n.getEnvironmentId().then((n=>{h(e,n,window.psi.clientId,window.psi.referrer,window.psi.state)}))})):console.error("The URL passed to the configuration is invalid. Please, correct it.")},O=function(){return t(window)};const M="__CONFIGURATION_PLACEHOLDER__";let j=null,S=!1,T=!1;function k(e,n){if(n&&("object"!=typeof(t=n)||t.constructor!==Object))return console.error("Provided object is incorrect");var t;if(0===Object.keys(y(e)).length){if(!n)return l;const e=JSON.parse(JSON.stringify(n));return e.source=o,e}return m(e,JSON.parse(JSON.stringify(n||l)),i)}function E(e,n){n.events=n.events||{};const t=n.events.onMessageReceiveData,s=n.events.onMessageChoiceSelect,o=n.events.onMessageReady,i=n.events.onPrivacyManagerAction,c=n.events.onConsentReady;let a,u=!0;const g={id:n.userId,realm:n.realm};n.events.onMessageReceiveData=function(e,s){b("Sourcepoint: onMessageReceiveData(data)",s),j=!0,p.pulseCommonData={"@id":"sdrn:privacy:cmp:"+(s.messageId||""),"@type":"CMP",messageName:s.msgDescription||"",propertyId:n.propertyId,messageId:s.messageId||void 0,bucket:s.bucket||void 0,jsSdkVersion:n.jsSdkVersion,partitionId:s.prtnUUID||"",language:n.consentLanguage},"function"==typeof t&&t(s)},n.events.onMessageReady=function(){var n;null==(n=window._tcf_)||null==(n=n.TCFCallbacks)||n.map((e=>{e()})),b("Sourcepoint: onMessageReady()");const t=f();if(b("Pulse: Detected message type: "+t),!j)return p(e,g,"View",{name:"CMP: Privacy Manager displayed",messageType:f()});a=e.performance.now(),p(e,g,"View",{name:"CMP: Message displayed",messageType:t}),"function"==typeof o&&o()},n.events.onMessageChoiceSelect=function(n,t,o){if(b("Sourcepoint: onMessageReady(choice_id="+t+", choice_type_id="+o+")"),5===o){const n=v(a,e);p(e,g,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:j,messageType:f()},n&&{duration:n})),j=null}if(9===o){u=!1,j=null;const n=v(a,e);p(e,g,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:f()},n&&{duration:n}))}if(11===o&&(u=!0),12===o&&(j=!1),15===o){const n=v(a,e);p(e,g,"engagementEvent",Object.assign({name:"CMP: Dismiss message",elementType:"button",messageType:f()},n&&{duration:n}))}"function"==typeof s&&s(t,o)},n.events.onPrivacyManagerAction=function(e,n){b("Sourcepoint: onPrivacyManagerAction()"),S=!0,j=!1,"none"===n.purposeConsent?(T=!0,u=!1):(T=!1,u=!0),"function"==typeof i&&i()},n.events.onConsentReady=function(n,t,s,o){var i,h;null==(i=window._tcf_)||null==(i=i.consentedCallback)||i.map((e=>{e(o.consentedToAll)})),null==(h=window._tcf_)||null==(h=h.TCFCallbacks)||h.map((e=>{e()})),b("Sourcepoint: onConsentReady(message_type="+n+", consentUUID="+t+", euconsent)",s),b("Sourcepoint: getMessageType() "+f());const _=v(a,e);if(void 0===f()&&!S)return;const C="PSI"===f()?null:y(e);if(function(e){const n=JSON.parse(JSON.stringify(l));d(e,(t=>{try{t.setConsents(m(e,n,r))}catch(e){console.error("Consents couldn't be set. The provided version of Pulse SDK does not support the setConsents event")}}))}(e),T&&p(e,g,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:f()},_&&{duration:_})),!u)return!1;p(e,g,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:f(),consentedToAll:null==o?void 0:o.consentedToAll,isFirstLayer:j||!!j},C&&{consents:C},_&&{duration:_})),j=null,S=!1,T=!1,"function"==typeof c&&c(n,t,s,o)}}function N(e,n,t){if(!n.groupPmId)throw new Error("groupPmId is required");const s="CMP:advertising",o="CMP:analytics",r="CMP:marketing",i="CMP:personalisation",c="CMP:performance_marketing";let a=[];const u={};function l(n){return b(t+": getPermissionFromCache(category="+n+")"),e.localStorage.getItem(n)}u[s]=[],u[o]=[],u[r]=[],u[i]=[],u[c]=[];let d=null;const f={subscribers:u,consentedCallback:[],TCFCallbacks:[],showPrivacyManager:function(){b("${type}: showPrivacyManager()"),p.pulseCommonData.messageName="",S=!0,j=!1,e._sp_.gdpr.loadPrivacyManagerModal(n.groupPmId)},getPermissionSync:function(e){return b(t+": getPermissionSync(category="+e+")"),l(e)},getPermission:function(e,n){b(t+": getPermission(category="+e+")");const s=f.subscribe(e,function(){let e=!1;return t=>{e?"function"==typeof s&&s():(e=!0,n(t),n=function(){})}}())},notify:(g={[s]:l(s),[o]:l(o),[r]:l(r),[i]:l(i),[c]:l(c)},(e,n)=>{if(g[e]===n)return void b(t+": notify(category="+e+", value="+n+") - IGNORED");null===d&&(d=setTimeout((()=>{const e=a;for(let n=0;n<e.length;n++)e[n]();d=null}),10)),b(t+": notify(category="+e+", value="+n+")"),g[e]=n;const s=f.subscribers[e];for(let e=0;e<s.length;e++)s[e](n)}),subscribe:function(e,n){if(b(t+": subscribe(category="+e+")"),!f.subscribers[e])return void console.error("Unable to subscribe - undefined consent category!");f.subscribers[e].push(n);const s=l(e);return null!==s&&n(s),()=>{f.subscribers[e]=f.subscribers[e].filter((e=>e!==n))}},subscribeAny:function(e){return b("subscribeAny"),a.push(e),()=>{a=a.filter((n=>n!==e))}},getCachedOrDefaultConsentsForPulse:function(n){return k(e,n)},isConsentedToAll:function(e){f.consentedCallback.push(e)},getConsentedToAllSync:function(){const e=l("_sp_user_consent_"+n.propertyId);try{var t;if(0===Object.keys(localStorage).filter((e=>e.startsWith("CMP:"))).length)return null;const n=null==(t=JSON.parse(e))||null==(t=t.gdpr)?void 0:t.consentStatus;if((null==n?void 0:n.vendorListAdditions)||(null==n?void 0:n.legalBasisChanges))return null;const s=null==n?void 0:n.consentedAll;return void 0===s?null:s}catch(e){return null}},onTcfReady:function(e){f.TCFCallbacks.push(e)}};var g;return e._SourcePoint_Consent_Callback=f.notify,f}const A=["Hermes","_app_","tv.nu","FinnApp","Omni"];!function(e,n){if(n.disableSentry)return;const t=e.createElement("script");C(t,{async:!0,type:"module",src:"https://static.privacy.schibsted.com/dist/index.js",fetchPriority:"low"}),e.head.appendChild(t)}(document,M),function(e,n){e._scc_=N(e,n)}(window,M),function(t,s,o,r,i){(function(e,t,s,o,r){var i,c,a;if(!o.baseEndpoint||!o.propertyId||!o.consentLanguage)throw new Error("One of missing: baseEndpoint, propertyId, consentLanguage");E(e,o),function(e){e.events=e.events||{},e.events.onError=function(n,t,s){try{n&&window._cmp_.sentry.setTag("message_type",n),t&&window._cmp_.sentry.setTag("errorCode",t),window._cmp_.sentry.setTag("propertyId",e.propertyId),window._cmp_.sentry.captureException(s)}catch(e){console.error("Can not send the error to Sentry, "+e)}},e.events.onMessageChoiceError=function(e,n){try{e&&window._cmp_.sentry.setTag("message_type",e),window._cmp_.sentry.captureException(n)}catch(n){console.error("Can not send the error to Sentry, "+n)}}}(o),o.accountId=o.accountId||1047,o.custom={},o.gdpr={includeTcfApi:"tcf"===r},o.propertyHref&&(o.joinHref=!0);const u=o.userId||(null==(i=n(e,o))?void 0:i._session.userId);u&&w(o,"loggedin","true");const l=o.clientId||(null==(c=n(e,o))?void 0:c.clientId);function p(){const e=/(_sch_cmp_[_a-z0-9]+)=(.+)/i;t.cookie.split(";").filter((n=>e.test(n))).map((e=>e.split("=").map((e=>e.trim())))).forEach((e=>{let[n,t]=e;return w(o,n,t)}))}(function(e){return A.some((n=>e.userAgent.includes(n)))})(s)&&w(o,"mobile-webview",!o.showInWebview||o.showInWebview);const d=null==t||null==(a=t.referrer)?void 0:a.split("/")[2];d&&w(o,"referrer-hostname",d);p(),o.propertyHref&&t.location.search&&-1===o.propertyHref.indexOf("?")&&(o.propertyHref+=t.location.search);const{pulseTracker:f,referrer:g,state:m,pulseObjectName:y,showInWebview:v,identityObject:b,identityObjectName:h,enableUserCentric:_,groupId:C}=o;e.psi=e.psi||{},e.psi.isLoggedInUser=!!u,e.psi.setUserId=P,e.psi.openPrivacySettings=I,e.psi.pulseTracker=f,e.psi.clientId=l,e.psi.referrer=g,e.psi.state=m,e.psi.pulseObjectName=y,e.psi.showInWebview=v,e.psi.identityObject=b,e.psi.identityObjectName=h,e.psi.triggerLogin=O,e.psi.enableUserCentric=_,e.psi.groupId=C})(t,s,o,r,i),function(e,n){e._sp_=e._sp_||{},e._sp_.config=n,b("Sourcepoint initiated with config: ",n)}(t,function(e,n){if(null==e)return{};var t={};for(var s in e)if({}.hasOwnProperty.call(e,s)){if(n.includes(s))continue;t[s]=e[s]}return t}(r,e))}(window,document,navigator,M,"scc"),b("dispatching cmp:ready event"),document.dispatchEvent(new Event("cmp:ready")),function(e,n){const t=e.baseEndpoint+"/unified/wrapperMessagingWithoutDetection.js",s=n.getElementsByTagName("script"),o=s[s.length-1],r=n.createElement("link");C(r,{rel:"preload",as:"script",crossorigin:"crossorigin",href:t}),n.head.appendChild(r);const i=n.createElement("script");C(i,{async:!0,src:t,onload:()=>(b("dispatching cmp:loaded event"),void document.dispatchEvent(new Event("cmp:loaded")))}),o.insertAdjacentElement("afterend",i)}(M,document)}();