Skip to content

Commit

Permalink
chore: hash userID on segment [INS-4260] (#7805)
Browse files Browse the repository at this point in the history
* chore: hash userID on segment [INS-4260]

* only hash once
  • Loading branch information
filfreire authored Aug 6, 2024
1 parent 4d5cd4d commit 0915d4d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 15 additions & 4 deletions packages/insomnia/src/main/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Analytics } from '@segment/analytics-node';
import crypto from 'crypto';
import { net } from 'electron';
import { v4 as uuidv4 } from 'uuid';

Expand Down Expand Up @@ -57,13 +58,20 @@ export enum SegmentEvent {
buttonClick = 'Button Clicked',
}

function hashString(input: string) {
return crypto.createHash('sha256').update(input).digest('hex');
}

export async function trackSegmentEvent(
event: SegmentEvent,
properties?: Record<string, any>,
) {
const settings = await models.settings.getOrCreate();
const userSession = await models.userSession.getOrCreate();
const allowAnalytics = settings.enableAnalytics || userSession?.accountId;
if (!userSession?.hashedAccountId) {
userSession.hashedAccountId = userSession?.accountId ? hashString(userSession.accountId) : '';
}
const allowAnalytics = settings.enableAnalytics || userSession?.hashedAccountId;
if (allowAnalytics) {
try {
const anonymousId = await getDeviceId() ?? '';
Expand All @@ -77,7 +85,7 @@ export async function trackSegmentEvent(
properties,
context,
anonymousId,
userId: userSession?.accountId || '',
userId: userSession?.hashedAccountId || '',
}, error => {
if (error) {
console.warn('[analytics] Error sending segment event', error);
Expand All @@ -92,8 +100,11 @@ export async function trackSegmentEvent(
export async function trackPageView(name: string) {
const settings = await models.settings.getOrCreate();
const userSession = await models.userSession.getOrCreate();
if (!userSession?.hashedAccountId) {
userSession.hashedAccountId = userSession?.accountId ? hashString(userSession.accountId) : '';
}

const allowAnalytics = settings.enableAnalytics || userSession?.accountId;
const allowAnalytics = settings.enableAnalytics || userSession?.hashedAccountId;
if (allowAnalytics) {
try {
const anonymousId = await getDeviceId() ?? '';
Expand All @@ -102,7 +113,7 @@ export async function trackPageView(name: string) {
os: { name: _getOsName(), version: process.getSystemVersion() },
};

analytics.page({ name, context, anonymousId, userId: userSession?.accountId }, error => {
analytics.page({ name, context, anonymousId, userId: userSession?.hashedAccountId }, error => {
if (error) {
console.warn('[analytics] Error sending segment event', error);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/insomnia/src/models/user-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ export interface BaseUserSession {
encPrivateKey: AESMessage;
};

export type UserSession = BaseModel & BaseUserSession;
export interface HashedUserSession {
hashedAccountId: string;
}

export type UserSession = BaseModel & BaseUserSession & HashedUserSession;
export const name = 'UserSession';
export const type = 'UserSession';
export const prefix = 'usr';
Expand Down

0 comments on commit 0915d4d

Please sign in to comment.