Skip to content

Comments

Refactor Google Tag Manager integration#418

Merged
stephenyeargin merged 1 commit intomainfrom
syeargin/refactor-gtm-integration
Feb 24, 2026
Merged

Refactor Google Tag Manager integration#418
stephenyeargin merged 1 commit intomainfrom
syeargin/refactor-gtm-integration

Conversation

@stephenyeargin
Copy link
Member

This pull request significantly refactors the analytics integration logic to improve privacy consent handling and script loading behavior. The main focus is to ensure that tracking scripts (HubSpot, Google Tag Manager, and Reo) are only loaded after the appropriate user consent is granted, and to make the consent logic more robust and maintainable.

Key improvements and changes:

Consent Management & Script Loading Logic:

  • Replaces the static HubSpot embed code with a dynamic loader that only injects the HubSpot script after verifying user consent, preventing tracking scripts from loading before consent is granted. [1] [2]
  • Adds helper functions (isLocalHost, isGrantedValue, firstDefined, etc.) to determine consent state from both the HubSpot consent listener and cookies, and to prevent script loading on localhost.
  • Introduces a centralized handleConsentState function that ensures tracking scripts are loaded only if consent is granted, and blocks tracking on local environments.

Google Tag Manager (GTM) Consent Enforcement:

  • Implements a default-deny consent policy for GTM by calling gtag('consent', 'default', ...) before any scripts load, and updates consent dynamically based on user actions. [1] [2]
  • Ensures GTM is initialized with proper consent events and does not load until consent is explicitly granted.

Robust Consent State Handling:

  • Adds logic to parse consent state from both the HubSpot listener and the __hs_cookie_cat_pref cookie, ensuring returning users’ preferences are respected. [1] [2]
  • Ensures all consent categories (analytics, ads, personalization) are handled and updated atomically to avoid inconsistent states.

These changes make the analytics integration more privacy-compliant, robust, and maintainable.

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Feb 19, 2026
@github-actions

This comment was marked as outdated.

@stephenyeargin stephenyeargin force-pushed the syeargin/refactor-gtm-integration branch from 28c8557 to ae7daf9 Compare February 19, 2026 03:53
@github-actions
Copy link

@stephenyeargin stephenyeargin merged commit c5691f5 into main Feb 24, 2026
3 checks passed
@stephenyeargin stephenyeargin deleted the syeargin/refactor-gtm-integration branch February 24, 2026 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants