mirror of
https://github.com/hanzo-io/cloud.git
synced 2026-10-30 18:57:42 +00:00
46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
from contextlib import suppress
|
|
|
|
import frappe
|
|
from posthog import Posthog
|
|
|
|
from press.utils import log_error
|
|
|
|
|
|
def init_telemetry():
|
|
"""Init posthog for server side telemetry."""
|
|
if hasattr(frappe.local, "posthog"):
|
|
return
|
|
|
|
posthog_host = frappe.conf.get("posthog_host")
|
|
posthog_project_id = frappe.conf.get("posthog_project_id")
|
|
|
|
if not posthog_host or not posthog_project_id:
|
|
return
|
|
|
|
with suppress(Exception):
|
|
frappe.local.posthog = Posthog(posthog_project_id, host=posthog_host)
|
|
|
|
|
|
def capture(event, app, site=None):
|
|
init_telemetry()
|
|
ph: Posthog = getattr(frappe.local, "posthog", None)
|
|
with suppress(Exception):
|
|
ph and ph.capture(site or frappe.local.site, f"{app}_{event}")
|
|
|
|
|
|
def identify(site, **kwargs):
|
|
init_telemetry()
|
|
ph: Posthog = getattr(frappe.local, "posthog", None)
|
|
with suppress(Exception):
|
|
ph and ph.identify(site, kwargs)
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
def capture_read_event(email: str = None):
|
|
try:
|
|
capture("read_email", "fc_signup", email)
|
|
except Exception as e:
|
|
log_error("Failed to capture read_email event", e)
|
|
finally:
|
|
frappe.response.update(frappe.utils.get_imaginary_pixel_response())
|