Skip to content

Commit

Permalink
Simplify analytics logic and ts
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-mosher committed Jul 31, 2024
1 parent acd05a3 commit 97c392c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
7 changes: 0 additions & 7 deletions src/types/global.d.ts

This file was deleted.

25 changes: 13 additions & 12 deletions src/utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
declare global {
interface Window {
dataLayer: unknown[];
gtag?: (...args: unknown[]) => void;
}
}

const loadGoogleAnalytics = () => {
// googleTagId is validated at build time, so we can trust it here
const googleTagId = import.meta.env.VITE_GOOGLE_TAG_ID || ''
Expand All @@ -10,21 +17,15 @@ const loadGoogleAnalytics = () => {

script.onload = () => {
window.dataLayer = window.dataLayer || []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function gtag(...args: any[]) { window.dataLayer.push(args) }
gtag('js', new Date())
gtag('config', googleTagId)
window.gtag = (...args: unknown[]) => {
window.dataLayer.push(args)
}
window.gtag('js', new Date())
window.gtag('config', googleTagId)
}
} else {
// eslint-disable-next-line no-console
console.log('Google Analytics script would be loaded here.')
window.dataLayer = window.dataLayer || []
// eslint-disable-next-line no-inner-declarations, @typescript-eslint/no-explicit-any
function gtag(...args: any[]) { window.dataLayer.push(args) }
gtag('js', new Date())
gtag('config', googleTagId)
// eslint-disable-next-line no-console
console.log('Simulating gtag call:', window.dataLayer)
console.log('Non-production build detected. Google Analytics script would be loaded here.')
}
}

Expand Down

0 comments on commit 97c392c

Please sign in to comment.