Skip to content

Commit

Permalink
Re-connect a slimmed down page-load metrics to fix filtering out non-…
Browse files Browse the repository at this point in the history
…logged in users in product metrics.
  • Loading branch information
MelissaAutumn committed Nov 6, 2024
1 parent 9ed70d0 commit 1e0f471
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
22 changes: 11 additions & 11 deletions backend/src/appointment/database/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
30 changes: 30 additions & 0 deletions backend/src/appointment/routes/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
});
Expand Down

0 comments on commit 1e0f471

Please sign in to comment.