From 34b61cfdb723822e03d0b3a857c170cc63f59668 Mon Sep 17 00:00:00 2001 From: Daniel Messias Date: Mon, 2 Oct 2023 17:28:49 +0100 Subject: [PATCH] Add keys and identify --- .drone.yml | 4 ++-- .../src/app/context/UserContextProvider.tsx | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index 33ce8495dd0e24..70fedf66bbdf6e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -51,13 +51,13 @@ steps: <<: *build_frontend environment: ENVIRONMENT: staging - AMPLITUDE_API_KEY: d624f9cc751933b0725858700bb1946e + AMPLITUDE_API_KEY: b46a366c22a2e7fb525ae99c14a693ec - name: build_frontend_production <<: *build_frontend environment: ENVIRONMENT: production - AMPLITUDE_API_KEY: 'foo' + AMPLITUDE_API_KEY: f0b9cf5c530426c3dbacb91e74f009a5 - name: docker_frontend_staging <<: *docker_build_config diff --git a/datahub-web-react/src/app/context/UserContextProvider.tsx b/datahub-web-react/src/app/context/UserContextProvider.tsx index 3bcff15cc27485..7878030b0a6d3c 100644 --- a/datahub-web-react/src/app/context/UserContextProvider.tsx +++ b/datahub-web-react/src/app/context/UserContextProvider.tsx @@ -3,6 +3,7 @@ import { useGetMeLazyQuery } from '../../graphql/me.generated'; import { useGetGlobalViewsSettingsLazyQuery } from '../../graphql/app.generated'; import { CorpUser, PlatformPrivileges } from '../../types.generated'; import { UserContext, LocalState, DEFAULT_STATE, State } from './userContext'; +import analytics from '../analytics'; // TODO: Migrate all usage of useAuthenticatedUser to using this provider. @@ -41,6 +42,27 @@ const UserContextProvider = ({ children }: { children: React.ReactNode }) => { const [getMe, { data: meData, refetch }] = useGetMeLazyQuery({ fetchPolicy: 'cache-first' }); useEffect(() => getMe(), [getMe]); + /** + * Identify the user in the analytics tool once on component mount. + * + * This lets Amplitude identify (and thus segment) the users on more attribu + * + * There's likely a more optimal place in the app to do this (ideally immedi + * the user logs in). However, the login flow (particular with SSO) is a bit + * follow, and I don't think these calls are particularly expensive, so here + */ + useEffect(() => { + if (meData?.me?.corpUser) { + const corpUser = meData.me.corpUser as CorpUser; + const info = corpUser.info ?? {}; + console.log('v1!'); + console.log('Identifying user', corpUser.urn, info); + analytics.identify(corpUser.urn, { + ...info, + }); + } + }, [meData]); + /** * Retrieve the Global View settings once on component mount. */