Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): load Google Analytics only when a measurement ID is set (#15748)

Krzysztof Czerwinski7aa5db58172d
diff --git a/autogpt_platform/frontend/.env.default b/autogpt_platform/frontend/.env.default
index 6aa2893b5f..a82c551c79 100644
--- a/autogpt_platform/frontend/.env.default
+++ b/autogpt_platform/frontend/.env.default
@@ -90,7 +90,11 @@ NEXT_PUBLIC_TOKEN_DEVTOOL=true
# React render counter for `pnpm perf` (window.__renderCounter). Off unless
# exactly "1"; see docs/engineering/frontend-perf-measurement.md.
# NEXT_PUBLIC_RENDER_COUNTER=1
-NEXT_PUBLIC_GA_MEASUREMENT_ID=G-FH2XK2W4GN
+
+# Google Analytics 4 measurement ID (G-...). Leave empty unless you run your
+# own GA property: the Google tag only loads when this (or the Google Ads ID
+# below) is set. Inlined at build time.
+NEXT_PUBLIC_GA_MEASUREMENT_ID=
# Google Ads conversion tracking (production only). The account's tag ID and
# one label per conversion action, e.g.
diff --git a/autogpt_platform/frontend/src/app/layout.tsx b/autogpt_platform/frontend/src/app/layout.tsx
index b30f298d2c..fb0565b0b4 100644
--- a/autogpt_platform/frontend/src/app/layout.tsx
+++ b/autogpt_platform/frontend/src/app/layout.tsx
@@ -125,10 +125,7 @@ export default async function RootLayout({
>
<SetupAnalytics
host={host}
- ga={{
- gaId:
- process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID || "G-FH2XK2W4GN",
- }}
+ ga={{ gaId: environment.getGAMeasurementID() }}
/>
<div className="flex min-h-screen flex-col items-stretch justify-items-stretch supports-[min-height:100dvh]:min-h-dvh">
{children}
diff --git a/autogpt_platform/frontend/src/services/analytics/__tests__/consent-consumers.test.tsx b/autogpt_platform/frontend/src/services/analytics/__tests__/consent-consumers.test.tsx
index c2a3e2cb38..7e7ff80b71 100644
--- a/autogpt_platform/frontend/src/services/analytics/__tests__/consent-consumers.test.tsx
+++ b/autogpt_platform/frontend/src/services/analytics/__tests__/consent-consumers.test.tsx
@@ -15,20 +15,21 @@ vi.mock("next/script", () => ({
id,
src,
strategy,
- dangerouslySetInnerHTML: _inline,
+ dangerouslySetInnerHTML: inline,
onLoad: _onLoad,
...attributes
}: {
id?: string;
src?: string;
strategy?: string;
- dangerouslySetInnerHTML?: unknown;
+ dangerouslySetInnerHTML?: { __html?: string };
onLoad?: unknown;
}) => (
<div
data-testid={`script:${id ?? src}`}
data-src={src}
data-strategy={strategy}
+ data-inline={inline?.__html}
{...attributes}
/>
),
@@ -160,6 +161,42 @@ describe("with NEXT_PUBLIC_COOKIEBOT_CBID", () => {
});
});
+describe("Google tag IDs", () => {
+ beforeEach(() => {
+ configureCookiebot();
+ installCookiebot({ statistics: true, marketing: true });
+ });
+
+ it("loads no Google tag when neither a GA nor a Google Ads ID is set", () => {
+ vi.stubEnv("NEXT_PUBLIC_GOOGLE_ADS_ID", "");
+
+ render(<SetupAnalytics host="platform.agpt.co" ga={{ gaId: "" }} />);
+
+ expect(queryScript("_custom-ga")).toBeNull();
+ expect(queryScript("_custom-ga-init")).toBeNull();
+ });
+
+ it("configures only Google Ads when the GA ID is not set", () => {
+ vi.stubEnv("NEXT_PUBLIC_GOOGLE_ADS_ID", "AW-TEST");
+
+ render(<SetupAnalytics host="platform.agpt.co" ga={{ gaId: "" }} />);
+
+ const init = queryScript("_custom-ga-init")?.getAttribute("data-inline");
+ expect(queryScript("_custom-ga")).not.toBeNull();
+ expect(init).toContain(`gtag('config', "AW-TEST"`);
+ expect(init).not.toContain(`gtag('config', ""`);
+ });
+
+ it("configures the GA property it is given", () => {
+ vi.stubEnv("NEXT_PUBLIC_GOOGLE_ADS_ID", "");
+
+ render(<SetupAnalytics host="platform.agpt.co" ga={GA} />);
+
+ const init = queryScript("_custom-ga-init")?.getAttribute("data-inline");
+ expect(init).toContain(`gtag('config', "G-TEST"`);
+ });
+});
+
describe("ConsentWithdrawalReload", () => {
const reload = vi.fn<() => void>();
diff --git a/autogpt_platform/frontend/src/services/analytics/index.tsx b/autogpt_platform/frontend/src/services/analytics/index.tsx
index 144f28e874..11a6d2f098 100644
--- a/autogpt_platform/frontend/src/services/analytics/index.tsx
+++ b/autogpt_platform/frontend/src/services/analytics/index.tsx
@@ -35,8 +35,9 @@ export function SetupAnalytics(props: SetupProps) {
return (
<>
- {/* Google tag: GA4 + Google Ads */}
- {googleTagEnabled ? (
+ {/* Google tag: GA4 + Google Ads. Nothing to load without an ID: a
+ self-hosted install must not report to anyone else's property */}
+ {googleTagEnabled && (gaId || adsID) ? (
<>
<GoogleConsentModeSync />
<Script
@@ -94,7 +95,9 @@ function buildGoogleTagInitScript({
`window['${DATA_LAYER_NAME}'] = window['${DATA_LAYER_NAME}'] || [];`,
`function gtag(){window['${DATA_LAYER_NAME}'].push(arguments);}`,
`gtag('js', new Date());`,
- `gtag('config', ${JSON.stringify(GAID)}${debugMode ? ", { 'debug_mode': true }" : ""});`,
+ GAID
+ ? `gtag('config', ${JSON.stringify(GAID)}${debugMode ? ", { 'debug_mode': true }" : ""});`
+ : "",
adsID
? `gtag('config', ${JSON.stringify(adsID)}, { 'allow_enhanced_conversions': true });`
: "",
diff --git a/autogpt_platform/frontend/src/services/environment/index.ts b/autogpt_platform/frontend/src/services/environment/index.ts
index 8fbdd841f0..58b85aa59e 100644
--- a/autogpt_platform/frontend/src/services/environment/index.ts
+++ b/autogpt_platform/frontend/src/services/environment/index.ts
@@ -87,6 +87,10 @@ function getLaunchDarklyClientId() {
return process.env.NEXT_PUBLIC_LAUNCHDARKLY_CLIENT_ID;
}
+function getGAMeasurementID() {
+ return process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID?.trim() || "";
+}
+
function getGoogleAdsID() {
return process.env.NEXT_PUBLIC_GOOGLE_ADS_ID || "";
}
@@ -195,6 +199,7 @@ export const environment = {
getRevision,
getPostHogCredentials,
getLaunchDarklyClientId,
+ getGAMeasurementID,
getGoogleAdsID,
getGoogleAdsConversionLabels,
getCookiebotCBID,