@schibsted/sourcepoint
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
2 lines (1 loc) • 9.17 kB
JavaScript
!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=["Mixed","PSI","SCC","TCF","PMC"],i="default",r="cmp",c="accepted",a="rejected",p="unknown",u=Object.freeze({purposes:{CMP_ANALYTICS:{optIn:!1,status:p},CMP_MARKETING:{optIn:!1,status:p},CMP_ADVERTISING:{optIn:!1,status:p},CMP_PERSONALIZATION:{optIn:!1,status:p}},source:i});function d(e,t,s,o){n("Pulse: "+s+"(actor, data):",t,o),l(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,d.pulseCommonData),provider:{component:"CMP"},actor:t})}))}function l(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=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 g(e){const n=JSON.parse(JSON.stringify(u));return l(e,(t=>{try{t.setConsents(function(e,n,t){const s=Object.keys(y(e)).filter((n=>y(e)[n])),o=Object.keys(y(e)).filter((n=>!y(e)[n]));return s.map((e=>{const t=n.purposes[m(e)];t.optIn=!0,t.status=c})),o.map((e=>{n.purposes[m(e)].status=a})),n.source=t,n}(e,n,r))}catch(e){console.error("Consents couldn't be set. The provided version of Pulse SDK does not support the setConsents event")}}))}function y(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 v(e,n){const t=n.performance.now();return e?Math.round((t-e)/1e3):null}let w=null,_=!1,b=!1;function h(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 C(e){const n=Array.from({length:20},(()=>String.fromCharCode(Math.floor(25*Math.random()+97)))).join(""),t=btoa(JSON.stringify(n));h(e)&&h(e).login({state:t})}const 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(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,(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 C(window)},j=function(e,n,t){e.targetingParams=e.targetingParams||{},e.targetingParams[n]=t},M=["Hermes","_app_","tv.nu","FinnApp","Omni"];const T="__CONFIGURATION_PLACEHOLDER__";!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,T),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),w=!0,d.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),!w)return d(e,u,"View",{name:"CMP: Privacy Manager displayed",messageType:f()});a=e.performance.now(),d(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=v(a,e);d(e,u,"engagementEvent",Object.assign({name:"CMP: Link to Privacy / Cookie Policy clicked",elementType:"anchor",isFirstLayer:w,messageType:f()},n&&{duration:n})),w=null}if(9===i){p=!1,w=null;const n=v(a,e);d(e,u,"engagementEvent",Object.assign({name:"CMP: Open Privacy Settings",elementType:"button",messageType:f()},n&&{duration:n}))}if(11===i&&(p=!0),12===i&&(w=!1),15===i){const n=v(a,e);d(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()"),_=!0,w=!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,l;null==(r=window._tcf_)||null==(r=r.consentedCallback)||r.map((e=>{e(i.consentedToAll)})),null==(l=window._tcf_)||null==(l=l.TCFCallbacks)||l.map((e=>{e()})),n("Sourcepoint: onConsentReady(message_type="+t+", consentUUID="+s+", euconsent)",o),n("Sourcepoint: getMessageType() "+f());const m=v(a,e);if(void 0===f()&&!_)return;const h="PSI"===f()?null:y(e);if(g(e),b&&d(e,u,"engagementEvent",Object.assign({name:"CMP: Reject all",elementType:"button",messageType:f()},m&&{duration:m})),!p)return!1;d(e,u,"engagementEvent",Object.assign({name:"CMP: Accept",elementType:"button",messageType:f(),consentedToAll:null==i?void 0:i.consentedToAll,isFirstLayer:w||!!w},h&&{consents:h},m&&{duration:m})),w=null,_=!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=h(e,o))?void 0:i._session.userId);a&&j(o,"loggedin","true");const p=o.clientId||(null==(r=h(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 j(o,n,t)}))}(function(e){return M.some((n=>e.userAgent.includes(n)))})(s)&&j(o,"mobile-webview",!o.showInWebview||o.showInWebview);const l=null==t||null==(c=t.referrer)?void 0:c.split("/")[2];l&&j(o,"referrer-hostname",l);u(),o.propertyHref&&t.location.search&&-1===o.propertyHref.indexOf("?")&&(o.propertyHref+=t.location.search);const{pulseTracker:m,referrer:C,state:T,pulseObjectName:S,showInWebview:k,identityObject:E,identityObjectName:N,enableUserCentric:A,groupId:R}=o;e.psi=e.psi||{},e.psi.isLoggedInUser=!!a,e.psi.setUserId=I,e.psi.openPrivacySettings=P,e.psi.pulseTracker=m,e.psi.clientId=p,e.psi.referrer=C,e.psi.state=T,e.psi.pulseObjectName=S,e.psi.showInWebview=k,e.psi.identityObject=E,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,T),dispatchCmpInitEvent()}();