-
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.
GoogleAnalitics established + VITE_GA_TOKEN in workflow
- Loading branch information
Showing
7 changed files
with
141 additions
and
17 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,111 @@ | ||
import React, {createContext, useContext, useEffect} from "react"; | ||
import ReactGA from "react-ga4"; | ||
|
||
// Initialize Google Analytics | ||
export const initGA = (measurementId: string): void => { | ||
try { | ||
if (!measurementId) { | ||
throw new Error( | ||
"Measurement ID is required to initialize Google Analytics." | ||
); | ||
} | ||
ReactGA.initialize(measurementId); | ||
} catch (error) { | ||
console.error("Error initializing Google Analytics:", error); | ||
} | ||
}; | ||
|
||
// Log a page view | ||
export const logPageViewGA = (path: string): void => { | ||
try { | ||
if (!path) { | ||
throw new Error("Path is required to log a page view."); | ||
} | ||
ReactGA.send({hitType: "pageview", page: path}); | ||
} catch (error) { | ||
console.error("Error logging page view:", error); | ||
} | ||
}; | ||
|
||
// Log an event | ||
export const logEventGA = ( | ||
category: string, | ||
action: string, | ||
label?: string | ||
): void => { | ||
try { | ||
if (!category || !action) { | ||
throw new Error("Category and action are required to log an event."); | ||
} | ||
ReactGA.event({ | ||
category, | ||
action, | ||
label, | ||
}); | ||
} catch (error) { | ||
console.error("Error logging event:", error); | ||
} | ||
}; | ||
|
||
// Analytics Context | ||
interface GoogleAnalyticsContextProps { | ||
logPageView: (path: string) => void; | ||
logEvent: (category: string, action: string, label?: string) => void; | ||
} | ||
|
||
const GoogleAnalyticsContext = createContext< | ||
GoogleAnalyticsContextProps | undefined | ||
>(undefined); | ||
|
||
interface GoogleAnalyticsProviderProps { | ||
measurementId: string; | ||
children: React.ReactNode; | ||
} | ||
|
||
export const GoogleAnalyticsProvider: React.FC< | ||
GoogleAnalyticsProviderProps | ||
> = ({measurementId, children}) => { | ||
if (!measurementId) { | ||
console.warn(new Error("Google Analytics measurement ID is required.")); | ||
return <>{children}</>; | ||
} | ||
|
||
useEffect(() => { | ||
try { | ||
initGA(measurementId); | ||
const path = window.location.pathname + window.location.search; | ||
logPageViewGA(path); // Log the initial page view | ||
console.log("Google Analytics Initialized and Pageview Logged:", path); | ||
} catch (error) { | ||
console.error("Error during Google Analytics setup:", error); | ||
} | ||
}, [measurementId]); | ||
|
||
useEffect(() => { | ||
try { | ||
const path = window.location.pathname + window.location.search; | ||
logPageViewGA(path); // Log each page view | ||
console.log("Pageview Logged:", path); | ||
} catch (error) { | ||
console.error("Error logging page view on path change:", error); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<GoogleAnalyticsContext.Provider | ||
value={{logPageView: logPageViewGA, logEvent: logEventGA}} | ||
> | ||
{children} | ||
</GoogleAnalyticsContext.Provider> | ||
); | ||
}; | ||
|
||
export const useGoogleAnalytics = (): GoogleAnalyticsContextProps => { | ||
const context = useContext(GoogleAnalyticsContext); | ||
if (!context) { | ||
throw new Error( | ||
"useGoogleAnalytics must be used within a GoogleAnalyticsProvider" | ||
); | ||
} | ||
return context; | ||
}; |
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 @@ | ||
export * from "./GoogleAnalyticsProvider"; |
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
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