generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #664 from hackforla/google-analytics
Add Google Analytics support
- Loading branch information
Showing
4 changed files
with
189 additions
and
124 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,65 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { BrowserRouter as Router } from "react-router-dom"; | ||
import { UserContext } from "./components/user-context"; | ||
import App from "./App"; | ||
import NavConfirmModal from "./components/NavConfirmModal"; | ||
|
||
const AppWrapper = () => { | ||
const [account, setAccount] = useState({}); | ||
const [confirmTransition, setConfirmTransition] = useState(null); | ||
const [hasConfirmedTransition, setHasConfirmedTransition] = useState(true); | ||
const [isOpenNavConfirmModal, setIsOpenNavConfirmModal] = useState(false); | ||
|
||
const setLoggedInAccount = loggedInUser => { | ||
setAccount(loggedInUser); | ||
localStorage.setItem("currentUser", JSON.stringify(loggedInUser)); | ||
}; | ||
|
||
useEffect(() => { | ||
const currentUser = localStorage.getItem("currentUser"); | ||
if (currentUser) { | ||
try { | ||
const parsedAccount = JSON.parse(currentUser); | ||
setAccount(parsedAccount); | ||
} catch (err) { | ||
// TODO: replace with production error logging. | ||
console.error( | ||
"Unable to parse current user from local storage.", | ||
currentUser | ||
); | ||
} | ||
} else { | ||
setLoggedInAccount({}); | ||
} | ||
}, []); | ||
|
||
const getUserConfirmation = (_message, defaultConfirmCallback) => { | ||
setHasConfirmedTransition(false); | ||
setConfirmTransition(() => ({ | ||
defaultConfirmCallback: defaultConfirmCallback, | ||
setHasConfirmedTransition: setHasConfirmedTransition | ||
})); | ||
setIsOpenNavConfirmModal(!isOpenNavConfirmModal); | ||
}; | ||
|
||
return ( | ||
<React.Fragment> | ||
<UserContext.Provider value={account}> | ||
<Router getUserConfirmation={getUserConfirmation}> | ||
<NavConfirmModal | ||
confirmTransition={confirmTransition} | ||
isOpenNavConfirmModal={isOpenNavConfirmModal} | ||
setIsOpenNavConfirmModal={setIsOpenNavConfirmModal} | ||
/> | ||
<App | ||
account={account} | ||
setLoggedInAccount={setLoggedInAccount} | ||
hasConfirmedTransition={hasConfirmedTransition} | ||
/> | ||
</Router> | ||
</UserContext.Provider> | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
export default AppWrapper; |
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,34 @@ | ||
import { useEffect } from "react"; | ||
import { useHistory } from "react-router-dom"; | ||
|
||
// https://medium.com/javascript-in-plain-english/google-analytics-with-react-router-and-hooks-16d403ddc528 | ||
|
||
// declare global { | ||
// interface Window { | ||
// gtag?: ( | ||
// key: string, | ||
// trackingId: string, | ||
// config: { page_path: string } | ||
// ) => void; | ||
// } | ||
// } | ||
|
||
export const useTracking = (trackingId = "G-MW23VES3G6") => { | ||
const { listen } = useHistory(); | ||
|
||
useEffect(() => { | ||
const unlisten = listen(location => { | ||
if (!window.gtag) return; | ||
if (!trackingId) { | ||
console.error( | ||
"Tracking not enabled, as `trackingId` was not given and there is no `GA_MEASUREMENT_ID`." | ||
); | ||
return; | ||
} | ||
|
||
window.gtag("config", trackingId, { page_path: location.pathname }); | ||
}); | ||
|
||
return unlisten; | ||
}, [trackingId, listen]); | ||
}; |
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