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
5 changed files with 63 additions and 3 deletions

View File

@@ -7,7 +7,7 @@ export * from './services';
export * from './config'; export * from './config';
export * from './types'; export * from './types';
export { loadPluginCss, SystemJS, PluginCssOptions } from './utils/plugin'; 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 { featureEnabled } from './utils/licensing';
export { logInfo, logDebug, logWarning, logError } from './utils/logging'; export { logInfo, logDebug, logWarning, logError } from './utils/logging';
export { export {

View File

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

View File

@@ -104,6 +104,24 @@ export interface InteractionEchoEventPayload {
*/ */
export type InteractionEchoEvent = EchoEvent<EchoEventType.Interaction, 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. * Pageview event typeguard.
* *
@@ -121,3 +139,12 @@ export const isPageviewEvent = (event: EchoEvent): event is PageviewEchoEvent =>
export const isInteractionEvent = (event: EchoEvent): event is InteractionEchoEvent => { export const isInteractionEvent = (event: EchoEvent): event is InteractionEchoEvent => {
return Boolean(event.payload.interactionName); 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 { getEchoSrv, EchoEventType } from '../services/EchoSrv';
import { import {
ExperimentViewEchoEvent,
InteractionEchoEvent, InteractionEchoEvent,
MetaAnalyticsEvent, MetaAnalyticsEvent,
MetaAnalyticsEventPayload, 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 $ 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'; import { User } from '../sentry/types';
export interface RudderstackBackendOptions { export interface RudderstackBackendOptions {
@@ -11,7 +18,7 @@ export interface RudderstackBackendOptions {
} }
export class RudderstackBackend implements EchoBackend<PageviewEchoEvent, RudderstackBackendOptions> { export class RudderstackBackend implements EchoBackend<PageviewEchoEvent, RudderstackBackendOptions> {
supportedEvents = [EchoEventType.Pageview, EchoEventType.Interaction]; supportedEvents = [EchoEventType.Pageview, EchoEventType.Interaction, EchoEventType.ExperimentView];
constructor(public options: RudderstackBackendOptions) { constructor(public options: RudderstackBackendOptions) {
const url = options.sdkUrl || `https://cdn.rudderlabs.com/v1/rudder-analytics.min.js`; 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)) { if (isInteractionEvent(e)) {
(window as any).rudderanalytics.track(e.payload.interactionName, e.payload.properties); (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 // Not using Echo buffering, addEvent above sends events to GA as soon as they appear