Skip to content

Commit

Permalink
fix missing session id
Browse files Browse the repository at this point in the history
  • Loading branch information
gatzjames committed May 8, 2024
1 parent cc421d1 commit 73d6e36
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions packages/insomnia/src/main/analytics.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 73d6e36

Please sign in to comment.