Skip to content

Commit 0b2cac4

Browse files
committed
fix(analytics): rename useAnalytics to useTrackFeature
1 parent 0a93c4d commit 0b2cac4

File tree

5 files changed

+63
-57
lines changed

5 files changed

+63
-57
lines changed

cookbooks/portal-analytics/src/components/LogReader.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@ import { SideSheet } from '@equinor/fusion-react-side-sheet';
66
import { Button, Icon } from '@equinor/eds-core-react';
77
import { info_circle } from '@equinor/eds-icons';
88
import { DateRange } from '@equinor/fusion-react-date';
9-
import { useAnalytics } from '@equinor/fusion-framework-react-app/analytics';
9+
import { useTrackFeature } from '@equinor/fusion-framework-react-app/analytics';
1010

1111
export const LogReader = () => {
1212
const [activeLogEntry, setActiveLogEntry] = useState<LogEntry | null>(null);
1313
const [entries, setEntries] = useState<LogEntry[]>([]);
1414
const [error, setError] = useState<string | null>(null);
15-
const analytics = useAnalytics();
15+
const trackFeature = useTrackFeature();
1616

1717
const fetchLogs = useCallback(async () => {
18-
analytics?.trackAnalytic({
19-
name: 'cookbook:portal-analytics:logs:fetch',
20-
value: undefined,
21-
});
18+
trackFeature('cookbook:portal-analytics:logs:fetch');
2219

2320
try {
2421
const response = await fetch('/@fusion-api/logs');
@@ -34,16 +31,13 @@ export const LogReader = () => {
3431
} catch (err) {
3532
setError(`Failed to fetch logs. ${err}`);
3633
}
37-
}, [analytics]);
34+
}, [trackFeature]);
3835

3936
const clearLogs = useCallback(async () => {
40-
analytics?.trackAnalytic({
41-
name: 'cookbook:portal-analytics:logs:clear',
42-
value: undefined,
43-
});
37+
trackFeature('cookbook:portal-analytics:logs:clear');
4438
await fetch('/@fusion-api/api/clearlogs');
4539
fetchLogs();
46-
}, [fetchLogs, analytics]);
40+
}, [fetchLogs, trackFeature]);
4741

4842
// biome-ignore lint/correctness/useExhaustiveDependencies: only run on mount
4943
useEffect(() => {
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
## Analytics
22

3-
### useAnalytics
3+
### useTrackFeature
44

5-
A hook to send analytic events.
5+
A hook to send feature used.
66

77
```typescript
8-
const { trackAnalytic } = useAnalytics();
8+
const trackFeature = useTrackFeature();
99

10-
trackAnalytic({
11-
name: 'foo',
12-
value: 'bar',
13-
attributes: {
14-
extra: 'data',
15-
}
16-
});
10+
trackFeature('foo');
1711
```
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { useAnalytics } from './useAnalytics';
1+
export { useTrackFeature } from './useTrackFeature';

packages/react/app/src/analytics/useAnalytics.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { useFrameworkModule } from '@equinor/fusion-framework-react';
2+
import type { AppModule } from '@equinor/fusion-framework-module-app';
3+
import type { AnalyticsModule } from '@equinor/fusion-framework-module-analytics';
4+
import { useCallback } from 'react';
5+
6+
/**
7+
* Hook for using analytics module configured in the framework.
8+
*/
9+
export const useTrackFeature = () => {
10+
const analyticsProvider = useFrameworkModule<AnalyticsModule>('analytics');
11+
const appProvider = useFrameworkModule<AppModule>('app');
12+
const contextProvider = useFrameworkModule('context');
13+
const telemetryProvider = useFrameworkModule('telemetry');
14+
15+
/**
16+
* Tracks a analytics event.
17+
*
18+
* @param name - The feature to track
19+
*/
20+
const trackFeature = useCallback(
21+
(name: string) => {
22+
if (!analyticsProvider) {
23+
telemetryProvider?.trackException({
24+
name: 'AnalyticsProviderNotFound',
25+
exception: new Error(`Analytics provider not found`),
26+
});
27+
}
28+
29+
analyticsProvider?.trackAnalytic({
30+
name: 'app-feature',
31+
value: {
32+
feature: name,
33+
},
34+
attributes: {
35+
appKey: appProvider?.current?.appKey,
36+
context: contextProvider?.currentContext
37+
? {
38+
id: contextProvider.currentContext.id,
39+
externalId: contextProvider.currentContext.externalId,
40+
title: contextProvider.currentContext.title,
41+
type: contextProvider.currentContext.type.id,
42+
source: contextProvider.currentContext.source,
43+
}
44+
: undefined,
45+
},
46+
});
47+
},
48+
[analyticsProvider, appProvider, contextProvider, telemetryProvider],
49+
);
50+
51+
return trackFeature;
52+
};

0 commit comments

Comments
 (0)