Skip to content

Commit b45c0b1

Browse files
committed
refactor(ui): posthog as composable and option for ui telemetry
relate to kestra-io/kestra-ee#4831
1 parent aed055d commit b45c0b1

File tree

5 files changed

+105
-108
lines changed

5 files changed

+105
-108
lines changed

cli/src/main/resources/application.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ kestra:
235235
traces:
236236
root: DISABLED
237237

238+
ui-anonymous-usage-report:
239+
enabled: true
240+
238241
anonymous-usage-report:
239242
enabled: true
240243
uri: https://api.kestra.io/v1/reports/server-events

ui/src/App.vue

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
import VueTour from "./components/onboarding/VueTour.vue";
1818
import DefaultLayout from "override/components/layout/DefaultLayout.vue";
1919
import DocIdDisplay from "./components/DocIdDisplay.vue";
20-
import posthog from "posthog-js";
2120
import "@kestra-io/ui-libs/style.css";
2221
2322
import {useApiStore} from "./stores/api";
2423
import {usePluginsStore} from "./stores/plugins";
2524
import {useLayoutStore} from "./stores/layout";
2625
import {useCoreStore} from "./stores/core";
2726
import {useDocStore} from "./stores/doc";
27+
import {initPostHogForSetup} from "./composables/usePosthog";
2828
import {useMiscStore} from "override/stores/misc";
2929
import {useExecutionsStore} from "./stores/executions";
3030
import * as BasicAuth from "./utils/basicAuth";
@@ -116,63 +116,10 @@
116116
uid: uid,
117117
});
118118
119-
const apiConfig = await this.apiStore.loadConfig();
120-
this.initStats(apiConfig, config, uid);
119+
await initPostHogForSetup(config);
121120
122121
return config;
123122
},
124-
initStats(apiConfig, config, uid) {
125-
if (this.miscStore.configs["isAnonymousUsageEnabled"] === false) {
126-
return;
127-
}
128-
129-
// only run posthog in production
130-
if (import.meta.env.MODE === "production") {
131-
posthog.init(
132-
apiConfig.posthog.token,
133-
{
134-
api_host: apiConfig.posthog.apiHost,
135-
ui_host: "https://eu.posthog.com",
136-
capture_pageview: false,
137-
capture_pageleave: true,
138-
autocapture: false,
139-
}
140-
)
141-
142-
posthog.register_once(this.statsGlobalData(config, uid));
143-
144-
if (!posthog.get_property("__alias")) {
145-
posthog.alias(apiConfig.id);
146-
}
147-
}
148-
149-
let surveyVisible = false;
150-
window.addEventListener("PHSurveyShown", () => {
151-
surveyVisible = true;
152-
});
153-
154-
window.addEventListener("PHSurveyClosed", () => {
155-
surveyVisible = false;
156-
})
157-
158-
window.addEventListener("KestraRouterAfterEach", () => {
159-
if (surveyVisible) {
160-
window.dispatchEvent(new Event("PHSurveyClosed"))
161-
surveyVisible = false;
162-
}
163-
})
164-
},
165-
statsGlobalData(config, uid) {
166-
return {
167-
from: "APP",
168-
iid: config.uuid,
169-
uid: uid,
170-
app: {
171-
version: config.version,
172-
type: "OSS"
173-
}
174-
}
175-
},
176123
},
177124
watch: {
178125
$route: {

ui/src/components/basicauth/BasicAuthSetup.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
import MailChecker from "mailchecker"
209209
import {useMiscStore} from "override/stores/misc"
210210
import {useSurveySkip} from "../../composables/useSurveyData"
211-
import {initPostHogForSetup, trackSetupEvent} from "../../utils/setupPosthog"
211+
import {initPostHogForSetup, trackSetupEvent} from "../../composables/usePosthog"
212212
213213
import Cogs from "vue-material-design-icons/Cogs.vue"
214214
import AccountPlus from "vue-material-design-icons/AccountPlus.vue"
@@ -312,9 +312,9 @@
312312
const setupConfigurationLines = computed<ConfigLine[]>(() => {
313313
if (!setupConfiguration.value) return []
314314
const configs = miscStore.configs
315-
315+
316316
const basicAuthValue = activeStep.value >= 1 || configs?.isBasicAuthInitialized
317-
317+
318318
return [
319319
{name: "repository", icon: Database, value: setupConfiguration.value.repositoryType || "NOT SETUP"},
320320
{name: "queue", icon: CurrentDc, value: setupConfiguration.value.queueType || "NOT SETUP"},
@@ -420,9 +420,9 @@
420420
user_email: userFormData.value.username
421421
}, userFormData.value)
422422
423-
423+
424424
localStorage.setItem("basicAuthUserCreated", "true")
425-
425+
426426
nextStep()
427427
} catch (error: any) {
428428
trackSetupEvent("setup_flow:account_creation_failed", {
Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,31 @@ interface UserFormData {
1616

1717
interface Config {
1818
isAnonymousUsageEnabled?: boolean
19+
isUiAnonymousUsageEnabled?: boolean
1920
uuid?: string
2021
version?: string
22+
edition?: string
23+
}
24+
25+
function statsGlobalData(config: Config, uid: string): any {
26+
return {
27+
from: "APP",
28+
iid: config.uuid,
29+
uid: uid,
30+
app: {
31+
version: config.version,
32+
type: config.edition
33+
}
34+
}
2135
}
2236

2337
export async function initPostHogForSetup(config: Config): Promise<void> {
2438
try {
25-
if (!config?.isAnonymousUsageEnabled) return
39+
if (!config.isUiAnonymousUsageEnabled) return
2640

2741
const apiStore = useApiStore()
2842
const apiConfig = await apiStore.loadConfig()
29-
43+
3044
if (!apiConfig?.posthog?.token || (window as any).posthog?.__loaded) return
3145

3246
const uid = getUID()
@@ -42,22 +56,40 @@ export async function initPostHogForSetup(config: Config): Promise<void> {
4256
autocapture: false,
4357
})
4458

59+
posthog.register_once(statsGlobalData(config, uid));
60+
4561
if (!posthog.get_property("__alias")) {
4662
posthog.alias(apiConfig.id)
4763
}
64+
65+
let surveyVisible = false;
66+
window.addEventListener("PHSurveyShown", () => {
67+
surveyVisible = true;
68+
});
69+
70+
window.addEventListener("PHSurveyClosed", () => {
71+
surveyVisible = false;
72+
})
73+
74+
window.addEventListener("KestraRouterAfterEach", () => {
75+
if (surveyVisible) {
76+
window.dispatchEvent(new Event("PHSurveyClosed"))
77+
surveyVisible = false;
78+
}
79+
})
4880
} catch (error) {
4981
console.error("Failed to initialize PostHog:", error)
5082
}
5183
}
5284

5385
export function trackSetupEvent(
54-
eventName: string,
55-
additionalData: Record<string, any>,
86+
eventName: string,
87+
additionalData: Record<string, any>,
5688
userFormData: UserFormData
5789
): void {
5890
const miscStore = useMiscStore()
5991
const uid = getUID()
60-
92+
6193
if (!miscStore.configs?.isAnonymousUsageEnabled || !uid) return
6294

6395
const userInfo = userFormData.firstName ? {

0 commit comments

Comments
 (0)