feat(frontend): add Google Analytics tracking#263
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Summary of ChangesHello @ComBba, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request integrates Google Analytics into the frontend of the application. By adding the gtag.js script and its configuration to the root layout, the application will now track user behavior, providing valuable insights into user interactions and overall site performance, all while ensuring optimal loading performance through Next.js's script optimization features. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds Google Analytics tracking to the application using next/script. The implementation is a good start, but there are a couple of areas for improvement. Firstly, the Google Analytics ID is hardcoded, which is not ideal for managing configurations across different environments. It should be moved to an environment variable. Secondly, the tracking scripts are loaded in all environments, including development, which can lead to skewed analytics data. These scripts should only be loaded in production. I've provided specific suggestions to address these points.
| import { AuthWrapper } from "@/components/AuthWrapper"; | ||
| import { SiteHeader } from "@/components/SiteHeader"; | ||
|
|
||
| const GA_ID = "G-65HJEVF1M4"; |
There was a problem hiding this comment.
The Google Analytics ID is hardcoded. It's a best practice to store configuration values like this in environment variables. This allows for different configurations across environments (development, staging, production) without code changes. For Next.js, you should use a public environment variable by prefixing it with NEXT_PUBLIC_ and adding it to a .env.local file.
| const GA_ID = "G-65HJEVF1M4"; | |
| const GA_ID = process.env.NEXT_PUBLIC_GA_ID; |
| <Script | ||
| src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`} | ||
| strategy="afterInteractive" | ||
| /> | ||
| <Script id="google-analytics" strategy="afterInteractive"> | ||
| {` | ||
| window.dataLayer = window.dataLayer || []; | ||
| function gtag(){dataLayer.push(arguments);} | ||
| gtag('js', new Date()); | ||
| gtag('config', '${GA_ID}'); | ||
| `} | ||
| </Script> |
There was a problem hiding this comment.
These analytics scripts will run in all environments, including local development. This can pollute your analytics data with development activity. It's recommended to only enable analytics in the production environment and to ensure the tracking ID is configured before attempting to load the scripts.
{process.env.NODE_ENV === "production" && GA_ID && (
<>
<Script
src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`}
strategy="afterInteractive"
/>
<Script id="google-analytics" strategy="afterInteractive">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_ID}');
`}
</Script>
</>
)}
feat(frontend): add Google Analytics tracking
Summary
Changes
next/scriptwithafterInteractivestrategy for optimal performanceG-65HJEVF1M4