-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleAnalytics.astro
More file actions
34 lines (33 loc) · 1.19 KB
/
GoogleAnalytics.astro
File metadata and controls
34 lines (33 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
---
// Google Analytics 4 - Non-blocking, performance-optimized implementation
// Only loads if PUBLIC_GA_MEASUREMENT_ID environment variable is configured
// Uses async loading with low priority to avoid blocking page rendering
const gaMeasurementId = import.meta.env.PUBLIC_GA_MEASUREMENT_ID;
---
{
gaMeasurementId && (
<>
{/* Google Analytics 4 - Optimized non-blocking implementation */}
{/* Script loads asynchronously with low priority to not compete with critical resources */}
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${gaMeasurementId}`}
fetchpriority="low"
/>
{/* Inline configuration script - executes immediately but doesn't block rendering */}
<script is:inline define:vars={{ measurementId: gaMeasurementId }}>
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${gaMeasurementId}', {
'send_page_view': true,
'anonymize_ip': true,
'allow_google_signals': false,
'allow_ad_personalization_signals': false
});
`}
</script>
</>
)
}