From 3e6be4b5a52120a12e2db4949bf4ba2e88b43a90 Mon Sep 17 00:00:00 2001 From: elecdeer <37701077+elecdeer@users.noreply.github.com> Date: Fri, 1 Sep 2023 23:13:09 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20NEXT=5FPUBLIC=5FGOOGLE=5FANALYTICS?= =?UTF-8?q?=5FID=E7=92=B0=E5=A2=83=E5=A4=89=E6=95=B0=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/environments.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/environments.ts b/src/lib/environments.ts index d0eed05..3e9a75e 100644 --- a/src/lib/environments.ts +++ b/src/lib/environments.ts @@ -20,3 +20,6 @@ export const CONTENTFUL_ENVIRONMENT = export const ON_DEMAND_SECRET = validateEnvSet("ON_DEMAND_SECRET"); export const SLACK_WEBHOOK_URL = process.env["SLACK_WEBHOOK_URL"]; + +export const GOOGLE_ANALYTICS_ID = + process.env["NEXT_PUBLIC_GOOGLE_ANALYTICS_ID"]; From 12eea9608b301cac9bc07a3410cbd3968fb3e21e Mon Sep 17 00:00:00 2001 From: elecdeer <37701077+elecdeer@users.noreply.github.com> Date: Fri, 1 Sep 2023 23:14:53 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20Analytics=E3=82=B3=E3=83=B3?= =?UTF-8?q?=E3=83=9D=E3=83=BC=E3=83=8D=E3=83=B3=E3=83=88=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../feature/analytics/Analytics.tsx | 37 +++++++++++++++++++ src/components/feature/analytics/index.ts | 2 + 2 files changed, 39 insertions(+) create mode 100644 src/components/feature/analytics/Analytics.tsx create mode 100644 src/components/feature/analytics/index.ts diff --git a/src/components/feature/analytics/Analytics.tsx b/src/components/feature/analytics/Analytics.tsx new file mode 100644 index 0000000..d2af87f --- /dev/null +++ b/src/components/feature/analytics/Analytics.tsx @@ -0,0 +1,37 @@ +import Script from "next/script"; +import { FC } from "react"; + +import { GOOGLE_ANALYTICS_ID } from "@/lib/environments"; + +/** + * Analytics関連のスクリプトを読み込む + */ +export const Analytics: FC = () => { + //本番環境以外では読み込まない + if (process.env.NODE_ENV !== "production") { + return <>>; + } + + if (!GOOGLE_ANALYTICS_ID) { + console.warn("NEXT_PUBLIC_GOOGLE_ANALYTICS_ID is not defined."); + return <>>; + } + + return ( + <> + {/* Google Analytics */} + + + > + ); +}; diff --git a/src/components/feature/analytics/index.ts b/src/components/feature/analytics/index.ts new file mode 100644 index 0000000..fbb6485 --- /dev/null +++ b/src/components/feature/analytics/index.ts @@ -0,0 +1,2 @@ +export { Analytics } from "./Analytics"; +export type { AnalyticsProps } from "./Analytics"; From 930c853d716175a21b46f99311fc20ca41079c6a Mon Sep 17 00:00:00 2001 From: elecdeer <37701077+elecdeer@users.noreply.github.com> Date: Fri, 1 Sep 2023 23:15:05 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=5Fdocument=E3=81=AB=E5=B7=AE?= =?UTF-8?q?=E3=81=97=E8=BE=BC=E3=81=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/_document.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/pages/_document.tsx b/src/pages/_document.tsx index b3f1eed..2bb8cf1 100644 --- a/src/pages/_document.tsx +++ b/src/pages/_document.tsx @@ -1,5 +1,7 @@ import { Html, Head, Main, NextScript } from "next/document"; +import { Analytics } from "@/components/feature/analytics"; + //ここに書いたものはstorybookでは読み込まれないので注意 export default function Document() { @@ -9,6 +11,7 @@ export default function Document() {