Skip to content

Commit

Permalink
[Search][Onboarding] Add telemetry for Start Page code view (#193099)
Browse files Browse the repository at this point in the history
## Summary

Added telemetry tracking events for the start page code view.
Specifically tracking when user selects a language and copies the
example code snippets.

(cherry picked from commit f01f68e)
  • Loading branch information
TattdCodeMonkey committed Sep 20, 2024
1 parent 2b63379 commit 00851e2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/search_indices/public/analytics/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ export enum AnalyticsEvents {
startPageShowCodeClick = 'start_page_show_code',
startPageShowCreateIndexUIClick = 'start_page_show_create_index_ui',
startCreateIndexClick = 'start_create_index',
startCreateIndexLanguageSelect = 'start_code_lang_select',
startCreateIndexCodeCopyInstall = 'start_code_copy_install',
startCreateIndexCodeCopy = 'start_code_copy',
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
// Disabled so we can track the on copy event by adding an onClick to a div
/* eslint-disable jsx-a11y/click-events-have-key-events */

import React from 'react';
import {
Expand All @@ -19,9 +21,22 @@ export interface CodeSampleProps {
title: string;
language: string;
code: string;
onCodeCopyClick?: React.MouseEventHandler<HTMLElement>;
}

export const CodeSample = ({ title, language, code }: CodeSampleProps) => {
export const CodeSample = ({ title, language, code, onCodeCopyClick }: CodeSampleProps) => {
const onCodeClick = React.useCallback(
(e: React.MouseEvent<HTMLElement, MouseEvent>) => {
if (onCodeCopyClick === undefined) return;
if (e.target instanceof HTMLElement) {
if (e.target.dataset?.testSubj === 'euiCodeBlockCopy') {
onCodeCopyClick(e);
}
}
},
[onCodeCopyClick]
);

return (
<EuiFlexItem>
<EuiText size="s">
Expand All @@ -30,15 +45,17 @@ export const CodeSample = ({ title, language, code }: CodeSampleProps) => {
<EuiSpacer size="s" />
<EuiThemeProvider colorMode="dark">
<EuiPanel color="subdued" paddingSize="none" hasShadow={false}>
<EuiCodeBlock
language={language}
fontSize="m"
paddingSize="m"
isCopyable
transparentBackground
>
{code}
</EuiCodeBlock>
<div onClick={onCodeClick}>
<EuiCodeBlock
language={language}
fontSize="m"
paddingSize="m"
isCopyable
transparentBackground
>
{code}
</EuiCodeBlock>
</div>
</EuiPanel>
</EuiThemeProvider>
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { useMemo, useState } from 'react';
import React, { useCallback, useMemo, useState } from 'react';
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { TryInConsoleButton } from '@kbn/try-in-console';

import { useKibana } from '../../hooks/use_kibana';
import { CodeSample } from './code_sample';
import { CreateIndexFormState } from './types';

import { AnalyticsEvents } from '../../analytics/constants';
import { Languages, AvailableLanguages, LanguageOptions } from '../../code_examples';
import { DenseVectorSeverlessCodeExamples } from '../../code_examples/create_index';
import { useUsageTracker } from '../../hooks/use_usage_tracker';
import { useKibana } from '../../hooks/use_kibana';
import { useElasticsearchUrl } from '../../hooks/use_elasticsearch_url';

import { LanguageSelector } from '../shared/language_selector';
import { useElasticsearchUrl } from '../../hooks/use_elasticsearch_url';

import { CodeSample } from './code_sample';
import { CreateIndexFormState } from './types';

export interface CreateIndexCodeViewProps {
createIndexForm: CreateIndexFormState;
Expand All @@ -28,10 +30,21 @@ const SelectedCodeExamples = DenseVectorSeverlessCodeExamples;

export const CreateIndexCodeView = ({ createIndexForm }: CreateIndexCodeViewProps) => {
const { application, share, console: consolePlugin } = useKibana().services;
const usageTracker = useUsageTracker();

// TODO: initing this should be dynamic and possibly saved in the form state
const [selectedLanguage, setSelectedLanguage] = useState<AvailableLanguages>('python');
const onSelectLanguage = useCallback(
(value: AvailableLanguages) => {
setSelectedLanguage(value);
usageTracker.count([
AnalyticsEvents.startCreateIndexLanguageSelect,
`${AnalyticsEvents.startCreateIndexLanguageSelect}_${value}`,
]);
},
[usageTracker]
);
const elasticsearchUrl = useElasticsearchUrl();

const codeParams = useMemo(() => {
return {
indexName: createIndexForm.indexName || undefined,
Expand All @@ -49,7 +62,7 @@ export const CreateIndexCodeView = ({ createIndexForm }: CreateIndexCodeViewProp
<LanguageSelector
options={LanguageOptions}
selectedLanguage={selectedLanguage}
onSelectLanguage={(value) => setSelectedLanguage(value)}
onSelectLanguage={onSelectLanguage}
/>
</EuiFlexItem>
{selectedLanguage === 'curl' && (
Expand All @@ -70,6 +83,12 @@ export const CreateIndexCodeView = ({ createIndexForm }: CreateIndexCodeViewProp
})}
language="shell"
code={selectedCodeExample.installCommand}
onCodeCopyClick={() => {
usageTracker.click([
AnalyticsEvents.startCreateIndexCodeCopyInstall,
`${AnalyticsEvents.startCreateIndexCodeCopyInstall}_${selectedLanguage}`,
]);
}}
/>
)}
<CodeSample
Expand All @@ -78,6 +97,14 @@ export const CreateIndexCodeView = ({ createIndexForm }: CreateIndexCodeViewProp
})}
language={Languages[selectedLanguage].codeBlockLanguage}
code={selectedCodeExample.createIndex(codeParams)}
onCodeCopyClick={() => {
usageTracker.click([
AnalyticsEvents.startCreateIndexCodeCopy,
`${AnalyticsEvents.startCreateIndexCodeCopy}_${selectedLanguage}`,
// TODO: vector should be a parameter when have multiple options
`${AnalyticsEvents.startCreateIndexCodeCopy}_${selectedLanguage}_vector`,
]);
}}
/>
</EuiFlexGroup>
);
Expand Down

0 comments on commit 00851e2

Please sign in to comment.