From 1e0f4717a5408788767f86d347dfa36ad1bb0401 Mon Sep 17 00:00:00 2001 From: Melissa Autumn Date: Wed, 6 Nov 2024 09:05:52 -0800 Subject: [PATCH] Re-connect a slimmed down page-load metrics to fix filtering out non-logged in users in product metrics. --- backend/src/appointment/database/schemas.py | 22 +++++++-------- backend/src/appointment/routes/metrics.py | 30 +++++++++++++++++++++ frontend/src/App.vue | 15 +++++++++++ 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/backend/src/appointment/database/schemas.py b/backend/src/appointment/database/schemas.py index 4de4e135b..1f3f4f254 100644 --- a/backend/src/appointment/database/schemas.py +++ b/backend/src/appointment/database/schemas.py @@ -482,17 +482,17 @@ class Config: class PageLoadIn(BaseModel): - browser: Optional[str] - browser_version: Optional[str] - os: Optional[str] - os_version: Optional[str] - device: Optional[str] - device_model: Optional[str] - resolution: Optional[str] - effective_resolution: Optional[str] - user_agent: Optional[str] - locale: Optional[str] - theme: Optional[str] + browser: Optional[str] = None + browser_version: Optional[str] = None + os: Optional[str] = None + os_version: Optional[str] = None + device: Optional[str] = None + device_model: Optional[str] = None + resolution: Optional[str] = None + effective_resolution: Optional[str] = None + user_agent: Optional[str] = None + locale: Optional[str] = None + theme: Optional[str] = None class FTUEStepIn(BaseModel): diff --git a/backend/src/appointment/routes/metrics.py b/backend/src/appointment/routes/metrics.py index 345b61a21..777e01473 100644 --- a/backend/src/appointment/routes/metrics.py +++ b/backend/src/appointment/routes/metrics.py @@ -21,6 +21,36 @@ def get_api_path(request: Request): return f'/api/v1{request.url.path}' +@router.post('/get-id') +async def get_id( + request: Request, + data: schemas.PageLoadIn, + posthog: Posthog | None = Depends(get_posthog), + subscriber: Subscriber | None = Depends(get_subscriber_or_none), +): + if posthog is None: + return False + + if not subscriber: + anon_id = '-'.join([request.client.host, data.browser_version, data.os_version]) + + hash_instance = hashlib.sha256() + hash_instance.update(anon_id.encode('utf-8')) + distinct_id = f'anon-{hash_instance.hexdigest()}' + else: + distinct_id = subscriber.unique_hash + + # Set a display id + posthog.set(distinct_id=distinct_id, properties={ + 'display_id': distinct_id + }) + posthog.set_once(distinct_id=distinct_id, properties={ + 'initial_service': APP_NAME_SHORT + }) + + return {'id': distinct_id} + + @router.post('/page-load') def page_load( request: Request, diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 93f5e12e2..e2a837138 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -181,6 +181,18 @@ const onPageLoad = async () => { const { data } = response; return data.value?.id ?? false; */ + + // Only used for non-subscribers + const parser = new UAParser(navigator.userAgent); + const browser = parser.getBrowser(); + const os = parser.getOS(); + + const response = await call('metrics/get-id').post({ + browser_version: `${browser.name}:${browser.version}`, + os_version: `${os.name}:${os.version}`, + }).json(); + const { data } = response; + return data.value?.id ?? false; }; // provide refresh functions for components @@ -276,9 +288,12 @@ onMounted(async () => { service: 'apmt', }); + const id = await onPageLoad(); if (isAuthenticated.value) { const profile = useUserStore(); posthog.identify(profile.data.uniqueHash); + } else if (id) { + posthog.identify(id); } } });