Skip to content

Commit

Permalink
feat: ignore ga when env is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed May 29, 2024
1 parent e5d18f1 commit ddf796f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion client/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
NEXT_PUBLIC_FIREBASE_CONFIG={}
NEXT_PUBLIC_AUTH_EMULATOR_URL=http://localhost:9099
NEXT_PUBLIC_GA_ID=UA-SOMENUMBER-X
NEXT_PUBLIC_GA_ID=
17 changes: 10 additions & 7 deletions client/pages/_document.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ function Document() {
<Html lang="ja">
<Head>
<title>{APP_NAME}</title>
<meta name="robots" content="noindex,nofollow" />
<meta name="description" content={APP_NAME} />
<link rel="icon" href={staticPath.favicon_png} />
<script async src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`} />
<script
dangerouslySetInnerHTML={{
__html: `
{GA_ID && (
<>
<script async src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`} />
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_ID}', {
page_path: window.location.pathname,
});
`,
}}
/>
}}
/>
</>
)}
</Head>
<body>
<Main />
Expand Down
6 changes: 4 additions & 2 deletions client/utils/gtag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const GA_ID = process.env.NEXT_PUBLIC_GA_ID ?? '';
export const GA_ID = process.env.NEXT_PUBLIC_GA_ID;

export const gaPageview = (url: string) => {
window.gtag('config', GA_ID, { page_path: url });
if (GA_ID) window.gtag('config', GA_ID, { page_path: url });
};

export const gaEvent = ({
Expand All @@ -15,6 +15,8 @@ export const gaEvent = ({
label: string;
value?: number;
}): void => {
if (!GA_ID) return;

window.gtag('event', action, {
event_category: category,
event_label: label,
Expand Down

0 comments on commit ddf796f

Please sign in to comment.