From 1b27e55e5a3540943b87e4ac604c91e724518585 Mon Sep 17 00:00:00 2001 From: Josh Hunt Date: Tue, 5 Apr 2022 16:41:22 +0100 Subject: [PATCH] Analytics: Identify users to Rudderstack by externalUserId where available (#47325) * wip * tests * move util, fix test * fixes * Update public/app/core/services/echo/utils.ts Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com> --- packages/grafana-data/src/types/config.ts | 1 + public/app/core/services/context_srv.ts | 2 + .../backends/analytics/RudderstackBackend.ts | 9 +++-- public/app/core/services/echo/utils.test.ts | 38 +++++++++++++++++++ public/app/core/services/echo/utils.ts | 15 ++++++++ 5 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 public/app/core/services/echo/utils.test.ts diff --git a/packages/grafana-data/src/types/config.ts b/packages/grafana-data/src/types/config.ts index b1a02c0ce95..bb003374d92 100644 --- a/packages/grafana-data/src/types/config.ts +++ b/packages/grafana-data/src/types/config.ts @@ -93,6 +93,7 @@ export type OAuthSettings = Partial { + it('should return the external user ID (gcom ID) if available', () => { + const id = getUserIdentifier(gcomUser); + expect(id).toBe('abc-123'); + }); + + it('should fall back to the email address', () => { + const id = getUserIdentifier(baseUser); + expect(id).toBe('email@example.com'); + }); +}); diff --git a/public/app/core/services/echo/utils.ts b/public/app/core/services/echo/utils.ts index 1765b0017fe..edcfe096d77 100644 --- a/public/app/core/services/echo/utils.ts +++ b/public/app/core/services/echo/utils.ts @@ -1,5 +1,20 @@ import { attachDebugger, createLogger } from '@grafana/ui'; +import { CurrentUserDTO } from '@grafana/data'; + +/** + * Returns an opaque identifier for a user, for reporting purposes. + * Because this is for use when reporting across multiple Grafana installations + * It cannot simply be user.id because that's not unique across two installations. + */ +export function getUserIdentifier(user: CurrentUserDTO) { + if (user.externalUserId.length) { + return user.externalUserId; + } + + return user.email; +} + /** @internal */ export const echoLogger = createLogger('EchoSrv'); export const echoLog = echoLogger.logger;