Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Fix GA4 and Google Ads tags on app

Carlos Monastyrskiab5b69a8c387
diff --git a/backend/src/server/plugins/serve-ui.ts b/backend/src/server/plugins/serve-ui.ts
index 1dfa1c8b6d..74affba309 100644
--- a/backend/src/server/plugins/serve-ui.ts
+++ b/backend/src/server/plugins/serve-ui.ts
@@ -75,6 +75,20 @@ export const registerServeUI = async (
}
}
+ if (appCfg.isCloud) {
+ const GTM_ID = "GTM-WL5C7MWT";
+
+ const gtmHeadSnippet = `<!-- Google Tag Manager -->\n<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':\nnew Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],\nj=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=\n'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);\n})(window,document,'script','dataLayer','${GTM_ID}');</script>\n<!-- End Google Tag Manager -->`;
+
+ const gtmBodySnippet = `<!-- Google Tag Manager (noscript) -->\n<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=${GTM_ID}"\nheight="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>\n<!-- End Google Tag Manager (noscript) -->`;
+
+ // Insert GTM script right before </head>
+ indexHtml = indexHtml.replace("</head>", `${gtmHeadSnippet}\n</head>`);
+
+ // Insert GTM noscript right after <body>
+ indexHtml = indexHtml.replace("<body>", `<body>\n${gtmBodySnippet}`);
+ }
+
// Inject GTM/analytics CSP domains only for Infisical Cloud deployments
if (appCfg.isCloud) {
const cloudCspExtensions: Record<string, string[]> = {
@@ -99,8 +113,10 @@ export const registerServeUI = async (
"connect-src": [
"https://www.googletagmanager.com",
"https://www.google-analytics.com",
+ "https://region1.google-analytics.com",
"https://analytics.google.com",
"https://stats.g.doubleclick.net",
+ "https://googleads.g.doubleclick.net",
"https://*.hubspot.com",
"https://forms.hubspot.com",
"https://api.hubapi.com"
diff --git a/frontend/index.html b/frontend/index.html
index 7828141a32..1e384f0142 100644
--- a/frontend/index.html
+++ b/frontend/index.html
@@ -12,7 +12,7 @@
style-src 'self' 'unsafe-inline' https://hcaptcha.com https://*.hcaptcha.com https://tags.srv.stackadapt.com;
child-src https://api.stripe.com;
frame-src https://js.stripe.com/ https://api.stripe.com https://www.youtube.com/ https://hcaptcha.com https://*.hcaptcha.com https://www.googletagmanager.com;
- connect-src 'self' wss://nexus-websocket-a.intercom.io https://api-iam.intercom.io https://api.heroku.com/ https://id.heroku.com/oauth/authorize https://id.heroku.com/oauth/token https://checkout.stripe.com https://app.posthog.com https://*.posthog.com https://api.stripe.com https://api.pwnedpasswords.com http://127.0.0.1:* https://hcaptcha.com https://*.hcaptcha.com https://www.googletagmanager.com https://www.google-analytics.com https://region1.google-analytics.com https://analytics.google.com https://stats.g.doubleclick.net https://forms.hscollectedforms.net https://forms.hubspot.com https://api.hubapi.com https://*.ads.linkedin.com https://tags.srv.stackadapt.com https://api.unifyintent.com https://pixel-config.reddit.com https://cdn.jsdelivr.net/npm/@lottiefiles/[email protected]/dist/dotlottie-player.wasm;
+ connect-src 'self' wss://nexus-websocket-a.intercom.io https://api-iam.intercom.io https://api.heroku.com/ https://id.heroku.com/oauth/authorize https://id.heroku.com/oauth/token https://checkout.stripe.com https://app.posthog.com https://*.posthog.com https://api.stripe.com https://api.pwnedpasswords.com http://127.0.0.1:* https://hcaptcha.com https://*.hcaptcha.com https://www.googletagmanager.com https://www.google-analytics.com https://region1.google-analytics.com https://analytics.google.com https://stats.g.doubleclick.net https://googleads.g.doubleclick.net https://forms.hscollectedforms.net https://forms.hubspot.com https://api.hubapi.com https://*.ads.linkedin.com https://tags.srv.stackadapt.com https://api.unifyintent.com https://pixel-config.reddit.com https://cdn.jsdelivr.net/npm/@lottiefiles/[email protected]/dist/dotlottie-player.wasm;
img-src 'self' https://static.intercomassets.com https://js.intercomcdn.com https://downloads.intercomcdn.com https://*.stripe.com https://i.ytimg.com/ data: https://www.googletagmanager.com https://www.google-analytics.com https://www.google.com https://googleads.g.doubleclick.net https://www.googleadservices.com https://track.hubspot.com https://forms.hubspot.com https://forms.hsforms.com https://alb.reddit.com https://*.ads.linkedin.com;
media-src 'self' https://js.intercomcdn.com;
font-src 'self' https://fonts.intercomcdn.com/ https://fonts.gstatic.com;
diff --git a/frontend/src/helpers/platform.ts b/frontend/src/helpers/platform.ts
index 73aaeb4ead..bf75e6a074 100644
--- a/frontend/src/helpers/platform.ts
+++ b/frontend/src/helpers/platform.ts
@@ -1,4 +1,5 @@
export const isInfisicalCloud = () =>
+ true ||
window.location.origin.includes("https://app.infisical.com") ||
window.location.origin.includes("https://us.infisical.com") ||
window.location.origin.includes("https://eu.infisical.com") ||
diff --git a/frontend/src/hooks/useGtm.tsx b/frontend/src/hooks/useGtm.tsx
index dba51665f9..ef2d7d16e9 100644
--- a/frontend/src/hooks/useGtm.tsx
+++ b/frontend/src/hooks/useGtm.tsx
@@ -1,10 +1,8 @@
import { useEffect } from "react";
-import { Helmet } from "react-helmet";
import { isInfisicalCloud } from "@app/helpers/platform";
const GTM_ID = "GTM-WL5C7MWT";
-const GTM_NOSCRIPT_ID = "gtm-noscript";
export const GtmHead = () => {
const shouldLoad = isInfisicalCloud();
@@ -12,9 +10,24 @@ export const GtmHead = () => {
useEffect(() => {
if (!shouldLoad) return undefined;
+ // If GTM was already injected by the backend (standalone mode), skip.
+ // The backend injects the full GTM snippet into the static HTML <head>.
+ if (window.dataLayer) return undefined;
+
+ // Initialize dataLayer and load GTM — this is the standard GTM snippet.
+ // It must run before gtm.js loads so that GA4 config commands fire correctly.
+ window.dataLayer = window.dataLayer || [];
+ window.dataLayer.push({ "gtm.start": new Date().getTime(), event: "gtm.js" });
+
+ const firstScript = document.getElementsByTagName("script")[0];
+ const gtmScript = document.createElement("script");
+ gtmScript.async = true;
+ gtmScript.src = `https://www.googletagmanager.com/gtm.js?id=${GTM_ID}`;
+ firstScript.parentNode?.insertBefore(gtmScript, firstScript);
+
// Inject noscript iframe into body
const noscript = document.createElement("noscript");
- noscript.id = GTM_NOSCRIPT_ID;
+ noscript.id = "gtm-noscript";
const iframe = document.createElement("iframe");
iframe.src = `https://www.googletagmanager.com/ns.html?id=${GTM_ID}`;
iframe.height = "0";
@@ -25,15 +38,10 @@ export const GtmHead = () => {
document.body.insertBefore(noscript, document.body.firstChild);
return () => {
- document.getElementById(GTM_NOSCRIPT_ID)?.remove();
+ gtmScript.remove();
+ document.getElementById("gtm-noscript")?.remove();
};
}, [shouldLoad]);
- if (!shouldLoad) return null;
-
- return (
- <Helmet>
- <script async src={`https://www.googletagmanager.com/gtm.js?id=${GTM_ID}`} />
- </Helmet>
- );
+ return null;
};