Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
feat(web): load Ahrefs with analytics consent on public pages
Gus1faaf977aef2
diff --git a/src/web/src/components/analytics-consent.dom.test.tsx b/src/web/src/components/analytics-consent.dom.test.tsxindex c4b64d155..b25de586d 100644--- a/src/web/src/components/analytics-consent.dom.test.tsx+++ b/src/web/src/components/analytics-consent.dom.test.tsx@@ -6,6 +6,9 @@ const platform = vi.hoisted(() => ({isMobile: vi.fn(() => false),}))+const route = vi.hoisted(() => ({ pathname: "/" }))+vi.mock("next/navigation", () => ({ usePathname: () => route.pathname }))+vi.mock("@alook/shared", async importOriginal => ({...await importOriginal<typeof import("@alook/shared")>(),isTauri: platform.isTauri,@@ -36,6 +39,7 @@ function successfulFetch(decision: "granted" | "denied") {}beforeEach(() => {+ route.pathname = "/"clearCookies()platform.isTauri.mockReset().mockReturnValue(false)platform.isMobile.mockReset().mockReturnValue(false)@@ -93,6 +97,7 @@ describe("AnalyticsConsent", () => {expect(within(banner).getByTestId(tid.analyticsConsentAvatarCluster).querySelectorAll("svg")).toHaveLength(3)expect(screen.queryByTestId("google-tag-manager")).not.toBeInTheDocument()+ expect(screen.queryByTestId(tid.ahrefsAnalyticsFrame)).not.toBeInTheDocument()})it("hides the first-visit banner in native mobile clients", async () => {@@ -120,6 +125,7 @@ describe("AnalyticsConsent", () => {"data-gtm-id","GTM-56VHCCQZ",)+ expect(await screen.findByTestId(tid.ahrefsAnalyticsFrame)).toHaveAttribute("sandbox", "allow-scripts")expect(screen.queryByTestId(tid.analyticsConsentBanner)).not.toBeInTheDocument()expect(window.dataLayer?.[0]).toEqual(["consent",@@ -147,6 +153,7 @@ describe("AnalyticsConsent", () => {expect(screen.queryByTestId(tid.analyticsConsentBanner)).not.toBeInTheDocument()})expect(screen.queryByTestId("google-tag-manager")).not.toBeInTheDocument()+ expect(screen.queryByTestId(tid.ahrefsAnalyticsFrame)).not.toBeInTheDocument()})it("honors stored grant and denial choices on mount", async () => {@@ -154,6 +161,19 @@ describe("AnalyticsConsent", () => {const granted = render(<AnalyticsConsent />)expect(await screen.findByTestId("google-tag-manager")).toBeInTheDocument()expect(screen.queryByTestId(tid.analyticsConsentBanner)).not.toBeInTheDocument()+ const home = screen.getByTestId(tid.ahrefsAnalyticsFrame)+ for (const pathname of ["/c", "/c/me", "/c/channels/private/channel", "/sign-in", "/auth/callback"]) {+ route.pathname = pathname+ granted.rerender(<AnalyticsConsent />)+ expect(screen.queryByTestId(tid.ahrefsAnalyticsFrame)).not.toBeInTheDocument()+ }+ route.pathname = "/pricing"+ granted.rerender(<AnalyticsConsent />)+ expect(screen.getByTestId(tid.ahrefsAnalyticsFrame)).not.toBe(home)+ expect(screen.getByTestId(tid.ahrefsAnalyticsFrame).getAttribute("srcdoc"))+ .toContain(`data-page-location="${window.location.origin}/pricing"`)+ fireEvent(window, new CustomEvent("alook:analytics-consent-change", { detail: "denied" }))+ await waitFor(() => expect(screen.queryByTestId(tid.ahrefsAnalyticsFrame)).not.toBeInTheDocument())granted.unmount()clearCookies()@@ -175,6 +195,7 @@ describe("AnalyticsConsent", () => {fireEvent.click(within(banner).getByRole("button", { name: "Allow analytics" }))expect(await within(banner).findByRole("alert")).toHaveTextContent("Couldn’t save this choice")expect(screen.queryByTestId("google-tag-manager")).not.toBeInTheDocument()+ expect(screen.queryByTestId(tid.ahrefsAnalyticsFrame)).not.toBeInTheDocument()})it("changes a stored preference from the Privacy control", async () => {@@ -199,4 +220,5 @@ describe("AnalyticsConsent", () => {expect.objectContaining({ analytics_storage: "granted", ad_storage: "denied" }),])})+})diff --git a/src/web/src/components/analytics-consent.tsx b/src/web/src/components/analytics-consent.tsxindex d9fe94ca7..076624976 100644--- a/src/web/src/components/analytics-consent.tsx+++ b/src/web/src/components/analytics-consent.tsx@@ -3,6 +3,7 @@import { useAtom, useCreateAtom } from "@tanstack/react-store";import { GoogleTagManager } from "@next/third-parties/google"import { isMobile, isTauri } from "@alook/shared"+import { usePathname } from "next/navigation"import { useCallback, useEffect, useRef } from "react"import { GeneratedAvatar } from "@/components/avatar"import {@@ -125,12 +126,24 @@ function ConsentButtons({}export function AnalyticsConsent() {+ const pathname = usePathname()const { ready, decision, nativeMobile } = useStoredAnalyticsConsent(true)const { choose, saving, error } = useAnalyticsConsentChoice()return (<>{ready && decision === "granted" ? <GoogleTagManager gtmId={GTM_ID} /> : null}+ {ready && decision === "granted" && pathname && /^\/(?:pricing|contact|privacy|templates(?:\/[^/]+)?|blog(?:\/.*)?)?$/.test(pathname) ? (+ <iframe+ key={pathname}+ title="Ahrefs analytics"+ data-testid={tid.ahrefsAnalyticsFrame}+ 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>`}+ />+ ) : null}{ready && !nativeMobile && decision === null ? (<sectionaria-label="Analytics choices"diff --git a/src/web/src/lib/community/testids.ts b/src/web/src/lib/community/testids.tsindex 754ccff7d..5c1f574b8 100644--- a/src/web/src/lib/community/testids.ts+++ b/src/web/src/lib/community/testids.ts@@ -39,6 +39,7 @@ export const tid = {settingsWebVersion: "community-settings-web-version",settingsCacheSize: "community-settings-cache-size",analyticsConsentBanner: "analytics-consent-banner",+ ahrefsAnalyticsFrame: "ahrefs-analytics-frame",analyticsPreferenceControl: "analytics-preference-control",analyticsConsentNecessary: "analytics-consent-necessary",analyticsConsentAllow: "analytics-consent-allow",