@equinor/fusion-framework-module-analytics 3.0.7 → 3.0.8-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +22 -214
- package/dist/esm/__tests__/MockAnalyticsAdapter.test.js +91 -0
- package/dist/esm/__tests__/MockAnalyticsAdapter.test.js.map +1 -0
- package/dist/esm/__tests__/mock/analytics-mock-adapter.test.js +60 -0
- package/dist/esm/__tests__/mock/analytics-mock-adapter.test.js.map +1 -0
- package/dist/esm/mock/MockAnalyticsAdapter.js +134 -0
- package/dist/esm/mock/MockAnalyticsAdapter.js.map +1 -0
- package/dist/esm/mock/index.js +26 -0
- package/dist/esm/mock/index.js.map +1 -0
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/__tests__/MockAnalyticsAdapter.test.d.ts +1 -0
- package/dist/types/__tests__/mock/analytics-mock-adapter.test.d.ts +1 -0
- package/dist/types/mock/MockAnalyticsAdapter.d.ts +76 -0
- package/dist/types/mock/index.d.ts +25 -0
- package/dist/types/version.d.ts +1 -1
- package/docs/adapters.md +79 -0
- package/docs/collectors.md +91 -0
- package/docs/testing.md +87 -0
- package/docs/tracking-events.md +16 -0
- package/package.json +13 -9
- package/src/__tests__/MockAnalyticsAdapter.test.ts +127 -0
- package/src/__tests__/mock/analytics-mock-adapter.test.ts +79 -0
- package/src/mock/MockAnalyticsAdapter.ts +178 -0
- package/src/mock/index.ts +29 -0
- package/src/version.ts +1 -1
- package/vitest.config.ts +11 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { IAnalyticsAdapter } from '../adapters/AnalyticsAdapter.interface.js';
|
|
2
|
+
import type { AnalyticsEvent } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Selects which recorded events {@link MockAnalyticsAdapter.waitForAnalytic} or
|
|
5
|
+
* {@link MockAnalyticsAdapter.getAnalytics} act on.
|
|
6
|
+
*
|
|
7
|
+
* - `string` — matches `event.name` exactly.
|
|
8
|
+
* - `string[]` — matches if `event.name` is any of the given entries.
|
|
9
|
+
* - `(event) => boolean` — arbitrary predicate over the full event.
|
|
10
|
+
*/
|
|
11
|
+
export type AnalyticsEventMatcher<T extends AnalyticsEvent = AnalyticsEvent> = string | string[] | ((event: T) => boolean);
|
|
12
|
+
/** Options accepted by {@link MockAnalyticsAdapter.waitForAnalytic}. */
|
|
13
|
+
export interface WaitForAnalyticOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Maximum time in milliseconds to wait for a matching event.
|
|
16
|
+
* When elapsed the returned promise rejects.
|
|
17
|
+
*/
|
|
18
|
+
timeout?: number;
|
|
19
|
+
/**
|
|
20
|
+
* AbortSignal that can cancel the wait early.
|
|
21
|
+
* When aborted the returned promise rejects with the signal's reason.
|
|
22
|
+
*/
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* An {@link IAnalyticsAdapter} that records every tracked event in-memory instead
|
|
27
|
+
* of exporting it to a backend, for asserting on analytics in tests.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* Register it like any other adapter via {@link IAnalyticsConfigurator.setAdapter};
|
|
31
|
+
* it does not interfere with other adapters registered alongside it.
|
|
32
|
+
*
|
|
33
|
+
* @template T - Analytics event type, defaults to {@link AnalyticsEvent}.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { MockAnalyticsAdapter } from '@equinor/fusion-framework-module-analytics/mock';
|
|
38
|
+
*
|
|
39
|
+
* const recorder = new MockAnalyticsAdapter();
|
|
40
|
+
* enableAnalytics(configurator, (builder) => {
|
|
41
|
+
* builder.setAdapter('mock', async () => recorder);
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* // ...later, in a test
|
|
45
|
+
* const event = await recorder.waitForAnalytic('button-click');
|
|
46
|
+
* expect(event.attributes?.section).toBe('header');
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare class MockAnalyticsAdapter<T extends AnalyticsEvent = AnalyticsEvent> implements IAnalyticsAdapter<T> {
|
|
50
|
+
#private;
|
|
51
|
+
/**
|
|
52
|
+
* Records the event so it is visible to {@link getAnalytics} and any pending
|
|
53
|
+
* {@link waitForAnalytic} calls.
|
|
54
|
+
*
|
|
55
|
+
* @param event - The analytics event to record.
|
|
56
|
+
*/
|
|
57
|
+
registerAnalytic(event: T): void;
|
|
58
|
+
/**
|
|
59
|
+
* Returns recorded events matching `matcher`, in dispatch order.
|
|
60
|
+
*
|
|
61
|
+
* @param matcher - Event name, array of names, or a predicate. Omit to get every recorded event.
|
|
62
|
+
* @returns Matching recorded events.
|
|
63
|
+
*/
|
|
64
|
+
getAnalytics(matcher?: AnalyticsEventMatcher<T>): T[];
|
|
65
|
+
/**
|
|
66
|
+
* Waits for the next event matching `matcher`, resolving immediately if a
|
|
67
|
+
* matching event was already recorded.
|
|
68
|
+
*
|
|
69
|
+
* @param matcher - Event name, array of names, or a predicate.
|
|
70
|
+
* @param options - Optional timeout (ms) or AbortSignal.
|
|
71
|
+
* @returns A promise that resolves with the first matching event.
|
|
72
|
+
*/
|
|
73
|
+
waitForAnalytic(matcher: AnalyticsEventMatcher<T>, options?: WaitForAnalyticOptions): Promise<T>;
|
|
74
|
+
/** Completes the internal event stream, rejecting any pending `waitForAnalytic` calls. */
|
|
75
|
+
[Symbol.dispose](): void;
|
|
76
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mock analytics adapter for tests: records tracked events in-memory instead
|
|
3
|
+
* of exporting them to a backend.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Register it like any other {@link IAnalyticsAdapter} via
|
|
7
|
+
* {@link IAnalyticsConfigurator.setAdapter} — it observes tracked events
|
|
8
|
+
* alongside real adapters without affecting their delivery.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { MockAnalyticsAdapter } from '@equinor/fusion-framework-module-analytics/mock';
|
|
13
|
+
*
|
|
14
|
+
* const recorder = new MockAnalyticsAdapter();
|
|
15
|
+
* enableAnalytics(configurator, (builder) => {
|
|
16
|
+
* builder.setAdapter('mock', async () => recorder);
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* const event = await recorder.waitForAnalytic('button-click');
|
|
20
|
+
* expect(event.attributes?.section).toBe('header');
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @packageDocumentation
|
|
24
|
+
*/
|
|
25
|
+
export { MockAnalyticsAdapter, type AnalyticsEventMatcher, type WaitForAnalyticOptions, } from './MockAnalyticsAdapter.js';
|
package/dist/types/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "3.0.
|
|
1
|
+
export declare const version = "3.0.8-next.0";
|
package/docs/adapters.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Adapters
|
|
2
|
+
|
|
3
|
+
Adapters implement `IAnalyticsAdapter` and are responsible for processing and
|
|
4
|
+
sending analytics data to their destinations. All adapters support async
|
|
5
|
+
initialisation and will be initialised automatically when the provider starts.
|
|
6
|
+
|
|
7
|
+
## ConsoleAnalyticsAdapter
|
|
8
|
+
|
|
9
|
+
Logs every analytics event to the browser console. Useful for development and
|
|
10
|
+
debugging. No configuration required.
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
builder.setAdapter('console', async () => new ConsoleAnalyticsAdapter());
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## FusionAnalyticsAdapter
|
|
17
|
+
|
|
18
|
+
Forwards analytics events to an OpenTelemetry-compatible log endpoint via a
|
|
19
|
+
bundled `LoggerProvider`.
|
|
20
|
+
|
|
21
|
+
Configuration options:
|
|
22
|
+
|
|
23
|
+
| Option | Type | Description |
|
|
24
|
+
|---|---|---|
|
|
25
|
+
| `portalId` | `string` | Portal identifier included in every log record |
|
|
26
|
+
| `logExporter` | `OTLPExporterBase` | OTLP log exporter for transport |
|
|
27
|
+
|
|
28
|
+
### Using `OTLPLogExporter` (direct HTTP)
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { OTLPLogExporter } from '@equinor/fusion-framework-module-analytics/logExporters';
|
|
32
|
+
import { FusionAnalyticsAdapter } from '@equinor/fusion-framework-module-analytics/adapters';
|
|
33
|
+
|
|
34
|
+
builder.setAdapter('fusion-log', async () => {
|
|
35
|
+
const logExporter = new OTLPLogExporter({
|
|
36
|
+
url: 'https://example.com/v1/logs',
|
|
37
|
+
headers: { 'Content-Type': 'application/json' },
|
|
38
|
+
});
|
|
39
|
+
return new FusionAnalyticsAdapter({ portalId: 'my-portal', logExporter });
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Using `FusionOTLPLogExporter` (service discovery HTTP client)
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { FusionOTLPLogExporter } from '@equinor/fusion-framework-module-analytics/logExporters';
|
|
47
|
+
import { FusionAnalyticsAdapter } from '@equinor/fusion-framework-module-analytics/adapters';
|
|
48
|
+
|
|
49
|
+
builder.setAdapter('fusion', async (args) => {
|
|
50
|
+
if (args.hasModule('serviceDiscovery')) {
|
|
51
|
+
const sd = await args.requireInstance('serviceDiscovery');
|
|
52
|
+
const httpClient = await sd.createClient('analytics');
|
|
53
|
+
const logExporter = new FusionOTLPLogExporter(httpClient);
|
|
54
|
+
return new FusionAnalyticsAdapter({ portalId: 'my-portal', logExporter });
|
|
55
|
+
}
|
|
56
|
+
console.error('Service discovery unavailable — analytics adapter not created');
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Creating a Custom Adapter
|
|
61
|
+
|
|
62
|
+
Implement `IAnalyticsAdapter` and register it with `setAdapter`:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import type { IAnalyticsAdapter } from '@equinor/fusion-framework-module-analytics/adapters';
|
|
66
|
+
import type { AnalyticsEvent } from '@equinor/fusion-framework-module-analytics';
|
|
67
|
+
|
|
68
|
+
class MyRemoteAdapter implements IAnalyticsAdapter {
|
|
69
|
+
registerAnalytic(event: AnalyticsEvent): void {
|
|
70
|
+
navigator.sendBeacon('/analytics', JSON.stringify(event));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
[Symbol.dispose](): void {
|
|
74
|
+
// cleanup if needed
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
builder.setAdapter('remote', async () => new MyRemoteAdapter());
|
|
79
|
+
```
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Collectors
|
|
2
|
+
|
|
3
|
+
Collectors implement `IAnalyticsCollector` (or extend `BaseCollector`) and emit
|
|
4
|
+
`AnalyticsEvent` objects that are forwarded to all adapters. All collectors
|
|
5
|
+
support async initialisation.
|
|
6
|
+
|
|
7
|
+
## ContextSelectedCollector
|
|
8
|
+
|
|
9
|
+
Emits an event when the active Fusion context changes. Includes the new context,
|
|
10
|
+
the previous context, and the current app key in attributes.
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
builder.setCollector('context-selected', async (args) => {
|
|
14
|
+
const ctx = await args.requireInstance('context');
|
|
15
|
+
const app = await args.requireInstance('app');
|
|
16
|
+
return new ContextSelectedCollector(ctx, app);
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## AppSelectedCollector
|
|
21
|
+
|
|
22
|
+
Emits an event when the active application changes. Includes the new and
|
|
23
|
+
previous app key metadata.
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
builder.setCollector('app-selected', async (args) => {
|
|
27
|
+
const app = await args.requireInstance('app');
|
|
28
|
+
return new AppSelectedCollector(app);
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## AppLoadedCollector
|
|
33
|
+
|
|
34
|
+
Emits an event when an application's modules finish loading. Includes app
|
|
35
|
+
manifest metadata and the current context (if available).
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
builder.setCollector('app-loaded', async (args) => {
|
|
39
|
+
const event = await args.requireInstance('event');
|
|
40
|
+
const app = await args.requireInstance('app');
|
|
41
|
+
return new AppLoadedCollector(event, app);
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Creating a Custom Collector
|
|
46
|
+
|
|
47
|
+
### Extending `BaseCollector`
|
|
48
|
+
|
|
49
|
+
Extend `BaseCollector` with a Zod schema for validation:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { BaseCollector, createSchema } from '@equinor/fusion-framework-module-analytics/collectors';
|
|
53
|
+
import { z } from 'zod';
|
|
54
|
+
import { of } from 'rxjs';
|
|
55
|
+
|
|
56
|
+
const schema = createSchema(z.string(), z.object({ page: z.string() }));
|
|
57
|
+
|
|
58
|
+
class PageViewCollector extends BaseCollector<string, { page: string }> {
|
|
59
|
+
constructor() {
|
|
60
|
+
super('page-view', schema);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_initialize() {
|
|
64
|
+
return of({ value: window.location.pathname, attributes: { page: document.title } });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Implementing `IAnalyticsCollector` directly
|
|
70
|
+
|
|
71
|
+
For cases that don't need schema validation, implement the `Subscribable` contract directly:
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { type AnalyticsEvent, enableAnalytics } from '@equinor/fusion-framework-module-analytics';
|
|
75
|
+
import { Subject } from 'rxjs';
|
|
76
|
+
|
|
77
|
+
const configure = (configurator: IModulesConfigurator<any, any>) => {
|
|
78
|
+
enableAnalytics(configurator, (builder) => {
|
|
79
|
+
builder.setCollector('click-test', async () => {
|
|
80
|
+
const subject = new Subject<AnalyticsEvent>();
|
|
81
|
+
window.addEventListener('click', () => {
|
|
82
|
+
subject.next({ name: 'window-clicker', value: 42 });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
subscribe: (subscriber) => subject.subscribe(subscriber),
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
```
|
package/docs/testing.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# Testing
|
|
2
|
+
|
|
3
|
+
Use `MockAnalyticsAdapter` from `@equinor/fusion-framework-module-analytics/mock` to assert on tracked analytics events without exporting them to a real backend. Register it like any other adapter via `setAdapter`, then query or await recorded events from your test:
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { enableAnalytics } from '@equinor/fusion-framework-module-analytics';
|
|
7
|
+
import { MockAnalyticsAdapter } from '@equinor/fusion-framework-module-analytics/mock';
|
|
8
|
+
|
|
9
|
+
const recorder = new MockAnalyticsAdapter();
|
|
10
|
+
|
|
11
|
+
enableAnalytics(configurator, (builder) => {
|
|
12
|
+
builder.setAdapter('mock', async () => recorder);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// ... exercise the app under test, then assert:
|
|
16
|
+
const event = await recorder.waitForAnalytic('button-click');
|
|
17
|
+
expect(event.attributes?.section).toBe('header');
|
|
18
|
+
|
|
19
|
+
// or synchronously inspect everything recorded so far:
|
|
20
|
+
expect(recorder.getAnalytics('page-view')).toHaveLength(1);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`MockAnalyticsAdapter` is a genuine `IAnalyticsAdapter` implementation — it works the same way through the real `enableAnalytics` configuration pipeline as `ConsoleAnalyticsAdapter` or `FusionAnalyticsAdapter`, so registering it alongside other adapters doesn't change their behavior.
|
|
24
|
+
|
|
25
|
+
## `getAnalytics(matcher?)`
|
|
26
|
+
|
|
27
|
+
Returns recorded events synchronously, filtered by an event name, an array of names, or a predicate. Omit the matcher to get every recorded event.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
recorder.getAnalytics(); // every recorded event
|
|
31
|
+
recorder.getAnalytics('button-click'); // by name
|
|
32
|
+
recorder.getAnalytics(['button-click', 'page-view']); // any of these names
|
|
33
|
+
recorder.getAnalytics((event) => event.attributes?.section === 'header'); // predicate
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## `waitForAnalytic(matcher, options?)`
|
|
37
|
+
|
|
38
|
+
Resolves with the first matching event — immediately if one was already recorded, or waiting for a future one. Supports an optional `timeout` (ms) and `signal` (`AbortSignal`) so a test can't hang indefinitely, and rejects if the adapter is disposed before a match occurs.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
// Rejects after 1000ms if the event never fires
|
|
42
|
+
const event = await recorder.waitForAnalytic('button-click', { timeout: 1000 });
|
|
43
|
+
|
|
44
|
+
// Rejects as soon as the signal aborts
|
|
45
|
+
const controller = new AbortController();
|
|
46
|
+
const pending = recorder.waitForAnalytic('button-click', { signal: controller.signal });
|
|
47
|
+
controller.abort();
|
|
48
|
+
|
|
49
|
+
await expect(pending).rejects.toThrow();
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Using a bespoke `ModulesConfigurator`
|
|
53
|
+
|
|
54
|
+
`MockAnalyticsAdapter` works the same way with a manually composed set of modules — no app or portal host required:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { ModulesConfigurator } from '@equinor/fusion-framework-module';
|
|
58
|
+
import { enableAnalytics } from '@equinor/fusion-framework-module-analytics';
|
|
59
|
+
import { MockAnalyticsAdapter } from '@equinor/fusion-framework-module-analytics/mock';
|
|
60
|
+
import type { IAnalyticsProvider } from '@equinor/fusion-framework-module-analytics';
|
|
61
|
+
|
|
62
|
+
const recorder = new MockAnalyticsAdapter();
|
|
63
|
+
const configurator = new ModulesConfigurator([]);
|
|
64
|
+
|
|
65
|
+
enableAnalytics(configurator, (builder) => {
|
|
66
|
+
builder.setAdapter('mock', async () => recorder);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// enableAnalytics only registers the module at runtime, so `initialize()` isn't
|
|
70
|
+
// statically typed with an `analytics` property — cast to the real provider type.
|
|
71
|
+
const instances = await configurator.initialize();
|
|
72
|
+
const { analytics } = instances as unknown as { analytics: IAnalyticsProvider };
|
|
73
|
+
analytics.trackAnalytic({ name: 'button-click', value: 'save' });
|
|
74
|
+
|
|
75
|
+
expect(recorder.getAnalytics('button-click')).toHaveLength(1);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Disposal
|
|
79
|
+
|
|
80
|
+
`MockAnalyticsAdapter` completes its internal event stream on `[Symbol.dispose]()`, rejecting any pending `waitForAnalytic` calls instead of leaving them hanging:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
const pending = recorder.waitForAnalytic('button-click');
|
|
84
|
+
recorder[Symbol.dispose]();
|
|
85
|
+
|
|
86
|
+
await expect(pending).rejects.toThrow('disposed before a matching event was recorded');
|
|
87
|
+
```
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Tracking Events Manually
|
|
2
|
+
|
|
3
|
+
The provider exposes methods for ad-hoc event tracking outside of collectors:
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
// Single event
|
|
7
|
+
provider.trackAnalytic({
|
|
8
|
+
name: 'button-click',
|
|
9
|
+
value: 'save',
|
|
10
|
+
attributes: { section: 'toolbar' },
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// Observable stream
|
|
14
|
+
const subscription = provider.trackAnalytic$(myEvent$);
|
|
15
|
+
// later: subscription.unsubscribe();
|
|
16
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@equinor/fusion-framework-module-analytics",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.8-next.0",
|
|
4
4
|
"description": "Fusion module for collecting and exporting application analytics using OpenTelemetry standards",
|
|
5
5
|
"main": "dist/esm/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -20,6 +20,10 @@
|
|
|
20
20
|
"./logExporters": {
|
|
21
21
|
"import": "./dist/esm/logExporters/index.js",
|
|
22
22
|
"types": "./dist/types/logExporters/index.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"./mock": {
|
|
25
|
+
"import": "./dist/esm/mock/index.js",
|
|
26
|
+
"types": "./dist/types/mock/index.d.ts"
|
|
23
27
|
}
|
|
24
28
|
},
|
|
25
29
|
"types": "dist/types/index.d.ts",
|
|
@@ -49,19 +53,19 @@
|
|
|
49
53
|
"rxjs": "^7.8.1",
|
|
50
54
|
"uuid": "^14.0.0",
|
|
51
55
|
"zod": "^4.4.3",
|
|
52
|
-
"@equinor/fusion-framework-module
|
|
53
|
-
"@equinor/fusion-framework-module-
|
|
54
|
-
"@equinor/fusion-framework-module-
|
|
55
|
-
"@equinor/fusion-framework-module": "
|
|
56
|
-
"@equinor/fusion-framework-module-
|
|
56
|
+
"@equinor/fusion-framework-module": "6.1.3-next.0",
|
|
57
|
+
"@equinor/fusion-framework-module-context": "9.0.0-next.0",
|
|
58
|
+
"@equinor/fusion-framework-module-event": "6.1.0-next.0",
|
|
59
|
+
"@equinor/fusion-framework-module-http": "8.1.0-next.0",
|
|
60
|
+
"@equinor/fusion-framework-module-app": "8.0.6-next.0"
|
|
57
61
|
},
|
|
58
62
|
"devDependencies": {
|
|
59
63
|
"typescript": "^7.0.2",
|
|
60
|
-
"vitest": "^4.1.
|
|
61
|
-
"@equinor/fusion-observable": "^9.1.1"
|
|
64
|
+
"vitest": "^4.1.10",
|
|
65
|
+
"@equinor/fusion-observable": "^9.1.2-next.1"
|
|
62
66
|
},
|
|
63
67
|
"peerDependencies": {
|
|
64
|
-
"@equinor/fusion-observable": "9.1.1"
|
|
68
|
+
"@equinor/fusion-observable": "9.1.2-next.1"
|
|
65
69
|
},
|
|
66
70
|
"scripts": {
|
|
67
71
|
"build": "tsc -b",
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { MockAnalyticsAdapter } from '../mock/MockAnalyticsAdapter.js';
|
|
4
|
+
import type { AnalyticsEvent } from '../types.js';
|
|
5
|
+
|
|
6
|
+
const createEvent = (name: string, overrides: Partial<AnalyticsEvent> = {}): AnalyticsEvent => ({
|
|
7
|
+
name,
|
|
8
|
+
value: null,
|
|
9
|
+
...overrides,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('MockAnalyticsAdapter', () => {
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
vi.useRealTimers();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('records events via registerAnalytic and returns them from getAnalytics', () => {
|
|
18
|
+
const adapter = new MockAnalyticsAdapter();
|
|
19
|
+
|
|
20
|
+
adapter.registerAnalytic(createEvent('button-click'));
|
|
21
|
+
adapter.registerAnalytic(createEvent('page-view'));
|
|
22
|
+
|
|
23
|
+
expect(adapter.getAnalytics().map((e) => e.name)).toEqual(['button-click', 'page-view']);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('filters getAnalytics by a single name, an array, and a predicate', () => {
|
|
27
|
+
const adapter = new MockAnalyticsAdapter();
|
|
28
|
+
adapter.registerAnalytic(createEvent('button-click', { attributes: { section: 'header' } }));
|
|
29
|
+
adapter.registerAnalytic(createEvent('page-view'));
|
|
30
|
+
|
|
31
|
+
expect(adapter.getAnalytics('button-click')).toHaveLength(1);
|
|
32
|
+
expect(adapter.getAnalytics(['button-click', 'page-view'])).toHaveLength(2);
|
|
33
|
+
expect(adapter.getAnalytics((e) => e.attributes?.section === 'header')).toHaveLength(1);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('resolves waitForAnalytic immediately when a matching event is already recorded', async () => {
|
|
37
|
+
const adapter = new MockAnalyticsAdapter();
|
|
38
|
+
adapter.registerAnalytic(createEvent('button-click'));
|
|
39
|
+
|
|
40
|
+
const event = await adapter.waitForAnalytic('button-click');
|
|
41
|
+
|
|
42
|
+
expect(event.name).toBe('button-click');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('resolves waitForAnalytic when a matching event is recorded later', async () => {
|
|
46
|
+
const adapter = new MockAnalyticsAdapter();
|
|
47
|
+
|
|
48
|
+
const promise = adapter.waitForAnalytic('button-click');
|
|
49
|
+
adapter.registerAnalytic(createEvent('page-view'));
|
|
50
|
+
adapter.registerAnalytic(createEvent('button-click'));
|
|
51
|
+
const event = await promise;
|
|
52
|
+
|
|
53
|
+
expect(event.name).toBe('button-click');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('resolves waitForAnalytic via predicate matcher', async () => {
|
|
57
|
+
const adapter = new MockAnalyticsAdapter();
|
|
58
|
+
|
|
59
|
+
const promise = adapter.waitForAnalytic((e) => e.attributes?.id === 42);
|
|
60
|
+
adapter.registerAnalytic(createEvent('button-click', { attributes: { id: 1 } }));
|
|
61
|
+
adapter.registerAnalytic(createEvent('button-click', { attributes: { id: 42 } }));
|
|
62
|
+
const event = await promise;
|
|
63
|
+
|
|
64
|
+
expect(event.attributes?.id).toBe(42);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('rejects when the timeout elapses before a matching event is recorded', async () => {
|
|
68
|
+
vi.useFakeTimers();
|
|
69
|
+
const adapter = new MockAnalyticsAdapter();
|
|
70
|
+
|
|
71
|
+
const promise = adapter.waitForAnalytic('button-click', { timeout: 500 });
|
|
72
|
+
vi.advanceTimersByTime(501);
|
|
73
|
+
|
|
74
|
+
await expect(promise).rejects.toThrow('waitForAnalytic timed out after 500ms');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('rejects when the AbortSignal fires before a matching event', async () => {
|
|
78
|
+
const adapter = new MockAnalyticsAdapter();
|
|
79
|
+
const controller = new AbortController();
|
|
80
|
+
|
|
81
|
+
const promise = adapter.waitForAnalytic('button-click', { signal: controller.signal });
|
|
82
|
+
controller.abort();
|
|
83
|
+
|
|
84
|
+
await expect(promise).rejects.toThrow();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('rejects immediately when passed an already-aborted signal', async () => {
|
|
88
|
+
const adapter = new MockAnalyticsAdapter();
|
|
89
|
+
const controller = new AbortController();
|
|
90
|
+
controller.abort();
|
|
91
|
+
|
|
92
|
+
await expect(
|
|
93
|
+
adapter.waitForAnalytic('button-click', { signal: controller.signal }),
|
|
94
|
+
).rejects.toThrow();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('rejects pending waitForAnalytic calls when the adapter is disposed', async () => {
|
|
98
|
+
const adapter = new MockAnalyticsAdapter();
|
|
99
|
+
|
|
100
|
+
const promise = adapter.waitForAnalytic('button-click');
|
|
101
|
+
adapter[Symbol.dispose]();
|
|
102
|
+
|
|
103
|
+
await expect(promise).rejects.toThrow('disposed before a matching event was recorded');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('rejects instead of hanging when a predicate matcher throws on a future event', async () => {
|
|
107
|
+
const adapter = new MockAnalyticsAdapter();
|
|
108
|
+
const boom = new Error('predicate boom');
|
|
109
|
+
|
|
110
|
+
const promise = adapter.waitForAnalytic(() => {
|
|
111
|
+
throw boom;
|
|
112
|
+
});
|
|
113
|
+
adapter.registerAnalytic(createEvent('button-click'));
|
|
114
|
+
|
|
115
|
+
await expect(promise).rejects.toThrow(boom);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('does not interfere with events recorded by another adapter instance', () => {
|
|
119
|
+
const adapterA = new MockAnalyticsAdapter();
|
|
120
|
+
const adapterB = new MockAnalyticsAdapter();
|
|
121
|
+
|
|
122
|
+
adapterA.registerAnalytic(createEvent('button-click'));
|
|
123
|
+
|
|
124
|
+
expect(adapterA.getAnalytics()).toHaveLength(1);
|
|
125
|
+
expect(adapterB.getAnalytics()).toHaveLength(0);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { ModulesConfigurator } from '@equinor/fusion-framework-module';
|
|
3
|
+
import { Subject } from 'rxjs';
|
|
4
|
+
|
|
5
|
+
import { enableAnalytics } from '../../enable-analytics.js';
|
|
6
|
+
import { ConsoleAnalyticsAdapter } from '../../adapters/ConsoleAnalyticsAdapter.js';
|
|
7
|
+
import { MockAnalyticsAdapter } from '../../mock/MockAnalyticsAdapter.js';
|
|
8
|
+
import type { IAnalyticsConfigurator } from '../../AnalyticsConfigurator.interface.js';
|
|
9
|
+
import type { IAnalyticsProvider } from '../../AnalyticsProvider.interface.js';
|
|
10
|
+
import type { AnalyticsEvent } from '../../types.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Initializes the analytics module through the real module system, with a
|
|
14
|
+
* `MockAnalyticsAdapter` registered alongside whatever the test configures.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* Deliberately avoids hand-building an `AnalyticsProvider`. Testing the
|
|
18
|
+
* adapter's own logic in isolation (see `MockAnalyticsAdapter.test.ts`) can't
|
|
19
|
+
* prove it actually receives events through the real configure -> initialize
|
|
20
|
+
* -> collector/adapter dispatch pipeline every other adapter goes through.
|
|
21
|
+
*
|
|
22
|
+
* @param configure - Optional callback to register additional adapters/collectors.
|
|
23
|
+
* @returns The real `IAnalyticsProvider` instance and the recording adapter.
|
|
24
|
+
*/
|
|
25
|
+
const initializeWith = async (
|
|
26
|
+
configure?: (builder: IAnalyticsConfigurator) => void,
|
|
27
|
+
): Promise<{ provider: IAnalyticsProvider; recorder: MockAnalyticsAdapter }> => {
|
|
28
|
+
const recorder = new MockAnalyticsAdapter();
|
|
29
|
+
const configurator = new ModulesConfigurator([]);
|
|
30
|
+
|
|
31
|
+
enableAnalytics(configurator, (builder) => {
|
|
32
|
+
builder.setAdapter('mock', async () => recorder);
|
|
33
|
+
configure?.(builder);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const instances = await configurator.initialize();
|
|
37
|
+
const provider = (instances as unknown as { analytics: IAnalyticsProvider }).analytics;
|
|
38
|
+
|
|
39
|
+
return { provider, recorder };
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
describe('MockAnalyticsAdapter (through the real analytics module)', () => {
|
|
43
|
+
it('observes events pushed via provider.trackAnalytic', async () => {
|
|
44
|
+
const { provider, recorder } = await initializeWith();
|
|
45
|
+
|
|
46
|
+
provider.trackAnalytic({ name: 'button-click', value: 'save' });
|
|
47
|
+
|
|
48
|
+
expect(recorder.getAnalytics('button-click')).toHaveLength(1);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('observes events emitted by a real registered collector', async () => {
|
|
52
|
+
const clicks$ = new Subject<AnalyticsEvent>();
|
|
53
|
+
const { recorder } = await initializeWith((builder) => {
|
|
54
|
+
builder.setCollector('clicks', async () => clicks$);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
clicks$.next({ name: 'window-click', value: 42 });
|
|
58
|
+
|
|
59
|
+
const event = await recorder.waitForAnalytic('window-click');
|
|
60
|
+
expect(event.value).toBe(42);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('does not interfere with other adapters registered alongside it', async () => {
|
|
64
|
+
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined);
|
|
65
|
+
const { provider, recorder } = await initializeWith((builder) => {
|
|
66
|
+
builder.setAdapter('console', async () => new ConsoleAnalyticsAdapter());
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
provider.trackAnalytic({ name: 'page-view', value: null });
|
|
70
|
+
|
|
71
|
+
expect(recorder.getAnalytics('page-view')).toHaveLength(1);
|
|
72
|
+
expect(logSpy).toHaveBeenCalledWith('Analytics::Adapter::Console', {
|
|
73
|
+
name: 'page-view',
|
|
74
|
+
value: null,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
logSpy.mockRestore();
|
|
78
|
+
});
|
|
79
|
+
});
|