From ce6c5dda02a3260cd3d5723f01a56492c6e4bbd6 Mon Sep 17 00:00:00 2001 From: contradiction29 Date: Sun, 21 Apr 2024 22:37:37 +0900 Subject: [PATCH 1/2] Remove google analytics-related scripts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit この形式だとpage_viewが正確に計測できないため、Cloudflareの組み込み機能に切り替える。 関連するスクリプトを削除した。 --- app/modules/gtags.client.ts | 49 ------------------------------------- app/root.tsx | 45 ---------------------------------- 2 files changed, 94 deletions(-) delete mode 100644 app/modules/gtags.client.ts diff --git a/app/modules/gtags.client.ts b/app/modules/gtags.client.ts deleted file mode 100644 index 44cf38f5..00000000 --- a/app/modules/gtags.client.ts +++ /dev/null @@ -1,49 +0,0 @@ -declare global { - interface Window { - gtag: ( - option: string, - gaTrackingId: string, - options: Record - ) => void; - } - } - - /** - * @example - * https://developers.google.com/analytics/devguides/collection/gtagjs/pages - */ - export const pageview = (url: string, trackingId: string) => { - if (!window.gtag) { - console.warn( - "window.gtag is not defined. This could mean your google anylatics script has not loaded on the page yet." - ); - return; - } - window.gtag("config", trackingId, { - page_path: url, - }); - }; - - /** - * @example - * https://developers.google.com/analytics/devguides/collection/gtagjs/events - */ - export const event = ({ - action, - category, - label, - value, - }: Record) => { - if (!window.gtag) { - console.warn( - "window.gtag is not defined. This could mean your google anylatics script has not loaded on the page yet." - ); - return; - } - window.gtag("event", action, { - event_category: category, - event_label: label, - value: value, - }); - }; - \ No newline at end of file diff --git a/app/root.tsx b/app/root.tsx index 00513a95..e7750937 100644 --- a/app/root.tsx +++ b/app/root.tsx @@ -6,18 +6,12 @@ import { Scripts, ScrollRestoration, isRouteErrorResponse, - json, - useLoaderData, - useLocation, useRouteError, } from "@remix-run/react"; import type { LinksFunction } from "@remix-run/node"; import stylesheet from "~/tailwind.css?url"; import { SpeedInsights } from "@vercel/speed-insights/remix" - -import * as gtag from "~/modules/gtags.client" -import { useEffect } from "react"; import { PageTransitionProgressBar } from "./components/PageTransitionProgressBar"; export const links: LinksFunction = () => [ @@ -30,25 +24,8 @@ export const links: LinksFunction = () => [ }, ]; -export async function loader() { - const gaTrackingId = process.env.NODE_ENV !== "development" ? process.env.GA_TRACKING_ID : null; - return json({ - gaTrackingId : gaTrackingId ?? "" - }); -} - - export function Layout({ children }: { children: React.ReactNode }) { - const { gaTrackingId } = useLoaderData(); - const location = useLocation(); - - useEffect(() => { - if (gaTrackingId?.length) { - gtag.pageview(location.pathname, gaTrackingId); - } - }, [location, gaTrackingId]); - return ( @@ -64,28 +41,6 @@ export function Layout({ children }: { children: React.ReactNode }) { - {!gaTrackingId ? null : ( - <> -