UNPKG

@schibsted/sourcepoint

Version:

Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP

2 lines (1 loc) 9.64 kB
!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 i=new URL(e);i.searchParams.append("utm_source","cmp"),i.searchParams.append("utm_medium","psi"),i.searchParams.append("referrer",s||window.location.href),t&&i.searchParams.append("client_id",t),n&&i.searchParams.append("environment_id",n),o&&i.searchParams.append("state",o),window.location.href=i.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 i=["Mixed","PSI","SCC","TCF","PMC"],r="default",c="cmp",a="accepted",p="rejected",u="unknown",d=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:r});function l(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,l.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 f(){var e;const n=null==(e=l.pulseCommonData)?void 0:e.messageName;if(n)return i.find((e=>n.includes(e)))}function g(e){return"CMP_"+("personalisation"===e?"personalization":e).toUpperCase()}function y(e){const n=JSON.parse(JSON.stringify(d));return m(e,(t=>{try{t.setConsents(function(e,n,t){const s=Object.keys(v(e)).filter((n=>v(e)[n])),o=Object.keys(v(e)).filter((n=>!v(e)[n]));return s.map((e=>{const t=n.purposes[g(e)];t.optIn=!0,t.status=a})),o.map((e=>{n.purposes[g(e)].status=p})),n.source=t,n}(e,n,c))}catch(e){console.error("Consents couldn't be set. The provided version of Pulse SDK does not support the setConsents event")}}))}function v(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")),i=s((e._scc_||e._tcf_).getPermissionSync("CMP:personalisation"));var r;return r={advertising:n,analytics:t,marketing:o,personalisation:i},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 w(e,n){const t=n.performance.now();return e?Math.round((t-e)/1e3):null}let _=null,h=!1,b=!1;function C(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 I(e){const n=Array.from({length:20},(()=>String.fromCharCode(Math.floor(25*Math.random()+97)))).join(""),t=btoa(JSON.stringify(n));C(e)&&C(e).login({state:t})}const P=function(e){if("number"==typeof e||void 0===e)return config.userId=e;throw new Error("Id value "+e+" is invalid")},j=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.")},O=function(){return I(window)},M=function(e,n,t){e.targetingParams=e.targetingParams||{},e.targetingParams[n]=t},T=["Hermes","_app_","tv.nu","FinnApp","Omni"];const S="__CONFIGURATION_PLACEHOLDER__";!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,S),function(t,s,o,i){(function(e,t,s,o){var i,r,c;if(!o.baseEndpoint||!o.propertyId||!o.consentLanguage)throw new Error("One of missing: baseEndpoint, propertyId, consentLanguage");(function(e,t){t.events=t.events||{};const s=t.events.onMessageReceiveData,o=t.events.onMessageChoiceSelect,i=t.events.onMessageReady,r=t.events.onPrivacyManagerAction,c=t.events.onConsentReady;let a,p=!0;const u={id:t.userId,realm:t.realm};t.events.onMessageReceiveData=function(e,o){n("Sourcepoint: onMessageReceiveData(data)",o),_=!0,l.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=f();if(n("Pulse: Detected message type: "+s),!_)return l(e,u,"View",{name:"CMP: Privacy Manager displayed",messageType:f()});a=e.performance.now(),l(e,u,"View",{name:"CMP: Message displayed",messageType:s}),"function"==typeof i&&i()},t.events.onMessageChoiceSelect=function(t,s,i){if(n("Sourcepoint: onMessageReady(choice_id="+s+", choice_type_id="+i+")"),5===i){const n=w(a,e);l(e,u,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:_,messageType:f()},n&&{duration:n})),_=null}if(9===i){p=!1,_=null;const n=w(a,e);l(e,u,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:f()},n&&{duration:n}))}if(11===i&&(p=!0),12===i&&(_=!1),15===i){const n=w(a,e);l(e,u,"engagementEvent",Object.assign({name:"CMP: Dismiss message",elementType:"button",messageType:f()},n&&{duration:n}))}"function"==typeof o&&o(s,i)},t.events.onPrivacyManagerAction=function(e,t){n("Sourcepoint: onPrivacyManagerAction()"),h=!0,_=!1,"none"===t.purposeConsent?(b=!0,p=!1):(b=!1,p=!0),"function"==typeof r&&r()},t.events.onConsentReady=function(t,s,o,i){var r,d;null==(r=window._tcf_)||null==(r=r.consentedCallback)||r.map((e=>{e(i.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() "+f());const m=w(a,e);if(void 0===f()&&!h)return;const g="PSI"===f()?null:v(e);if(y(e),b&&l(e,u,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:f()},m&&{duration:m})),!p)return!1;l(e,u,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:f(),consentedToAll:null==i?void 0:i.consentedToAll,isFirstLayer:_||!!_},g&&{consents:g},m&&{duration:m})),_=null,h=!1,b=!1,"function"==typeof c&&c(t,s,o,i)}})(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.propertyHref&&(o.joinHref=!0);const a=o.userId||(null==(i=C(e,o))?void 0:i._session.userId);a&&M(o,"loggedin","true");const p=o.clientId||(null==(r=C(e,o))?void 0:r.clientId);function u(){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 M(o,n,t)}))}(function(e){return T.some((n=>e.userAgent.includes(n)))})(s)&&M(o,"mobile-webview",!o.showInWebview||o.showInWebview);const d=null==t||null==(c=t.referrer)?void 0:c.split("/")[2];d&&M(o,"referrer-hostname",d);u(),o.propertyHref&&t.location.search&&-1===o.propertyHref.indexOf("?")&&(o.propertyHref+=t.location.search);const{pulseTracker:m,referrer:g,state:I,pulseObjectName:S,showInWebview:E,identityObject:k,identityObjectName:N,enableUserCentric:A,groupId:R}=o;e.psi=e.psi||{},e.psi.isLoggedInUser=!!a,e.psi.setUserId=P,e.psi.openPrivacySettings=j,e.psi.pulseTracker=m,e.psi.clientId=p,e.psi.referrer=g,e.psi.state=I,e.psi.pulseObjectName=S,e.psi.showInWebview=E,e.psi.identityObject=k,e.psi.identityObjectName=N,e.psi.triggerLogin=O,e.psi.enableUserCentric=A,e.psi.groupId=R})(t,s,o,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}(i,e))}(window,document,navigator,S),dispatchCmpInitEvent(),function(e,t){const s=e.baseEndpoint+"/unified/wrapperMessagingWithoutDetection.js",i=t.getElementsByTagName("script"),r=i[i.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")))}),r.insertAdjacentElement("afterend",a)}(S,document)}();