Skip to content

[astro-gtm] Add GDPR Consent Mode v2 with configurable default#1291

Merged
mhdcodes merged 3 commits intomainfrom
astro-gtm/add-consent-for-gpdr
Feb 14, 2026
Merged

[astro-gtm] Add GDPR Consent Mode v2 with configurable default#1291
mhdcodes merged 3 commits intomainfrom
astro-gtm/add-consent-for-gpdr

Conversation

@mhdcodes
Copy link
Copy Markdown
Member

@mhdcodes mhdcodes commented Feb 14, 2026

Summary by CodeRabbit

  • New Features

    • Added GDPR Consent Mode v2 support with configurable default consent states
    • Exported ConsentDefaults type to configure defaultConsent
    • Applies default consent to the tag manager data layer early and logs a warning for invalid GTM IDs
  • Documentation

    • Updated Astro requirement to 5.0+
    • Added guides for consent management and using environment variables for GTM IDs
  • Chores

    • Added MIT license and release changeset for astro-gtm

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 14, 2026

🦋 Changeset detected

Latest commit: 55a2d6d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
astro-gtm Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 14, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Adds GDPR Consent Mode v2 support to the astro-gtm package: new ConsentDefaults type and defaultConsent prop, GTM ID validation, injection of consent defaults into dataLayer before other data, README updates (Astro 5 requirement, env var guidance), MIT license, and a changeset for a minor release.

Changes

Cohort / File(s) Summary
License
packages/astro-gtm/LICENSE
Adds MIT license file for the astro-gtm package.
Documentation
packages/astro-gtm/README.md
Bumps required Astro to 5.0+, adds "Using Environment Variables" (PUBLIC_GTM_ID), documents defaultConsent, GDPR/Consent Mode v2 usage, examples for default and post-consent updates.
Core Implementation
packages/astro-gtm/src/GoogleTagManager.astro
Introduces exported ConsentDefaults type and defaultConsent?: ConsentDefaults prop; validates GTM ID format and logs warnings; injects defaultConsent into template variables and ensures a consent default is pushed into dataLayer as the first entry.
Public Types
packages/astro-gtm/src/index.ts
Re-exports ConsentDefaults alongside GTMProps, expanding the package's public type surface.
Release Metadata
.changeset/forty-buckets-act.md
Adds a changeset describing a minor release that introduces GDPR Consent Mode v2 with configurable defaults.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant Site as "Astro Component\n(GoogleTagManager.astro)"
participant Browser as "Browser\n(dataLayer)"
participant GTM as "GTM Script / Tag Manager"

Site->>Browser: Render inline script with template vars (GTM ID, defaultConsent)
Note right of Browser: Inline script executes on page load
Browser->>Browser: Ensure dataLayer array exists
Browser->>Browser: Push { 'default': { consent: defaultConsent } } as first entry
Browser->>GTM: GTM script loads and reads dataLayer (including consent defaults)
GTM->>GTM: Apply Consent Mode v2 using provided defaults

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 I nibble code beneath the moon,
I tuck consent where scripts attune.
DataLayer hums a careful tune,
GTM dances — soft and soon.
A rabbit hops: "Default set, hooray!"

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main
Title check ✅ Passed The title accurately describes the main change: adding GDPR Consent Mode v2 with configurable default consent settings to the astro-gtm package.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch astro-gtm/add-consent-for-gpdr

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.

@mhdcodes mhdcodes changed the title [astro-gtm] Add consent defaults GDPR compliance [astro-gtm] Add google consent mode v2 Feb 14, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Feb 14, 2026

Open in StackBlitz

npm i https://pkg.pr.new/codiume/orbit/astro-gtm@1291
npm i https://pkg.pr.new/codiume/orbit/astro-purgecss@1291
npm i https://pkg.pr.new/codiume/orbit/astro-seo-meta@1291
npm i https://pkg.pr.new/codiume/orbit/astro-seo-schema@1291

commit: 2c0722b

Copy link
Copy Markdown
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

🤖 Fix all issues with AI agents
In `@packages/astro-gtm/README.md`:
- Around line 154-169: The consent example uses window.dataLayer directly in the
allConsentGranted function, but the component supports a custom dataLayerName
prop; update the example and documentation to reference the configurable name
(e.g., use the value of dataLayerName or fallback to window.dataLayer) when
pushing consent updates, and show an explicit note or example demonstrating how
to call allConsentGranted with a custom dataLayerName (referencing the
allConsentGranted function and the dataLayerName prop/customDataLayer
identifier) so pushes go to the correct global array.
🧹 Nitpick comments (2)
packages/astro-gtm/src/GoogleTagManager.astro (2)

92-96: Consider guarding against an empty defaultConsent object.

If a consumer passes defaultConsent={{}} (empty object), it's truthy and will push an empty consent default to the dataLayer, which is a no-op but unnecessary. A stricter guard would also check that the object has at least one key.

Proposed fix
-    if (defaultConsent) {
+    if (defaultConsent && Object.keys(defaultConsent).length > 0) {
       w[dataLayerName].push(['consent', 'default', defaultConsent]);
     }

2-10: Add wait_for_update parameter to ConsentDefaults type for Google Consent Mode v2.

Google Consent Mode v2 supports the wait_for_update property (in milliseconds) to tell GTM how long to wait for a consent update before firing tags, commonly used with async consent management platforms (e.g., wait_for_update: 500). Add it to the type for completeness.

Proposed addition to ConsentDefaults
 export type ConsentDefaults = {
   ad_storage?: 'granted' | 'denied';
   analytics_storage?: 'granted' | 'denied';
   ad_user_data?: 'granted' | 'denied';
   ad_personalization?: 'granted' | 'denied';
   functionality_storage?: 'granted' | 'denied';
   personalization_storage?: 'granted' | 'denied';
   security_storage?: 'granted' | 'denied';
+  wait_for_update?: number;
 };

Comment thread packages/astro-gtm/README.md
@mhdcodes mhdcodes changed the title [astro-gtm] Add google consent mode v2 [astro-gtm] Added GDPR Consent Mode v2 support with configurable default Feb 14, 2026
@mhdcodes mhdcodes changed the title [astro-gtm] Added GDPR Consent Mode v2 support with configurable default [astro-gtm] Add GDPR Consent Mode v2 support with configurable default Feb 14, 2026
@mhdcodes mhdcodes changed the title [astro-gtm] Add GDPR Consent Mode v2 support with configurable default [astro-gtm] Add GDPR Consent Mode v2 with configurable default Feb 14, 2026
@mhdcodes mhdcodes merged commit 116bc8a into main Feb 14, 2026
5 of 6 checks passed
@mhdcodes mhdcodes deleted the astro-gtm/add-consent-for-gpdr branch February 14, 2026 17:23
@github-actions github-actions Bot mentioned this pull request Feb 14, 2026
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.

1 participant