UNPKG

@m10s/cmp

Version:

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

2 lines (1 loc) 11.2 kB
!function(){var e=["jsSdkVersion","id","realm","clientId","pulseTracker","referrer","state","pulseObjectName","showInWebview","identityObject","identityObjectName","disableSentry","disableNativeConsentCheck","enableUserCentric","groupId"];function n(){var e;"undefined"!=typeof document&&document.location.href.indexOf("sp_debug=1")>0&&(e=console).debug.apply(e,["[CMP]"].concat(Array.prototype.slice.call(arguments)))}function t(e,n,t,r,i){var o=new URL(e);o.searchParams.append("utm_source","cmp"),o.searchParams.append("utm_medium","psi"),o.searchParams.append("referrer",r||window.location.href),t&&o.searchParams.append("client_id",t),n&&o.searchParams.append("environment_id",n),i&&o.searchParams.append("state",i),window.location.href=o.href}function r(e){return null===e?null:Boolean(parseInt(e))}var i=function(e,n){return Object.keys(n).filter((function(n){return void 0!==e[n]})).forEach((function(t){return"object"==typeof n[t]?Object.keys(n[t]).forEach((function(r){return e[t][r]=n[t][r]})):e[t]=n[t]}))};function o(e){for(var n=e+"=",t=decodeURIComponent(document.cookie).split(";"),r=0;r<t.length;r++){for(var i=t[r];" "==i.charAt(0);)i=i.substring(1);if(0==i.indexOf(n))return i.substring(n.length,i.length)}return""}var s=["Mixed","PSI","SCC","TCF","PMC"],a="default",c="cmp",u="accepted",p="rejected",d="unknown",l=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:a});function f(e,t,r,i){n("Pulse: "+r+"(actor, data):",t,i),m(e,(function(e){e&&"function"==typeof e.track&&e.track(r,{schema:"http://schema.schibsted.com/events/tracker-event.json/348.json",object:Object.assign({},i,f.pulseCommonData),provider:{component:"CMP-Marketplaces"},actor:t})}))}function m(e,n){if(e.psi.pulseTracker)return n(e.psi.pulseTracker);var t=e.psi.pulseObjectName||"pulse";return"function"!=typeof e[t]?console.error("No Pulse integration detected. Aborting."):e[t]((function(e){"function"==typeof n&&n(e)}))}function g(){var e,n=null==(e=f.pulseCommonData)?void 0:e.messageName;if(n)return s.find((function(e){return n.includes(e)}))}function v(e){return"CMP_"+("personalisation"===e?"personalization":e).toUpperCase()}function y(e){var n=JSON.parse(JSON.stringify(l));return m(e,(function(t){try{t.setConsents(function(e,n,t){var r=Object.keys(b(e)).filter((function(n){return b(e)[n]})),i=Object.keys(b(e)).filter((function(n){return!b(e)[n]}));return r.map((function(e){var t=n.purposes[v(e)];t.optIn=!0,t.status=u})),i.map((function(e){n.purposes[v(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 b(e){var n,t=r((e._scc_||e._tcf_).getPermissionSync("CMP:advertising")),i=r((e._scc_||e._tcf_).getPermissionSync("CMP:analytics")),o=r((e._scc_||e._tcf_).getPermissionSync("CMP:marketing")),s=r((e._scc_||e._tcf_).getPermissionSync("CMP:personalisation"));return n={advertising:t,analytics:i,marketing:o,personalisation:s},Object.entries(n).filter((function(e){return e[0],null!=e[1]})).reduce((function(e,n){var t,r=n[0],i=n[1];return Object.assign({},e,((t={})[r]=i,t))}),{})}function h(e,n){var t=n.performance.now();return e?Math.round((t-e)/1e3):null}var w="CMP:retrieveMessage";function C(e){e.enableUserCentric&&o("CMP:isLoggedIn")&&!o(w)&&(function(e,n,t){var r=new Date;r.setTime(r.getTime()+24*t*60*60*1e3);var i="expires="+r.toUTCString();document.cookie=e+"="+n+";"+i+";path=/"}(w,!0,400),function(e){var n,t,r,i,o=document.createElement("script");o.src="https://static.dev.cmp.m10s.io/dist/retrieve.js",document.body.appendChild(o);var s=document.createElement("retrieve-message");a=s,c=Object.assign({isWeb:!0,lang:e.consentLanguage},(null==(n=e.consentStatusMessage)?void 0:n.verticalPosition)&&{verticalPosition:null==(t=e.consentStatusMessage)?void 0:t.verticalPosition},(null==(r=e.consentStatusMessage)?void 0:r.horizontalPosition)&&{horizontalPosition:null==(i=e.consentStatusMessage)?void 0:i.horizontalPosition}),Object.entries(c).forEach((function(e){return a.setAttribute.apply(a,e)})),document.body.appendChild(s);var a,c}(e))}var _=null,P=!1,I=!1;function j(e,n){var t,r;if(null!=(t=e.psi)&&t.identityObject)return e.psi.identityObject;if(null!=n&&n.identityObject)return n.identityObject;var i=(null==(r=e.psi)?void 0:r.identityObjectName)||(null==n?void 0:n.identityObjectName)||"Identity";return e[i]?e[i]:console.warn("No Schibsted Account integration detected.")}function M(e){var n=Array.from({length:20},(function(){return String.fromCharCode(Math.floor(25*Math.random()+97))})).join(""),t=btoa(JSON.stringify(n));j(e)&&j(e).login({state:t})}var O=function(e){if("number"==typeof e||void 0===e)return config.userId=e;throw new Error("Id value "+e+" is invalid")},S=function(e){return function(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,(function(n){n.getEnvironmentId().then((function(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.")},T=function(){return M(window)},k=function(e,n,t){e.targetingParams=e.targetingParams||{},e.targetingParams[n]=t},E=["Hermes","_app_","tv.nu","FinnApp","Omni"];var N="__CONFIGURATION_PLACEHOLDER__";!function(e,n){if(!n.disableSentry){var t=e.createElement("script");i(t,{async:!0,type:"module",src:"https://static.pro.cmp.m10s.io/dist/index.js",fetchPriority:"low"}),e.head.appendChild(t)}}(document,N),function(t,r,i,o,s){(function(e,t,r,i,o){var s,a,c;if(!(i&&i.baseEndpoint&&i.propertyId&&i.consentLanguage))throw new Error("One of missing: baseEndpoint, propertyId, consentLanguage");(function(e,t){t.events=t.events||{};var r,i=t.events.onMessageReceiveData,o=t.events.onMessageChoiceSelect,s=t.events.onMessageReady,a=t.events.onPrivacyManagerAction,c=t.events.onConsentReady,u=!0,p={id:t.userId,realm:t.realm};t.events.onMessageReceiveData=function(e,r){n("Sourcepoint: onMessageReceiveData(data)",r),_=!0,f.pulseCommonData={"@id":"sdrn:privacy:cmp:"+(r.messageId||""),"@type":"CMP",messageName:r.msgDescription||"",propertyId:t.propertyId,messageId:r.messageId||void 0,bucket:r.bucket||void 0,jsSdkVersion:t.jsSdkVersion,partitionId:r.prtnUUID||"",language:t.consentLanguage},"function"==typeof i&&i(r)},t.events.onMessageReady=function(){var t,i;null==(t=window._tcf_)||null==(i=t.TCFCallbacks)||i.map((function(e){e()})),n("Sourcepoint: onMessageReady()");var o=g();if(n("Pulse: Detected message type: "+o),!_)return f(e,p,"View",{name:"CMP: Privacy Manager displayed",messageType:g()});r=e.performance.now(),f(e,p,"View",{name:"CMP: Message displayed",messageType:o}),"function"==typeof s&&s()},t.events.onMessageChoiceSelect=function(t,i,s){if(n("Sourcepoint: onMessageReady(choice_id="+i+", choice_type_id="+s+")"),5===s){var a=h(r,e);f(e,p,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:_,messageType:g()},a&&{duration:a})),_=null}if(9===s){u=!1,_=null;var c=h(r,e);f(e,p,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:g()},c&&{duration:c}))}if(11===s&&(u=!0),12===s&&(_=!1),15===s){var d=h(r,e);f(e,p,"engagementEvent",Object.assign({name:"CMP: Dismiss message",elementType:"button",messageType:g()},d&&{duration:d}))}"function"==typeof o&&o(i,s)},t.events.onPrivacyManagerAction=function(e,t){n("Sourcepoint: onPrivacyManagerAction()"),P=!0,_=!1,"none"===t.purposeConsent?(I=!0,u=!1):(I=!1,u=!0),"function"==typeof a&&a()},t.events.onConsentReady=function(i,o,s,a){var d,l,m,v;C(t),null==(d=window._tcf_)||null==(l=d.consentedCallback)||l.map((function(e){e(a.consentedToAll)})),null==(m=window._tcf_)||null==(v=m.TCFCallbacks)||v.map((function(e){e()})),n("Sourcepoint: onConsentReady(message_type="+i+", consentUUID="+o+", euconsent)",s),n("Sourcepoint: getMessageType() "+g());var w=h(r,e);if(void 0!==g()||P){var j="PSI"===g()?null:b(e);if(y(e),I&&f(e,p,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:g()},w&&{duration:w})),!u)return!1;f(e,p,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:g(),consentedToAll:null==a?void 0:a.consentedToAll,isFirstLayer:_||!!_},j&&{consents:j},w&&{duration:w})),_=null,P=!1,I=!1,"function"==typeof c&&c(i,o,s,a)}}})(e,i),function(e){e.events=e.events||{},e.events.onError=function(n,t,r){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(r)}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)}}}(i),i.accountId=i.accountId||1960,i.custom={},"psi"!==o&&(i.gdpr={includeTcfApi:"tcf"===o});i.propertyHref&&(i.joinHref=!0);var u=i.userId||(null==(s=j(e,i))?void 0:s._session.userId);u&&k(i,"loggedin","true");var p=i.clientId||(null==(a=j(window,i))?void 0:a.clientId);function d(){var e=/(_sch_cmp_[_a-z0-9]+)=(.+)/i;t.cookie.split(";").filter((function(n){return e.test(n)})).map((function(e){return e.split("=").map((function(e){return e.trim()}))})).forEach((function(e){var n=e[0],t=e[1];return k(i,n,t)}))}(function(e){return E.some((function(n){return e.userAgent.includes(n)}))})(r)&&k(i,"mobile-webview",!i.showInWebview||i.showInWebview);var l=null==t||null==(c=t.referrer)?void 0:c.split("/")[2];l&&k(i,"referrer-hostname",l);d(),i.propertyHref&&t.location.search&&-1===i.propertyHref.indexOf("?")&&(i.propertyHref+=t.location.search);var m=i.pulseTracker,v=i.referrer,w=i.state,M=i.pulseObjectName,N=i.showInWebview,A=i.identityObject,R=i.identityObjectName,U=i.enableUserCentric,L=i.groupId;e.psi=e.psi||{},e.psi.isLoggedInUser=!!u,e.psi.setUserId=O,e.psi.openPrivacySettings=S,e.psi.pulseTracker=m,e.psi.clientId=p,e.psi.referrer=v,e.psi.state=w,e.psi.pulseObjectName=M,e.psi.showInWebview=N,e.psi.identityObject=A,e.psi.identityObjectName=R,e.psi.triggerLogin=T,e.psi.enableUserCentric=U,e.psi.groupId=L})(t,r,i,o,s),o.jsSdkVersion,o.id,o.realm,o.clientId,o.pulseTracker,o.referrer,o.state,o.pulseObjectName,o.showInWebview,o.identityObject,o.identityObjectName,o.disableSentry,o.disableNativeConsentCheck,o.enableUserCentric,o.groupId,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 r in e)if({}.hasOwnProperty.call(e,r)){if(-1!==n.indexOf(r))continue;t[r]=e[r]}return t}(o,e))}(window,document,navigator,N,"psi"),function(e,n){var t=e.baseEndpoint+"/unified/wrapperMessagingWithoutDetection.js",r=n.getElementsByTagName("script"),o=r[r.length-1],s=n.createElement("link");i(s,{rel:"preload",as:"script",crossorigin:"crossorigin",href:t}),n.head.appendChild(s);var a=n.createElement("script");i(a,{async:!0,src:t}),o.insertAdjacentElement("afterend",a)}(N,document)}();