From 723e5478c79f2f3ab3d1071ec50a4b632939ab30 Mon Sep 17 00:00:00 2001 From: gaurav Date: Sat, 7 Jun 2025 20:11:44 +0530 Subject: [PATCH 1/4] feat: added analytics on auth Signed-off-by: gaurav --- .../src/layout/SignInPage/SignInPage.tsx | 7 +++++++ .../src/layout/SignInPage/providers.tsx | 12 ++++++++++-- .../src/components/General/UserSettingsMenu.tsx | 15 ++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/core-components/src/layout/SignInPage/SignInPage.tsx b/packages/core-components/src/layout/SignInPage/SignInPage.tsx index d9617c5c4b..cce77b1974 100644 --- a/packages/core-components/src/layout/SignInPage/SignInPage.tsx +++ b/packages/core-components/src/layout/SignInPage/SignInPage.tsx @@ -18,6 +18,7 @@ import { BackstageIdentityResponse, configApiRef, SignInPageProps, + useAnalytics, useApi, } from '@backstage/core-plugin-api'; import { UserIdentity } from './UserIdentity'; @@ -116,6 +117,7 @@ export const SingleSignInPage = ({ const authApi = useApi(provider.apiRef); const configApi = useApi(configApiRef); const { t } = useTranslationRef(coreComponentsTranslationRef); + const analytics = useAnalytics(); const [error, setError] = useState(); @@ -167,6 +169,11 @@ export const SingleSignInPage = ({ profile, }), ); + analytics.captureEvent('signIn', 'success', { + attributes: { + userEntityRef: identityResponse.identity.userEntityRef, + }, + }); } catch (err: any) { // User closed the sign-in modal setError(err); diff --git a/packages/core-components/src/layout/SignInPage/providers.tsx b/packages/core-components/src/layout/SignInPage/providers.tsx index 179c7a4542..d1da5deccd 100644 --- a/packages/core-components/src/layout/SignInPage/providers.tsx +++ b/packages/core-components/src/layout/SignInPage/providers.tsx @@ -21,6 +21,7 @@ import { useApiHolder, errorApiRef, IdentityApi, + useAnalytics, } from '@backstage/core-plugin-api'; import { IdentityProviders, @@ -92,6 +93,7 @@ export const useSignInProviders = ( const errorApi = useApi(errorApiRef); const apiHolder = useApiHolder(); const [loading, setLoading] = useState(true); + const analytics = useAnalytics(); const { t } = useTranslationRef(coreComponentsTranslationRef); // User was redirected back to sign in page with error from auth redirect flow @@ -108,7 +110,7 @@ export const useSignInProviders = ( // This decorates the result with sign out logic from this hook const handleWrappedResult = useCallback( - (identityApi: IdentityApi) => { + async (identityApi: IdentityApi) => { onSignInSuccess( IdentityApiSignOutProxy.from({ identityApi, @@ -118,8 +120,14 @@ export const useSignInProviders = ( }, }), ); + const identityResponse = await identityApi.getBackstageIdentity(); + analytics.captureEvent('signIn', 'success', { + attributes: { + userEntityRef: identityResponse.userEntityRef, + }, + }); }, - [onSignInSuccess], + [onSignInSuccess, analytics], ); // In this effect we check if the user has already selected an existing login diff --git a/plugins/user-settings/src/components/General/UserSettingsMenu.tsx b/plugins/user-settings/src/components/General/UserSettingsMenu.tsx index 6fb55896e9..e7906a971a 100644 --- a/plugins/user-settings/src/components/General/UserSettingsMenu.tsx +++ b/plugins/user-settings/src/components/General/UserSettingsMenu.tsx @@ -25,6 +25,7 @@ import { identityApiRef, errorApiRef, useApi, + useAnalytics, } from '@backstage/core-plugin-api'; import { useTranslationRef } from '@backstage/frontend-plugin-api'; import { userSettingsTranslationRef } from '../../translation'; @@ -36,6 +37,7 @@ export const UserSettingsMenu = () => { const [open, setOpen] = useState(false); const [anchorEl, setAnchorEl] = useState(undefined); const { t } = useTranslationRef(userSettingsTranslationRef); + const analytics = useAnalytics(); const handleOpen = (event: MouseEvent) => { setAnchorEl(event.currentTarget); @@ -59,9 +61,16 @@ export const UserSettingsMenu = () => { - identityApi.signOut().catch(error => errorApi.post(error)) - } + onClick={() => { + identityApi.signOut().catch(error => errorApi.post(error)); + identityApi.getBackstageIdentity().then(identity => { + analytics.captureEvent('signOut', 'success', { + attributes: { + userEntityRef: identity.userEntityRef, + }, + }); + }); + }} > From aa3b0544701d60502f57f86728a76559fce3ec73 Mon Sep 17 00:00:00 2001 From: gaurav Date: Sat, 7 Jun 2025 20:16:52 +0530 Subject: [PATCH 2/4] added changeset Signed-off-by: gaurav --- .changeset/tiny-lions-watch.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/tiny-lions-watch.md diff --git a/.changeset/tiny-lions-watch.md b/.changeset/tiny-lions-watch.md new file mode 100644 index 0000000000..c735528934 --- /dev/null +++ b/.changeset/tiny-lions-watch.md @@ -0,0 +1,6 @@ +--- +'@backstage/core-components': minor +'@backstage/plugin-user-settings': minor +--- + +Added `signIn` and `signOut` analytic events to the core-components of signin and signout. used `userEntityRef` for determining the user From d375ba6868a31921f2b5adbe0ac8250b52e74b42 Mon Sep 17 00:00:00 2001 From: gaurav Date: Tue, 10 Jun 2025 12:01:17 +0530 Subject: [PATCH 3/4] wip Signed-off-by: gaurav --- .../core-components/src/layout/SignInPage/SignInPage.tsx | 6 +----- .../core-components/src/layout/SignInPage/providers.tsx | 8 ++------ .../src/components/General/UserSettingsMenu.tsx | 8 +------- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/packages/core-components/src/layout/SignInPage/SignInPage.tsx b/packages/core-components/src/layout/SignInPage/SignInPage.tsx index cce77b1974..24de4d4647 100644 --- a/packages/core-components/src/layout/SignInPage/SignInPage.tsx +++ b/packages/core-components/src/layout/SignInPage/SignInPage.tsx @@ -169,11 +169,7 @@ export const SingleSignInPage = ({ profile, }), ); - analytics.captureEvent('signIn', 'success', { - attributes: { - userEntityRef: identityResponse.identity.userEntityRef, - }, - }); + analytics.captureEvent('signIn', 'success'); } catch (err: any) { // User closed the sign-in modal setError(err); diff --git a/packages/core-components/src/layout/SignInPage/providers.tsx b/packages/core-components/src/layout/SignInPage/providers.tsx index d1da5deccd..e9e43e9c7e 100644 --- a/packages/core-components/src/layout/SignInPage/providers.tsx +++ b/packages/core-components/src/layout/SignInPage/providers.tsx @@ -117,15 +117,11 @@ export const useSignInProviders = ( signOut: async () => { localStorage.removeItem(PROVIDER_STORAGE_KEY); await identityApi.signOut?.(); + analytics.captureEvent('signOut', 'success'); }, }), ); - const identityResponse = await identityApi.getBackstageIdentity(); - analytics.captureEvent('signIn', 'success', { - attributes: { - userEntityRef: identityResponse.userEntityRef, - }, - }); + analytics.captureEvent('signIn', 'success'); }, [onSignInSuccess, analytics], ); diff --git a/plugins/user-settings/src/components/General/UserSettingsMenu.tsx b/plugins/user-settings/src/components/General/UserSettingsMenu.tsx index e7906a971a..dc954e862f 100644 --- a/plugins/user-settings/src/components/General/UserSettingsMenu.tsx +++ b/plugins/user-settings/src/components/General/UserSettingsMenu.tsx @@ -63,13 +63,7 @@ export const UserSettingsMenu = () => { data-testid="sign-out" onClick={() => { identityApi.signOut().catch(error => errorApi.post(error)); - identityApi.getBackstageIdentity().then(identity => { - analytics.captureEvent('signOut', 'success', { - attributes: { - userEntityRef: identity.userEntityRef, - }, - }); - }); + analytics.captureEvent('signOut', 'success'); }} > From a14674c023f53a6e07f8085ccf1794820e009de8 Mon Sep 17 00:00:00 2001 From: benjdlambert Date: Mon, 7 Jul 2025 10:29:20 +0200 Subject: [PATCH 4/4] chore: fixing changeset Signed-off-by: benjdlambert --- .changeset/tiny-lions-watch.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.changeset/tiny-lions-watch.md b/.changeset/tiny-lions-watch.md index c735528934..e23ba791f9 100644 --- a/.changeset/tiny-lions-watch.md +++ b/.changeset/tiny-lions-watch.md @@ -1,6 +1,6 @@ --- -'@backstage/core-components': minor -'@backstage/plugin-user-settings': minor +'@backstage/core-components': patch +'@backstage/plugin-user-settings': patch --- -Added `signIn` and `signOut` analytic events to the core-components of signin and signout. used `userEntityRef` for determining the user +Added `signIn` and `signOut` analytic events to the `@backstage/core-components` of sign in and sign out.