Skip to content

Commit

Permalink
chore(ui): add analytic event to metrics chart views (#3570)
Browse files Browse the repository at this point in the history
  • Loading branch information
gtarpenning authored Feb 11, 2025
1 parent dbfe828 commit 59c726f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {GridFilterModel, GridSortModel} from '@mui/x-data-grid-pro';
import React, {useMemo} from 'react';
import React, {useEffect, useMemo, useRef, useState} from 'react';

import {MOON_400} from '../../../../../../common/css/color.styles';
import * as userEvents from '../../../../../../integrations/analytics/userEvents';
import {IconInfo} from '../../../../../Icon';
import {WaveLoader} from '../../../../../Loaders/WaveLoader';
import {Tailwind} from '../../../../../Tailwind';
Expand Down Expand Up @@ -114,6 +115,8 @@ export const CallsCharts = ({
columns
);

useFireAnalyticsForMetricsPlotsViewed(entity, project, calls.loading);

const chartData = useMemo(() => {
if (calls.loading || !calls.result || calls.result.length === 0) {
return {latency: [], errors: [], requests: []};
Expand Down Expand Up @@ -188,3 +191,37 @@ export const CallsCharts = ({
</Tailwind>
);
};

/**
* Fires an analytics event when the metrics plots are viewed.
* This is used to track the usage and latency of the metrics plots.
* Only fires once when opened.
*/
const useFireAnalyticsForMetricsPlotsViewed = (
entity: string,
project: string,
loading: boolean
) => {
const [callsQueryStartTime, setCallsQueryStartTime] = useState<number | null>(
null
);
const sentEvent = useRef(false);
useEffect(() => {
if (sentEvent.current) {
return;
}
if (loading) {
const startTime = Date.now();
setCallsQueryStartTime(startTime);
} else if (!loading && callsQueryStartTime !== null) {
const endTime = Date.now();
const latency = endTime - callsQueryStartTime;
userEvents.metricsPlotsViewed({
entity,
project,
latency,
});
sentEvent.current = true;
}
}, [loading, callsQueryStartTime, entity, project]);
};
25 changes: 25 additions & 0 deletions weave-js/src/integrations/analytics/userEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,28 @@ export const callTreeCellClicked = makeTrackEvent<
};
}
>('Weave call tree cell clicked');

export const metricsPlotsViewed = makeTrackEvent<
{
entity: string;
project: string;
latency: number;
},
{
_description: `User viewed metrics plots`;
_location: '';
_motivation: 'Used for tracking metrics plots';
entity: {
description: 'Entity of call';
exampleValues: ['my-entity'];
};
project: {
description: 'Project of call';
exampleValues: ['my-project'];
};
latency: {
description: 'Latency of calls stream query (ms)';
exampleValues: [1000, 500];
};
}
>('Weave metrics plots viewed');

0 comments on commit 59c726f

Please sign in to comment.