From 8e0a5949e63461e1a7c7dcdd971523787e93396b Mon Sep 17 00:00:00 2001 From: tomolld Date: Thu, 27 Feb 2025 21:35:22 +0900 Subject: [PATCH 1/6] =?UTF-8?q?=E3=83=9A=E3=83=BC=E3=82=B8=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=A9=9F=E8=83=BD=E3=81=AE=E8=AA=8D=E8=A8=BC=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E6=94=B9=E5=96=84=E3=81=97=E3=80=81=E3=83=A6?= =?UTF-8?q?=E3=83=BC=E3=82=B6=E3=83=BC=E3=81=8C=E6=9C=AA=E8=AA=8D=E8=A8=BC?= =?UTF-8?q?=E3=81=AE=E5=A0=B4=E5=90=88=E3=81=AB=E3=83=AA=E3=83=80=E3=82=A4?= =?UTF-8?q?=E3=83=AC=E3=82=AF=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97?= =?UTF-8?q?=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82=E3=81=BE=E3=81=9F=E3=80=81?= =?UTF-8?q?=E3=83=AD=E3=82=B0=E5=8F=96=E5=BE=97=E8=A8=AD=E5=AE=9A=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E3=81=97=E3=80=81HeroSection=E3=81=A8OG?= =?UTF-8?q?=E3=83=AB=E3=83=BC=E3=83=88=E3=81=AE=E5=86=8D=E6=A4=9C=E8=A8=BC?= =?UTF-8?q?=E6=99=82=E9=96=93=E3=82=92=E8=A8=AD=E5=AE=9A=E3=81=97=E3=81=BE?= =?UTF-8?q?=E3=81=97=E3=81=9F=E3=80=82EditHeader=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=81=AE=E3=82=B9?= =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=AB=E3=82=92=E8=AA=BF=E6=95=B4=E3=81=97?= =?UTF-8?q?=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- next/next.config.ts | 5 +++++ .../user/[handle]/page-management/page.tsx | 8 ++++---- .../page/[slug]/edit/components/header/index.tsx | 13 +++++-------- .../app/[locale]/components/hero-section/index.tsx | 2 ++ next/src/app/api/og/route.tsx | 1 + 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/next/next.config.ts b/next/next.config.ts index 3fb69046..6c0434e4 100644 --- a/next/next.config.ts +++ b/next/next.config.ts @@ -8,6 +8,11 @@ const analyzeBundles = withBundleAnalyzer({ }); /** @type {import('next').NextConfig} */ const config: NextConfig = { + logging: { + fetches: { + fullUrl: true, + }, + }, experimental: { serverActions: { bodySizeLimit: "5mb", diff --git a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/page.tsx b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/page.tsx index 2dd2d502..b1e15512 100644 --- a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/page.tsx +++ b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/page.tsx @@ -1,4 +1,5 @@ -import { auth } from "@/auth"; +import { getCurrentUser } from "@/auth"; +import { redirect } from "next/navigation"; import { PageManagementTab } from "./components/page-management-tab"; import { getGeoViewData } from "./components/page-view-data/view-data"; import { fetchPaginatedOwnPages } from "./db/queries.server"; @@ -9,10 +10,9 @@ export default async function PageManagementPage({ params: Promise<{ locale: string }>; searchParams: Promise<{ [key: string]: string | string[] | undefined }>; }) { - const session = await auth(); - const currentUser = session?.user; + const currentUser = await getCurrentUser(); if (!currentUser || !currentUser.id) { - throw new Error("Unauthorized"); + return redirect("/auth/login"); } const { locale } = await params; const { page = "1", query = "" } = await searchParams; diff --git a/next/src/app/[locale]/(edit-layout)/user/[handle]/page/[slug]/edit/components/header/index.tsx b/next/src/app/[locale]/(edit-layout)/user/[handle]/page/[slug]/edit/components/header/index.tsx index 98b79c69..aebcd010 100644 --- a/next/src/app/[locale]/(edit-layout)/user/[handle]/page/[slug]/edit/components/header/index.tsx +++ b/next/src/app/[locale]/(edit-layout)/user/[handle]/page/[slug]/edit/components/header/index.tsx @@ -11,7 +11,6 @@ import { Separator } from "@/components/ui/separator"; import { Link } from "@/i18n/routing"; import type { PageStatus } from "@prisma/client"; import { - ArrowRight, Check, Globe, InfoIcon, @@ -113,8 +112,8 @@ export function EditHeader({ {initialStatus === "PUBLIC" ? "Public" : "Private"} - -
+ +
- Public - Translates to:
-
+
EN
JP
diff --git a/next/src/app/[locale]/components/hero-section/index.tsx b/next/src/app/[locale]/components/hero-section/index.tsx index 2c23881c..919f98b0 100644 --- a/next/src/app/[locale]/components/hero-section/index.tsx +++ b/next/src/app/[locale]/components/hero-section/index.tsx @@ -7,6 +7,8 @@ import { TranslateActionSection } from "@/app/[locale]/components/translate-acti import { fetchPageWithTranslations } from "@/app/[locale]/db/queries.server"; import { fetchLatestPageAITranslationInfo } from "@/app/[locale]/db/queries.server"; import { notFound } from "next/navigation"; + +export const revalidate = 3600; export default async function HeroSection({ locale }: { locale: string }) { const pageSlug = locale === "ja" ? "evame" : "evame-ja"; const topPageWithTranslations = await fetchPageWithTranslations( diff --git a/next/src/app/api/og/route.tsx b/next/src/app/api/og/route.tsx index 02496b4a..194981ca 100644 --- a/next/src/app/api/og/route.tsx +++ b/next/src/app/api/og/route.tsx @@ -3,6 +3,7 @@ import { join } from "node:path"; import { fetchPageContext } from "@/app/[locale]/(common-layout)/user/[handle]/page/[slug]/page"; import { ImageResponse } from "next/og"; +export const revalidate = 3600; export async function GET(req: Request): Promise { const { searchParams } = new URL(req.url); const interFontSemiBold = await readFile( From 1f45f77020be8dd973c3a70757712a45c33b759b Mon Sep 17 00:00:00 2001 From: tomolld Date: Thu, 27 Feb 2025 21:43:38 +0900 Subject: [PATCH 2/6] =?UTF-8?q?PageManagementTab=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=81=A7=E3=83=9A?= =?UTF-8?q?=E3=83=BC=E3=82=B8=E5=A4=89=E6=9B=B4=E3=83=8F=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=82=92=E5=89=8A=E9=99=A4=E3=81=97=E3=80=81?= =?UTF-8?q?PaginationBar=E3=81=AEonPageChange=E3=83=97=E3=83=AD=E3=83=91?= =?UTF-8?q?=E3=83=86=E3=82=A3=E3=81=ABsetPage=E3=82=92=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E6=B8=A1=E3=81=99=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/page-management-tab.tsx | 5 +---- .../components/page-view-data/view-data.ts | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-management-tab.tsx b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-management-tab.tsx index 9d33853f..18615746 100644 --- a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-management-tab.tsx +++ b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-management-tab.tsx @@ -34,9 +34,6 @@ export function PageManagementTab({ shallow: false, }), ); - const handlePageChange = (newPage: number) => { - setPage(newPage); - }; const getStatusBadge = (status: PageStatus) => { if (status === "PUBLIC") { @@ -100,7 +97,7 @@ export function PageManagementTab({
diff --git a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts index 796ae6bc..f15dfe9d 100644 --- a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts +++ b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts @@ -1,5 +1,5 @@ import { BetaAnalyticsDataClient } from "@google-analytics/data"; - +import { unstable_cache } from "next/cache"; export interface GeoViewData { country: string; views: number; @@ -24,10 +24,11 @@ function getCredentialsFromBase64() { } // Google Analytics データ取得関数 -export async function getGeoViewData(path: string): Promise { - try { - // サービスアカウント認証と Analytics Data クライアントの初期化 - const analyticsDataClient = new BetaAnalyticsDataClient({ +export const getGeoViewData = unstable_cache( + async (path: string): Promise => { + try { + // サービスアカウント認証と Analytics Data クライアントの初期化 + const analyticsDataClient = new BetaAnalyticsDataClient({ credentials: getCredentialsFromBase64(), }); @@ -72,4 +73,10 @@ export async function getGeoViewData(path: string): Promise { console.error("Analytics API error:", error); return []; } -} + }, + ['geo-view-data'], + { + revalidate: 3600, // 1時間でキャッシュを再検証 + tags: ["analytics-data"], + }, +); From 80616572b48e650eb2c4fd5331c690197ed09925 Mon Sep 17 00:00:00 2001 From: tomolld Date: Thu, 27 Feb 2025 21:43:43 +0900 Subject: [PATCH 3/6] =?UTF-8?q?Analytics=20API=E3=81=AE=E3=82=A8=E3=83=A9?= =?UTF-8?q?=E3=83=BC=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0?= =?UTF-8?q?=E3=82=92=E6=94=B9=E5=96=84=E3=81=97=E3=80=81=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E3=81=AE=E5=8F=AF=E8=AA=AD=E6=80=A7=E3=82=92=E5=90=91?= =?UTF-8?q?=E4=B8=8A=E3=81=95=E3=81=9B=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= =?UTF-8?q?=E3=81=BE=E3=81=9F=E3=80=81=E3=83=87=E3=83=BC=E3=82=BF=E6=95=B4?= =?UTF-8?q?=E5=BD=A2=E3=81=AE=E3=83=AD=E3=82=B8=E3=83=83=E3=82=AF=E3=82=92?= =?UTF-8?q?=E6=95=B4=E7=90=86=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/page-view-data/view-data.ts | 80 +++++++++---------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts index f15dfe9d..27affdb6 100644 --- a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts +++ b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts @@ -29,53 +29,53 @@ export const getGeoViewData = unstable_cache( try { // サービスアカウント認証と Analytics Data クライアントの初期化 const analyticsDataClient = new BetaAnalyticsDataClient({ - credentials: getCredentialsFromBase64(), - }); + credentials: getCredentialsFromBase64(), + }); - // 地域別ページビューデータを取得 - const propertyId = process.env.GOOGLE_ANALYTICS_PROPERTY_ID; + // 地域別ページビューデータを取得 + const propertyId = process.env.GOOGLE_ANALYTICS_PROPERTY_ID; - if (!propertyId) { - throw new Error("GA4_PROPERTY_ID is not defined"); - } + if (!propertyId) { + throw new Error("GA4_PROPERTY_ID is not defined"); + } - const [response] = await analyticsDataClient.runReport({ - property: `properties/${propertyId}`, - dateRanges: [{ startDate: "420daysAgo", endDate: "today" }], - dimensions: [{ name: "country" }], - metrics: [{ name: "screenPageViews" }], - dimensionFilter: { - filter: { - fieldName: "pagePath", - stringFilter: { - matchType: "CONTAINS", - value: path, + const [response] = await analyticsDataClient.runReport({ + property: `properties/${propertyId}`, + dateRanges: [{ startDate: "420daysAgo", endDate: "today" }], + dimensions: [{ name: "country" }], + metrics: [{ name: "screenPageViews" }], + dimensionFilter: { + filter: { + fieldName: "pagePath", + stringFilter: { + matchType: "CONTAINS", + value: path, + }, }, }, - }, - orderBys: [ - { - metric: { metricName: "screenPageViews" }, - desc: true, - }, - ], - limit: 10, - }); + orderBys: [ + { + metric: { metricName: "screenPageViews" }, + desc: true, + }, + ], + limit: 10, + }); - // データを整形 - return ( - response.rows?.map((row) => ({ - country: row.dimensionValues?.[0].value || "", - views: Number(row.metricValues?.[0].value || "0"), - })) || [] - ); - } catch (error) { - console.error("Analytics API error:", error); - return []; - } + // データを整形 + return ( + response.rows?.map((row) => ({ + country: row.dimensionValues?.[0].value || "", + views: Number(row.metricValues?.[0].value || "0"), + })) || [] + ); + } catch (error) { + console.error("Analytics API error:", error); + return []; + } }, - ['geo-view-data'], - { + ["geo-view-data"], + { revalidate: 3600, // 1時間でキャッシュを再検証 tags: ["analytics-data"], }, From 01917af2f4addc6705fbd87e72f4ec7ea041384f Mon Sep 17 00:00:00 2001 From: tomolld Date: Thu, 27 Feb 2025 21:52:15 +0900 Subject: [PATCH 4/6] =?UTF-8?q?getGeoViewData=E9=96=A2=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E3=82=AD=E3=83=A3=E3=83=83=E3=82=B7=E3=83=A5=E3=82=AD=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E3=83=91=E3=82=B9=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97?= =?UTF-8?q?=E3=80=81=E3=82=AD=E3=83=A3=E3=83=83=E3=82=B7=E3=83=A5=E3=81=AE?= =?UTF-8?q?=E5=86=8D=E6=A4=9C=E8=A8=BC=E3=82=92=E6=94=B9=E5=96=84=E3=81=97?= =?UTF-8?q?=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/page-view-data/view-data.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts index 27affdb6..3b29f5e4 100644 --- a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts +++ b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts @@ -24,10 +24,11 @@ function getCredentialsFromBase64() { } // Google Analytics データ取得関数 -export const getGeoViewData = unstable_cache( - async (path: string): Promise => { - try { - // サービスアカウント認証と Analytics Data クライアントの初期化 +export const getGeoViewData = (path: string) => + unstable_cache( + async (): Promise => { + try { + // サービスアカウント認証と Analytics Data クライアントの初期化 const analyticsDataClient = new BetaAnalyticsDataClient({ credentials: getCredentialsFromBase64(), }); @@ -74,7 +75,7 @@ export const getGeoViewData = unstable_cache( return []; } }, - ["geo-view-data"], + ["geo-view-data", path], { revalidate: 3600, // 1時間でキャッシュを再検証 tags: ["analytics-data"], From 2e7f24f94559905f45a35af91fd518e9105e2f7e Mon Sep 17 00:00:00 2001 From: tomolld Date: Thu, 27 Feb 2025 21:52:21 +0900 Subject: [PATCH 5/6] =?UTF-8?q?getGeoViewData=E9=96=A2=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E6=95=B4=E7=90=86=E3=81=97?= =?UTF-8?q?=E3=80=81=E5=8F=AF=E8=AA=AD=E6=80=A7=E3=82=92=E5=90=91=E4=B8=8A?= =?UTF-8?q?=E3=81=95=E3=81=9B=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82=E3=81=BE?= =?UTF-8?q?=E3=81=9F=E3=80=81=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=8F=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0=E3=82=92=E5=BC=B7=E5=8C=96?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/page-view-data/view-data.ts | 92 +++++++++---------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts index 3b29f5e4..8e1ec51d 100644 --- a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts +++ b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts @@ -29,55 +29,55 @@ export const getGeoViewData = (path: string) => async (): Promise => { try { // サービスアカウント認証と Analytics Data クライアントの初期化 - const analyticsDataClient = new BetaAnalyticsDataClient({ - credentials: getCredentialsFromBase64(), - }); + const analyticsDataClient = new BetaAnalyticsDataClient({ + credentials: getCredentialsFromBase64(), + }); - // 地域別ページビューデータを取得 - const propertyId = process.env.GOOGLE_ANALYTICS_PROPERTY_ID; + // 地域別ページビューデータを取得 + const propertyId = process.env.GOOGLE_ANALYTICS_PROPERTY_ID; - if (!propertyId) { - throw new Error("GA4_PROPERTY_ID is not defined"); - } + if (!propertyId) { + throw new Error("GA4_PROPERTY_ID is not defined"); + } - const [response] = await analyticsDataClient.runReport({ - property: `properties/${propertyId}`, - dateRanges: [{ startDate: "420daysAgo", endDate: "today" }], - dimensions: [{ name: "country" }], - metrics: [{ name: "screenPageViews" }], - dimensionFilter: { - filter: { - fieldName: "pagePath", - stringFilter: { - matchType: "CONTAINS", - value: path, + const [response] = await analyticsDataClient.runReport({ + property: `properties/${propertyId}`, + dateRanges: [{ startDate: "420daysAgo", endDate: "today" }], + dimensions: [{ name: "country" }], + metrics: [{ name: "screenPageViews" }], + dimensionFilter: { + filter: { + fieldName: "pagePath", + stringFilter: { + matchType: "CONTAINS", + value: path, + }, }, }, - }, - orderBys: [ - { - metric: { metricName: "screenPageViews" }, - desc: true, - }, - ], - limit: 10, - }); + orderBys: [ + { + metric: { metricName: "screenPageViews" }, + desc: true, + }, + ], + limit: 10, + }); - // データを整形 - return ( - response.rows?.map((row) => ({ - country: row.dimensionValues?.[0].value || "", - views: Number(row.metricValues?.[0].value || "0"), - })) || [] - ); - } catch (error) { - console.error("Analytics API error:", error); - return []; - } - }, - ["geo-view-data", path], - { - revalidate: 3600, // 1時間でキャッシュを再検証 - tags: ["analytics-data"], - }, -); + // データを整形 + return ( + response.rows?.map((row) => ({ + country: row.dimensionValues?.[0].value || "", + views: Number(row.metricValues?.[0].value || "0"), + })) || [] + ); + } catch (error) { + console.error("Analytics API error:", error); + return []; + } + }, + ["geo-view-data", path], + { + revalidate: 3600, // 1時間でキャッシュを再検証 + tags: ["analytics-data"], + }, + ); From 586ab63bba256a138ac513f94b50447518c68f09 Mon Sep 17 00:00:00 2001 From: tomolld Date: Thu, 27 Feb 2025 22:01:40 +0900 Subject: [PATCH 6/6] =?UTF-8?q?getGeoViewData=E9=96=A2=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E6=A7=8B=E9=80=A0=E3=82=92=E6=94=B9=E5=96=84=E3=81=97=E3=80=81?= =?UTF-8?q?=E3=82=AD=E3=83=A3=E3=83=83=E3=82=B7=E3=83=A5=E3=81=AE=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=96=B9=E6=B3=95=E3=82=92=E6=9C=80=E9=81=A9=E5=8C=96?= =?UTF-8?q?=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82=E3=81=BE=E3=81=9F?= =?UTF-8?q?=E3=80=81=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=8F=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=B0=E3=82=92=E5=BC=B7=E5=8C=96=E3=81=97?= =?UTF-8?q?=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/page-view-data/view-data.ts | 101 +++++++++--------- 1 file changed, 50 insertions(+), 51 deletions(-) diff --git a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts index 8e1ec51d..cb08373c 100644 --- a/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts +++ b/next/src/app/[locale]/(common-layout)/user/[handle]/page-management/components/page-view-data/view-data.ts @@ -24,60 +24,59 @@ function getCredentialsFromBase64() { } // Google Analytics データ取得関数 -export const getGeoViewData = (path: string) => - unstable_cache( - async (): Promise => { - try { - // サービスアカウント認証と Analytics Data クライアントの初期化 - const analyticsDataClient = new BetaAnalyticsDataClient({ - credentials: getCredentialsFromBase64(), - }); +export const getGeoViewData = unstable_cache( + async (path: string) => { + try { + // サービスアカウント認証と Analytics Data クライアントの初期化 + const analyticsDataClient = new BetaAnalyticsDataClient({ + credentials: getCredentialsFromBase64(), + }); - // 地域別ページビューデータを取得 - const propertyId = process.env.GOOGLE_ANALYTICS_PROPERTY_ID; + // 地域別ページビューデータを取得 + const propertyId = process.env.GOOGLE_ANALYTICS_PROPERTY_ID; - if (!propertyId) { - throw new Error("GA4_PROPERTY_ID is not defined"); - } + if (!propertyId) { + throw new Error("GA4_PROPERTY_ID is not defined"); + } - const [response] = await analyticsDataClient.runReport({ - property: `properties/${propertyId}`, - dateRanges: [{ startDate: "420daysAgo", endDate: "today" }], - dimensions: [{ name: "country" }], - metrics: [{ name: "screenPageViews" }], - dimensionFilter: { - filter: { - fieldName: "pagePath", - stringFilter: { - matchType: "CONTAINS", - value: path, - }, + const [response] = await analyticsDataClient.runReport({ + property: `properties/${propertyId}`, + dateRanges: [{ startDate: "420daysAgo", endDate: "today" }], + dimensions: [{ name: "country" }], + metrics: [{ name: "screenPageViews" }], + dimensionFilter: { + filter: { + fieldName: "pagePath", + stringFilter: { + matchType: "CONTAINS", + value: path, }, }, - orderBys: [ - { - metric: { metricName: "screenPageViews" }, - desc: true, - }, - ], - limit: 10, - }); + }, + orderBys: [ + { + metric: { metricName: "screenPageViews" }, + desc: true, + }, + ], + limit: 10, + }); - // データを整形 - return ( - response.rows?.map((row) => ({ - country: row.dimensionValues?.[0].value || "", - views: Number(row.metricValues?.[0].value || "0"), - })) || [] - ); - } catch (error) { - console.error("Analytics API error:", error); - return []; - } - }, - ["geo-view-data", path], - { - revalidate: 3600, // 1時間でキャッシュを再検証 - tags: ["analytics-data"], - }, - ); + // データを整形 + return ( + response.rows?.map((row) => ({ + country: row.dimensionValues?.[0].value || "", + views: Number(row.metricValues?.[0].value || "0"), + })) || [] + ); + } catch (error) { + console.error("Analytics API error:", error); + return []; + } + }, + [], + { + revalidate: 3600, // 1時間でキャッシュを再検証 + tags: ["analytics-data"], + }, +);