@kreonovo/basic-gtag-consent-functions
v2.2.2
Published
Basic functions to manage gtag datalayer
Downloads
2
Readme
Basic ConsentManager Class
The ConsentManager
class is designed to manage user consent for various tracking functionalities commonly used in web analytics and advertising. This revised version includes enhancements for managing consent settings based on user-defined groups.
Installation
No installation is required for this class as it is designed to be used within a JavaScript environment.
Usage
To use the ConsentManager
class, simply instantiate an object of the class, optionally passing in groups of consent settings:
// Example with predefined consent groups
const consentGroups = {
analytics: ["analytics_storage"],
targeting: ["ad_storage", "ad_user_data", "ad_personalization"],
};
const consentManager = new ConsentManager(consentGroups);
This will create a new instance of the ConsentManager
class with the specified consent groups and default consent settings applied.
Methods
consentDefault()
- Sets default consent preferences for each group.
consentDenyAll()
- Updates consent preferences to deny all tracking functionalities for each group.
consentGrantAll()
- Updates consent preferences to grant all tracking functionalities for each group.
consentDenyGroup(groupName)
- Updates consent preferences to deny all tracking functionalities for the specified group.
consentGrantGroup(groupName)
- Updates consent preferences to grant all tracking functionalities for the specified group.
consentGrant(key)
- Grants consent for a specific tracking functionality identified by the provided key.
consentDeny(key)
- Denies consent for a specific tracking functionality identified by the provided key.
Example
// Instantiate ConsentManager with predefined consent groups
const consentGroups = {
analytics: ["analytics_storage"],
targeting: ["ad_storage", "ad_user_data", "ad_personalization"],
};
const consentManager = new ConsentManager(consentGroups);
// Example: Grant consent for all tracking functionalities in the "targeting" group
consentManager.consentGrantGroup("targeting");
// Example: Deny consent for analytics tracking
consentManager.consentDeny("analytics_storage");
Note
- This class assumes the presence of the
window.dataLayer
object for tracking purposes. - Ensure that appropriate tracking scripts such as Google Analytics are integrated with your application to utilize the consent management functionality effectively.
License
This class is provided under the MIT License.