Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ const initializeGA4 = () => {
script.onload = () => {
(window as any).dataLayer = (window as any).dataLayer || [];

function gtag(...args: any[]) {
(window as any).gtag = function (...args: any[]) {
(window as any).dataLayer.push(args);
}
gtag("js", new Date());
};

gtag("config", ga4Id);
(window as any).gtag("js", new Date());
(window as any).gtag("config", ga4Id);
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ declare global {
}
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
gtag: (...args: any[]) => void;
gtag: (event: string, action: string, params?: Record<string, any>) => void;
response?: {
receiveScanResult: (jsonData: string) => void;
receiveGeneratedReview: (jsonData: string) => void;
Expand Down
12 changes: 8 additions & 4 deletions src/utils/gtag.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export const gTagLogEvent = (eventLabel: string) => {
window.gtag("event", "click", {
event_category: "button",
event_label: eventLabel,
});
if (typeof window !== "undefined" && typeof window.gtag === "function") {
window.gtag("event", "click", {
event_category: "button",
event_label: eventLabel,
});
} else {
console.warn("Google Analytics is not initialized yet.");
}
};