Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] [Search][Onboarding] Add telemetry for Start Page code view (#193099) #193641

Merged
merged 1 commit into from
Sep 23, 2024
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
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