From d79d30d9403266029b0db9542c6775cb78a038da Mon Sep 17 00:00:00 2001 From: Joshua Goodwin Date: Mon, 29 May 2023 12:49:46 +0100 Subject: [PATCH] fix: cope with pubCommonUids being an empty list --- pubcid.js/docs/pubcid.min.js | 2 +- pubcid.js/docs/pubcid.min.js.map | 2 +- pubcid.js/src/index.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pubcid.js/docs/pubcid.min.js b/pubcid.js/docs/pubcid.min.js index 79d0a81..e93fb0e 100644 --- a/pubcid.js/docs/pubcid.min.js +++ b/pubcid.js/docs/pubcid.min.js @@ -1,2 +1,2 @@ -(()=>{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")}))}})})(); +(()=>{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.length?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 44b10f0..b55a863 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","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 +{"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","length","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,KAElC,IAAI0B,EAAcL,GAAiBA,EAAcM,OAASN,EAAc,GAAGO,KAAK,GAAGZ,GAAKf,IACxFE,OAAO0B,aAAaC,QAAQ9B,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.length ? 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 8720d7c..494e6cb 100644 --- a/pubcid.js/src/index.js +++ b/pubcid.js/src/index.js @@ -9,7 +9,7 @@ function collector() { const pubCommonUids = pbjs.getUserIdsAsEids().filter(function (eids) { return eids && eids.source == SOURCE; }); - let pubcidValue = pubCommonUids ? pubCommonUids[0].uids[0].id : generateUUID(); + let pubcidValue = pubCommonUids && pubCommonUids.length ? pubCommonUids[0].uids[0].id : generateUUID(); window.localStorage.setItem(SOURCE, pubcidValue); if (pubcidValue) { resolve(pubcidValue);