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
pull/61187/head^2
Virginia Cepeda 2 years ago committed by GitHub
parent ddc8beda07
commit 115218b1ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      public/app/features/alerting/unified/Analytics.test.ts
  2. 39
      public/app/features/alerting/unified/Analytics.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');
});
});

@ -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<TFunc extends (...args: any[]) => 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;
};

Loading…
Cancel
Save