Skip to main content

GTM dataLayer

The script pushes these events to window.dataLayer automatically:

EventWhen
OptSensLoadedThe script finished loading
OptSensConsentReadyConsent state resolved on page load
OptSensConsentUpdateConsent given or changed
OptSensBannerShownBanner displayed
OptSensBannerHiddenBanner closed
OptSensPreferencesShownPreference center opened
OptSensPageViewSPA route change (when using onRouteChange())

GTM trigger example

Create a Custom Event trigger in Google Tag Manager:

  • Trigger type: Custom Event
  • Event name: OptSensConsentUpdate

Use it to fire tags only after consent.

Consent Mode is integrated automatically. No dataLayer work is needed for it. The script fires the default state before any Google tag runs:

gtag('consent', 'default', {
ad_storage: 'denied',
analytics_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
functionality_storage: 'denied',
personalization_storage: 'denied',
security_storage: 'granted',
});

After consent:

gtag('consent', 'update', {
ad_storage: 'granted', // advertising category
analytics_storage: 'granted', // analytics category
ad_user_data: 'granted', // advertising category
ad_personalization: 'granted', // advertising category
functionality_storage: 'granted', // functional category
personalization_storage: 'granted', // performance category
});

Works with Google Analytics 4, Google Ads and Google Tag Manager. See Google Consent Mode v2 for the full framework page.