Skip to content

Commit

Permalink
Merge pull request #2078 from RitvikSardana/telemetry-localhost
Browse files Browse the repository at this point in the history
fix: telemetry
  • Loading branch information
RitvikSardana authored Dec 11, 2024
2 parents c73e04d + 5eda51d commit 9cd783f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 46 deletions.
1 change: 0 additions & 1 deletion desk/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ onMounted(async () => {
iconClasses: "stroke-red-600",
});
});
await initTelemetry();
});
onUnmounted(() => {
Expand Down
3 changes: 2 additions & 1 deletion desk/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import "./index.css";
import { router } from "./router";
import { socket } from "./socket";
import { createToast } from "@/utils";
import { posthogPlugin } from "./telemetry";

const globalComponents = {
Badge,
Expand Down Expand Up @@ -48,7 +49,7 @@ const app = createApp(App);
app.use(resourcesPlugin);
app.use(pinia);
app.use(router);

app.use(posthogPlugin);
for (const c in globalComponents) {
app.component(c, globalComponents[c]);
}
Expand Down
90 changes: 47 additions & 43 deletions desk/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useStorage } from "@vueuse/core";
import { call } from "frappe-ui";
import { ref } from "vue";
import { createResource } from "frappe-ui";
import "../../../frappe/frappe/public/js/lib/posthog.js";

const APP = "helpdesk";
Expand All @@ -12,57 +12,56 @@ declare global {
posthog: any;
}
}
type PosthogSettings = {
posthog_project_id: string;
posthog_host: string;
enable_telemetry: boolean;
telemetry_site_age: number;
};

const telemetry = useStorage("telemetry", {
const telemetry = ref({
enabled: false,
project_id: "",
host: "",
});

export async function init() {
await set_enabled();
if (!telemetry.value.enabled) return;
try {
await set_credentials();
window.posthog.init(telemetry.value.project_id, {
api_host: telemetry.value.host,
autocapture: false,
person_profiles: "always",
capture_pageview: true,
capture_pageleave: true,
disable_session_recording: false,
session_recording: {
maskAllInputs: false,
maskInputOptions: {
password: true,
},
},
loaded: (posthog) => {
window.posthog = posthog;
window.posthog.identify(SITENAME);
},
});
} catch (e) {
console.trace("Failed to initialize telemetry", e);
telemetry.value.enabled = false;
}
}
let posthog: typeof window.posthog = window.posthog;

async function set_enabled() {
if (telemetry.value.enabled) return;
let posthogSettings = createResource({
url: "helpdesk.api.telemetry.get_posthog_settings",
cache: "posthog_settings",
onSuccess: (ps: PosthogSettings) => init(ps),
});

await call("helpdesk.api.telemetry.is_enabled").then((res) => {
telemetry.value.enabled = res;
});
}
function isTelemetryEnabled() {
if (!posthogSettings.data) return false;

async function set_credentials() {
if (!telemetry.value.enabled) return;
if (telemetry.value.project_id && telemetry.value.host) return;
return (
posthogSettings.data.enable_telemetry &&
posthogSettings.data.posthog_project_id &&
posthogSettings.data.posthog_host
);
}

await call("helpdesk.api.telemetry.get_credentials").then((res) => {
telemetry.value.project_id = res.project_id;
telemetry.value.host = res.telemetry_host;
export async function init(ps: PosthogSettings) {
if (!isTelemetryEnabled()) return;
posthog.init(ps.posthog_project_id, {
api_host: ps.posthog_host,
autocapture: false,
person_profiles: "identified_only",
capture_pageview: true,
capture_pageleave: true,
disable_session_recording: false,
session_recording: {
maskAllInputs: false,
maskInputOptions: {
password: true,
},
},
loaded: (ph: typeof posthog) => {
window.posthog = ph;
ph.identify(SITENAME);
},
});
}

Expand Down Expand Up @@ -98,3 +97,8 @@ export function stopSession() {
window.posthog.stopSessionRecording();
}
}

export function posthogPlugin(app: any) {
app.config.globalProperties.posthog = window.posthog;
if (!window.posthog?.length) posthogSettings.fetch();
}
2 changes: 1 addition & 1 deletion frappe-ui
11 changes: 11 additions & 0 deletions helpdesk/api/telemetry.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import frappe
from frappe.utils.telemetry import POSTHOG_HOST_FIELD, POSTHOG_PROJECT_FIELD


@frappe.whitelist()
Expand All @@ -16,3 +17,13 @@ def get_credentials():
"project_id": frappe.conf.get("posthog_project_id"),
"telemetry_host": frappe.conf.get("posthog_host"),
}


@frappe.whitelist()
def get_posthog_settings():
return {
"posthog_project_id": frappe.conf.get(POSTHOG_PROJECT_FIELD),
"posthog_host": frappe.conf.get(POSTHOG_HOST_FIELD),
"enable_telemetry": frappe.get_system_settings("enable_telemetry"),
"telemetry_site_age": frappe.utils.telemetry.site_age(),
}

0 comments on commit 9cd783f

Please sign in to comment.