@m10s/cmp
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
2 lines (1 loc) • 13.6 kB
JavaScript
!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="__CONFIGURATION_PLACEHOLDER__",a=["Mixed","PSI","SCC","TCF","PMC"],c="default",u="cmp",l="cache",p="accepted",d="rejected",f="unknown",g=Object.freeze({purposes:{CMP_ANALYTICS:{optIn:!1,status:f},CMP_MARKETING:{optIn:!1,status:f},CMP_ADVERTISING:{optIn:!1,status:f},CMP_PERSONALIZATION:{optIn:!1,status:f}},source:c});function m(e,t,r,i){n("Pulse: "+r+"(actor, data):",t,i),v(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,m.pulseCommonData),provider:{component:"CMP-Marketplaces"},actor:t})}))}function v(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 y(){var e,n=null==(e=m.pulseCommonData)?void 0:e.messageName;if(n)return a.find((function(e){return n.includes(e)}))}function b(e){return"CMP_"+("personalisation"===e?"personalization":e).toUpperCase()}function h(e,n,t){var r=Object.keys(C(e)).filter((function(n){return C(e)[n]})),i=Object.keys(C(e)).filter((function(n){return!C(e)[n]}));return r.map((function(e){var t=n.purposes[b(e)];t.optIn=!0,t.status=p})),i.map((function(e){n.purposes[b(e)].status=d})),n.source=t,n}function C(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 w(e,n){var t=n.performance.now();return e?Math.round((t-e)/1e3):null}var _="CMP:retrieveMessage";function P(e){e.enableUserCentric&&o("CMP:isLoggedIn")&&!o(_)&&(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=/"}(_,!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 I=null,M=!1,O=!1;function j(e,n){if(n&&("object"!=typeof(t=n)||t.constructor!==Object))return console.error("Provided object is incorrect");var t;if(0===Object.keys(C(e)).length){if(!n)return g;var r=JSON.parse(JSON.stringify(n));return r.source=c,r}return h(e,JSON.parse(JSON.stringify(n||g)),l)}function S(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,l=!0,p={id:t.userId,realm:t.realm};t.events.onMessageReceiveData=function(e,r){n("Sourcepoint: onMessageReceiveData(data)",r),I=!0,m.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=y();if(n("Pulse: Detected message type: "+o),!I)return m(e,p,"View",{name:"CMP: Privacy Manager displayed",messageType:y()});r=e.performance.now(),m(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=w(r,e);m(e,p,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:I,messageType:y()},a&&{duration:a})),I=null}if(9===s){l=!1,I=null;var c=w(r,e);m(e,p,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:y()},c&&{duration:c}))}if(11===s&&(l=!0),12===s&&(I=!1),15===s){var u=w(r,e);m(e,p,"engagementEvent",Object.assign({name:"CMP: Dismiss message",elementType:"button",messageType:y()},u&&{duration:u}))}"function"==typeof o&&o(i,s)},t.events.onPrivacyManagerAction=function(e,t){n("Sourcepoint: onPrivacyManagerAction()"),M=!0,I=!1,"none"===t.purposeConsent?(O=!0,l=!1):(O=!1,l=!0),"function"==typeof a&&a()},t.events.onConsentReady=function(i,o,s,a){var d,f,b,_;P(t),null==(d=window._tcf_)||null==(f=d.consentedCallback)||f.map((function(e){e(a.consentedToAll)})),null==(b=window._tcf_)||null==(_=b.TCFCallbacks)||_.map((function(e){e()})),n("Sourcepoint: onConsentReady(message_type="+i+", consentUUID="+o+", euconsent)",s),n("Sourcepoint: getMessageType() "+y());var j=w(r,e);if(void 0!==y()||M){var S="PSI"===y()?null:C(e);if(function(e){var n=JSON.parse(JSON.stringify(g));v(e,(function(t){try{t.setConsents(h(e,n,u))}catch(e){console.error("Consents couldn't be set. The provided version of Pulse SDK does not support the setConsents event")}}))}(e),O&&m(e,p,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:y()},j&&{duration:j})),!l)return!1;m(e,p,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:y(),consentedToAll:null==a?void 0:a.consentedToAll,isFirstLayer:I||!!I},S&&{consents:S},j&&{duration:j})),I=null,M=!1,O=!1,"function"==typeof c&&c(i,o,s,a)}}}function T(e,t,r){var i;if(!t||!t.groupPmId)throw new Error("groupPmId is required");var o="CMP:advertising",s="CMP:analytics",a="CMP:marketing",c="CMP:personalisation",u="CMP:performance_marketing",l={};function p(t){return n(r+": getPermissionFromCache(category="+t+")"),e.localStorage.getItem(t)}l[o]=[],l[s]=[],l[a]=[],l[c]=[],l[u]=[];var d,f={subscribers:l,consentedCallback:[],TCFCallbacks:[],showPrivacyManager:function(){n("${type}: showPrivacyManager()"),m.pulseCommonData.messageName="",M=!0,I=!1,e._sp_.gdpr.loadPrivacyManagerModal()},getPermissionSync:function(e){return n(r+": getPermissionSync(category="+e+")"),p(e)},getPermission:function(e,t){n(r+": getPermission(category="+e+")");var i,o=f.subscribe(e,(i=!1,function(e){i?"function"==typeof o&&o():(i=!0,t(e),t=function(){})}))},notify:(i={},i[o]=p(o),i[s]=p(s),i[a]=p(a),i[c]=p(c),i[u]=p(u),d=i,function(e,t){if(d[e]!==t){n(r+": notify(category="+e+", value="+t+")"),d[e]=t;for(var i=f.subscribers[e],o=0;o<i.length;o++)i[o](t)}else n(r+": notify(category="+e+", value="+t+") - IGNORED")}),subscribe:function(e,t){if(n(r+": subscribe(category="+e+")"),f.subscribers[e]){f.subscribers[e].push(t);var i=p(e);return null!==i&&t(i),function(){f.subscribers[e]=f.subscribers[e].filter((function(e){return e!==t}))}}console.error("Unable to subscribe - undefined consent category!")},getCachedOrDefaultConsentsForPulse:function(n){return j(e,n)},isConsentedToAll:function(e){f.consentedCallback.push(e)},getConsentedToAllSync:function(){var e=p("_sp_user_consent_"+t.propertyId);try{var n,r;if(0===Object.keys(localStorage).filter((function(e){return e.startsWith("CMP:")})).length)return null;var i=null==(n=JSON.parse(e))||null==(r=n.gdpr)?void 0:r.consentStatus;if((null==i?void 0:i.vendorListAdditions)||(null==i?void 0:i.legalBasisChanges))return null;var o=null==i?void 0:i.consentedAll;return void 0===o?null:o}catch(e){return null}},onTcfReady:function(e){f.TCFCallbacks.push(e)}};return e._SourcePoint_Consent_Callback=f.notify,f}function k(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 N(e){var n=Array.from({length:20},(function(){return String.fromCharCode(Math.floor(25*Math.random()+97))})).join(""),t=btoa(JSON.stringify(n));k(e)&&k(e).login({state:t})}var E=function(e){if("number"==typeof e||void 0===e)return config.userId=e;throw new Error("Id value "+e+" is invalid")},A=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):v(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.")},R=function(){return N(window)},U=function(e,n,t){e.targetingParams=e.targetingParams||{},e.targetingParams[n]=t},D=["Hermes","_app_","tv.nu","FinnApp","Omni"];!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,s),function(e,n){e._scc_=T(e,n)}(window,s),function(t,r,i,o,s){(function(e,n,t,r,i){var o,s,a;if(!(r&&r.baseEndpoint&&r.propertyId&&r.consentLanguage))throw new Error("One of missing: baseEndpoint, propertyId, consentLanguage");S(e,r),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)}}}(r),r.accountId=r.accountId||1960,r.custom={},"psi"!==i&&(r.gdpr={includeTcfApi:"tcf"===i});r.propertyHref&&(r.joinHref=!0);var c=r.userId||(null==(o=k(e,r))?void 0:o._session.userId);c&&U(r,"loggedin","true");var u=r.clientId||(null==(s=k(window,r))?void 0:s.clientId);function l(){var e=/(_sch_cmp_[_a-z0-9]+)=(.+)/i;n.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 U(r,n,t)}))}(function(e){return D.some((function(n){return e.userAgent.includes(n)}))})(t)&&U(r,"mobile-webview",!r.showInWebview||r.showInWebview);var p=null==n||null==(a=n.referrer)?void 0:a.split("/")[2];p&&U(r,"referrer-hostname",p);l(),r.propertyHref&&n.location.search&&-1===r.propertyHref.indexOf("?")&&(r.propertyHref+=n.location.search);var d=r.pulseTracker,f=r.referrer,g=r.state,m=r.pulseObjectName,v=r.showInWebview,y=r.identityObject,b=r.identityObjectName,h=r.enableUserCentric,C=r.groupId;e.psi=e.psi||{},e.psi.isLoggedInUser=!!c,e.psi.setUserId=E,e.psi.openPrivacySettings=A,e.psi.pulseTracker=d,e.psi.clientId=u,e.psi.referrer=f,e.psi.state=g,e.psi.pulseObjectName=m,e.psi.showInWebview=v,e.psi.identityObject=y,e.psi.identityObjectName=b,e.psi.triggerLogin=R,e.psi.enableUserCentric=h,e.psi.groupId=C})(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,s,"scc"),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)}(s,document)}();