Skip to content

Commit

Permalink
fix: add app client id uuid for mixpanel client id
Browse files Browse the repository at this point in the history
  • Loading branch information
jgresham committed Oct 4, 2023
1 parent 584e212 commit dad6b6f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ MP_PROJECT_ENV=<...>
NICENODE_ENV=development
```

`SENTRY_DSN` and `MP_PROJECT_TOKEN` should be fake unless testing. Contact Johns, @jgresham, if you want to test new error or event reporting code.

## Packaging for Production

To package apps for the local platform:
Expand Down
2 changes: 2 additions & 0 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
stopNodePackage,
} from './nodePackageManager';
import { checkPorts } from './ports';
import { getAppClientId } from './state/eventReporting';

// eslint-disable-next-line import/prefer-default-export
export const initialize = () => {
Expand Down Expand Up @@ -205,6 +206,7 @@ export const initialize = () => {
return getSetHasSeenAlphaModal(hasSeen);
});
ipcMain.handle('getSettings', getSettings);
ipcMain.handle('getAppClientId', getAppClientId);
ipcMain.handle('setLanguage', (_event, languageCode: string) => {
return setLanguage(languageCode);
});
Expand Down
1 change: 1 addition & 0 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ contextBridge.exposeInMainWorld('electron', {
getSetHasSeenAlphaModal: (hasSeen?: boolean) =>
ipcRenderer.invoke('getSetHasSeenAlphaModal', hasSeen),
getSettings: () => ipcRenderer.invoke('getSettings'),
getAppClientId: () => ipcRenderer.invoke('getAppClientId'),
setLanguage: (languageCode: string) => {
ipcRenderer.invoke('setLanguage', languageCode);
},
Expand Down
43 changes: 43 additions & 0 deletions src/main/state/eventReporting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { v4 as uuidv4 } from 'uuid';

import logger from '../logger';

import store from './store';

// export type EventReportingState = Record<string, string | object | boolean>;
const EVENT_REPORTING_KEY = 'eventReporting';
const CLIENT_ID_KEY = 'clientId';

export type EventReportingState = {
[CLIENT_ID_KEY]?: string;
};

/**
* Called on app launch.
* Initializes internal data structures for readiness.
*/
const initialize = () => {
logger.info('Intializing store settings key');
let eventReporting = store.get(EVENT_REPORTING_KEY);
if (!eventReporting || typeof eventReporting !== 'object') {
// create the default settings if no settings are saved yet
logger.info(
'No eventReporting state found. Creating a new instance and with a new clientId.',
);
eventReporting = {
[CLIENT_ID_KEY]: uuidv4(),
};
store.set(EVENT_REPORTING_KEY, eventReporting);
} else if (eventReporting?.[CLIENT_ID_KEY] === undefined) {
eventReporting = {
...eventReporting,
[CLIENT_ID_KEY]: uuidv4(),
};
store.set(EVENT_REPORTING_KEY, eventReporting);
}
};
initialize();

export const getAppClientId = (): boolean => {
return store.get(`${EVENT_REPORTING_KEY}.${CLIENT_ID_KEY}`);
};
3 changes: 3 additions & 0 deletions src/renderer/__mocks__/custom-preload-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ export const getSettings = () => {
appThemeSetting: 'auto',
};
};
export const getAppClientId = () => {
return 'random-unique-uuidv4';
};
export const getSetHasSeenSplashscreen = () => true;
export const getSetHasSeenAlphaModal = () => true;
export const getIsPodmanInstalled = () => true;
Expand Down
8 changes: 6 additions & 2 deletions src/renderer/events/reportEvent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import mixpanel from 'mixpanel-browser';

import { MP_PROJECT_ENV, MP_PROJECT_TOKEN } from './environment';
import { NNEvent } from './events';
import electron from '../electronGlobal';

/**
* Enable or disable remote event reporting service from in the front-end.
Expand Down Expand Up @@ -43,7 +45,7 @@ export const reportEvent = (
mixpanel.track(event, properties);
};

export const initialize = () => {
export const initialize = async () => {
if (MP_PROJECT_ENV === 'dev') {
return;
}
Expand All @@ -54,7 +56,9 @@ export const initialize = () => {
track_pageview: true,
persistence: 'localStorage',
});
mixpanel.identify('johns');
const appClientId = await electron.getAppClientId();
console.log('event reporting: appClientId: ', appClientId);
mixpanel.identify(appClientId);
} else {
console.error('MP_PROJECT_TOKEN not found!');
}
Expand Down
1 change: 1 addition & 0 deletions src/renderer/preload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ declare global {
getSetHasSeenSplashscreen(hasSeen?: boolean): boolean;
getSetHasSeenAlphaModal(hasSeen?: boolean): boolean;
getSettings(): Settings;
getAppClientId(): string;
setLanguage(languageCode: string): void;
setThemeSetting(theme: ThemeSetting): void;
setIsOpenOnStartup(isOpenOnStartup: boolean): void;
Expand Down

0 comments on commit dad6b6f

Please sign in to comment.