Skip to content

Commit 57f1b1d

Browse files
authored
fix: ensure Google Analytics components load correctly based on environment variables
1 parent 561d830 commit 57f1b1d

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

src/layouts/BaseLayout.astro

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ const {
2929
const defaultKeywords =
3030
"AI Growth Engineer, AI Automation, Full-Stack Developer, SEO Expert, Miami Florida, Web Development, LLM Integration, Workflow Automation";
3131
const finalKeywords = keywords || defaultKeywords;
32-
const gaMeasurementId = import.meta.env.PUBLIC_GA_MEASUREMENT_ID?.trim();
32+
const gaMeasurementId =
33+
import.meta.env.PUBLIC_GA_MEASUREMENT_ID?.trim() || "G-LKVQYYL9XZ";
34+
const shouldLoadAnalytics = import.meta.env.PROD && gaMeasurementId.length > 0;
3335
3436
const currentPath = Astro.url.pathname;
3537
---
@@ -39,7 +41,7 @@ const currentPath = Astro.url.pathname;
3941
<head>
4042
<meta charset="UTF-8" />
4143

42-
{import.meta.env.PROD && gaMeasurementId && (
44+
{shouldLoadAnalytics && (
4345
<>
4446
<!-- Google Analytics -->
4547
<GoogleAnalytics id={gaMeasurementId} />
@@ -127,7 +129,7 @@ const currentPath = Astro.url.pathname;
127129

128130
<FooterNav />
129131

130-
{import.meta.env.PROD && gaMeasurementId && <AnalyticsConsent />}
132+
{shouldLoadAnalytics && <AnalyticsConsent />}
131133

132134
<script>
133135
import "../scripts/homepage.ts";

src/layouts/Layout.astro

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ const {
2323
structuredData,
2424
headerBackground,
2525
} = Astro.props as Props;
26-
const gaMeasurementId = import.meta.env.PUBLIC_GA_MEASUREMENT_ID?.trim();
26+
const gaMeasurementId =
27+
import.meta.env.PUBLIC_GA_MEASUREMENT_ID?.trim() || "G-LKVQYYL9XZ";
28+
const shouldLoadAnalytics = import.meta.env.PROD && gaMeasurementId.length > 0;
2729
---
2830

2931
<html lang="en" class="scroll-smooth">
3032
<head>
3133
<meta charset="UTF-8" />
3234

33-
{import.meta.env.PROD && gaMeasurementId && (
35+
{shouldLoadAnalytics && (
3436
<>
3537
<!-- Google Analytics -->
3638
<GoogleAnalytics id={gaMeasurementId} />
@@ -104,7 +106,7 @@ const gaMeasurementId = import.meta.env.PUBLIC_GA_MEASUREMENT_ID?.trim();
104106
</main>
105107

106108
<Footer />
107-
{import.meta.env.PROD && gaMeasurementId && <AnalyticsConsent />}
109+
{shouldLoadAnalytics && <AnalyticsConsent />}
108110
<script>
109111
import "../scripts/homepage.ts";
110112
</script>

0 commit comments

Comments
 (0)