feat: add api backward compatibility for more analytics modules
Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
"@backstage/config": "workspace:^",
|
||||
"@backstage/core-components": "workspace:^",
|
||||
"@backstage/core-plugin-api": "workspace:^",
|
||||
"@backstage/frontend-plugin-api": "workspace:^",
|
||||
"react-ga": "^3.3.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
+59
@@ -508,4 +508,63 @@ describe('GoogleAnalytics', () => {
|
||||
expect(lastData.queueTime).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('api backward compatibility', () => {
|
||||
it('continue working with legacy App category', () => {
|
||||
const api = GoogleAnalytics.fromConfig(basicValidConfig);
|
||||
|
||||
expect(api.captureEvent).toBeDefined();
|
||||
|
||||
api.captureEvent({
|
||||
action: 'navigate',
|
||||
subject: '/',
|
||||
context,
|
||||
});
|
||||
|
||||
const [command, data] = ReactGA.testModeAPI.calls[1];
|
||||
expect(command).toBe('send');
|
||||
expect(data).toMatchObject({
|
||||
hitType: 'pageview',
|
||||
page: '/',
|
||||
});
|
||||
});
|
||||
|
||||
it('use lowercase app as the new default category', () => {
|
||||
const api = GoogleAnalytics.fromConfig(basicValidConfig);
|
||||
|
||||
expect(api.captureEvent).toBeDefined();
|
||||
|
||||
api.captureEvent({
|
||||
action: 'navigate',
|
||||
subject: '/',
|
||||
context: { ...context, extensionId: '', extension: '' },
|
||||
});
|
||||
|
||||
const [command, data] = ReactGA.testModeAPI.calls[1];
|
||||
expect(command).toBe('send');
|
||||
expect(data).toMatchObject({
|
||||
hitType: 'pageview',
|
||||
page: '/',
|
||||
});
|
||||
});
|
||||
|
||||
it('prioritize new context extension id over old extension property', () => {
|
||||
const api = GoogleAnalytics.fromConfig(basicValidConfig);
|
||||
|
||||
expect(api.captureEvent).toBeDefined();
|
||||
|
||||
api.captureEvent({
|
||||
action: 'navigate',
|
||||
subject: '/',
|
||||
context: { ...context, extensionId: 'app', extension: '' },
|
||||
});
|
||||
|
||||
const [command, data] = ReactGA.testModeAPI.calls[1];
|
||||
expect(command).toBe('send');
|
||||
expect(data).toMatchObject({
|
||||
hitType: 'pageview',
|
||||
page: '/',
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+14
-5
@@ -22,6 +22,11 @@ import {
|
||||
AnalyticsEventAttributes,
|
||||
IdentityApi,
|
||||
} from '@backstage/core-plugin-api';
|
||||
import {
|
||||
AnalyticsApi as NewAnalyticsApi,
|
||||
AnalyticsEvent as NewAnalyticsEvent,
|
||||
AnalyticsContextValue as NewAnalyticsContextValue,
|
||||
} from '@backstage/frontend-plugin-api';
|
||||
import { Config } from '@backstage/config';
|
||||
import { DeferredCapture } from '../../../util';
|
||||
import {
|
||||
@@ -40,7 +45,7 @@ type CustomDimensionOrMetricConfig = {
|
||||
* Google Analytics API provider for the Backstage Analytics API.
|
||||
* @public
|
||||
*/
|
||||
export class GoogleAnalytics implements AnalyticsApi {
|
||||
export class GoogleAnalytics implements AnalyticsApi, NewAnalyticsApi {
|
||||
private readonly cdmConfig: CustomDimensionOrMetricConfig[];
|
||||
private customUserIdTransform?: (userEntityRef: string) => Promise<string>;
|
||||
private readonly capture: DeferredCapture;
|
||||
@@ -157,11 +162,15 @@ export class GoogleAnalytics implements AnalyticsApi {
|
||||
* pageview and the rest as custom events. All custom dimensions/metrics are
|
||||
* applied as they should be (set on pageview, merged object on events).
|
||||
*/
|
||||
captureEvent(event: AnalyticsEvent) {
|
||||
captureEvent(event: AnalyticsEvent | NewAnalyticsEvent) {
|
||||
const { context, action, subject, value, attributes } = event;
|
||||
const customMetadata = this.getCustomDimensionMetrics(context, attributes);
|
||||
|
||||
if (action === 'navigate' && context.extension === 'App') {
|
||||
const extensionId = context.extensionId || context.extension;
|
||||
const category = extensionId ? String(extensionId) : 'app';
|
||||
|
||||
// The legacy default extension was 'App' and the new one is 'app'
|
||||
if (action === 'navigate' && category.toLocaleLowerCase() === 'app') {
|
||||
this.capture.pageview(subject, customMetadata);
|
||||
return;
|
||||
}
|
||||
@@ -184,7 +193,7 @@ export class GoogleAnalytics implements AnalyticsApi {
|
||||
}
|
||||
|
||||
this.capture.event({
|
||||
category: context.extension || 'App',
|
||||
category,
|
||||
action,
|
||||
label: subject,
|
||||
value,
|
||||
@@ -197,7 +206,7 @@ export class GoogleAnalytics implements AnalyticsApi {
|
||||
* Event Attributes, e.g. { dimension1: "some value", metric8: 42 }
|
||||
*/
|
||||
private getCustomDimensionMetrics(
|
||||
context: AnalyticsContextValue,
|
||||
context: AnalyticsContextValue | NewAnalyticsContextValue,
|
||||
attributes: AnalyticsEventAttributes = {},
|
||||
) {
|
||||
const customDimensionsMetrics: { [x: string]: string | number | boolean } =
|
||||
|
||||
Reference in New Issue
Block a user