Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): exclude private routes from Google analytics

Gus6975ca2cbcab
diff --git a/src/web/src/components/analytics-consent.dom.test.tsx b/src/web/src/components/analytics-consent.dom.test.tsx
index b25de586d..f3f892d5e 100644
--- a/src/web/src/components/analytics-consent.dom.test.tsx
+++ b/src/web/src/components/analytics-consent.dom.test.tsx
@@ -40,6 +40,7 @@ function successfulFetch(decision: "granted" | "denied") {
beforeEach(() => {
route.pathname = "/"
+ window.history.replaceState({}, "", "/")
clearCookies()
platform.isTauri.mockReset().mockReturnValue(false)
platform.isMobile.mockReset().mockReturnValue(false)
@@ -50,6 +51,7 @@ afterEach(() => {
clearCookies()
vi.unstubAllGlobals()
delete window.dataLayer
+ Reflect.deleteProperty(window, "ga-disable-G-STBCL8F4ZY")
})
describe("AnalyticsConsent", () => {
@@ -166,6 +168,7 @@ describe("AnalyticsConsent", () => {
route.pathname = pathname
granted.rerender(<AnalyticsConsent />)
expect(screen.queryByTestId(tid.ahrefsAnalyticsFrame)).not.toBeInTheDocument()
+ expect(screen.queryByTestId("google-tag-manager")).not.toBeInTheDocument()
}
route.pathname = "/pricing"
granted.rerender(<AnalyticsConsent />)
@@ -221,4 +224,29 @@ describe("AnalyticsConsent", () => {
])
})
+ it("blocks already-loaded GA4 immediately on private history changes", async () => {
+ document.cookie = "alook_analytics_consent=v1.granted; Path=/"
+ const view = render(<AnalyticsConsent />)
+ await screen.findByTestId("google-tag-manager")
+ expect(Reflect.get(window, "ga-disable-G-STBCL8F4ZY")).toBe(false)
+ window.history.pushState({}, "", "/c/me?private=canary")
+ expect(Reflect.get(window, "ga-disable-G-STBCL8F4ZY")).toBe(true)
+ window.history.replaceState({}, "", "/pricing")
+ expect(Reflect.get(window, "ga-disable-G-STBCL8F4ZY")).toBe(false)
+ document.cookie = "alook_analytics_consent=v1.denied; Path=/"
+ expect(Reflect.get(window, "ga-disable-G-STBCL8F4ZY")).toBe(true)
+ view.unmount()
+ window.history.replaceState({}, "", "/")
+ })
+
+ it.each(["/blog", "/blog/public-article", "/pricing", "/contact"])("keeps %s measured", async pathname => {
+ route.pathname = pathname
+ window.history.replaceState({}, "", `${pathname}?private=canary`)
+ document.cookie = "alook_analytics_consent=v1.granted; Path=/"
+ render(<AnalyticsConsent />)
+ expect(await screen.findByTestId("google-tag-manager")).toBeInTheDocument()
+ expect(screen.getByTestId(tid.ahrefsAnalyticsFrame).getAttribute("srcdoc"))
+ .toContain(`data-page-location="${window.location.origin}${pathname}"`)
+ })
+
})
diff --git a/src/web/src/components/analytics-consent.tsx b/src/web/src/components/analytics-consent.tsx
index 076624976..f9bd4f214 100644
--- a/src/web/src/components/analytics-consent.tsx
+++ b/src/web/src/components/analytics-consent.tsx
@@ -18,6 +18,8 @@ import { tid } from "@/lib/community/testids"
import styles from "./analytics-consent.module.css"
const GTM_ID = "GTM-56VHCCQZ"
+const GA_DISABLE = "ga-disable-G-STBCL8F4ZY"
+const isPublicAnalyticsPath = (path: string) => /^\/(?:pricing|contact|privacy|templates(?:\/[^/]+)?|blog(?:\/.*)?)?$/.test(path)
function useStoredAnalyticsConsent(syncGoogleConsent = false) {
const [ready, setReady] = useAtom(useCreateAtom(false))
@@ -129,11 +131,27 @@ export function AnalyticsConsent() {
const pathname = usePathname()
const { ready, decision, nativeMobile } = useStoredAnalyticsConsent(true)
const { choose, saving, error } = useAnalyticsConsentChoice()
+ const allowed = ready && decision === "granted" && !!pathname && isPublicAnalyticsPath(pathname)
+
+ useEffect(() => {
+ Object.defineProperty(window, GA_DISABLE, {
+ configurable: true,
+ get: () => readAnalyticsConsent() !== "granted" || !isPublicAnalyticsPath(window.location.pathname),
+ })
+ }, [])
+ useEffect(() => {
+ if (decision !== "granted") return
+ const referrer = new URL(document.referrer || window.location.origin)
+ const safeReferrer = referrer.origin !== window.location.origin || isPublicAnalyticsPath(referrer.pathname)
+ ? `${referrer.origin}${referrer.pathname}` : ""
+ window.dataLayer ??= []
+ window.dataLayer.push(["set", "page_referrer", safeReferrer])
+ }, [decision])
return (
<>
- {ready && decision === "granted" ? <GoogleTagManager gtmId={GTM_ID} /> : null}
- {ready && decision === "granted" && pathname && /^\/(?:pricing|contact|privacy|templates(?:\/[^/]+)?|blog(?:\/.*)?)?$/.test(pathname) ? (
+ {allowed ? <GoogleTagManager gtmId={GTM_ID} /> : null}
+ {allowed ? (
<iframe
key={pathname}
title="Ahrefs analytics"
@@ -141,7 +159,7 @@ export function AnalyticsConsent() {
hidden aria-hidden="true"
sandbox="allow-scripts"
referrerPolicy="no-referrer"
- srcDoc={`<title>Alook</title><script async src="https://analytics.ahrefs.com/analytics.js" data-key="Td2Wr/poHD0pDEV30W9xMw" data-page-location="${new URL(pathname, window.location.origin).href.replace(/"/g, "%22")}"></script>`}
+ srcDoc={`<title>Alook</title><script async src="https://analytics.ahrefs.com/analytics.js" data-key="Td2Wr/poHD0pDEV30W9xMw" data-page-location="${new URL(pathname!, window.location.origin).href.replace(/"/g, "%22")}"></script>`}
/>
) : null}
{ready && !nativeMobile && decision === null ? (