fix missing session id (#7389)

This commit is contained in:
James Gatz 2024-05-08 13:06:37 +02:00 committed by GitHub
parent cc421d1a3c
commit 40db09191b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,5 @@
import { Analytics } from '@segment/analytics-node';
import { BrowserWindow, net } from 'electron';
import { net } from 'electron';
import { v4 as uuidv4 } from 'uuid';
import {
@ -43,28 +43,13 @@ export enum SegmentEvent {
vcsAction = 'VCS Action Executed',
buttonClick = 'Button Clicked',
}
// TODO: migrating session state to main would be more suitable than doing this.
const getAccountIdFromRenderer = async () => {
let sessionId = '';
let accountId = '';
try {
const windows = BrowserWindow.getAllWindows();
const mainWindow = windows[0];
sessionId = await mainWindow.webContents.executeJavaScript('localStorage.getItem("currentSessionId");');
const sessionJSON = await mainWindow.webContents.executeJavaScript(`localStorage.getItem("session__${(sessionId).slice(0, 10)}");`);
const sessionData = JSON.parse(sessionJSON);
accountId = sessionData?.accountId ?? '';
} catch (e) {
return { sessionId, accountId };
}
return { sessionId, accountId };
};
export async function trackSegmentEvent(
event: SegmentEvent,
properties?: Record<string, any>,
) {
const settings = await models.settings.get();
const { accountId } = await getAccountIdFromRenderer();
const { accountId } = await models.userSession.get();
const allowAnalytics = settings.enableAnalytics || accountId;
if (allowAnalytics) {
@ -93,7 +78,7 @@ export async function trackSegmentEvent(
export async function trackPageView(name: string) {
const settings = await models.settings.get();
const { sessionId, accountId } = await getAccountIdFromRenderer();
const { id: sessionId, accountId } = await models.userSession.get();
const allowAnalytics = settings.enableAnalytics || accountId;
if (allowAnalytics) {
try {