From 115218b1fff8aca0cb8cbeb9ea328eb1c0d8a71a Mon Sep 17 00:00:00 2001 From: Virginia Cepeda Date: Mon, 9 Jan 2023 14:41:03 -0300 Subject: [PATCH] Alerting: only track events for survey if user is not new (#61005) * Only track events data if user is not new To know this we evaluate the creation date to be older than two weeks * Address PR comments --- .../alerting/unified/Analytics.test.ts | 40 +++++++++++++++++++ .../features/alerting/unified/Analytics.ts | 39 ++++++++++++++++-- 2 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 public/app/features/alerting/unified/Analytics.test.ts diff --git a/public/app/features/alerting/unified/Analytics.test.ts b/public/app/features/alerting/unified/Analytics.test.ts new file mode 100644 index 00000000000..f884c8d40eb --- /dev/null +++ b/public/app/features/alerting/unified/Analytics.test.ts @@ -0,0 +1,40 @@ +import { dateTime } from '@grafana/data'; +import { getBackendSrv } from '@grafana/runtime'; + +import { isNewUser, USER_CREATION_MIN_DAYS } from './Analytics'; + +jest.mock('@grafana/runtime', () => ({ + getBackendSrv: jest.fn().mockReturnValue({ + get: jest.fn(), + }), +})); + +describe('isNewUser', function () { + it('should return true if the user has been created within the last two weeks', async () => { + const newUser = { + id: 1, + createdAt: dateTime().subtract(14, 'days'), + }; + + getBackendSrv().get = jest.fn().mockResolvedValue(newUser); + + const isNew = await isNewUser(1); + expect(isNew).toBe(true); + expect(getBackendSrv().get).toHaveBeenCalledTimes(1); + expect(getBackendSrv().get).toHaveBeenCalledWith('/api/users/1'); + }); + + it('should return false if the user has been created prior to the last two weeks', async () => { + const oldUser = { + id: 2, + createdAt: dateTime().subtract(USER_CREATION_MIN_DAYS, 'days'), + }; + + getBackendSrv().get = jest.fn().mockResolvedValue(oldUser); + + const isNew = await isNewUser(2); + expect(isNew).toBe(false); + expect(getBackendSrv().get).toHaveBeenCalledTimes(1); + expect(getBackendSrv().get).toHaveBeenCalledWith('/api/users/2'); + }); +}); diff --git a/public/app/features/alerting/unified/Analytics.ts b/public/app/features/alerting/unified/Analytics.ts index e872a66a960..51c150d968b 100644 --- a/public/app/features/alerting/unified/Analytics.ts +++ b/public/app/features/alerting/unified/Analytics.ts @@ -1,6 +1,10 @@ +import { dateTime } from '@grafana/data'; import { faro, LogLevel as GrafanaLogLevel } from '@grafana/faro-web-sdk'; +import { getBackendSrv } from '@grafana/runtime'; import { config, reportInteraction } from '@grafana/runtime/src'; +export const USER_CREATION_MIN_DAYS = 15; + export const LogMessages = { filterByLabel: 'filtering alert instances by label', loadedList: 'loaded Alert Rules list', @@ -40,20 +44,47 @@ export function withPerformanceLogging Promise }; } -export const trackNewAlerRuleFormSaved = (props: AlertRuleTrackingProps) => { +export async function isNewUser(userId: number) { + try { + const { createdAt } = await getBackendSrv().get(`/api/users/${userId}`); + + const limitDateForNewUser = dateTime().subtract(USER_CREATION_MIN_DAYS, 'days'); + const userCreationDate = dateTime(createdAt); + + const isNew = limitDateForNewUser.isBefore(userCreationDate); + + return isNew; + } catch { + return true; //if no date is returned, we assume the user is new to prevent tracking actions + } +} + +export const trackNewAlerRuleFormSaved = async (props: AlertRuleTrackingProps) => { + const isNew = await isNewUser(props.user_id); + if (isNew) { + return; + } reportInteraction('grafana_alerting_rule_creation', props); }; -export const trackNewAlerRuleFormCancelled = (props: AlertRuleTrackingProps) => { +export const trackNewAlerRuleFormCancelled = async (props: AlertRuleTrackingProps) => { + const isNew = await isNewUser(props.user_id); + if (isNew) { + return; + } reportInteraction('grafana_alerting_rule_aborted', props); }; -export const trackNewAlerRuleFormError = (props: AlertRuleTrackingProps & { error: string }) => { +export const trackNewAlerRuleFormError = async (props: AlertRuleTrackingProps & { error: string }) => { + const isNew = await isNewUser(props.user_id); + if (isNew) { + return; + } reportInteraction('grafana_alerting_rule_form_error', props); }; export type AlertRuleTrackingProps = { + user_id: number; grafana_version?: string; org_id?: number; - user_id?: number; };