Analytics: add experimentview events (#44120)

* Analytics: add experimentview event

* remove ref to feature-highlights

* Experimentview -> ExperimentView
This commit is contained in:
Agnès Toulet 2022-01-19 11:07:18 +01:00 committed by GitHub
parent 252645b330
commit 99cdb56f72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 3 deletions

View File

@ -7,7 +7,7 @@ export * from './services';
export * from './config';
export * from './types';
export { loadPluginCss, SystemJS, PluginCssOptions } from './utils/plugin';
export { reportMetaAnalytics, reportInteraction, reportPageview } from './utils/analytics';
export { reportMetaAnalytics, reportInteraction, reportPageview, reportExperimentView } from './utils/analytics';
export { featureEnabled } from './utils/licensing';
export { logInfo, logDebug, logWarning, logError } from './utils/logging';
export {

View File

@ -81,6 +81,7 @@ export enum EchoEventType {
Sentry = 'sentry',
Pageview = 'pageview',
Interaction = 'interaction',
ExperimentView = 'experimentview',
}
/**

View File

@ -104,6 +104,24 @@ export interface InteractionEchoEventPayload {
*/
export type InteractionEchoEvent = EchoEvent<EchoEventType.Interaction, InteractionEchoEventPayload>;
/**
* Describes the payload of an experimentview event.
*
* @public
*/
export interface ExperimentViewEchoEventPayload {
experimentId: string;
experimentGroup: string;
experimentVariant: string;
}
/**
* Describes experimentview event with predefined {@link EchoEventType.EchoEventType} type.
*
* @public
*/
export type ExperimentViewEchoEvent = EchoEvent<EchoEventType.ExperimentView, ExperimentViewEchoEventPayload>;
/**
* Pageview event typeguard.
*
@ -121,3 +139,12 @@ export const isPageviewEvent = (event: EchoEvent): event is PageviewEchoEvent =>
export const isInteractionEvent = (event: EchoEvent): event is InteractionEchoEvent => {
return Boolean(event.payload.interactionName);
};
/**
* Experimentview event typeguard.
*
* @public
*/
export const isExperimentViewEvent = (event: EchoEvent): event is ExperimentViewEchoEvent => {
return Boolean(event.payload.experimentId);
};

View File

@ -1,5 +1,6 @@
import { getEchoSrv, EchoEventType } from '../services/EchoSrv';
import {
ExperimentViewEchoEvent,
InteractionEchoEvent,
MetaAnalyticsEvent,
MetaAnalyticsEventPayload,
@ -50,3 +51,19 @@ export const reportInteraction = (interactionName: string, properties?: Record<s
},
});
};
/**
* Helper function to report experimentview events to the {@link EchoSrv}.
*
* @public
*/
export const reportExperimentView = (id: string, group: string, variant: string) => {
getEchoSrv().addEvent<ExperimentViewEchoEvent>({
type: EchoEventType.ExperimentView,
payload: {
experimentId: id,
experimentGroup: group,
experimentVariant: variant,
},
});
};

View File

@ -1,5 +1,12 @@
import $ from 'jquery';
import { EchoBackend, EchoEventType, isInteractionEvent, isPageviewEvent, PageviewEchoEvent } from '@grafana/runtime';
import {
EchoBackend,
EchoEventType,
isExperimentViewEvent,
isInteractionEvent,
isPageviewEvent,
PageviewEchoEvent,
} from '@grafana/runtime';
import { User } from '../sentry/types';
export interface RudderstackBackendOptions {
@ -11,7 +18,7 @@ export interface RudderstackBackendOptions {
}
export class RudderstackBackend implements EchoBackend<PageviewEchoEvent, RudderstackBackendOptions> {
supportedEvents = [EchoEventType.Pageview, EchoEventType.Interaction];
supportedEvents = [EchoEventType.Pageview, EchoEventType.Interaction, EchoEventType.ExperimentView];
constructor(public options: RudderstackBackendOptions) {
const url = options.sdkUrl || `https://cdn.rudderlabs.com/v1/rudder-analytics.min.js`;
@ -69,6 +76,14 @@ export class RudderstackBackend implements EchoBackend<PageviewEchoEvent, Rudder
if (isInteractionEvent(e)) {
(window as any).rudderanalytics.track(e.payload.interactionName, e.payload.properties);
}
if (isExperimentViewEvent(e)) {
(window as any).rudderanalytics.track('experiment_viewed', {
experiment_id: e.payload.experimentId,
experiment_group: e.payload.experimentGroup,
experiment_variant: e.payload.experimentVariant,
});
}
};
// Not using Echo buffering, addEvent above sends events to GA as soon as they appear