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