Skip to content

Commit

Permalink
Growthcode UserId: Bug fixes & Better Error Catching (prebid#9785)
Browse files Browse the repository at this point in the history
* Remove the cookie storage for data.

* Clean up error checking on return JSON data.

* Code cleanup
  • Loading branch information
southern-growthcode authored and jorgeluisrocha committed May 18, 2023
1 parent bac2986 commit 14abe1c
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions modules/growthCodeIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { submodule } from '../src/hook.js'
import {getStorageManager} from '../src/storageManager.js';
import {MODULE_TYPE_UID} from '../src/activities/modules.js';

const GCID_EXPIRY = 7;
const MODULE_NAME = 'growthCodeId';
const GC_DATA_KEY = '_gc_data';
const GCID_KEY = 'gcid';
const ENDPOINT_URL = 'https://p2.gcprivacy.com/v1/pb?'

export const storage = getStorageManager({ moduleType: MODULE_TYPE_UID, moduleName: MODULE_NAME });
Expand All @@ -26,14 +26,16 @@ export const storage = getStorageManager({ moduleType: MODULE_TYPE_UID, moduleNa
export function readData(key) {
try {
let payload
if (storage.cookiesAreEnabled()) {
payload = tryParse(storage.getCookie(key))
if (storage.cookiesAreEnabled(null)) {
payload = tryParse(storage.getCookie(key, null))
}
if (storage.hasLocalStorage()) {
payload = tryParse(storage.getDataFromLocalStorage(key))
payload = tryParse(storage.getDataFromLocalStorage(key, null))
}
if ((payload.expire_at !== undefined) && (payload.expire_at > (Date.now() / 1000))) {
return payload
if (payload !== undefined) {
if (payload.expire_at > (Date.now() / 1000)) {
return payload
}
}
} catch (error) {
logError(error);
Expand All @@ -51,12 +53,8 @@ function storeData(key, value) {
logInfo(MODULE_NAME + ': storing data: key=' + key + ' value=' + value);

if (value) {
if (storage.hasLocalStorage()) {
storage.setDataInLocalStorage(key, value);
}
const expiresStr = (new Date(Date.now() + (GCID_EXPIRY * (60 * 60 * 24 * 1000)))).toUTCString();
if (storage.cookiesAreEnabled()) {
storage.setCookie(key, value, expiresStr, 'LAX');
if (storage.hasLocalStorage(null)) {
storage.setDataInLocalStorage(key, value, null);
}
}
} catch (error) {
Expand All @@ -70,11 +68,15 @@ function storeData(key, value) {
* @param {object|null}
*/
function tryParse(data) {
let payload;
try {
return JSON.parse(data);
payload = JSON.parse(data);
if (payload == null) {
return undefined
}
return payload
} catch (err) {
logError(err);
return null;
return undefined;
}
}

Expand Down Expand Up @@ -150,6 +152,7 @@ export const growthCodeIdSubmodule = {
// If response is a valid json and should save is true
if (respJson) {
storeData(GC_DATA_KEY, JSON.stringify(respJson))
storeData(GCID_KEY, respJson.gc_id);
callback(respJson);
} else {
callback();
Expand Down

0 comments on commit 14abe1c

Please sign in to comment.