Skip to content

Commit 2039bd4

Browse files
authored
Merge pull request backstage#30178 from krakenftw/feat/analytics-on-auth
feat: added analytics on auth
2 parents e8f1530 + a14674c commit 2039bd4

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

.changeset/tiny-lions-watch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@backstage/core-components': patch
3+
'@backstage/plugin-user-settings': patch
4+
---
5+
6+
Added `signIn` and `signOut` analytic events to the `@backstage/core-components` of sign in and sign out.

packages/core-components/src/layout/SignInPage/SignInPage.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
BackstageIdentityResponse,
1919
configApiRef,
2020
SignInPageProps,
21+
useAnalytics,
2122
useApi,
2223
} from '@backstage/core-plugin-api';
2324
import { UserIdentity } from './UserIdentity';
@@ -116,6 +117,7 @@ export const SingleSignInPage = ({
116117
const authApi = useApi(provider.apiRef);
117118
const configApi = useApi(configApiRef);
118119
const { t } = useTranslationRef(coreComponentsTranslationRef);
120+
const analytics = useAnalytics();
119121

120122
const [error, setError] = useState<Error>();
121123

@@ -167,6 +169,7 @@ export const SingleSignInPage = ({
167169
profile,
168170
}),
169171
);
172+
analytics.captureEvent('signIn', 'success');
170173
} catch (err: any) {
171174
// User closed the sign-in modal
172175
setError(err);

packages/core-components/src/layout/SignInPage/providers.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
useApiHolder,
2222
errorApiRef,
2323
IdentityApi,
24+
useAnalytics,
2425
} from '@backstage/core-plugin-api';
2526
import {
2627
IdentityProviders,
@@ -92,6 +93,7 @@ export const useSignInProviders = (
9293
const errorApi = useApi(errorApiRef);
9394
const apiHolder = useApiHolder();
9495
const [loading, setLoading] = useState(true);
96+
const analytics = useAnalytics();
9597

9698
const { t } = useTranslationRef(coreComponentsTranslationRef);
9799
// User was redirected back to sign in page with error from auth redirect flow
@@ -108,18 +110,20 @@ export const useSignInProviders = (
108110

109111
// This decorates the result with sign out logic from this hook
110112
const handleWrappedResult = useCallback(
111-
(identityApi: IdentityApi) => {
113+
async (identityApi: IdentityApi) => {
112114
onSignInSuccess(
113115
IdentityApiSignOutProxy.from({
114116
identityApi,
115117
signOut: async () => {
116118
localStorage.removeItem(PROVIDER_STORAGE_KEY);
117119
await identityApi.signOut?.();
120+
analytics.captureEvent('signOut', 'success');
118121
},
119122
}),
120123
);
124+
analytics.captureEvent('signIn', 'success');
121125
},
122-
[onSignInSuccess],
126+
[onSignInSuccess, analytics],
123127
);
124128

125129
// In this effect we check if the user has already selected an existing login

plugins/user-settings/src/components/General/UserSettingsMenu.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
identityApiRef,
2626
errorApiRef,
2727
useApi,
28+
useAnalytics,
2829
} from '@backstage/core-plugin-api';
2930
import { useTranslationRef } from '@backstage/frontend-plugin-api';
3031
import { userSettingsTranslationRef } from '../../translation';
@@ -36,6 +37,7 @@ export const UserSettingsMenu = () => {
3637
const [open, setOpen] = useState(false);
3738
const [anchorEl, setAnchorEl] = useState<undefined | HTMLElement>(undefined);
3839
const { t } = useTranslationRef(userSettingsTranslationRef);
40+
const analytics = useAnalytics();
3941

4042
const handleOpen = (event: MouseEvent<HTMLButtonElement>) => {
4143
setAnchorEl(event.currentTarget);
@@ -59,9 +61,10 @@ export const UserSettingsMenu = () => {
5961
<Menu anchorEl={anchorEl} open={open} onClose={handleClose}>
6062
<MenuItem
6163
data-testid="sign-out"
62-
onClick={() =>
63-
identityApi.signOut().catch(error => errorApi.post(error))
64-
}
64+
onClick={() => {
65+
identityApi.signOut().catch(error => errorApi.post(error));
66+
analytics.captureEvent('signOut', 'success');
67+
}}
6568
>
6669
<ListItemIcon>
6770
<SignOutIcon />

0 commit comments

Comments
 (0)