generated from Open-Attestation/react-template
-
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.
* chore: npm i react-ga4 * feat: add google analytics via react-ga4 https://github.com/codler/react-ga4?tab=readme-ov-file#usage
- Loading branch information
Showing
4 changed files
with
36 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import React from "react"; | ||
import { Router } from "./router"; | ||
import { useGoogleAnalytics } from "./utils/google-analytics"; | ||
|
||
export const App: React.FunctionComponent = () => ( | ||
<div className="flex flex-col min-h-screen"> | ||
<Router /> | ||
</div> | ||
); | ||
export const App: React.FunctionComponent = () => { | ||
useGoogleAnalytics(); | ||
return ( | ||
<div className="flex flex-col min-h-screen"> | ||
<Router /> | ||
</div> | ||
); | ||
}; |
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,20 @@ | ||
import { useEffect } from "react"; | ||
import ReactGA from "react-ga4"; | ||
|
||
/** | ||
* Initialise Google Analytics 4 only once (if GTAG_ID is provided) | ||
* | ||
* The ReactGA.send("pageview") is explicitly called afterwards because | ||
* the pageview event is not automatically triggered on first initialisation | ||
* but will be automatically triggered by the GA script on subsequent page views. | ||
*/ | ||
export const useGoogleAnalytics = (): void => { | ||
useEffect(() => { | ||
try { | ||
ReactGA.initialize("G-GXV2BSGK4K"); | ||
ReactGA.send("pageview"); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}, []); | ||
}; |