From 828c2b661287edd5857946331950a4491040548b Mon Sep 17 00:00:00 2001 From: Collin Fingar Date: Mon, 18 Aug 2025 10:13:04 -0400 Subject: [PATCH] QueryLibrary: Added new way to trigger events w/ context (#109637) --- .../features/explore/QueryLibrary/QueryLibraryContext.tsx | 8 +++++++- public/app/features/explore/QueryLibrary/mocks.tsx | 1 + public/app/features/explore/QueryLibrary/types.ts | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx b/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx index 1e0e571f8e4..1ac7ef7cd97 100644 --- a/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx +++ b/public/app/features/explore/QueryLibrary/QueryLibraryContext.tsx @@ -3,7 +3,7 @@ import { createContext, ReactNode, useContext } from 'react'; import { CoreApp } from '@grafana/data'; import { DataQuery } from '@grafana/schema'; -import { OnSelectQueryType, QueryTemplate } from './types'; +import { OnSelectQueryType, QueryTemplate, QueryLibraryEventsPropertyMap } from './types'; export type QueryLibraryDrawerOptions = { datasourceFilters?: string[]; @@ -46,6 +46,11 @@ export type QueryLibraryContextType = { ) => ReactNode; queryLibraryEnabled: boolean; context: string; + triggerAnalyticsEvent: ( + handleAnalyticEvent: (properties?: QueryLibraryEventsPropertyMap) => void, + properties?: QueryLibraryEventsPropertyMap, + contextOverride?: string + ) => void; setNewQuery: (query?: QueryTemplate) => void; }; @@ -63,6 +68,7 @@ export const QueryLibraryContext = createContext({ queryLibraryEnabled: false, context: 'unknown', + triggerAnalyticsEvent: () => {}, }); export function useQueryLibraryContext() { diff --git a/public/app/features/explore/QueryLibrary/mocks.tsx b/public/app/features/explore/QueryLibrary/mocks.tsx index ccb6b1c6b99..2c25a64e0bc 100644 --- a/public/app/features/explore/QueryLibrary/mocks.tsx +++ b/public/app/features/explore/QueryLibrary/mocks.tsx @@ -16,6 +16,7 @@ export function QueryLibraryContextProviderMock(props: PropsWithChildren) renderSaveQueryButton: jest.fn(), queryLibraryEnabled: Boolean(props.queryLibraryEnabled), context: 'explore', + triggerAnalyticsEvent: jest.fn(), setNewQuery: jest.fn(), }} > diff --git a/public/app/features/explore/QueryLibrary/types.ts b/public/app/features/explore/QueryLibrary/types.ts index 1877b30c3df..e8890c0b778 100644 --- a/public/app/features/explore/QueryLibrary/types.ts +++ b/public/app/features/explore/QueryLibrary/types.ts @@ -8,6 +8,8 @@ export type User = { export type OnSelectQueryType = (query: DataQuery) => void; +export type QueryLibraryEventsPropertyMap = Record; + export type QueryTemplate = { query: DataQuery; datasourceName?: string;