From 39d8de6c6eaf5343250fe3ed09ccb17b8ccacee7 Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Wed, 4 Aug 2021 12:34:49 +0530 Subject: [PATCH] make sentry userID anonymized --- sentry.client.config.js | 5 ++--- src/pages/index.tsx | 2 ++ src/utils/sentry/index.ts | 6 +++--- src/utils/storage/localStorage.ts | 3 ++- src/utils/user/index.ts | 21 +++++++++++++++++++++ 5 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 src/utils/user/index.ts diff --git a/sentry.client.config.js b/sentry.client.config.js index 0b58a12e8..228a35171 100644 --- a/sentry.client.config.js +++ b/sentry.client.config.js @@ -1,13 +1,12 @@ import * as Sentry from '@sentry/nextjs'; import { getSentryTunnelUrl } from 'utils/common/apiUtil'; -import { getData, LS_KEYS } from 'utils/storage/localStorage'; +import { getUserAnonymizedID } from 'utils/user'; const SENTRY_DSN = process.env.NEXT_PUBLIC_SENTRY_DSN ?? 'https://860186db60c54c7fbacfe255124958e8@errors.ente.io/4'; const SENTRY_ENV = process.env.NEXT_PUBLIC_SENTRY_ENV ?? 'development'; -const userID = getData(LS_KEYS.USER)?.id; -Sentry.setUser({ id: userID }); +Sentry.setUser({ id: getUserAnonymizedID() }); Sentry.init({ dsn: SENTRY_DSN, enabled: SENTRY_ENV !== 'development', diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 9ca7502f4..acb2d86ea 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -11,6 +11,7 @@ import SignUp from 'components/SignUp'; import constants from 'utils/strings/constants'; import localForage from 'utils/storage/localForage'; import IncognitoWarning from 'components/IncognitoWarning'; +import { logError } from 'utils/sentry'; const Container = styled.div` display: flex; @@ -110,6 +111,7 @@ export default function LandingPage() { try { await localForage.ready(); } catch (e) { + logError(e, 'usage in incognito mode tried'); setBlockUsage(true); } setLoading(false); diff --git a/src/utils/sentry/index.ts b/src/utils/sentry/index.ts index 42c182bdc..f4ffb5a5c 100644 --- a/src/utils/sentry/index.ts +++ b/src/utils/sentry/index.ts @@ -1,11 +1,11 @@ import * as Sentry from '@sentry/nextjs'; -import { getData, LS_KEYS } from 'utils/storage/localStorage'; +import { getUserAnonymizedID } from 'utils/user'; + export const logError = (e: any, msg?: string) => { - const userID = getData(LS_KEYS.USER)?.id; Sentry.captureException(e, { level: Sentry.Severity.Info, - user: { id: userID }, + user: { id: getUserAnonymizedID() }, contexts: { context: { message: msg, diff --git a/src/utils/storage/localStorage.ts b/src/utils/storage/localStorage.ts index 669dfff08..6892a3a74 100644 --- a/src/utils/storage/localStorage.ts +++ b/src/utils/storage/localStorage.ts @@ -10,7 +10,8 @@ export enum LS_KEYS { IS_FIRST_LOGIN = 'isFirstLogin', JUST_SIGNED_UP = 'justSignedUp', SHOW_BACK_BUTTON = 'showBackButton', - EXPORT = 'export' + EXPORT = 'export', + AnonymizeUserID='anonymizedUserID' } export const setData = (key: LS_KEYS, value: object) => { diff --git a/src/utils/user/index.ts b/src/utils/user/index.ts new file mode 100644 index 000000000..5c808f816 --- /dev/null +++ b/src/utils/user/index.ts @@ -0,0 +1,21 @@ +import { getData, LS_KEYS, setData } from 'utils/storage/localStorage'; + +export function makeID(length) { + let result = ''; + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + const charactersLength = characters.length; + for ( let i = 0; i < length; i++ ) { + result += characters.charAt(Math.floor(Math.random() * + charactersLength)); + } + return result; +} + +export function getUserAnonymizedID() { + let anonymizeUserID = getData(LS_KEYS.AnonymizeUserID)?.id; + if (!anonymizeUserID) { + anonymizeUserID=makeID(6); + setData(LS_KEYS.AnonymizeUserID, { id: anonymizeUserID }); + } + return anonymizeUserID; +}