Skip to content

Commit 3718be9

Browse files
tchiotludoPiyush-r-bhaskar
authored andcommitted
refactor(ui): posthog as composable and option for ui telemetry
relate to kestra-io/kestra-ee#4831
1 parent 2df6c1b commit 3718be9

File tree

5 files changed

+62
-68
lines changed

5 files changed

+62
-68
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 & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
import VueTour from "./components/onboarding/VueTour.vue";
1919
import DefaultLayout from "override/components/layout/DefaultLayout.vue";
2020
import DocIdDisplay from "./components/DocIdDisplay.vue";
21-
import posthog from "posthog-js";
2221
import "@kestra-io/ui-libs/style.css";
2322
2423
import {useApiStore} from "./stores/api";
2524
import {usePluginsStore} from "./stores/plugins";
2625
import {useLayoutStore} from "./stores/layout";
2726
import {useCoreStore} from "./stores/core";
2827
import {useDocStore} from "./stores/doc";
28+
import {initPostHogForSetup} from "./composables/usePosthog";
2929
import {useMiscStore} from "./stores/misc";
3030
import {useExecutionsStore} from "./stores/executions";
3131
import * as BasicAuth from "./utils/basicAuth";
@@ -118,66 +118,10 @@
118118
uid: uid,
119119
});
120120
121-
this.apiStore.loadConfig()
122-
.then(apiConfig => {
123-
this.initStats(apiConfig, config, uid);
124-
})
121+
await initPostHogForSetup(config);
125122
126123
return config;
127124
},
128-
initStats(apiConfig, config, uid) {
129-
if (!this.configs || this.configs["isAnonymousUsageEnabled"] === false) {
130-
return;
131-
}
132-
133-
// only run posthog in production
134-
if (import.meta.env.MODE === "production") {
135-
posthog.init(
136-
apiConfig.posthog.token,
137-
{
138-
api_host: apiConfig.posthog.apiHost,
139-
ui_host: "https://eu.posthog.com",
140-
capture_pageview: false,
141-
capture_pageleave: true,
142-
autocapture: false,
143-
}
144-
)
145-
146-
posthog.register_once(this.statsGlobalData(config, uid));
147-
148-
if (!posthog.get_property("__alias")) {
149-
posthog.alias(apiConfig.id);
150-
}
151-
}
152-
153-
154-
let surveyVisible = false;
155-
window.addEventListener("PHSurveyShown", () => {
156-
surveyVisible = true;
157-
});
158-
159-
window.addEventListener("PHSurveyClosed", () => {
160-
surveyVisible = false;
161-
})
162-
163-
window.addEventListener("KestraRouterAfterEach", () => {
164-
if (surveyVisible) {
165-
window.dispatchEvent(new Event("PHSurveyClosed"))
166-
surveyVisible = false;
167-
}
168-
})
169-
},
170-
statsGlobalData(config, uid) {
171-
return {
172-
from: "APP",
173-
iid: config.uuid,
174-
uid: uid,
175-
app: {
176-
version: config.version,
177-
type: "OSS"
178-
}
179-
}
180-
},
181125
},
182126
watch: {
183127
$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 "../../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", {

ui/src/utils/setupPosthog.ts renamed to ui/src/composables/usePosthog.ts

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 ? {

webserver/src/main/java/io/kestra/webserver/controllers/api/MiscController.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public class MiscController {
6565
@io.micronaut.context.annotation.Value("${kestra.anonymous-usage-report.enabled}")
6666
protected Boolean isAnonymousUsageEnabled;
6767

68+
@io.micronaut.context.annotation.Value("${kestra.ui-anonymous-usage-report.enabled}")
69+
protected Boolean isUiAnonymousUsageEnabled;
70+
6871
@io.micronaut.context.annotation.Value("${kestra.environment.name}")
6972
@Nullable
7073
protected String environmentName;
@@ -94,12 +97,14 @@ public Configuration getConfiguration() throws JsonProcessingException { // Json
9497
Configuration.ConfigurationBuilder<?, ?> builder = Configuration
9598
.builder()
9699
.uuid(instanceService.fetch())
100+
.edition(Edition.OSS)
97101
.version(versionProvider.getVersion())
98102
.commitId(versionProvider.getRevision())
99103
.commitDate(versionProvider.getDate())
100104
.isCustomDashboardsEnabled(dashboardRepository.isEnabled())
101105
.isTaskRunEnabled(executionRepository.isTaskRunEnabled())
102106
.isAnonymousUsageEnabled(this.isAnonymousUsageEnabled)
107+
.isUiAnonymousUsageEnabled(this.isUiAnonymousUsageEnabled)
103108
.isTemplateEnabled(templateRepository.isPresent())
104109
.preview(Preview.builder()
105110
.initial(this.initialPreviewRows)
@@ -124,6 +129,11 @@ public Configuration getConfiguration() throws JsonProcessingException { // Json
124129
return builder.build();
125130
}
126131

132+
public enum Edition {
133+
OSS,
134+
EE
135+
}
136+
127137
@Get("/{tenant}/usages/all")
128138
@ExecuteOn(TaskExecutors.IO)
129139
@Operation(tags = {"Misc"}, summary = "Retrieve instance usage information")
@@ -158,6 +168,8 @@ public static class Configuration {
158168

159169
String version;
160170

171+
Edition edition;
172+
161173
String commitId;
162174

163175
ZonedDateTime commitDate;
@@ -171,6 +183,9 @@ public static class Configuration {
171183
@JsonInclude
172184
Boolean isAnonymousUsageEnabled;
173185

186+
@JsonInclude
187+
Boolean isUiAnonymousUsageEnabled;
188+
174189
@JsonInclude
175190
Boolean isTemplateEnabled;
176191

0 commit comments

Comments
 (0)