Skip to content

Commit ac42c42

Browse files
committed
Fix Google Analytics tag detection by implementing build-time environment variable resolution
- Add TypeScript utility system in src/utils/google-config.ts for centralized Google service configuration - Update src/components/bases/head.astro to use define:vars for proper inline script variable injection - Fix Google Tag Manager and Google Analytics scripts to use actual resolved values instead of template literals - Ensure all Google services (GA, GTM, AdSense, Site Verification) are properly configured at build time - Build completed successfully with all 52 pages generated and Google tags properly resolved
1 parent 1686a79 commit ac42c42

File tree

2 files changed

+68
-12
lines changed

2 files changed

+68
-12
lines changed

src/components/bases/head.astro

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import "@fontsource-variable/source-serif-4";
77
import { ClientRouter } from "astro:transitions";
88
import { SITE } from "@/lib/config";
99
import type { ArticleMeta, Meta } from "@/lib/types";
10+
import { GOOGLE_CONFIG, getGATagConfig, getGTMContainerId, hasGoogleAnalytics, hasGoogleTagManager, hasGoogleAdSense } from "@/utils/google-config";
1011
1112
type Props = {
1213
meta: Meta | ArticleMeta;
@@ -27,8 +28,8 @@ const OGImage = new URL(meta.ogImage, Astro.url).href;
2728
<meta charset={SITE.charset} />
2829
<meta name="viewport" content="width=device-width,initial-scale=1" />
2930
<meta name="generator" content={Astro.generator} />
30-
{import.meta.env.PUBLIC_GOOGLE_SITE_VERIFICATION && (
31-
<meta name="google-site-verification" content={import.meta.env.PUBLIC_GOOGLE_SITE_VERIFICATION} />
31+
{GOOGLE_CONFIG.SITE_VERIFICATION && (
32+
<meta name="google-site-verification" content={GOOGLE_CONFIG.SITE_VERIFICATION} />
3233
)}
3334
<!-- Favicons -->
3435
<link rel="icon" type="image/png" href="/favicon.png" sizes="96x96" />
@@ -39,13 +40,13 @@ const OGImage = new URL(meta.ogImage, Astro.url).href;
3940
<link rel="manifest" href="/site.webmanifest" />
4041

4142
<!-- Google AdSense -->
42-
{import.meta.env.PUBLIC_GOOGLE_ADSENSE_CLIENT_ID && (
43-
<script async src={`https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=${import.meta.env.PUBLIC_GOOGLE_ADSENSE_CLIENT_ID}`}
43+
{hasGoogleAdSense() && (
44+
<script async src={`https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=${GOOGLE_CONFIG.ADSENSE_CLIENT_ID}`}
4445
crossorigin="anonymous"></script>
4546
)}
4647

4748
<!-- Google Consent Mode v2 -->
48-
{import.meta.env.PUBLIC_GOOGLE_ADSENSE_CLIENT_ID && (
49+
{hasGoogleAdSense() && (
4950
<script is:inline>
5051
// Initialize Google Consent Mode v2
5152
window.dataLayer = window.dataLayer || [];
@@ -65,26 +66,26 @@ const OGImage = new URL(meta.ogImage, Astro.url).href;
6566
)}
6667

6768
<!-- Google Tag Manager -->
68-
{import.meta.env.PUBLIC_GTM_ID && (
69-
<script is:inline>
69+
{hasGoogleTagManager() && (
70+
<script is:inline define:vars={{ gtmId: GOOGLE_CONFIG.GTM_ID }}>
7071
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
7172
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
7273
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
7374
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
74-
})(window,document,'script','dataLayer',import.meta.env.PUBLIC_GTM_ID);
75+
})(window,document,'script','dataLayer', gtmId);
7576
</script>
7677
)}
7778

7879
<!-- Google Analytics (gtag.js) -->
79-
{import.meta.env.PUBLIC_GA_MEASUREMENT_ID && (
80+
{hasGoogleAnalytics() && (
8081
<>
81-
<script async src={`https://www.googletagmanager.com/gtag/js?id=${import.meta.env.PUBLIC_GA_MEASUREMENT_ID}`}></script>
82-
<script is:inline>
82+
<script async src={`https://www.googletagmanager.com/gtag/js?id=${GOOGLE_CONFIG.GA_MEASUREMENT_ID}`}></script>
83+
<script is:inline define:vars={{ gaId: GOOGLE_CONFIG.GA_MEASUREMENT_ID }}>
8384
window.dataLayer = window.dataLayer || [];
8485
function gtag(){dataLayer.push(arguments);}
8586
gtag('js', new Date());
8687

87-
gtag('config', import.meta.env.PUBLIC_GA_MEASUREMENT_ID);
88+
gtag('config', gaId);
8889
</script>
8990
</>
9091
)}

src/utils/google-config.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Google Analytics and Tag Manager configuration utilities
3+
* Values are resolved at build time from environment variables
4+
*/
5+
6+
export const GOOGLE_CONFIG = {
7+
// Google Analytics Measurement ID
8+
GA_MEASUREMENT_ID: import.meta.env.PUBLIC_GA_MEASUREMENT_ID || '',
9+
10+
// Google Tag Manager Container ID
11+
GTM_ID: import.meta.env.PUBLIC_GTM_ID || '',
12+
13+
// Google AdSense Client ID
14+
ADSENSE_CLIENT_ID: import.meta.env.PUBLIC_GOOGLE_ADSENSE_CLIENT_ID || '',
15+
16+
// Google Site Verification
17+
SITE_VERIFICATION: import.meta.env.PUBLIC_GOOGLE_SITE_VERIFICATION || '',
18+
} as const;
19+
20+
/**
21+
* Get the Google Analytics gtag configuration script
22+
* This returns the actual measurement ID for inline scripts
23+
*/
24+
export function getGATagConfig(): string {
25+
return GOOGLE_CONFIG.GA_MEASUREMENT_ID;
26+
}
27+
28+
/**
29+
* Get the Google Tag Manager container ID
30+
* This returns the actual container ID for inline scripts
31+
*/
32+
export function getGTMContainerId(): string {
33+
return GOOGLE_CONFIG.GTM_ID;
34+
}
35+
36+
/**
37+
* Check if Google Analytics is configured
38+
*/
39+
export function hasGoogleAnalytics(): boolean {
40+
return Boolean(GOOGLE_CONFIG.GA_MEASUREMENT_ID);
41+
}
42+
43+
/**
44+
* Check if Google Tag Manager is configured
45+
*/
46+
export function hasGoogleTagManager(): boolean {
47+
return Boolean(GOOGLE_CONFIG.GTM_ID);
48+
}
49+
50+
/**
51+
* Check if Google AdSense is configured
52+
*/
53+
export function hasGoogleAdSense(): boolean {
54+
return Boolean(GOOGLE_CONFIG.ADSENSE_CLIENT_ID);
55+
}

0 commit comments

Comments
 (0)