nishimura.clubnishimura.club

Next.jsにGoogle Analyticsを導入する

作成日
2022-11-27
更新日
2022-11-27
yarn add @types/gtag.js
touch lib/gtag.ts
echo "NEXT_PUBLIC_GA_TRACKING_ID=" >> .env echo "NEXT_PUBLIC_GA_TRACKING_ID=" >> .env.sample
gtag.ts
export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_TRACKING_ID; // https://developers.google.com/analytics/devguides/collection/gtagjs/pages export const pageview = (url: string): void => { if (!GA_TRACKING_ID) return; window.gtag('config', GA_TRACKING_ID, { page_path: url, }); };

Related

Tags

NextJS

Share