Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Growthcode UserId: Bug fixes & Better Error Catching #9785

Merged
merged 3 commits into from
Apr 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions modules/growthCodeIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {ajax} from '../src/ajax.js';
import { submodule } from '../src/hook.js'
import { getStorageManager } from '../src/storageManager.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({ gvlid: undefined, moduleName: MODULE_NAME });
Expand All @@ -25,14 +25,16 @@ export const storage = getStorageManager({ gvlid: undefined, moduleName: MODULE_
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 @@ -50,12 +52,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 @@ -69,11 +67,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 @@ -149,6 +151,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