Changeset View
Changeset View
Standalone View
Standalone View
web/e.cash/app/components/Analytics.tsx
| // Copyright (c) 2025 The Bitcoin developers | // Copyright (c) 2025 The Bitcoin developers | ||||
| // Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
| "use client"; | "use client"; | ||||
| import { useEffect } from "react"; | import { useEffect } from "react"; | ||||
| import Script from "next/script"; | import Script from "next/script"; | ||||
| import { usePathname, useSearchParams } from "next/navigation"; | import { usePathname, useSearchParams } from "next/navigation"; | ||||
| import type { ComponentProps } from "react"; | |||||
| const GA_ID = process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID; | const GA_ID = process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID; | ||||
| const isEnabled = | const isEnabled = | ||||
| typeof GA_ID === "string" && | typeof GA_ID === "string" && | ||||
| GA_ID.trim() !== "" && | GA_ID.trim() !== "" && | ||||
| process.env.NODE_ENV === "production"; | process.env.NODE_ENV === "production"; | ||||
| Show All 14 Lines | useEffect(() => { | ||||
| const url = searchParams?.toString() | const url = searchParams?.toString() | ||||
| ? `${pathname}?${searchParams.toString()}` | ? `${pathname}?${searchParams.toString()}` | ||||
| : pathname || "/"; | : pathname || "/"; | ||||
| pageview(url); | pageview(url); | ||||
| }, [pathname, searchParams]); | }, [pathname, searchParams]); | ||||
| if (!isEnabled) return null; | if (!isEnabled) return null; | ||||
| const scriptProps: ComponentProps<typeof Script> = { | const scriptProps = { | ||||
| src: `https://www.googletagmanager.com/gtag/js?id=${GA_ID}`, | src: `https://www.googletagmanager.com/gtag/js?id=${GA_ID}`, | ||||
| strategy: "afterInteractive", | strategy: "afterInteractive" as const, | ||||
| }; | }; | ||||
| return ( | return ( | ||||
| <> | <> | ||||
| <Script {...scriptProps} /> | <Script {...(scriptProps as Record<string, unknown>)} /> | ||||
| <Script id="gtag-init" strategy="afterInteractive"> | <Script id="gtag-init" strategy="afterInteractive"> | ||||
| {` | {` | ||||
| window.dataLayer = window.dataLayer || []; | window.dataLayer = window.dataLayer || []; | ||||
| function gtag(){dataLayer.push(arguments);} | function gtag(){dataLayer.push(arguments);} | ||||
| gtag('js', new Date()); | gtag('js', new Date()); | ||||
| gtag('config', '${GA_ID}'); | gtag('config', '${GA_ID}'); | ||||
| `} | `} | ||||
| </Script> | </Script> | ||||
| </> | </> | ||||
| ); | ); | ||||
| } | } | ||||