-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
Showing
7 changed files
with
76 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import throttle from "lodash/throttle"; | ||
import React from "react"; | ||
import { GTMEvent, gtmTrack } from "./utils/gtm"; | ||
|
||
// timeToRead: minutes of reading | ||
export const useReportReadingRate = (timeToRead: number) => { | ||
const lastPercentage = React.useRef(0); | ||
|
||
const handleScroll = throttle(() => { | ||
const scrollTop = window.scrollY || document.documentElement.scrollTop; | ||
const clientHeight = window.innerHeight; | ||
const scrollHeight = document.documentElement.scrollHeight; | ||
|
||
const scrollPercentage = Math.min( | ||
(scrollTop / (scrollHeight - clientHeight)) * 100, | ||
100 | ||
); | ||
|
||
if ( | ||
Math.floor(scrollPercentage / 10) > | ||
Math.floor(lastPercentage.current / 10) | ||
) { | ||
lastPercentage.current = Math.floor(scrollPercentage / 10) * 10; | ||
} | ||
}, 1000); | ||
|
||
const startTime = React.useRef(Date.now()); | ||
const timeToReadSecond = timeToRead * 60; | ||
|
||
const reportReadingRate = () => { | ||
const endTime = Date.now(); | ||
const timeSpent = Math.floor((endTime - startTime.current) / 1000); | ||
|
||
if (document.visibilityState === "hidden") { | ||
if (timeSpent < 6) { | ||
return; | ||
} | ||
gtmTrack(GTMEvent.ReadingRate, { | ||
readingRate: | ||
Math.min(Math.floor((timeSpent / timeToReadSecond) * 100) / 100, 1) * | ||
lastPercentage.current, | ||
timeToRead: timeToReadSecond, | ||
timeSpent, | ||
scrollPercentage: lastPercentage.current, | ||
}); | ||
} else { | ||
startTime.current = Date.now(); | ||
} | ||
}; | ||
|
||
React.useEffect(() => { | ||
window.addEventListener("visibilitychange", reportReadingRate); | ||
window.addEventListener("scroll", handleScroll); | ||
return () => { | ||
window.removeEventListener("visibilitychange", reportReadingRate); | ||
window.removeEventListener("scroll", handleScroll); | ||
}; | ||
}, []); | ||
}; |
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