Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading react-ga to react-ga4 #48932

Merged
merged 12 commits into from
May 16, 2023
Merged
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# React-GA example
styfle marked this conversation as resolved.
Show resolved Hide resolved

styfle marked this conversation as resolved.
Show resolved Hide resolved
This example shows the most basic way to use [react-ga](https://github.com/react-ga/react-ga) using custom [App](https://github.com/vercel/next.js#custom-app)
This example shows the most basic way to use [react-ga4](https://github.com/codler/react-ga4) using custom [App](https://github.com/vercel/next.js#custom-app)
component with NextJs. Modify `Tracking ID` in `utils/analytics.js` file for testing this example.

## Deploy your own
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-ga": "2.5.3"
"react-ga4": "2.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import ReactGA from 'react-ga'
import ReactGA from 'react-ga4'

export const initGA = () => {
console.log('GA init')
ReactGA.initialize('UA-xxxxxxxxx-1')
ReactGA.initialize('your GA measurement id')
}

export const logPageView = () => {
console.log(`Logging pageview for ${window.location.pathname}`)
ReactGA.set({ page: window.location.pathname })
ReactGA.pageview(window.location.pathname)
ReactGA.send({ hitType: 'pageview', page: window.location.pathname })
}

export const logEvent = (category = '', action = '') => {
Expand All @@ -19,6 +19,9 @@ export const logEvent = (category = '', action = '') => {

export const logException = (description = '', fatal = false) => {
if (description) {
ReactGA.exception({ description, fatal })
ReactGA.gtag('event', 'exception', {
description: description,
fatal: fatal, // set to true if the error is fatal
})
styfle marked this conversation as resolved.
Show resolved Hide resolved
}
}