diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..4a13b9d --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +NEXT_PUBLIC_API_KEY= +NEXT_PUBLIC_AUTH_DOMAIN= +NEXT_PUBLIC_PROJECT_ID= +NEXT_PUBLIC_STORAGE_BUCKET= +NEXT_PUBLIC_MESSAGING_SENDER_ID= +NEXT_PUBLIC_APP_ID= +NEXT_PUBLIC_MEASUREMENT_ID= diff --git a/.gitignore b/.gitignore index 7de6a74..12d0f28 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ yarn-error.log* # local env files .env*.local +.env # vercel .vercel diff --git a/app/_component/Analytics.tsx b/app/_component/Analytics.tsx new file mode 100644 index 0000000..5f0ce1f --- /dev/null +++ b/app/_component/Analytics.tsx @@ -0,0 +1,19 @@ +'use client'; + +import { logEvent } from 'firebase/analytics'; +import { usePathname } from 'next/navigation'; +import { useEffect } from 'react'; +import { analytics } from '../firebase'; + +export default function Analytics() { + const pathname = usePathname(); + + useEffect(() => { + if (!analytics) return; + logEvent(analytics, 'page_view', { + page_location: pathname, + }); + }, [pathname]); + + return <>; +} diff --git a/app/firebase.ts b/app/firebase.ts new file mode 100644 index 0000000..02bab8d --- /dev/null +++ b/app/firebase.ts @@ -0,0 +1,20 @@ +// Import the functions you need from the SDKs you need +import { getAnalytics } from 'firebase/analytics'; +import { initializeApp } from 'firebase/app'; + +// Your web app's Firebase configuration +// For Firebase JS SDK v7.20.0 and later, measurementId is optional +const firebaseConfig = { + apiKey: process.env.NEXT_PUBLIC_API_KEY, + authDomain: process.env.NEXT_PUBLIC_AUTH_DOMAIN, + projectId: process.env.NEXT_PUBLIC_PROJECT_ID, + storageBucket: process.env.NEXT_PUBLIC_STORAGE_BUCKET, + messagingSenderId: process.env.NEXT_PUBLIC_MESSAGING_SENDER_ID, + appId: process.env.NEXT_PUBLIC_APP_ID, + measurementId: process.env.NEXT_PUBLIC_MEASUREMENT_ID, +}; + +const firebaseApp = initializeApp(firebaseConfig); +const analytics = getAnalytics(firebaseApp); + +export { firebaseApp, analytics }; diff --git a/app/layout.tsx b/app/layout.tsx index ef6fd18..0a181fa 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -3,6 +3,7 @@ import { Inter } from 'next/font/google'; import React from 'react'; import './globals.css'; import 'swiper/css/bundle'; +import Analytics from './_component/Analytics'; import Footer from './_component/footer'; import Header from './_component/header/header'; import { getPages } from '@/components/loadFiles'; @@ -57,6 +58,7 @@ export default function RootLayout({ children }: Props) {
{children}