Skip to content

Commit 53b37a9

Browse files
authored
Merge pull request #10148 from gitbutlerapp/analytics-refactor
refactor: restructure analytics initialization
2 parents 35a8b1d + fe249e0 commit 53b37a9

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

apps/desktop/src/lib/analytics/analytics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import posthog from 'posthog-js';
66
export function initAnalyticsIfEnabled(appSettings: AppSettings, postHog: PostHogWrapper) {
77
if (import.meta.env.MODE === 'development') return;
88

9-
appSettings.appAnalyticsConfirmed.onDisk().then((confirmed) => {
9+
appSettings.appAnalyticsConfirmed.onDisk().then(async (confirmed) => {
1010
if (confirmed) {
1111
appSettings.appErrorReportingEnabled.onDisk().then((enabled) => {
1212
if (enabled) initSentry();
1313
});
14-
appSettings.appMetricsEnabled.onDisk().then(async (enabled) => {
14+
await appSettings.appMetricsEnabled.onDisk().then(async (enabled) => {
1515
if (enabled) {
1616
await postHog.init();
1717
}

apps/desktop/src/lib/bootstrap/deps.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ export function initDependencies(args: {
8888
backend: IBackend;
8989
appSettings: AppSettings;
9090
settingsService: SettingsService;
91+
posthog: PostHogWrapper;
92+
eventContext: EventContext;
9193
homeDir: string;
9294
}) {
93-
const { backend, settingsService, appSettings, homeDir } = args;
95+
const { backend, settingsService, appSettings, homeDir, posthog, eventContext } = args;
9496

9597
// ============================================================================
9698
// FOUNDATION LAYER - Core services that others depend on
@@ -105,9 +107,6 @@ export function initDependencies(args: {
105107
// ANALYTICS & TELEMETRY
106108
// ============================================================================
107109

108-
const eventContext = new EventContext();
109-
const posthog = new PostHogWrapper(settingsService, backend, eventContext);
110-
111110
// ============================================================================
112111
// AUTHENTICATION & SECURITY
113112
// ============================================================================

apps/desktop/src/routes/+layout.svelte

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
import SwitchThemeMenuAction from '$components/SwitchThemeMenuAction.svelte';
1515
import ToastController from '$components/ToastController.svelte';
1616
import ZoomInOutMenuAction from '$components/ZoomInOutMenuAction.svelte';
17-
import { initAnalyticsIfEnabled } from '$lib/analytics/analytics';
1817
import { POSTHOG_WRAPPER } from '$lib/analytics/posthog';
1918
import { initDependencies } from '$lib/bootstrap/deps';
20-
import { APP_SETTINGS } from '$lib/config/appSettings';
2119
import { SETTINGS_SERVICE } from '$lib/config/appSettingsV2';
2220
import { ircEnabled, ircServer, codegenEnabled } from '$lib/config/uiFeatureFlags';
2321
import { GITHUB_CLIENT } from '$lib/forge/github/githubClient';
@@ -47,11 +45,9 @@
4745
initDependencies(data);
4846
4947
const clientState = inject(CLIENT_STATE);
50-
const appSettings = inject(APP_SETTINGS);
5148
const posthog = inject(POSTHOG_WRAPPER);
5249
5350
clientState.initPersist();
54-
initAnalyticsIfEnabled(appSettings, posthog);
5551
5652
// =============================================================================
5753
// CORE REACTIVE STATE & EFFECTS

apps/desktop/src/routes/+layout.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { initAnalyticsIfEnabled } from '$lib/analytics/analytics';
2+
import { EventContext } from '$lib/analytics/eventContext';
3+
import { PostHogWrapper } from '$lib/analytics/posthog';
14
import createBackend from '$lib/backend';
25
import { loadAppSettings } from '$lib/config/appSettings';
36
import { SettingsService } from '$lib/config/appSettingsV2';
@@ -21,15 +24,20 @@ export const load: LayoutLoad = async () => {
2124

2225
// TODO: Migrate telemetry settings from here to `SettingsService`
2326
const appSettings = await loadAppSettings(backend);
27+
const eventContext = new EventContext();
2428

2529
// TODO: This should be the only settings service.
2630
const settingsService = new SettingsService(backend);
2731
await settingsService.refresh();
32+
const posthog = new PostHogWrapper(settingsService, backend, eventContext);
33+
initAnalyticsIfEnabled(appSettings, posthog);
2834

2935
return {
3036
homeDir,
3137
backend,
3238
settingsService,
33-
appSettings
39+
appSettings,
40+
posthog,
41+
eventContext
3442
};
3543
};

0 commit comments

Comments
 (0)