Skip to content

Commit

Permalink
Distinguish users in posthog (#2965)
Browse files Browse the repository at this point in the history
* distinguish tenants in posthog

* nit
  • Loading branch information
pablonyx authored Oct 28, 2024
1 parent e5af468 commit a40082c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backend/danswer/server/manage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ class UserInfo(BaseModel):
oidc_expiry: datetime | None = None
current_token_created_at: datetime | None = None
current_token_expiry_length: int | None = None
organization_name: str | None = None

@classmethod
def from_model(
cls,
user: User,
current_token_created_at: datetime | None = None,
expiry_length: int | None = None,
organization_name: str | None = None,
) -> "UserInfo":
return cls(
id=str(user.id),
Expand All @@ -80,6 +82,7 @@ def from_model(
visible_assistants=user.visible_assistants,
)
),
organization_name=organization_name,
# set to None if TRACK_EXTERNAL_IDP_EXPIRY is False so that we avoid cases
# where they previously had this set + used OIDC, and now they switched to
# basic auth are now constantly getting redirected back to the login page
Expand Down
4 changes: 4 additions & 0 deletions backend/danswer/server/manage/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from danswer.auth.users import current_admin_user
from danswer.auth.users import current_curator_or_admin_user
from danswer.auth.users import current_user
from danswer.auth.users import get_tenant_id_for_email
from danswer.auth.users import optional_user
from danswer.configs.app_configs import AUTH_TYPE
from danswer.configs.app_configs import ENABLE_EMAIL_INVITES
Expand Down Expand Up @@ -493,10 +494,13 @@ def verify_user_logged_in(
token_created_at = (
None if MULTI_TENANT else get_current_token_creation(user, db_session)
)
organization_name = get_tenant_id_for_email(user.email)

user_info = UserInfo.from_model(
user,
current_token_created_at=token_created_at,
expiry_length=SESSION_EXPIRE_TIME_SECONDS,
organization_name=organization_name,
)

return user_info
Expand Down
19 changes: 19 additions & 0 deletions web/src/components/user/UserProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React, { createContext, useContext, useState, useEffect } from "react";
import { User, UserRole } from "@/lib/types";
import { getCurrentUser } from "@/lib/user";
import { usePostHog } from "posthog-js/react";

interface UserContextType {
user: User | null;
Expand All @@ -24,6 +25,24 @@ export function UserProvider({
const [upToDateUser, setUpToDateUser] = useState<User | null>(user);
const [isLoadingUser, setIsLoadingUser] = useState(false);

const posthog = usePostHog();

useEffect(() => {
if (!posthog) return;

if (user?.id) {
const identifyData: Record<string, any> = {
email: user.email,
};
if (user.organization_name) {
identifyData.organization_name = user.organization_name;
}
posthog.identify(user.id, identifyData);
} else {
posthog.reset();
}
}, [posthog, user]);

const fetchUser = async () => {
try {
setIsLoadingUser(true);
Expand Down
1 change: 1 addition & 0 deletions web/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface User {
current_token_created_at?: Date;
current_token_expiry_length?: number;
oidc_expiry?: Date;
organization_name: string | null;
}

export interface MinimalUserSnapshot {
Expand Down

0 comments on commit a40082c

Please sign in to comment.