UNPKG

@airgrid/edgekit

Version:

A privacy focused library for cookie-less audience creation.

3 lines (2 loc) 7.5 kB
function e(){return(e=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var t,n,r=function(){return Math.round(Date.now()/1e3)},i=function(e,t){try{var n=JSON.stringify(t);localStorage.setItem(e,n)}catch(e){}},u=function(e){var t=localStorage.getItem(e);if(t)try{return JSON.parse(t)}catch(e){return}};!function(e){e.COSINE_SIMILARITY="cosineSimilarity",e.LOGISTIC_REGRESSION="logisticRegression"}(t||(t={})),function(e){e.PAGE_VIEWS="edkt_page_views",e.MATCHED_AUDIENCES="edkt_matched_audiences",e.MATCHED_AUDIENCE_IDS="edkt_matched_audience_ids",e.CACHED_AUDIENCES="edkt_cached_audiences",e.CACHED_AUDIENCE_META_DATA="edkt_cached_audience_meta_data"}(n||(n={}));var c=new(function(){function t(){this.pageViews=[],this.storageSize=300,this._load()}var c=t.prototype;return c._load=function(){this.pageViews=u(n.PAGE_VIEWS)||[]},c._save=function(){i(n.PAGE_VIEWS,this.pageViews)},c._trim=function(){this.pageViews.length<=this.storageSize||(this.pageViews.sort(function(e,t){return t.ts-e.ts}),this.pageViews=this.pageViews.slice(0,this.storageSize))},c.setStorageSize=function(e){!e||e<0||(this.storageSize=e)},c.savePageView=function(t,n){if(t&&!(Object.keys(t).length<1)){var i=e({ts:r(),features:t},n);this.pageViews.push(i),this._trim(),this._save()}},c.getCopyOfPageViews=function(){return[].concat(this.pageViews)},t}()),o=new(function(){function e(){this.matchedAudiences={},this.matchedAudienceIds=[],this.unsetDueToVersionIncAudienceIds=[],this.storeLoadedAt=r(),this._load()}var t=e.prototype;return t._save=function(){i(n.MATCHED_AUDIENCES,this.matchedAudiences),i(n.MATCHED_AUDIENCE_IDS,this.matchedAudienceIds)},t._hasAudienceExpired=function(e){return e<this.storeLoadedAt},t._unsetAudience=function(e){delete this.matchedAudiences[e]},t._updatePageViewFlag=function(e,t){this.matchedAudiences[e].matchedOnCurrentPageView=t},t._load=function(){var e=this,t=u(n.MATCHED_AUDIENCES)||{};Array.isArray(t)&&(function(e){try{localStorage.removeItem(e)}catch(e){}}(n.MATCHED_AUDIENCES),t=t.reduce(function(e,t){e[t.id]=t},{})),this.matchedAudiences=t,Object.entries(t).forEach(function(t){var n=t[0];e._hasAudienceExpired(t[1].expiresAt)?e._unsetAudience(n):e._updatePageViewFlag(n,!1)}),this.matchedAudienceIds=Object.keys(this.matchedAudiences),this._save()},t.unsetAudiencesIfVersionIncremented=function(e){var t=this;e.forEach(function(e){var n=t.matchedAudiences[e.id]?t.matchedAudiences[e.id].version:null;n&&n<e.version&&(t._unsetAudience(e.id),t.unsetDueToVersionIncAudienceIds.push(e.id))})},t.isMatched=function(e,t){return!!(this.matchedAudiences[e]&&this.matchedAudiences[e].version>=t)},t.setAudiences=function(e){var t=this;e.forEach(function(e){t.matchedAudiences[e.id]=e,t.unsetDueToVersionIncAudienceIds.includes(e.id)&&t._updatePageViewFlag(e.id,!1)}),this.matchedAudienceIds=Object.keys(this.matchedAudiences),this._save()},t.getMatchedAudiences=function(){return Object.entries(this.matchedAudiences).map(function(e){return e[1]})},e}()),s={__proto__:null,count:function(){return function(e){return e.length}}},a={__proto__:null,eq:function(e){return function(t){return e===t}},gt:function(e){return function(t){return t>e}},lt:function(e){return function(t){return t<e}},ge:function(e){return function(t){return t>=e}},le:function(e){return function(t){return t<=e}}},d=function(e){return Math.sqrt(e.reduce(function(e,t){return e+Math.pow(t,2)},0))},f=function(e,t){return e.reduce(function(e,n,r){return e+n*t[r]},0)},h=function(e){return e instanceof Array&&e.every(function(e){return"number"==typeof e})},l=function(e,n){return function(e){return e.queryFilterComparisonType===t.COSINE_SIMILARITY&&h((n=e.queryValue).vector)&&"number"==typeof n.threshold;var n}(e)&&h(n.value)&&function(e,t){return t.length===e.vector.length&&((i=(f(n=t,r=e.vector)/(d(n)*d(r))- -.5)/1.4)>=1?1:i<=0?0:i)>e.threshold;var n,r,i}(e.queryValue,n.value)},v=function(e,n){return function(e){return e.queryFilterComparisonType===t.LOGISTIC_REGRESSION&&h((n=e.queryValue).vector)&&"number"==typeof n.threshold&&"number"==typeof n.bias;var n}(e)&&h(n.value)&&function(e,t){return t.length===e.vector.length&&(n=f(e.vector,t)+e.bias,1/(1+Math.exp(-n))>e.threshold);var n}(e.queryValue,n.value)},m=1e4,_=function(e,t){var n=t.vendor;return!!n&&e.length>0&&e.every(function(e){return!!n.consents[e]})},g=function(e){return new Promise(function(t){e.listenerId&&window.__tcfapi("removeEventListener",2,function(e){return t(e)},e.listenerId)})},A=function(){return Promise.race([new Promise(function(e){var t=null;t=window.setInterval(function(){window.__tcfapi&&(t&&window.clearInterval(t),e(!0))},100)}),new Promise(function(e,t){setTimeout(function(){return t(new Error("TCF API is missing"))},1e4)})])},p=function(e){void 0===e&&(e=[]);try{return Promise.resolve(A()).then(function(){return new Promise(function(t,n){window.__tcfapi?window.__tcfapi("addEventListener",2,function(n,r){if(r&&"loaded"===n.cmpStatus){var i=_(e,n);t({eventStatus:n.eventStatus,hasConsent:i}),g(n)}}):n(new Error("TCF API is missing"))})})}catch(e){return Promise.reject(e)}},I=function(e){void 0===e&&(e=[]);try{return Promise.resolve(A()).then(function(){return new Promise(function(t,n){window.__tcfapi?window.__tcfapi("addEventListener",2,function(n,r){!r||"loaded"!==n.cmpStatus||"tcloaded"!==n.eventStatus&&"useractioncomplete"!==n.eventStatus||(t(_(e,n)),g(n))}):n(new Error("TCF API is missing"))})})}catch(e){return Promise.reject(e)}},w=function(e,t,n){void 0===n&&(n=!1);try{var r=function(){return Promise.resolve(t())},i=function(){if(!n)return Promise.resolve(I(e)).then(function(){})}();return Promise.resolve(i&&i.then?i.then(r):r())}catch(e){return Promise.reject(e)}},E={run:function(e){try{var t,n=function(e){if(t)return e;c.setStorageSize(m),c.savePageView(u,d);var n=c.getCopyOfPageViews();o.unsetAudiencesIfVersionIncremented(h);var i=function(e,t){var n=r();return e.reduce(function(e,r){var i;return function(e,t){try{var n=e.filter,r=e.rules;if(0===t.length||0===n.queries.length)return!1;var i=t.filter(function(e){return n.queries.some(function(t){return function(e,t){return!(!t||!function(e,t){return t.version===e.featureVersion}(e,t))&&[l,v].some(function(n){return n(e,t)})}(t,e.features[t.queryProperty])})});return r.every(function(e){var t=s[e.reducer.name]()(i);return a[e.matcher.name](e.matcher.args)(t)})}catch(e){return!1}}({filter:{any:!1,queries:(i=r).definition},rules:[{reducer:{name:"count"},matcher:{name:"gt",args:i.occurrences}}]},t.filter(function(e){return 0===r.lookBack||e.ts>n-r.lookBack}))?[].concat(e,[{id:r.id,version:r.version,matchedAt:n,expiresAt:n+r.ttl,matchedOnCurrentPageView:!0}]):e},[])}(h.filter(function(e){return!o.isMatched(e.id,e.version)}),n);o.setAudiences(i)},i=e.vendorIds,u=e.pageFeatures,d=e.pageMetadata,f=e.omitGdprConsent,h=e.audienceDefinitions,m=e.featureStorageSize,_=function(){if(!f)return Promise.resolve(I(i)).then(function(e){e||(t=1)})}();return Promise.resolve(_&&_.then?_.then(n):n(_))}catch(e){return Promise.reject(e)}},getMatchedAudiences:function(){return o.getMatchedAudiences()},getCopyOfPageViews:function(){return c.getCopyOfPageViews()}};export{t as QueryFilterComparisonType,n as StorageKeys,m as WAIT_FOR_TCF_API,p as checkConsentStatus,E as edkt,o as matchedAudienceStore,w as runOnConsent,c as viewStore,I as waitForConsent}; //# sourceMappingURL=edgekit.min.esm.js.map