Skip to content

Commit 3172d49

Browse files
authored
Merge pull request #5204 from aws-amplify/copyTracking
Code Copy Tracking
2 parents 951dff2 + a154614 commit 3172d49

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/components/CodeBlock/index.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import {
22
CodeBlockStyle,
33
CodeHighlightStyle,
44
CopyButtonStyle,
5-
LineCountStyle,
6-
} from "./styles";
7-
import React from "react";
8-
import copy from "copy-to-clipboard";
5+
LineCountStyle
6+
} from './styles';
7+
import React from 'react';
8+
import copy from 'copy-to-clipboard';
9+
import { trackCopyClicks } from '../../utils/track';
910

10-
const COPY = "copy";
11-
const COPIED = "copied";
12-
const FAILED = "failed to copy";
13-
const CONSOLE = "console";
11+
const COPY = 'copy';
12+
const COPIED = 'copied';
13+
const FAILED = 'failed to copy';
14+
const CONSOLE = 'console';
1415

1516
type CopyMessageType = typeof COPY | typeof COPIED | typeof FAILED;
1617

@@ -29,7 +30,7 @@ class CodeBlock extends React.Component<CodeBlockProps, CodeBlockState> {
2930

3031
constructor(props) {
3132
super(props);
32-
this.state = {copyMessage: COPY};
33+
this.state = { copyMessage: COPY };
3334
}
3435

3536
lineNumbers = () => {
@@ -56,12 +57,13 @@ class CodeBlock extends React.Component<CodeBlockProps, CodeBlockState> {
5657
copyToClipboard = () => {
5758
if (this.element && this.element.textContent) {
5859
copy(this.element.textContent);
59-
this.setState({copyMessage: COPIED});
60+
trackCopyClicks(this.element.textContent);
61+
this.setState({ copyMessage: COPIED });
6062
} else {
61-
this.setState({copyMessage: FAILED});
63+
this.setState({ copyMessage: FAILED });
6264
}
6365
setTimeout(() => {
64-
this.setState({copyMessage: COPY});
66+
this.setState({ copyMessage: COPY });
6567
}, 1500);
6668
};
6769

@@ -78,7 +80,7 @@ class CodeBlock extends React.Component<CodeBlockProps, CodeBlockState> {
7880
render() {
7981
if (this.props.children === undefined) return <div></div>;
8082
const oneLine =
81-
this.props.lineCount === "1" || this.props.language === CONSOLE;
83+
this.props.lineCount === '1' || this.props.language === CONSOLE;
8284

8385
return (
8486
<CodeBlockStyle oneLine={oneLine}>

src/utils/track.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,20 @@ export const trackFeedbackSubmission = (feedback: boolean) => {
159159
}
160160
}
161161

162+
AWSMA.ready(() => {
163+
document.dispatchEvent(new CustomEvent(AWSMA.TRIGGER_EVENT, {detail: opt}));
164+
});
165+
}
166+
167+
export const trackCopyClicks = (data) => {
168+
const opt = {
169+
event: {
170+
type: 'click',
171+
name: 'CopyCode'
172+
},
173+
data: data
174+
}
175+
162176
AWSMA.ready(() => {
163177
document.dispatchEvent(new CustomEvent(AWSMA.TRIGGER_EVENT, {detail: opt}));
164178
});

0 commit comments

Comments
 (0)