Skip to content

Commit

Permalink
Merge pull request #115 from hufs-sports-live/feat/ga
Browse files Browse the repository at this point in the history
[FEAT] google analytics 추가
  • Loading branch information
seongminn authored Nov 29, 2023
2 parents 0cbc5bf + 7226f44 commit 94ebec1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sentry.client.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import * as Sentry from '@sentry/nextjs';

Sentry.init({
dsn: 'https://cc1dcf1845d69a9079c615d462e122b2@o4506026798088192.ingest.sentry.io/4506026816503808',
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,

// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 0.2,
Expand Down
5 changes: 2 additions & 3 deletions sentry.edge.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// Note that this config is unrelated to the Vercel Edge Runtime and is also required when running locally.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import * as Sentry from "@sentry/nextjs";
import * as Sentry from '@sentry/nextjs';

Sentry.init({
dsn: "https://cc1dcf1845d69a9079c615d462e122b2@o4506026798088192.ingest.sentry.io/4506026816503808",

dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1,

Expand Down
3 changes: 1 addition & 2 deletions sentry.server.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import * as Sentry from '@sentry/nextjs';

Sentry.init({
dsn: 'https://cc1dcf1845d69a9079c615d462e122b2@o4506026798088192.ingest.sentry.io/4506026816503808',

dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 0.2,

Expand Down
36 changes: 36 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import './globals.css';
import { Analytics } from '@vercel/analytics/react';
import type { Metadata } from 'next';
import { Noto_Sans_KR } from 'next/font/google';
import { useRouter } from 'next/router';
import { useEffect } from 'react';

import Footer from '@/components/layout/Footer';
import Header from '@/components/layout/Header';
import * as ga from '@/types/ga/gtag';

import ReactQueryProvider from './ReactQueryProvider';

Expand All @@ -27,8 +30,41 @@ export default function RootLayout({
}: {
children: React.ReactNode;
}) {
const router = useRouter();

useEffect(() => {
const handleRouteChange = (url: URL) => {
ga.pageView(url);
};

router.events.on('routeChangeComplete', handleRouteChange);

return () => {
router.events.off('routeChangeComplete', handleRouteChange);
};
}, [router.events]);

return (
<html lang="en">
<head>
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_TAG}`}
/>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_TAG}', {
page_path: window.location.pathname,
});
`,
}}
/>
</head>
<body className={`${inter.className} m-auto max-w-md`}>
<ReactQueryProvider>
<Header />
Expand Down
11 changes: 11 additions & 0 deletions src/types/ga/gtag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare global {
interface Window {
gtag: (param1: string, param2: string, param3: object) => void;
}
}

export const pageView = (url: URL) => {
window.gtag('config', process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_TAG!, {
page_path: url,
});
};

0 comments on commit 94ebec1

Please sign in to comment.