Skip to content

fix: replace hardcoded GA tracking ID with environment variable#313

Open
mohitjeswani01 wants to merge 1 commit into
kmesh-net:mainfrom
mohitjeswani01:fix/ga-tracking-id-env-variable
Open

fix: replace hardcoded GA tracking ID with environment variable#313
mohitjeswani01 wants to merge 1 commit into
kmesh-net:mainfrom
mohitjeswani01:fix/ga-tracking-id-env-variable

Conversation

@mohitjeswani01

Copy link
Copy Markdown

Summary

Fixes #252

Replaces the hardcoded Google Analytics tracking ID in docusaurus.config.js with an environment variable (GA_TRACKING_ID), following maintainer guidance from @hzxuzhonghu to use vars.GA_TRACKING_ID as a repository configuration variable.

Why not secrets?

The maintainer explicitly created a repository variable (not secret) named GA_TRACKING_ID. This PR uses ${{ vars.GA_TRACKING_ID }} accordingly — GA tracking IDs are not sensitive and don't need to be encrypted.

What went wrong with PR #283

PR #283 attempted to fix this issue but fails the Netlify build CI check because it uses:

trackingID: process.env.GA_TRACKING_ID || "",

Docusaurus's @docusaurus/plugin-google-gtag validates that trackingID must be a non-empty string matching the GA format. An empty string "" fails Joi schema validation with:

"trackingID" does not match any of the allowed types

This PR's approach — conditional spread

Instead of providing a fallback empty string, this PR conditionally includes the entire gtag block only when the environment variable is set:

...(process.env.GA_TRACKING_ID && {
  gtag: {
    trackingID: process.env.GA_TRACKING_ID,
    anonymizeIP: true,
  },
}),

GA_TRACKING_ID is set → gtag config is included → analytics works
GA_TRACKING_ID is NOT set → gtag config is omitted entirely → build succeeds with no analytics (Netlify, local dev, forks)

Changes

File Change
docusaurus.config.js Replace hardcoded tracking ID with conditional process.env.GA_TRACKING_ID
.github/workflows/build.yml New — CI build workflow passing vars.GA_TRACKING_ID
.env.example New — Template for local development
.gitignore Add .env entry

Build verification

Tested locally with npm run build — both en and zh locales build successfully without GA_TRACKING_ID set.
image

Checklist

  • Hardcoded tracking ID removed from source code
  • Uses vars.GA_TRACKING_ID (not secrets) per maintainer guidance
  • Build passes without GA_TRACKING_ID set (graceful fallback)
  • Build passes with GA_TRACKING_ID set (analytics works)
  • .env added to .gitignore

Copilot AI review requested due to automatic review settings May 20, 2026 20:50
@netlify

netlify Bot commented May 20, 2026

Copy link
Copy Markdown

Deploy Preview for kmesh-net ready!

Name Link
🔨 Latest commit c55e1cc
🔍 Latest deploy log https://app.netlify.com/projects/kmesh-net/deploys/6a0e202752d20500071164f1
😎 Deploy Preview https://deploy-preview-313--kmesh-net.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@kmesh-bot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign yaozengzeng for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kmesh-bot

Copy link
Copy Markdown
Collaborator

Welcome @mohitjeswani01! It looks like this is your first PR to kmesh-net/website 🎉

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes the hardcoded Google Analytics tracking ID from the Docusaurus configuration and switches to using GA_TRACKING_ID from the build environment, with a guard that omits the gtag config entirely when the variable is unset.

Changes:

  • Replace hardcoded GA tracking ID with a conditional process.env.GA_TRACKING_ID config block in docusaurus.config.js.
  • Add a new GitHub Actions workflow to build the site and pass GA_TRACKING_ID from repository variables.
  • Add .env.example and ignore .env in git to support local configuration without committing env files.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
docusaurus.config.js Conditionally includes gtag config only when GA_TRACKING_ID is set.
.gitignore Ignores .env to avoid committing local environment files.
.github/workflows/build.yml Adds CI build job and injects GA_TRACKING_ID from ${{ vars.GA_TRACKING_ID }}.
.env.example Provides a template for setting GA_TRACKING_ID.
Comments suppressed due to low confidence (1)

.github/workflows/build.yml:26

  • The workflow runs npm ci, but this repository does not include a package-lock.json (only yarn.lock). npm ci will fail without a lockfile. Switch to npm install (as done in .github/workflows/versioning.yml) or adopt Yarn and install with a frozen lockfile.
      - name: Install dependencies
        run: npm ci


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread .github/workflows/build.yml Outdated
Comment thread docusaurus.config.js
Comment thread .env.example Outdated
@mohitjeswani01

Copy link
Copy Markdown
Author

@yashisrani @Jayesh0167 @LiZhenCheng9527 interested and looking forward for your review and ready to align as you guide !

Signed-off-by: Mohit Jeswani <2022.mohit.jeswani@ves.ac.in>
@mohitjeswani01 mohitjeswani01 force-pushed the fix/ga-tracking-id-env-variable branch from 092e6df to c55e1cc Compare May 20, 2026 20:57

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces environment variable support for Google Analytics tracking by adding an .env.example file, updating .gitignore, and modifying docusaurus.config.js to use process.env.GA_TRACKING_ID. Feedback indicates that the CI workflow file required to pass this variable is missing from the PR, which will cause tracking to be disabled in production. Furthermore, it was noted that Docusaurus does not automatically load .env files into the configuration, so the use of dotenv or manual shell configuration is needed for local development.

Comment thread docusaurus.config.js
Comment thread docusaurus.config.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hardcoded Google Analytics Tracking ID

3 participants