refactor: add app not to route objs

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2023-11-27 14:39:17 +01:00
parent f27ee7d937
commit 5c794cd583
13 changed files with 73 additions and 96 deletions
@@ -23,9 +23,8 @@ const AnalyticsSpy = () => {
const context = useAnalyticsContext();
return (
<>
<div data-testid="route-ref">{context.routeRef}</div>
<div data-testid="plugin-id">{context.pluginId}</div>
<div data-testid="extension">{context.extension}</div>
<div data-testid="extension-id">{context.extensionId}</div>
<div data-testid="custom">{context.custom}</div>
</>
);
@@ -36,9 +35,8 @@ describe('AnalyticsContext', () => {
it('returns default values', () => {
const { result } = renderHook(() => useAnalyticsContext());
expect(result.current).toEqual({
extension: 'App',
extensionId: 'App',
pluginId: 'root',
routeRef: 'unknown',
});
});
});
@@ -51,9 +49,8 @@ describe('AnalyticsContext', () => {
</AnalyticsContext>,
);
expect(result.getByTestId('extension')).toHaveTextContent('App');
expect(result.getByTestId('extension-id')).toHaveTextContent('App');
expect(result.getByTestId('plugin-id')).toHaveTextContent('root');
expect(result.getByTestId('route-ref')).toHaveTextContent('unknown');
});
it('uses provided analytics context', () => {
@@ -63,23 +60,23 @@ describe('AnalyticsContext', () => {
</AnalyticsContext>,
);
expect(result.getByTestId('extension')).toHaveTextContent('App');
expect(result.getByTestId('extension-id')).toHaveTextContent('App');
expect(result.getByTestId('plugin-id')).toHaveTextContent('custom');
expect(result.getByTestId('route-ref')).toHaveTextContent('unknown');
});
it('uses nested analytics context', () => {
const result = render(
<AnalyticsContext attributes={{ pluginId: 'custom' }}>
<AnalyticsContext attributes={{ extension: 'AnalyticsSpy' }}>
<AnalyticsContext attributes={{ extensionId: 'AnalyticsSpy' }}>
<AnalyticsSpy />
</AnalyticsContext>
</AnalyticsContext>,
);
expect(result.getByTestId('extension')).toHaveTextContent('AnalyticsSpy');
expect(result.getByTestId('extension-id')).toHaveTextContent(
'AnalyticsSpy',
);
expect(result.getByTestId('plugin-id')).toHaveTextContent('custom');
expect(result.getByTestId('route-ref')).toHaveTextContent('unknown');
});
});
});
@@ -37,9 +37,8 @@ export const useAnalyticsContext = (): AnalyticsContextValue => {
// Provide a default value if no value exists.
if (theContext === undefined) {
return {
routeRef: 'unknown',
pluginId: 'root',
extension: 'App',
extensionId: 'App',
};
}
@@ -19,9 +19,8 @@ import { Tracker, routableExtensionRenderedEvent } from './Tracker';
describe('Tracker', () => {
const defaultContext = {
routeRef: 'unknown',
pluginId: 'root',
extension: 'App',
extensionId: 'App',
};
const globalEvents = getOrCreateGlobalSingleton<any>(
'core-plugin-api:analytics-tracker-events',
@@ -56,9 +55,8 @@ describe('Tracker', () => {
it('captures simple event with set context', () => {
trackerUnderTest.setContext({
routeRef: 'catalog:entity',
pluginId: 'catalog',
extension: 'App',
extensionId: 'App',
});
trackerUnderTest.captureEvent('click', 'test 2', {
value: 2,
@@ -72,9 +70,8 @@ describe('Tracker', () => {
value: 2,
attributes: { to: '/page-2' },
context: {
routeRef: 'catalog:entity',
pluginId: 'catalog',
extension: 'App',
extensionId: 'App',
},
}),
);
@@ -83,10 +80,8 @@ describe('Tracker', () => {
describe('accurate navigate events', () => {
it('never captures _routeNodeType context key on navigate event', () => {
trackerUnderTest.setContext({
routeRef: 'catalog:entity',
pluginId: 'catalog',
extension: 'App',
_routeNodeType: 'mounted',
extensionId: 'App',
});
trackerUnderTest.captureEvent('navigate', '/page-3');
@@ -99,7 +94,6 @@ describe('Tracker', () => {
it('never immediately captures navigate event with _routeNodeType "gathered"', () => {
trackerUnderTest.setContext({
...defaultContext,
_routeNodeType: 'gathered',
});
trackerUnderTest.captureEvent('navigate', '/page-4');
@@ -116,15 +110,13 @@ describe('Tracker', () => {
// User navigates to a gathered mountpoint with multiple plugins
trackerUnderTest.setContext({
...defaultContext,
_routeNodeType: 'gathered',
});
trackerUnderTest.captureEvent('navigate', '/page-5');
// A routable extension is rendered with specific plugin context
trackerUnderTest.setContext({
routeRef: 'some:ref',
pluginId: 'some-plugin',
extension: 'App',
extensionId: 'App',
});
trackerUnderTest.captureEvent(routableExtensionRenderedEvent, '');
@@ -138,9 +130,8 @@ describe('Tracker', () => {
action: 'navigate',
subject: '/page-5',
context: {
routeRef: 'some:ref',
pluginId: 'some-plugin',
extension: 'App',
extensionId: 'App',
},
}),
);
@@ -150,9 +141,8 @@ describe('Tracker', () => {
action: 'click',
subject: 'test 5',
context: {
routeRef: 'some:ref',
pluginId: 'some-plugin',
extension: 'App',
extensionId: 'App',
},
}),
);
@@ -162,22 +152,19 @@ describe('Tracker', () => {
// User navigates to a gathered mountpoint with multiple plugins
trackerUnderTest.setContext({
...defaultContext,
_routeNodeType: 'gathered',
});
trackerUnderTest.captureEvent('navigate', '/page-6');
// A routable extension is rendered with specific plugin context
trackerUnderTest.setContext({
routeRef: 'another:ref',
pluginId: 'another-plugin',
extension: 'App',
extensionId: 'App',
});
trackerUnderTest.captureEvent(routableExtensionRenderedEvent, '');
// User navigates to another gathered mountpoint with multiple plugins
trackerUnderTest.setContext({
...defaultContext,
_routeNodeType: 'gathered',
});
trackerUnderTest.captureEvent('navigate', '/page-6-2');
@@ -188,9 +175,8 @@ describe('Tracker', () => {
action: 'navigate',
subject: '/page-6',
context: {
routeRef: 'another:ref',
pluginId: 'another-plugin',
extension: 'App',
extensionId: 'App',
},
}),
);
@@ -200,7 +186,6 @@ describe('Tracker', () => {
// User navigates to a gathered mountpoint with multiple plugins
trackerUnderTest.setContext({
...defaultContext,
_routeNodeType: 'gathered',
});
trackerUnderTest.captureEvent('navigate', '/page-7');
@@ -230,16 +215,14 @@ describe('Tracker', () => {
// User navigates to a gathered mountpoint with multiple plugins
trackerUnderTest.setContext({
...defaultContext,
_routeNodeType: 'gathered',
});
trackerUnderTest.captureEvent('navigate', '/page-8');
// A routable extension is rendered with specific plugin context and
// captured via a separate tracker instance.
const anotherTracker = new Tracker(analyticsApiSpy, {
routeRef: 'the:ref',
pluginId: 'the-plugin',
extension: 'App',
extensionId: 'App',
});
anotherTracker.captureEvent(routableExtensionRenderedEvent, '');
@@ -254,9 +237,8 @@ describe('Tracker', () => {
action: 'navigate',
subject: '/page-8',
context: {
routeRef: 'the:ref',
pluginId: 'the-plugin',
extension: 'App',
extensionId: 'App',
},
}),
);
@@ -273,15 +255,13 @@ describe('Tracker', () => {
// User navigates to a gathered mountpoint with multiple plugins
trackerUnderTest.setContext({
...defaultContext,
_routeNodeType: 'gathered',
});
trackerUnderTest.captureEvent('navigate', '/page-9');
// A routable extension is rendered with specific plugin context
trackerUnderTest.setContext({
routeRef: 'very:ref',
pluginId: 'very-plugin',
extension: 'ShouldBeReplaced',
extensionId: 'ShouldBeReplaced',
});
trackerUnderTest.captureEvent(routableExtensionRenderedEvent, '');
@@ -295,9 +275,8 @@ describe('Tracker', () => {
action: 'navigate',
subject: '/page-9',
context: {
routeRef: 'very:ref',
pluginId: 'very-plugin',
extension: 'App',
extensionId: 'App',
},
}),
);
@@ -68,9 +68,8 @@ export class Tracker implements AnalyticsTracker {
constructor(
private readonly analyticsApi: AnalyticsApi,
private context: AnalyticsContextValue = {
routeRef: 'unknown',
pluginId: 'root',
extension: 'App',
extensionId: 'App',
},
) {
// Only register a single beforeunload event across all trackers.
@@ -110,7 +109,7 @@ export class Tracker implements AnalyticsTracker {
}: { value?: number; attributes?: AnalyticsEventAttributes } = {},
) {
// Never pass internal "_routeNodeType" context value.
const { _routeNodeType, ...context } = this.context;
const context = this.context;
// Never fire the special "_routable-extension-rendered" internal event.
if (action === routableExtensionRenderedEvent) {
@@ -120,7 +119,7 @@ export class Tracker implements AnalyticsTracker {
globalEvents.mostRecentRoutableExtensionRender = {
context: {
...context,
extension: 'App',
extensionId: 'App',
},
};
}
@@ -148,11 +147,7 @@ export class Tracker implements AnalyticsTracker {
// Never directly fire a navigation event on a gathered route with default
// contextual details.
if (
action === 'navigate' &&
_routeNodeType === 'gathered' &&
context.pluginId === 'root'
) {
if (action === 'navigate' && context.pluginId === 'root') {
// Instead, set it on the global store.
globalEvents.mostRecentGatheredNavigation = {
action,
@@ -26,14 +26,12 @@ export type CommonAnalyticsContext = {
pluginId: string;
/**
* The ID of the routeRef that was active when the event was captured.
* The nearest known parent extension where the event was captured.
*/
routeRef: string;
/**
* The nearest known parent extension where the event was captured.
*/
extension: string;
extensionId: string;
};
/**
@@ -58,9 +58,8 @@ describe('useAnalytics', () => {
some: 'value',
},
context: {
extension: 'App',
extensionId: 'App',
pluginId: 'root',
routeRef: 'unknown',
},
});
});