Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/lib/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const GA_MEASUREMENT_ID = "G-M2NP1M6YSK";
const isAnalyticsEnabled = import.meta.env.PROD;

let initialized = false;
let webMcpAvailabilityTracked = false;

const cleanParams = (params) =>
Object.fromEntries(
Expand Down Expand Up @@ -115,3 +116,39 @@ export const trackTruncationWarning = () => {
has_results: true,
});
};

export const trackWebMcpAvailable = () => {
if (!isAnalyticsEnabled || webMcpAvailabilityTracked) return;

initializeAnalytics();
webMcpAvailabilityTracked = true;
ReactGA.event("webmcp_available");
};

export const trackWebMcpToolCompleted = ({
toolName,
durationMs,
resultCount,
}) => {
if (!isAnalyticsEnabled) return;

initializeAnalytics();
ReactGA.event(
"webmcp_tool_completed",
cleanParams({
tool_name: toolName,
duration_ms: durationMs,
result_count: resultCount,
}),
);
};

export const trackWebMcpToolFailed = ({ toolName, durationMs }) => {
if (!isAnalyticsEnabled) return;

initializeAnalytics();
ReactGA.event("webmcp_tool_failed", {
tool_name: toolName,
duration_ms: durationMs,
});
};
16 changes: 16 additions & 0 deletions src/lib/webmcp/__tests__/useScheduleWebMcp.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
import { render, waitFor } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { useScheduleWebMcp } from "../useScheduleWebMcp";
import { trackWebMcpAvailable, trackWebMcpToolCompleted } from "@/lib/metrics";

vi.mock("@/lib/metrics", () => ({
trackWebMcpAvailable: vi.fn(),
trackWebMcpToolCompleted: vi.fn(),
trackWebMcpToolFailed: vi.fn(),
}));

const setters = {
setTimetables: vi.fn(),
Expand Down Expand Up @@ -42,6 +49,7 @@ describe("useScheduleWebMcp", () => {

const { unmount } = render(<Harness />);
await waitFor(() => expect(registerTool).toHaveBeenCalledTimes(15));
expect(trackWebMcpAvailable).toHaveBeenCalledTimes(1);

expect(
registerTool.mock.calls.map(([definition]) => definition.name),
Expand All @@ -63,6 +71,14 @@ describe("useScheduleWebMcp", () => {
"exportSchedule",
]);

const currentScheduleTool = registerTool.mock.calls
.map(([definition]) => definition)
.find(({ name }) => name === "getCurrentSchedule");
await currentScheduleTool.execute({});
expect(trackWebMcpToolCompleted).toHaveBeenCalledWith(
expect.objectContaining({ toolName: "getCurrentSchedule" }),
);

unmount();
expect(
registerTool.mock.calls.every(([, options]) => options.signal.aborted),
Expand Down
23 changes: 22 additions & 1 deletion src/lib/webmcp/useScheduleWebMcp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,39 @@ import {
rankScheduleOptions,
toTimeSlots,
} from "./scheduleOptions";
import {
trackWebMcpAvailable,
trackWebMcpToolCompleted,
trackWebMcpToolFailed,
} from "@/lib/metrics";

const json = (value) => JSON.stringify(value);
const asArray = (value) => (Array.isArray(value) ? value : []);
const resultCount = (result) =>
[result.totalOptions, result.totalMatches, result.conflictFreeOptions].find(
Number.isFinite,
);

const tool = (name, description, inputSchema, execute, annotations = {}) => ({
name,
description,
inputSchema,
annotations,
execute: async (input, context) => {
const startedAt = performance.now();
try {
return json({ ok: true, ...(await execute(input || {}, context || {})) });
const result = await execute(input || {}, context || {});
trackWebMcpToolCompleted({
toolName: name,
durationMs: Math.round(performance.now() - startedAt),
resultCount: resultCount(result),
});
return json({ ok: true, ...result });
} catch (error) {
trackWebMcpToolFailed({
toolName: name,
durationMs: Math.round(performance.now() - startedAt),
});
return json({
ok: false,
error:
Expand Down Expand Up @@ -380,6 +400,7 @@ export function useScheduleWebMcp({
useEffect(() => {
const modelContext = document.modelContext;
if (!modelContext?.registerTool) return undefined;
trackWebMcpAvailable();

const tools = [
tool(
Expand Down
Loading