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