Skip to content

Commit 932c32f

Browse files
committed
fix: broken tracking of custom events
1 parent 2662b55 commit 932c32f

5 files changed

Lines changed: 22 additions & 66 deletions

File tree

astro.config.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@ import sitemap from '@astrojs/sitemap'
55
import tailwindcss from '@tailwindcss/vite'
66
import { defineConfig, fontProviders } from 'astro/config'
77
import expressiveCode from 'astro-expressive-code'
8-
import partytown from '@astrojs/partytown'
98
import remarkDirective from 'remark-directive' /* Handle ::: directives as nodes */
109
import { remarkAdmonitions } from './src/plugins/remark-admonitions' /* Add admonitions */
1110

1211
// https://astro.build/config
1312
export default defineConfig({
1413
site: 'https://nodejsdesignpatterns.com',
15-
integrations: [
16-
react(),
17-
expressiveCode(),
18-
sitemap(),
19-
partytown({ config: { forward: ['dataLayer.push'] } }),
20-
],
14+
integrations: [react(), expressiveCode(), sitemap()],
2115
markdown: {
2216
remarkPlugins: [remarkDirective, remarkAdmonitions],
2317
},

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"typecheck": "tsc --noEmit"
1515
},
1616
"dependencies": {
17-
"@astrojs/partytown": "^2.1.4",
1817
"@astrojs/react": "^4.4.2",
1918
"@astrojs/sitemap": "^3.7.0",
2019
"@expressive-code/plugin-collapsible-sections": "^0.41.3",

pnpm-lock.yaml

Lines changed: 0 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Layout.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ const {
249249
<!-- Google tag (gtag.js) -->
250250
<script
251251
is:inline
252-
type="text/partytown"
252+
async
253253
src="https://www.googletagmanager.com/gtag/js?id=G-NFE37ZH2W3"></script>
254-
<script is:inline type="text/partytown">
254+
<script is:inline>
255255
window.dataLayer = window.dataLayer || []
256256
function gtag() {
257257
// eslint-disable-next-line no-undef

src/lib/analytics.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@
22
* Analytics Utility Module for GA4 Event Tracking
33
*
44
* This module provides type-safe helper functions for sending GA4 events.
5-
* It handles Partytown's async nature and provides debug mode for development.
5+
* It uses the global gtag() function which runs in the main thread.
66
*
77
* @module analytics
88
*/
99

10-
// Type for gtag function (loaded by Google Tag Manager via Partytown)
11-
type GtagFunction = (
12-
command: string,
13-
eventNameOrConfig: string,
14-
params?: Record<string, unknown>,
15-
) => void
16-
1710
// ============================================================================
1811
// Types & Interfaces
1912
// ============================================================================
@@ -198,36 +191,41 @@ function debugLog<T extends Record<string, unknown>>(
198191
// Core Tracking Function
199192
// ============================================================================
200193

194+
// Extend Window interface for gtag
195+
declare global {
196+
interface Window {
197+
gtag?: (...args: unknown[]) => void
198+
}
199+
}
200+
201201
/**
202-
* Safely get the gtag function, handling Partytown's async loading
202+
* Get the gtag function from the window object
203203
*/
204-
function getGtag(): GtagFunction | null {
204+
function getGtag(): ((...args: unknown[]) => void) | null {
205205
if (typeof window === 'undefined') return null
206-
// gtag is loaded via Partytown, may not be immediately available
207-
return (window as unknown as { gtag?: GtagFunction }).gtag ?? null
206+
return window.gtag ?? null
208207
}
209208

210209
/**
211210
* Generic event tracking function
212-
* Handles cases where gtag might not be loaded yet (Partytown delay)
211+
* Uses the global gtag() function directly for event tracking.
213212
*/
214213
export function trackEvent<T extends Record<string, unknown>>(
215214
eventName: string,
216215
params: T,
217216
): void {
218217
debugLog(eventName, params)
219218

220-
const gtagFn = getGtag()
221-
if (gtagFn) {
222-
gtagFn('event', eventName, params)
219+
const gtag = getGtag()
220+
if (gtag) {
221+
gtag('event', eventName, params)
223222
} else {
224-
// If gtag not ready, queue the event for when it becomes available
225-
// This is a fallback for slow Partytown initialization
223+
// If gtag not ready, queue the event with retry
226224
let timeoutId: ReturnType<typeof setTimeout> | null = null
227225
const checkInterval = setInterval(() => {
228-
const fn = getGtag()
229-
if (fn) {
230-
fn('event', eventName, params)
226+
const g = getGtag()
227+
if (g) {
228+
g('event', eventName, params)
231229
clearInterval(checkInterval)
232230
if (timeoutId) clearTimeout(timeoutId)
233231
}

0 commit comments

Comments
 (0)