Skip to content

Commit

Permalink
chore: track route views for first few days
Browse files Browse the repository at this point in the history
Identifying where users drop off is tricky without knowing what they are
doing in system. Tracking first few days routes will likely give some
insights.

(cherry picked from commit 5b88163)
  • Loading branch information
ankush committed May 23, 2023
1 parent 1d8ebf2 commit c48d636
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions frappe/public/js/telemetry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TelemetryManager {

this.project_id = frappe.boot.posthog_project_id;
this.telemetry_host = frappe.boot.posthog_host;
this.site_age = frappe.boot.telemetry_site_age;

if (cint(frappe.boot.enable_telemetry) && this.project_id && this.telemetry_host) {
this.enabled = true;
Expand All @@ -24,6 +25,7 @@ class TelemetryManager {
});
posthog.identify(frappe.boot.sitename);
this.send_heartbeat();
this.register_pageview_handler();
} catch (e) {
console.trace("Failed to initialize telemetry", e);
this.enabled = false;
Expand All @@ -50,6 +52,16 @@ class TelemetryManager {
this.capture("heartbeat", "frappe");
}
}

register_pageview_handler() {
if (this.site_age && this.site_age > 5) {
return;
}

frappe.router.on("change", () => {
posthog.capture("$pageview");
});
}
}

frappe.telemetry = new TelemetryManager();
Expand Down
12 changes: 12 additions & 0 deletions frappe/utils/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from posthog import Posthog

import frappe
from frappe.utils import getdate
from frappe.utils.caching import site_cache

POSTHOG_PROJECT_FIELD = "posthog_project_id"
POSTHOG_HOST_FIELD = "posthog_host"
Expand All @@ -20,6 +22,16 @@ def add_bootinfo(bootinfo):
bootinfo.posthog_host = frappe.conf.get(POSTHOG_HOST_FIELD)
bootinfo.posthog_project_id = frappe.conf.get(POSTHOG_PROJECT_FIELD)
bootinfo.enable_telemetry = True
bootinfo.telemetry_site_age = site_age()


@site_cache(ttl=60 * 60 * 12)
def site_age():
try:
est_creation = frappe.db.get_value("User", "Administrator", "creation")
return (getdate() - getdate(est_creation)).days
except Exception:
pass


def init_telemetry():
Expand Down

0 comments on commit c48d636

Please sign in to comment.