Skip to content

Commit 8f53aa8

Browse files
authored
tweak(gtm): optimize events (#558)
* feat(gtm): add search type tracing * tweak(gtm): remove ask_tidb_ai event * tweak(gtm): remove docs_download * tweak(gtm): remove signin_click/go_to_cloud_signup * chore: update GTMEvent * tweak: reading rate * chore: remove console * chore: update comment * fix: min of time ratio
1 parent dd3be74 commit 8f53aa8

File tree

7 files changed

+76
-40
lines changed

7 files changed

+76
-40
lines changed

gatsby-node.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
require("ts-node").register({ transpileOnly: true });
22
require("dotenv").config();
33

4-
const path = require("path");
5-
64
const {
75
createDocs,
86
createDocHome,

src/components/Layout/HeaderAction.tsx

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import CloudIcon from "@mui/icons-material/Cloud";
1818
import Search from "components/Search";
1919

2020
import { Locale, BuildType } from "shared/interface";
21-
import { GTMEvent, gtmTrack } from "shared/utils/gtm";
2221
import { ActionButton } from "components/Card/FeedbackSection/components";
2322
import { Link } from "gatsby";
2423

@@ -96,9 +95,6 @@ export default function HeaderAction(props: {
9695
}}
9796
onClick={() => {
9897
window.tidbai.open = true;
99-
gtmTrack(GTMEvent.AskTiDBAI, {
100-
position: "header",
101-
});
10298
}}
10399
>
104100
Ask AI
@@ -251,11 +247,6 @@ const TiDBCloudBtnGroup = () => {
251247
// https://developer.chrome.com/blog/referrer-policy-new-chrome-default/
252248
referrerPolicy="no-referrer-when-downgrade"
253249
target="_blank"
254-
onClick={() =>
255-
gtmTrack(GTMEvent.SigninCloud, {
256-
position: "header",
257-
})
258-
}
259250
>
260251
Sign In
261252
</Button>
@@ -265,13 +256,6 @@ const TiDBCloudBtnGroup = () => {
265256
href="https://tidbcloud.com/free-trial"
266257
// https://developer.chrome.com/blog/referrer-policy-new-chrome-default/
267258
referrerPolicy="no-referrer-when-downgrade"
268-
onClick={() =>
269-
gtmTrack(GTMEvent.SignupCloud, {
270-
product_type: "general cloud",
271-
button_name: "Start for free",
272-
position: "header",
273-
})
274-
}
275259
>
276260
Start for Free
277261
</Button>
@@ -302,12 +286,6 @@ const TiDBCloudBtnGroup = () => {
302286
}}
303287
>
304288
<MenuItem
305-
onClick={() => {
306-
handleClose();
307-
gtmTrack(GTMEvent.SigninCloud, {
308-
position: "header",
309-
});
310-
}}
311289
component={Link}
312290
to={`https://tidbcloud.com/signin`}
313291
target="_blank"
@@ -321,11 +299,6 @@ const TiDBCloudBtnGroup = () => {
321299
<MenuItem
322300
onClick={() => {
323301
handleClose();
324-
gtmTrack(GTMEvent.SignupCloud, {
325-
product_type: "general cloud",
326-
button_name: "Try Free",
327-
position: "header",
328-
});
329302
}}
330303
component={Link}
331304
to={`https://tidbcloud.com/free-trial`}

src/components/Navigation/RightNav.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ export default function RightNav(props: RightNavProps) {
8686
label={t("doc.download-pdf")}
8787
rel="noreferrer"
8888
download
89-
onClick={() => {
90-
gtmTrack(GTMEvent.DownloadPDF, {
91-
position: "right_nav",
92-
});
93-
}}
9489
/>
9590
)}
9691
{buildType !== "archive" && (

src/components/Search/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { styled } from "@mui/material/styles";
1010
import SearchIcon from "@mui/icons-material/Search";
1111
import { Card, MenuItem, Popper, PopperProps } from "@mui/material";
1212
import { Locale } from "shared/interface";
13+
import { GTMEvent, gtmTrack } from "shared/utils/gtm";
1314

1415
const StyledTextField = styled((props: TextFieldProps) => (
1516
<TextField {...props} />
@@ -67,6 +68,7 @@ export default function Search(props: {
6768
inputEl.current?.blur();
6869

6970
if (searchType === SearchType.Onsite) {
71+
gtmTrack(GTMEvent.UseOnsiteSearch);
7072
navigate(
7173
`/search?type=${docInfo.type}&version=${docInfo.version}&q=${q}`,
7274
{
@@ -85,6 +87,9 @@ export default function Search(props: {
8587
}`;
8688

8789
if (searchType === SearchType.Google) {
90+
gtmTrack(GTMEvent.UseExternalSearch, {
91+
search_type: searchType,
92+
});
8893
window.open(
8994
`https://www.google.com/search?q=site%3Adocs.pingcap.com/${segmentPath}+${q}`,
9095
"_blank"
@@ -93,6 +98,9 @@ export default function Search(props: {
9398
}
9499

95100
if (searchType === SearchType.Bing) {
101+
gtmTrack(GTMEvent.UseExternalSearch, {
102+
search_type: searchType,
103+
});
96104
window.open(
97105
`https://cn.bing.com/search?q=site%3Adocs.pingcap.com/${segmentPath}+${q}`,
98106
"_blank"

src/shared/useReportReadingRate.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import throttle from "lodash/throttle";
2+
import React from "react";
3+
import { GTMEvent, gtmTrack } from "./utils/gtm";
4+
5+
// timeToRead: minutes of reading
6+
export const useReportReadingRate = (timeToRead: number) => {
7+
const lastPercentage = React.useRef(0);
8+
9+
const handleScroll = throttle(() => {
10+
const scrollTop = window.scrollY || document.documentElement.scrollTop;
11+
const clientHeight = window.innerHeight;
12+
const scrollHeight = document.documentElement.scrollHeight;
13+
14+
const scrollPercentage = Math.min(
15+
(scrollTop / (scrollHeight - clientHeight)) * 100,
16+
100
17+
);
18+
19+
if (
20+
Math.floor(scrollPercentage / 10) >
21+
Math.floor(lastPercentage.current / 10)
22+
) {
23+
lastPercentage.current = Math.floor(scrollPercentage / 10) * 10;
24+
}
25+
}, 1000);
26+
27+
const startTime = React.useRef(Date.now());
28+
const timeToReadSecond = timeToRead * 60;
29+
30+
const reportReadingRate = () => {
31+
const endTime = Date.now();
32+
const timeSpent = Math.floor((endTime - startTime.current) / 1000);
33+
34+
if (document.visibilityState === "hidden") {
35+
if (timeSpent < 6) {
36+
return;
37+
}
38+
gtmTrack(GTMEvent.ReadingRate, {
39+
readingRate:
40+
Math.min(Math.floor((timeSpent / timeToReadSecond) * 100) / 100, 1) *
41+
lastPercentage.current,
42+
timeToRead: timeToReadSecond,
43+
timeSpent,
44+
scrollPercentage: lastPercentage.current,
45+
});
46+
} else {
47+
startTime.current = Date.now();
48+
}
49+
};
50+
51+
React.useEffect(() => {
52+
window.addEventListener("visibilitychange", reportReadingRate);
53+
window.addEventListener("scroll", handleScroll);
54+
return () => {
55+
window.removeEventListener("visibilitychange", reportReadingRate);
56+
window.removeEventListener("scroll", handleScroll);
57+
};
58+
}, []);
59+
};

src/shared/utils/gtm.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
export const GTMEvent = {
22
ClickHeadNav: "docs_navi_click",
33
ClickFooter: "docs_footer_click",
4-
SignupCloud: "go_to_cloud_signup",
5-
SigninCloud: "signin_click",
6-
GotoPlayground: "go_to_playground",
7-
DownloadPDF: "docs_download",
8-
AskTiDBAI: "ask_tidb_ai",
4+
UseOnsiteSearch: "use_onsite_search",
5+
UseExternalSearch: "use_external_search",
6+
ReadingRate: "reading_rate",
97
} as const;
108

119
export function gtmTrack(

src/templates/DocTemplate.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import GitCommitInfoCard from "components/Card/GitCommitInfoCard";
2525
import { FeedbackSection } from "components/Card/FeedbackSection";
2626
import { FeedbackSurveyCampaign } from "components/Campaign/FeedbackSurvey";
2727
import { DOC_HOME_URL } from "shared/resources";
28+
import { useReportReadingRate } from "shared/useReportReadingRate";
2829

2930
interface DocTemplateProps {
3031
pageContext: PageContext & {
@@ -46,6 +47,7 @@ interface DocTemplateProps {
4647
frontmatter: FrontMatter;
4748
body: string;
4849
tableOfContents: TableOfContent;
50+
timeToRead: number;
4951
};
5052
navigation?: {
5153
navigation: RepoNav;
@@ -66,10 +68,12 @@ export default function DocTemplate({
6668
data,
6769
}: DocTemplateProps) {
6870
const {
69-
mdx: { frontmatter, tableOfContents, body },
71+
mdx: { frontmatter, tableOfContents, body, timeToRead },
7072
navigation: originNav,
7173
} = data;
7274

75+
useReportReadingRate(timeToRead);
76+
7377
const navigation = originNav ? originNav.navigation : [];
7478

7579
const tocData: TableOfContent[] | undefined = React.useMemo(() => {
@@ -301,6 +305,7 @@ export const query = graphql`
301305
}
302306
body
303307
tableOfContents
308+
timeToRead
304309
}
305310
306311
navigation: mdx(slug: { eq: $navUrl }) {

0 commit comments

Comments
 (0)