Skip to content

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

Reworked GTM to only happen on signup/login pages

Carlos Monastyrskic0633b2ee15f
diff --git a/backend/src/server/plugins/serve-ui.ts b/backend/src/server/plugins/serve-ui.ts
index a12b7ee423..1dfa1c8b6d 100644
--- a/backend/src/server/plugins/serve-ui.ts
+++ b/backend/src/server/plugins/serve-ui.ts
@@ -120,11 +120,6 @@ export const registerServeUI = async (
const regex = new RE2(`(${directive}\\s+[^;]+)(;)`, "g");
indexHtml = indexHtml.replace(regex, `$1 ${domains.join(" ")}$2`);
}
-
- indexHtml = indexHtml.replace(
- "<!-- GTM_NOSCRIPT_PLACEHOLDER -->",
- `<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-WL5C7MWT" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>`
- );
}
await server.register(staticServe, {
diff --git a/frontend/index.html b/frontend/index.html
index 1b877695c7..d1441cf6ca 100644
--- a/frontend/index.html
+++ b/frontend/index.html
@@ -2,15 +2,6 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
- <!-- Google Tag Manager (only on Infisical Cloud) -->
- <script>(function(w,d,s,l,i){var h=w.location.hostname;
- if(h==='app.infisical.com'||h==='eu.infisical.com'||h==='us.infisical.com'||h==='gamma.infisical.com'){
- w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});
- var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';
- j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;
- f.parentNode.insertBefore(j,f);
- }})(window,document,'script','dataLayer','GTM-WL5C7MWT');</script>
- <!-- End Google Tag Manager -->
<link rel="icon" href="/infisical.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta
@@ -31,7 +22,6 @@
<script src="/runtime-ui-env.js"></script>
</head>
<body>
- <!-- GTM_NOSCRIPT_PLACEHOLDER -->
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
diff --git a/frontend/src/hooks/useGtm.ts b/frontend/src/hooks/useGtm.ts
new file mode 100644
index 0000000000..4a1907eaa6
--- /dev/null
+++ b/frontend/src/hooks/useGtm.ts
@@ -0,0 +1,43 @@
+import { useEffect } from "react";
+
+const GTM_ID = "GTM-WL5C7MWT";
+const GTM_SCRIPT_ID = "gtm-script";
+const GTM_NOSCRIPT_ID = "gtm-noscript";
+
+const CLOUD_HOSTNAMES = ["app.infisical.com", "eu.infisical.com", "us.infisical.com"];
+
+const isCloudHostname = () => CLOUD_HOSTNAMES.includes(window.location.hostname);
+
+/**
+ * Loads GTM only on Infisical Cloud pre-auth pages.
+ * Cleans up GTM scripts on unmount (when navigating to authenticated pages).
+ */
+export const useGtm = () => {
+ useEffect(() => {
+ if (!isCloudHostname()) return undefined;
+
+ // Inject GTM script
+ const script = document.createElement("script");
+ script.id = GTM_SCRIPT_ID;
+ script.async = true;
+ script.src = `https://www.googletagmanager.com/gtm.js?id=${GTM_ID}`;
+ document.head.appendChild(script);
+
+ // Inject noscript iframe
+ const noscript = document.createElement("noscript");
+ noscript.id = GTM_NOSCRIPT_ID;
+ const iframe = document.createElement("iframe");
+ iframe.src = `https://www.googletagmanager.com/ns.html?id=${GTM_ID}`;
+ iframe.height = "0";
+ iframe.width = "0";
+ iframe.style.display = "none";
+ iframe.style.visibility = "hidden";
+ noscript.appendChild(iframe);
+ document.body.insertBefore(noscript, document.body.firstChild);
+
+ return () => {
+ document.getElementById(GTM_SCRIPT_ID)?.remove();
+ document.getElementById(GTM_NOSCRIPT_ID)?.remove();
+ };
+ }, []);
+};
diff --git a/frontend/src/pages/middlewares/restrict-login-signup.tsx b/frontend/src/pages/middlewares/restrict-login-signup.tsx
index 582b6331b7..59ef9faba4 100644
--- a/frontend/src/pages/middlewares/restrict-login-signup.tsx
+++ b/frontend/src/pages/middlewares/restrict-login-signup.tsx
@@ -12,6 +12,7 @@ import { SessionStorageKeys } from "@app/const";
import { useServerConfig } from "@app/context";
import { authKeys, fetchAuthToken } from "@app/hooks/api/auth/queries";
import { setAuthToken } from "@app/hooks/api/reactQuery";
+import { useGtm } from "@app/hooks/useGtm";
const QueryParamsSchema = z.object({
callback_port: z.coerce.number().optional().catch(undefined),
@@ -21,6 +22,7 @@ const QueryParamsSchema = z.object({
export const AuthConsentWrapper = () => {
const { config } = useServerConfig();
+ useGtm();
const [hasConsented, setHasConsented] = useState(() => {
const consentInfo = sessionStorage.getItem(SessionStorageKeys.AUTH_CONSENT);
if (!consentInfo) {