Commit ec23ca18 authored by Kalle Kipinä's avatar Kalle Kipinä Committed by Heikki Ylipaavalniemi
Browse files

Issue #3194493 by kekkis, HeikkiY, ilari242: Add support for Google consent mode

parent 499c5d60
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
consent_mode:
  js:
    js/consent_mode.js: {}
+22 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ function cookieinformation_page_attachments_alter(array &$page) {
    return;
  }

  // Create default tag for Cookie information.
  $cookie_popup = [
    '#type' => 'html_tag',
    '#tag' => 'script',
@@ -52,4 +53,25 @@ function cookieinformation_page_attachments_alter(array &$page) {
    ],
  ];
  $page['#attached']['html_head'][] = [$cookie_popup, 'cookieinformation.ucjs'];

  // Embed Google Consent Mode tag as early as possible in the page load.
  $module_path = drupal_get_path('module', 'cookieinformation');
  $js_path = $module_path . '/js/consent_mode.init.js';
  $js_url = file_url_transform_relative(file_create_url($js_path));

  // Create inline <script> from Google Consent Mode tag.
  $consent_mode_init_script = [
    '#type' => 'html_tag',
    '#tag' => 'script',
    '#attributes' => [
      'type' => 'text/javascript',
      'id' => 'ConsentMode',
      'src' => $js_url,
    ],
    '#weight' => -200,
  ];
  if (!empty($config->get('enable_google_consent_mode'))) {
    $page['#attached']['html_head'][] = [$consent_mode_init_script, 'consent_mode_init_script_key'];
    $page['#attached']['library']['cookieinformation/sdk'] = 'cookieinformation/consent_mode';
  }
}
+17 −0
Original line number Diff line number Diff line
/**
 * @file
 * Creates Google Consent mode default tag.
 *
 * @see https://support.cookieinformation.com/en/articles/5411279-google-consent-mode-implementation
 */

window.dataLayer = window.dataLayer || [];
function gtag() {
  dataLayer.push(arguments);
}
gtag('consent', 'default', {
  'ad_storage': 'denied',
  'analytics_storage': 'denied',
  'wait_for_update': 500,
});
gtag('set', 'ads_data_redaction', true);

js/consent_mode.js

0 → 100644
+16 −0
Original line number Diff line number Diff line
/**
 * @file
 * Adds event listener to follow changes in Google Consent mode.
 *
 * @see https://support.cookieinformation.com/en/articles/5411279-google-consent-mode-implementation
 *
 */

window.addEventListener('CookieInformationConsentGiven', function (event) {
  if (CookieInformation.getConsentGivenFor('cookie_cat_statistic')) {
    gtag('consent', 'update', {'analytics_storage': 'granted'});
  }
  if (CookieInformation.getConsentGivenFor('cookie_cat_marketing')){
    gtag('consent', 'update', {'ad_storage': 'granted'});
  }
}, false);
 No newline at end of file
+7 −0
Original line number Diff line number Diff line
@@ -76,6 +76,12 @@ public function buildForm(array $form, FormStateInterface $form_state) {
        ],
      ],
    ];
    $form['enable_google_consent_mode'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable google consent mode'),
      '#description' => $this->t('Check to enable Google consent mode.'),
      '#default_value' => $this->config->get('enable_google_consent_mode'),
    ];
    $form['visibility'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Cookie Information consent popup visibility'),
@@ -124,6 +130,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $this->config->set("enable_popup", $form_state->getValue('enable_popup'));
    $this->config->set("enable_iab", $form_state->getValue('enable_iab'));
    $this->config->set("enable_google_consent_mode", $form_state->getValue('enable_google_consent_mode'));
    $this->config->set('exclude_paths', $form_state->getValue('exclude_paths'));
    $this->config->set('exclude_admin', $form_state->getValue('exclude_admin'));
    $this->config->set('exclude_uid_1', $form_state->getValue('exclude_uid_1'));