Browse Source

make sentry userID anonymized

Abhinav-grd 4 years ago
parent
commit
39d8de6c6e

+ 2 - 3
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',

+ 2 - 0
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);

+ 3 - 3
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,

+ 2 - 1
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) => {

+ 21 - 0
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;
+}