Skip to content

Commit

Permalink
Add pageleave event tracking (#860)
Browse files Browse the repository at this point in the history
Resolves: #696 

How to test:
1. Open Dapp 
2. Close window

On events list in PostHog service  you should find `pageleave` event:


<img width="1424" alt="Screenshot 2023-12-08 at 09 44 28"
src="https://github.com/tahowallet/dapp/assets/28560653/c2a9f175-4da3-40a3-8124-9ccf60a09525">
  • Loading branch information
jagodarybacka authored Dec 8, 2023
2 parents ca2666c + acce74a commit 2476043
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/shared/hooks/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,20 @@ export function useMobileScreen() {
return width < MOBILE_BREAKPOINT
}

export function useUnLoad() {
const posthog = usePostHog()

useEffect(() => {
const onUnload = () => {
posthog?.capture("$pageleave")
}
window.addEventListener("beforeunload", onUnload)
return () => {
window.removeEventListener("beforeunload", onUnload)
}
}, [posthog])
}

export function useTrackEvents() {
const location = useLocation()
const posthog = usePostHog()
Expand Down
3 changes: 2 additions & 1 deletion src/ui/DApps/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from "react"
import { useMobileScreen, useTrackEvents } from "shared/hooks"
import { useMobileScreen, useTrackEvents, useUnLoad } from "shared/hooks"
import GlobalStyles from "ui/GlobalStyles"
import MobileDApp from "./MobileDApp"
import DesktopDApp from "./DesktopDApp"

export default function DApp() {
const isMobileScreen = useMobileScreen()
useTrackEvents()
useUnLoad()

return (
<>
Expand Down

0 comments on commit 2476043

Please sign in to comment.