diff --git a/pubcid.js/docs/index.html b/pubcid.js/docs/index.html index a3f177d..94d7518 100644 --- a/pubcid.js/docs/index.html +++ b/pubcid.js/docs/index.html @@ -10,6 +10,6 @@

Pubcid Collector Script Demo

const pubcid = googletag.encryptedSignalProviders.find(el => el.id === 'pubcid.org'); pubcid.collectorFunction(); -Cookie pubcid.org stored +Local Storage pubcid.org stored diff --git a/pubcid.js/docs/pubcid.min.js b/pubcid.js/docs/pubcid.min.js index 0a956b9..79d0a81 100644 --- a/pubcid.js/docs/pubcid.min.js +++ b/pubcid.js/docs/pubcid.min.js @@ -1,2 +1,2 @@ -(()=>{const o="pubcid.org";window.googletag=window.googletag||{cmd:[]},window.googletag.encryptedSignalProviders=window.googletag.encryptedSignalProviders||[],googletag.encryptedSignalProviders.push({id:"pubcid.org",collectorFunction:function(){return new Promise(((e,n)=>fetch("https://id.sharedid.org/pubcid").then((o=>o.json())).then((e=>document.cookie=o+"="+e[o])).then(e).catch(n)))}})})(); +(()=>{const o="pubcid.org";function e(o){return o?(o^(window&&window.crypto&&window.crypto.getRandomValues?crypto.getRandomValues(new Uint8Array(1))[0]%16:16*Math.random())>>o/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,e)}window.googletag=window.googletag||{cmd:[]},window.googletag.encryptedSignalProviders=window.googletag.encryptedSignalProviders||[],window.pbjs=window.pbjs||[],googletag.encryptedSignalProviders.push({id:o,collectorFunction:function(){return new Promise(((n,t)=>{const i=pbjs.getUserIdsAsEids().filter((function(e){return e&&e.source==o}));let r=i?i[0].uids[0].id:e();window.localStorage.setItem(o,r),r?n(r):t("Setting local storage failed")}))}})})(); //# sourceMappingURL=pubcid.min.js.map \ No newline at end of file diff --git a/pubcid.js/docs/pubcid.min.js.map b/pubcid.js/docs/pubcid.min.js.map index 269cb3a..44b10f0 100644 --- a/pubcid.js/docs/pubcid.min.js.map +++ b/pubcid.js/docs/pubcid.min.js.map @@ -1 +1 @@ -{"version":3,"sources":["webpack://pubcid.js/./src/index.js"],"names":["SOURCE","window","googletag","cmd","encryptedSignalProviders","push","id","collectorFunction","Promise","resolve","reject","fetch","then","response","json","data","document","cookie","catch"],"mappings":"MAAA,MAAMA,EAAS,aAGfC,OAAOC,UAAYD,OAAOC,WAAa,CAACC,IAAK,IAC7CF,OAAOC,UAAUE,yBAA2BH,OAAOC,UAAUE,0BAA4B,GAWzFF,UAAUE,yBAAyBC,KAAK,CACpCC,GAAI,aACJC,kBAXJ,WACI,OAAO,IAAIC,SAAQ,CAACC,EAASC,IAAWC,MANhC,kCAOHC,MAAKC,GAAYA,EAASC,SAC1BF,MAAKG,GAAQC,SAASC,OAASjB,EAAS,IAAMe,EAAKf,KACnDY,KAAKH,GACLS,MAAMR,S","file":"pubcid.min.js","sourcesContent":["const SOURCE = 'pubcid.org';\nconst URL = 'https://id.sharedid.org/pubcid';\n\nwindow.googletag = window.googletag || {cmd: []};\nwindow.googletag.encryptedSignalProviders = window.googletag.encryptedSignalProviders || [];\n\nfunction collector() {\n return new Promise((resolve, reject) => fetch(URL)\n .then(response => response.json())\n .then(data => document.cookie = SOURCE + '=' + data[SOURCE])\n .then(resolve)\n .catch(reject)\n );\n}\n\ngoogletag.encryptedSignalProviders.push({\n id: 'pubcid.org',\n collectorFunction: collector\n});\n"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack://pubcid.js/./src/index.js"],"names":["SOURCE","generateUUID","placeholder","window","crypto","getRandomValues","Uint8Array","Math","random","toString","replace","googletag","cmd","encryptedSignalProviders","pbjs","push","id","collectorFunction","Promise","resolve","reject","pubCommonUids","getUserIdsAsEids","filter","eids","source","pubcidValue","uids","localStorage","setItem"],"mappings":"MAAA,MAAMA,EAAS,aA2Bf,SAASC,EAAaC,GAClB,OAAOA,GACAA,GASHC,QAAUA,OAAOC,QAAUD,OAAOC,OAAOC,gBAClCD,OAAOC,gBAAgB,IAAIC,WAAW,IAAI,GAAK,GAE/B,GAAhBC,KAAKC,WAZwBN,EAAc,GAAGO,SAAS,KAC3D,CAAC,MAAQ,KAAO,KAAO,KAAO,MAAMC,QAAQ,SAAUT,GA5BjEE,OAAOQ,UAAYR,OAAOQ,WAAa,CAACC,IAAK,IAC7CT,OAAOQ,UAAUE,yBAA2BV,OAAOQ,UAAUE,0BAA4B,GACzFV,OAAOW,KAAQX,OAAOW,MAAQ,GAyC9BH,UAAUE,yBAAyBE,KAAK,CACpCC,GAAIhB,EACJiB,kBAzCJ,WACI,OAAO,IAAIC,SAAQ,CAACC,EAASC,KACzB,MAAMC,EAAgBP,KAAKQ,mBAAmBC,QAAO,SAAUC,GAC3D,OAAOA,GAAQA,EAAKC,QAAUzB,KAEnC,IAAI0B,EAAcL,EAAgBA,EAAc,GAAGM,KAAK,GAAGX,GAAKf,IAC/DE,OAAOyB,aAAaC,QAAQ7B,EAAQ0B,GAChCA,EACAP,EAAQO,GAERN,EAAO,uC","file":"pubcid.min.js","sourcesContent":["const SOURCE = 'pubcid.org';\n\nwindow.googletag = window.googletag || {cmd: []};\nwindow.googletag.encryptedSignalProviders = window.googletag.encryptedSignalProviders || [];\nwindow.pbjs = window.pbjs || [];\n\nfunction collector() {\n return new Promise((resolve, reject) => {\n const pubCommonUids = pbjs.getUserIdsAsEids().filter(function (eids) {\n return eids && eids.source == SOURCE;\n });\n let pubcidValue = pubCommonUids ? pubCommonUids[0].uids[0].id : generateUUID();\n window.localStorage.setItem(SOURCE, pubcidValue);\n if (pubcidValue) {\n resolve(pubcidValue);\n } else {\n reject(\"Setting local storage failed\");\n }\n });\n}\n\n/**\n * Returns a random v4 UUID of the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,\n * where each x is replaced with a random hexadecimal digit from 0 to f,\n * and y is replaced with a random hexadecimal digit from 8 to b.\n * https://gist.github.com/jed/982883 via node-uuid\n */\nfunction generateUUID(placeholder) {\n return placeholder\n ? (placeholder ^ getRandomData() >> placeholder / 4).toString(16)\n : ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, generateUUID);\n}\n\n/**\n * Returns random data using the Crypto API if available and Math.random if not\n * Method is from https://gist.github.com/jed/982883 like generateUUID, direct link https://gist.github.com/jed/982883#gistcomment-45104\n */\nfunction getRandomData() {\n if (window && window.crypto && window.crypto.getRandomValues) {\n return crypto.getRandomValues(new Uint8Array(1))[0] % 16;\n } else {\n return Math.random() * 16;\n }\n}\n\ngoogletag.encryptedSignalProviders.push({\n id: SOURCE,\n collectorFunction: collector\n});\n"],"sourceRoot":""} \ No newline at end of file diff --git a/pubcid.js/src/index.js b/pubcid.js/src/index.js index 571b503..8720d7c 100644 --- a/pubcid.js/src/index.js +++ b/pubcid.js/src/index.js @@ -2,33 +2,21 @@ const SOURCE = 'pubcid.org'; window.googletag = window.googletag || {cmd: []}; window.googletag.encryptedSignalProviders = window.googletag.encryptedSignalProviders || []; +window.pbjs = window.pbjs || []; function collector() { - let value = getCookie(); - if(value){ - return; - } - value = generateUUID(); - document.cookie = SOURCE + '=' + value; -} - -/** - * Returns the cookie - * @returns {string} - */ -function getCookie() { - let name = SOURCE + "="; - let ca = document.cookie.split(';'); - for(let i = 0; i < ca.length; i++) { - let c = ca[i]; - while (c.charAt(0) == ' ') { - c = c.substring(1); - } - if (c.indexOf(name) == 0) { - return c.substring(name.length, c.length); + return new Promise((resolve, reject) => { + const pubCommonUids = pbjs.getUserIdsAsEids().filter(function (eids) { + return eids && eids.source == SOURCE; + }); + let pubcidValue = pubCommonUids ? pubCommonUids[0].uids[0].id : generateUUID(); + window.localStorage.setItem(SOURCE, pubcidValue); + if (pubcidValue) { + resolve(pubcidValue); + } else { + reject("Setting local storage failed"); } - } - return ""; + }); } /** @@ -56,7 +44,6 @@ function getRandomData() { } googletag.encryptedSignalProviders.push({ - id: 'pubcid.org', + id: SOURCE, collectorFunction: collector }); -