Skip to content

fix: make NEXT_PUBLIC_GTM_ID optional in client environment schema#172

Merged
MantisClone merged 2 commits intoRequestNetwork:mainfrom
mayur1377:env-fix
Oct 31, 2025
Merged

fix: make NEXT_PUBLIC_GTM_ID optional in client environment schema#172
MantisClone merged 2 commits intoRequestNetwork:mainfrom
mayur1377:env-fix

Conversation

@mayur1377
Copy link
Contributor

@mayur1377 mayur1377 commented Oct 29, 2025

Fixes #159

Summary by CodeRabbit

  • Chores

    • Google Tag Manager ID is now optional, allowing deployments without analytics configured.
  • Bug Fixes

    • Tag manager is only initialized when an ID is provided, preventing unnecessary tracker loading when unset.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 29, 2025

Walkthrough

Made the client env var NEXT_PUBLIC_GTM_ID optional in the schema and updated the app layout to conditionally mount Google Tag Manager only when that env var is defined.

Changes

Cohort / File(s) Summary
Environment schema validation
src/lib/env/client.mjs
Changed NEXT_PUBLIC_GTM_ID validation from z.string().min(1, "NEXT_PUBLIC_GTM_ID is required") to z.string().optional(), allowing the value to be undefined.
App layout — GTM conditional mount
src/app/layout.tsx
Replaced unconditional rendering of GoogleTagManager with conditional rendering that only mounts when NEXT_PUBLIC_GTM_ID is defined; removed the type assertion cast and pass the raw string when present.

Sequence Diagram(s)

sequenceDiagram
    participant Browser
    participant Layout
    participant GTM as GoogleTagManager

    Browser->>Layout: render app layout
    alt NEXT_PUBLIC_GTM_ID defined
        Layout->>GTM: mount(gtmId)
        GTM-->>Layout: initialized
    else NEXT_PUBLIC_GTM_ID undefined
        Layout-->>Browser: skip mounting GTM
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Two related but small edits (schema + conditional rendering).
  • Pay attention to:
    • Any other places that assume NEXT_PUBLIC_GTM_ID is always present.
    • Type expectations where the env value is consumed (props, components).

Possibly related PRs

Suggested reviewers

  • sstefdev

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The PR title "fix: make NEXT_PUBLIC_GTM_ID optional in client environment schema" clearly and specifically describes the main change in the pull request. The title accurately reflects the schema modification in src/lib/env/client.mjs where NEXT_PUBLIC_GTM_ID is changed from required to optional. The title is concise, uses appropriate terminology, and is specific enough that a teammate scanning history would immediately understand the primary change.
Linked Issues Check ✅ Passed The code changes directly address the requirement from issue #159. The schema modification in src/lib/env/client.mjs makes NEXT_PUBLIC_GTM_ID optional (changing from required with validation to z.string().optional()), which aligns with the objective to allow the environment variable to be absent instead of requiring a placeholder like "GTM-INVALID". The corresponding change in src/app/layout.tsx adds conditional rendering of GoogleTagManager only when NEXT_PUBLIC_GTM_ID is defined, which is a necessary implementation detail to properly support the optional environment variable.
Out of Scope Changes Check ✅ Passed All changes in the pull request are directly related to the linked issue #159 objective. The modification to src/lib/env/client.mjs makes the environment variable optional as required, and the change to src/app/layout.tsx implements the necessary logic to handle the optional variable by conditionally rendering the GoogleTagManager component only when the variable is defined. Both changes form a cohesive solution to the stated requirement with no unrelated or extraneous modifications.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ee985e1 and 6e78fb5.

📒 Files selected for processing (1)
  • src/app/layout.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: bassgeta
PR: RequestNetwork/easy-invoice#149
File: src/lib/env/server.js:29-43
Timestamp: 2025-09-24T12:28:41.783Z
Learning: In the RequestNetwork/easy-invoice project, VERCEL_URL is a code remnant that is not actually used, so it should be removed from environment schemas rather than validated.
Learnt from: bassgeta
PR: RequestNetwork/easy-invoice#149
File: src/lib/env/server.js:28-42
Timestamp: 2025-09-25T09:39:21.320Z
Learning: In the RequestNetwork/easy-invoice project, environment variable validation should only cover user-defined variables, not system/runtime injected variables like NODE_ENV and PORT which are always provided by the runtime environment.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Build
🔇 Additional comments (1)
src/app/layout.tsx (1)

44-46: LGTM! Conditional rendering correctly implements optional GTM.

The conditional rendering pattern correctly prevents GoogleTagManager from mounting when NEXT_PUBLIC_GTM_ID is absent or empty. The schema in src/lib/env/client.mjs properly defines NEXT_PUBLIC_GTM_ID: z.string().optional(), confirming the environment variable is now optional as intended by the PR.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6c9b2b3 and ee985e1.

📒 Files selected for processing (1)
  • src/lib/env/client.mjs (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: aimensahnoun
PR: RequestNetwork/easy-invoice#20
File: src/app/layout.tsx:34-34
Timestamp: 2025-02-19T14:23:43.558Z
Learning: In the Easy Invoice project, type assertion for the NEXT_PUBLIC_GTM_ID environment variable in Google Tag Manager integration is acceptable without additional runtime validation.
📚 Learning: 2025-02-19T14:23:43.558Z
Learnt from: aimensahnoun
PR: RequestNetwork/easy-invoice#20
File: src/app/layout.tsx:34-34
Timestamp: 2025-02-19T14:23:43.558Z
Learning: In the Easy Invoice project, type assertion for the NEXT_PUBLIC_GTM_ID environment variable in Google Tag Manager integration is acceptable without additional runtime validation.

Applied to files:

  • src/lib/env/client.mjs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Build
  • GitHub Check: Lint

@MantisClone MantisClone linked an issue Oct 29, 2025 that may be closed by this pull request
Copy link
Member

@MantisClone MantisClone left a comment

Choose a reason for hiding this comment

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

@mayur1377 Please resolve the CodeRabbit comment. 🙏

@MantisClone MantisClone merged commit a381b2b into RequestNetwork:main Oct 31, 2025
5 checks passed
@MantisClone
Copy link
Member

Congratulations, your pull request has been merged! Thank you for your valuable contribution to Request Network. As a reminder, every merged PR is automatically entered into our Best PR Initiative, offering a quarterly prize of $500. Your work significantly supports our project's growth, and we encourage you to continue engaging with our community. Additionally, if you want to build or add crypto payments and invoicing features, explore how our API can reduce deployment time from months to hours while offering advanced features. Book a call with our expert to learn more and fast-track your development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EasyInvoice - The NEXT_PUBLIC_GTM_ID env var should be optional

2 participants