Skip to content

Conversation

@zehjotkah
Copy link

@zehjotkah zehjotkah commented Oct 9, 2025

Summary

This PR adds a dependsOn feature to Klaro Cookie Consent Manager, allowing services to declare dependencies on other services. When a service is enabled, all its dependencies are automatically enabled. When a dependency is disabled, all dependent services are automatically disabled.

Changes

Core Implementation

File: src/consent-manager.js

  1. Forward Dependencies (Lines 134-144): When enabling a service, automatically enable all services it depends on
  2. Reverse Dependencies (Lines 145-154): When disabling a service, automatically disable all services that depend on it
  3. Accept All Support (Lines 112-125): Ensure dependencies are properly handled when accepting all services

Bug Fix

File: src/translations/hu.yml

  • Fixed YAML syntax error on line 54 that was preventing the build from completing

Usage Example

var klaroConfig = {
    services: [
        {
            name: 'googleTagManager',
            title: 'Google Tag Manager',
            purposes: ['functional'],
            required: false,
        },
        {
            name: 'googleAnalytics',
            title: 'Google Analytics',
            purposes: ['statistical'],
            dependsOn: ['googleTagManager'],  // Declares dependency
            callback: function(consent, serviceConfig) {
                if (consent) {
                    window.dataLayer.push({'event': 'loadgtm-analytics'});
                }
            },
        }
    ]
};

Behavior

Forward Dependencies

  • Enabling googleAnalytics automatically enables googleTagManager

Reverse Dependencies

  • Disabling googleTagManager automatically disables googleAnalytics and any other services that depend on it

Multiple Dependencies

  • The dependsOn property accepts either a string (single dependency) or an array of strings (multiple dependencies)

Testing

The feature has been tested with:

  • Individual service toggles
  • Accept all functionality
  • Decline all functionality
  • Multiple dependent services
  • Both forward and reverse dependency scenarios

Backward Compatibility

This feature is fully backward compatible. Services without the dependsOn property continue to work exactly as before.

Files Changed

  • src/consent-manager.js - Core implementation
  • src/translations/hu.yml - YAML syntax fix

- Implement forward dependencies: enabling a service auto-enables its dependencies
- Implement reverse dependencies: disabling a service auto-disables dependent services
- Fix Hungarian translation YAML syntax error
@zehjotkah zehjotkah changed the title Add dependsOn feature for service dependencies feat: dependsOn for service dependencies Oct 10, 2025
…/src comparison

- Changed script element comparison to use getAttribute() instead of DOM properties
- Fixes browser freeze caused by type normalization mismatches (e.g. application/javascript vs text/javascript)
- Fixes src comparison issues between resolved URLs and attribute values
- Prevents MutationObserver infinite loop when managing consent-controlled scripts
- Fully backward compatible with non-SPA websites
- Resolves React hydration errors when scripts are dynamically managed
- Prevent infinite loop: Added data-klaro-internal attribute to scripts created by Klaro and updated MutationObserver to ignore them.
- Fix contextual consent state: Added temporaryConsents tracking to properly handle acceptOnce (Ja) actions during re-evaluations.
- Fix element visibility: Corrected logic for restoring original-display when it was empty.
- Optimized Google Maps example: Removed debug logs and added custom styling.
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