-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GoogleAnalytics): added react-ga
- Loading branch information
Daniel Dobkowski
committed
Dec 11, 2024
1 parent
aa5fc74
commit a7f4a2b
Showing
11 changed files
with
112 additions
and
18 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
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,9 @@ | ||
import React, { PropsWithChildren } from "react"; | ||
import { AnalyticsContext } from "./contexts/AnalyticsContext"; | ||
import { useGoogleAnalytics } from "./hooks"; | ||
|
||
export const AnalyticsWrapper: React.FC<PropsWithChildren> = ({ children }) => { | ||
const googleAnalytics = useGoogleAnalytics(); | ||
|
||
return <AnalyticsContext.Provider value={googleAnalytics}>{children}</AnalyticsContext.Provider>; | ||
}; |
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,4 @@ | ||
import React from "react"; | ||
import { UseGoogleAnalyticsReturn } from "../types"; | ||
|
||
export const AnalyticsContext = React.createContext({} as UseGoogleAnalyticsReturn); |
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 { useGoogleAnalytics } from "./useGoogleAnalytics"; |
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,38 @@ | ||
import { useState } from "react"; | ||
import ReactGA from "react-ga4"; | ||
import { GoogleAnalyticsEvent, UseGoogleAnalytics } from "../types"; | ||
|
||
export const useGoogleAnalytics: UseGoogleAnalytics = () => { | ||
const [trackingId, setTrackingId] = useState<string>(""); | ||
|
||
const init = (id: string) => { | ||
if (!id) { | ||
throw new Error("Google Analytics tracking ID is required"); | ||
} | ||
ReactGA.initialize(id); | ||
setTrackingId(id); | ||
console.log(`Google Analytics initialized with tracking ID: ${id}`); | ||
}; | ||
|
||
const sendEvent = (event: GoogleAnalyticsEvent) => { | ||
if (!trackingId) { | ||
throw new Error("Google Analytics is not initialized"); | ||
} | ||
ReactGA.event(event); | ||
console.log(`Google Analytics event sent: ${JSON.stringify(event)}`); | ||
}; | ||
|
||
const send = (path: any) => { | ||
if (!trackingId) { | ||
throw new Error("Google Analytics is not initialized"); | ||
} | ||
ReactGA.send(path); | ||
console.log(`Google Analytics pageview sent: ${path}`); | ||
}; | ||
|
||
return { | ||
init, | ||
sendEvent, | ||
send, | ||
}; | ||
}; |
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 { AnalyticsWrapper } from "./AnalyticsWrapper"; |
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,14 @@ | ||
export interface UseGoogleAnalyticsReturn { | ||
init: (trackingId: string) => void; | ||
sendEvent: (event: any) => void; | ||
send: (fieldObject: any) => void; | ||
} | ||
|
||
export type UseGoogleAnalytics = () => UseGoogleAnalyticsReturn; | ||
|
||
export interface GoogleAnalyticsEvent { | ||
category: string; | ||
action: string; | ||
label?: string; | ||
value?: number; | ||
} |
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