@m10s/cmp
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
2 lines (1 loc) • 9.74 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=["Mixed","PSI","SCC","TCF","PMC"],o="default",s="cmp",a="accepted",c="rejected",u="unknown",p=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 d(e,t,r,i){n("Pulse: "+r+"(actor, data):",t,i),l(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,d.pulseCommonData),provider:{component:"CMP-Marketplaces"},actor:t})}))}function l(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 f(){var e,n=null==(e=d.pulseCommonData)?void 0:e.messageName;if(n)return i.find((function(e){return n.includes(e)}))}function m(e){return"CMP_"+("personalisation"===e?"personalization":e).toUpperCase()}function g(e){var n=JSON.parse(JSON.stringify(p));return l(e,(function(t){try{t.setConsents(function(e,n,t){var r=Object.keys(y(e)).filter((function(n){return y(e)[n]})),i=Object.keys(y(e)).filter((function(n){return!y(e)[n]}));return r.map((function(e){var t=n.purposes[m(e)];t.optIn=!0,t.status=a})),i.map((function(e){n.purposes[m(e)].status=c})),n.source=t,n}(e,n,s))}catch(e){console.error("Consents couldn't be set. The provided version of Pulse SDK does not support the setConsents event")}}))}function y(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 v(e,n){var t=n.performance.now();return e?Math.round((t-e)/1e3):null}var w=null,b=!1,_=!1;function h(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 C(e){var n=Array.from({length:20},(function(){return String.fromCharCode(Math.floor(25*Math.random()+97))})).join(""),t=btoa(JSON.stringify(n));h(e)&&h(e).login({state:t})}var I=function(e){if("number"==typeof e||void 0===e)return config.userId=e;throw new Error("Id value "+e+" is invalid")},P=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):l(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.")},O=function(){return C(window)},j=function(e,n,t){e.targetingParams=e.targetingParams||{},e.targetingParams[n]=t},M=["Hermes","_app_","tv.nu","FinnApp","Omni"];var T="__CONFIGURATION_PLACEHOLDER__";!function(e,n){if(!n.disableSentry){var t,r,i=e.createElement("script");t=i,r={async:!0,type:"module",src:"https://static.pro.cmp.m10s.io/dist/index.js",fetchPriority:"low"},Object.keys(r).filter((function(e){return void 0!==t[e]})).forEach((function(e){return"object"==typeof r[e]?Object.keys(r[e]).forEach((function(n){return t[e][n]=r[e][n]})):t[e]=r[e]})),e.head.appendChild(i)}}(document,T),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),w=!0,d.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=f();if(n("Pulse: Detected message type: "+o),!w)return d(e,p,"View",{name:"CMP: Privacy Manager displayed",messageType:f()});r=e.performance.now(),d(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=v(r,e);d(e,p,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:w,messageType:f()},a&&{duration:a})),w=null}if(9===s){u=!1,w=null;var c=v(r,e);d(e,p,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:f()},c&&{duration:c}))}if(11===s&&(u=!0),12===s&&(w=!1),15===s){var l=v(r,e);d(e,p,"engagementEvent",Object.assign({name:"CMP: Dismiss message",elementType:"button",messageType:f()},l&&{duration:l}))}"function"==typeof o&&o(i,s)},t.events.onPrivacyManagerAction=function(e,t){n("Sourcepoint: onPrivacyManagerAction()"),b=!0,w=!1,"none"===t.purposeConsent?(_=!0,u=!1):(_=!1,u=!0),"function"==typeof a&&a()},t.events.onConsentReady=function(t,i,o,s){var a,l,m,h;null==(a=window._tcf_)||null==(l=a.consentedCallback)||l.map((function(e){e(s.consentedToAll)})),null==(m=window._tcf_)||null==(h=m.TCFCallbacks)||h.map((function(e){e()})),n("Sourcepoint: onConsentReady(message_type="+t+", consentUUID="+i+", euconsent)",o),n("Sourcepoint: getMessageType() "+f());var C=v(r,e);if(void 0!==f()||b){var I="PSI"===f()?null:y(e);if(g(e),_&&d(e,p,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:f()},C&&{duration:C})),!u)return!1;d(e,p,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:f(),consentedToAll:null==s?void 0:s.consentedToAll,isFirstLayer:w||!!w},I&&{consents:I},C&&{duration:C})),w=null,b=!1,_=!1,"function"==typeof c&&c(t,i,o,s)}}})(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=h(e,i))?void 0:s._session.userId);u&&j(i,"loggedin","true");var p=i.clientId||(null==(a=h(window,i))?void 0:a.clientId);function l(){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 j(i,n,t)}))}(function(e){return M.some((function(n){return e.userAgent.includes(n)}))})(r)&&j(i,"mobile-webview",!i.showInWebview||i.showInWebview);var m=null==t||null==(c=t.referrer)?void 0:c.split("/")[2];m&&j(i,"referrer-hostname",m);l(),i.propertyHref&&t.location.search&&-1===i.propertyHref.indexOf("?")&&(i.propertyHref+=t.location.search);var C=i.pulseTracker,T=i.referrer,S=i.state,k=i.pulseObjectName,N=i.showInWebview,E=i.identityObject,A=i.identityObjectName,R=i.enableUserCentric,L=i.groupId;e.psi=e.psi||{},e.psi.isLoggedInUser=!!u,e.psi.setUserId=I,e.psi.openPrivacySettings=P,e.psi.pulseTracker=C,e.psi.clientId=p,e.psi.referrer=T,e.psi.state=S,e.psi.pulseObjectName=k,e.psi.showInWebview=N,e.psi.identityObject=E,e.psi.identityObjectName=A,e.psi.triggerLogin=O,e.psi.enableUserCentric=R,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,r,i={},o=Object.keys(e);for(r=0;r<o.length;r++)t=o[r],n.indexOf(t)>=0||(i[t]=e[t]);return i}(o,e))}(window,document,navigator,T,"psi")}();