Add Telemetry (#466)
* Telemetry v1 * Add package-lock.json to gitignore
This commit is contained in:
32
front/src/modules/analytics/hooks/useEventTracker.ts
Normal file
32
front/src/modules/analytics/hooks/useEventTracker.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import { useCreateEventMutation } from '~/generated/graphql';
|
||||
|
||||
import { useIsTelemetryEnabled } from './useIsTelemetryEnabled';
|
||||
|
||||
interface EventLocation {
|
||||
pathname: string;
|
||||
}
|
||||
|
||||
export interface EventData {
|
||||
location: EventLocation;
|
||||
}
|
||||
|
||||
export function useEventTracker() {
|
||||
const telemetryEnabled = useIsTelemetryEnabled();
|
||||
const [createEventMutation] = useCreateEventMutation();
|
||||
|
||||
return useCallback(
|
||||
(eventType: string, eventData: EventData) => {
|
||||
if (telemetryEnabled) {
|
||||
createEventMutation({
|
||||
variables: {
|
||||
type: eventType,
|
||||
data: eventData,
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
[createEventMutation, telemetryEnabled],
|
||||
);
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
export function useIsTelemetryEnabled() {
|
||||
// TODO: replace by clientConfig
|
||||
return process.env.IS_TELEMETRY_ENABLED !== 'false';
|
||||
}
|
||||
7
front/src/modules/analytics/hooks/useTrackEvent.ts
Normal file
7
front/src/modules/analytics/hooks/useTrackEvent.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { EventData, useEventTracker } from './useEventTracker';
|
||||
|
||||
export function useTrackEvent(eventType: string, eventData: EventData) {
|
||||
const eventTracker = useEventTracker();
|
||||
|
||||
return eventTracker(eventType, eventData);
|
||||
}
|
||||
17
front/src/modules/analytics/hooks/useTrackPageView.ts
Normal file
17
front/src/modules/analytics/hooks/useTrackPageView.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
|
||||
import { useEventTracker } from './useEventTracker';
|
||||
|
||||
export function useTrackPageView() {
|
||||
const location = useLocation();
|
||||
const eventTracker = useEventTracker();
|
||||
|
||||
useEffect(() => {
|
||||
eventTracker('pageview', {
|
||||
location: {
|
||||
pathname: location.pathname,
|
||||
},
|
||||
});
|
||||
}, [location, eventTracker]);
|
||||
}
|
||||
9
front/src/modules/analytics/services/index.ts
Normal file
9
front/src/modules/analytics/services/index.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { gql } from '@apollo/client';
|
||||
|
||||
export const CREATE_EVENT = gql`
|
||||
mutation CreateEvent($type: String!, $data: JSON!) {
|
||||
createEvent(type: $type, data: $data) {
|
||||
success
|
||||
}
|
||||
}
|
||||
`;
|
||||
Reference in New Issue
Block a user