Skip to content

Commit 350c9f7

Browse files
authored
chore: maintain app distinct_id on factory reset (#4541)
1 parent 144b836 commit 350c9f7

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type AppConfiguration = {
22
data_folder: string
33
quick_ask: boolean
4+
distinct_id?: string
45
}

web/containers/Layout/index.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import ImportModelOptionModal from '@/screens/Settings/ImportModelOptionModal'
2626
import ImportingModelModal from '@/screens/Settings/ImportingModelModal'
2727
import SelectingModelModal from '@/screens/Settings/SelectingModelModal'
2828

29+
import { getAppDistinctId, updateDistinctId } from '@/utils/settings'
30+
2931
import LoadingModal from '../LoadingModal'
3032

3133
import MainViewContainer from '../MainViewContainer'
@@ -93,8 +95,16 @@ const BaseLayout = () => {
9395
return properties
9496
},
9597
})
96-
posthog.opt_in_capturing()
97-
posthog.register({ app_version: VERSION })
98+
// Attempt to restore distinct Id from app global settings
99+
getAppDistinctId()
100+
.then((id) => {
101+
if (id) posthog.identify(id)
102+
})
103+
.finally(() => {
104+
posthog.opt_in_capturing()
105+
posthog.register({ app_version: VERSION })
106+
updateDistinctId(posthog.get_distinct_id())
107+
})
98108
} else {
99109
posthog.opt_out_capturing()
100110
}

web/hooks/useFactoryReset.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export default function useFactoryReset() {
5858
const configuration: AppConfiguration = {
5959
data_folder: defaultJanDataFolder,
6060
quick_ask: appConfiguration?.quick_ask ?? false,
61+
distinct_id: appConfiguration?.distinct_id,
6162
}
6263
await window.core?.api?.updateAppConfiguration(configuration)
6364
}

web/utils/settings.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { AppConfiguration } from '@janhq/core'
2+
3+
/**
4+
* Update app distinct Id
5+
* @param id
6+
*/
7+
export const updateDistinctId = async (id: string) => {
8+
const appConfiguration: AppConfiguration =
9+
await window.core?.api?.getAppConfigurations()
10+
appConfiguration.distinct_id = id
11+
await window.core?.api?.updateAppConfiguration(appConfiguration)
12+
}
13+
14+
/**
15+
* Retrieve app distinct Id
16+
* @param id
17+
*/
18+
export const getAppDistinctId = async (): Promise<string | undefined> => {
19+
const appConfiguration: AppConfiguration =
20+
await window.core?.api?.getAppConfigurations()
21+
return appConfiguration.distinct_id
22+
}

0 commit comments

Comments
 (0)