-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Search][Homepage] Add Telemetry support (#188364)
## Summary Adds support for telemetry tracking to Search Homepage for both ES3 & Stack. Additionally fixed a small markup bug with the page header.
- Loading branch information
1 parent
5756b29
commit ef2d7b2
Showing
11 changed files
with
141 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
x-pack/plugins/search_homepage/public/contexts/usage_tracker_context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { createContext, useContext, useMemo } from 'react'; | ||
import type { | ||
UsageCollectionSetup, | ||
UsageCollectionStart, | ||
} from '@kbn/usage-collection-plugin/public'; | ||
|
||
import { createUsageTracker, createEmptyUsageTracker } from '../usage_tracker'; | ||
import { AppUsageTracker } from '../types'; | ||
|
||
const UsageTrackerContext = createContext<AppUsageTracker>(createEmptyUsageTracker()); | ||
|
||
export interface UsageTrackerContextProviderProps { | ||
children: React.ReactNode | React.ReactNode[]; | ||
usageCollection?: UsageCollectionSetup | UsageCollectionStart; | ||
} | ||
export function UsageTrackerContextProvider({ | ||
children, | ||
usageCollection, | ||
}: UsageTrackerContextProviderProps) { | ||
const usageTracker = useMemo(() => { | ||
const homePageUsageTracker = createUsageTracker(usageCollection); | ||
homePageUsageTracker.load('opened_app'); | ||
return homePageUsageTracker; | ||
}, [usageCollection]); | ||
return ( | ||
<UsageTrackerContext.Provider value={usageTracker}>{children}</UsageTrackerContext.Provider> | ||
); | ||
} | ||
|
||
export const useUsageTracker = () => { | ||
const ctx = useContext(UsageTrackerContext); | ||
if (!ctx) { | ||
throw new Error('UsageTrackerContext should be used inside of the UsageTrackerContextProvider'); | ||
} | ||
return ctx; | ||
}; |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/search_homepage/public/hooks/use_usage_tracker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { useUsageTracker } from '../contexts/usage_tracker_context'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { METRIC_TYPE, UiCounterMetricType } from '@kbn/analytics'; | ||
import type { | ||
UsageCollectionSetup, | ||
UsageCollectionStart, | ||
} from '@kbn/usage-collection-plugin/public'; | ||
import { AppUsageTracker } from './types'; | ||
|
||
const APP_TRACKER_NAME = 'searchHomepage'; | ||
|
||
export function createUsageTracker( | ||
usageCollection?: UsageCollectionSetup | UsageCollectionStart | ||
): AppUsageTracker { | ||
const track = (type: UiCounterMetricType, name: string | string[]) => | ||
usageCollection?.reportUiCounter(APP_TRACKER_NAME, type, name); | ||
|
||
return { | ||
click: (eventName: string | string[]) => { | ||
track(METRIC_TYPE.CLICK, eventName); | ||
}, | ||
count: (eventName: string | string[]) => { | ||
track(METRIC_TYPE.COUNT, eventName); | ||
}, | ||
load: (eventName: string | string[]) => { | ||
track(METRIC_TYPE.LOADED, eventName); | ||
}, | ||
}; | ||
} | ||
|
||
export function createEmptyUsageTracker(): AppUsageTracker { | ||
return { | ||
click: (_eventName: string | string[]) => {}, | ||
count: (_eventName: string | string[]) => {}, | ||
load: (_eventName: string | string[]) => {}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters