forked from oursky/likedao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
react-app: Create RouterAnalytics component
refs oursky#349
- Loading branch information
Rico
committed
Aug 2, 2022
1 parent
e3f0b7f
commit d566c14
Showing
2 changed files
with
73 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import * as ReactGA from "react-ga"; | ||
import { useLocation } from "react-router-dom"; | ||
import { useEffectOnce } from "../hooks/useEffectOnce"; | ||
|
||
interface RouterAnalyticsProps { | ||
id: string | null; | ||
children: React.ReactNode; | ||
} | ||
|
||
const RouterAnalytics: React.FC<RouterAnalyticsProps> = (props) => { | ||
const { id, children } = props; | ||
const location = useLocation(); | ||
const [isInitialized, setIsInitialized] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
if (!isInitialized) return; | ||
console.log(location); | ||
ReactGA.pageview(location.pathname + location.search); | ||
}, [location, isInitialized]); | ||
|
||
useEffectOnce( | ||
() => { | ||
if (id) { | ||
ReactGA.initialize(id); | ||
} | ||
setIsInitialized(true); | ||
}, | ||
() => true | ||
); | ||
|
||
return <>{children}</>; | ||
}; | ||
|
||
export default RouterAnalytics; |