Skip to content

Commit 16e5a82

Browse files
authored
Merge pull request #157 from os2loop/hotfix/add-cookieinformation
OS2WEB-189: Added cookieconsent functionality with cookieinformation.com
2 parents 8015a12 + ebabdb4 commit 16e5a82

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = ITK Cookie information
2+
description = Adds javascript for cookieinformation.com
3+
core = 7.x
4+
package = ITK Aarhus
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* Implements hook_init().
5+
*/
6+
function itk_cookieinformation_init() {
7+
drupal_add_js('https://policy.app.cookieinformation.com/uc.js', array(
8+
'type' => 'external',
9+
'group' => JS_LIBRARY - 1000,
10+
'every_page' => TRUE,
11+
'scope' => 'header',
12+
'weight' => -10000,
13+
));
14+
}
15+
16+
/**
17+
* Implements hook_preprocess_html_tag().
18+
*/
19+
function itk_cookieinformation_preprocess_html_tag(&$vars) {
20+
// Change html tags to apply cookie information.
21+
if ($vars['element']['#tag'] == 'script') {
22+
// Add attributes to cookieinformation tag.
23+
if (strpos($vars['element']['#attributes']['src'], 'policy.app.cookieinformation.com/uc.js')) {
24+
$vars['element']['#attributes']['id'] = 'CookieConsent';
25+
$vars['element']['#attributes']['data-culture'] = 'DA';
26+
}
27+
28+
// Add consent check to google analytics.
29+
if(strpos($vars['element']['#attributes']['src'], 'google_analytics/googleanalytics.js')) {
30+
$src = $vars['element']['#attributes']['src'];
31+
$vars['element']['#attributes']['data-consent-src'] = $src;
32+
$vars['element']['#attributes']['src'] = '';
33+
$vars['element']['#attributes']['data-category-consent'] = 'cookie_cat_marketing';
34+
}
35+
36+
// Add consent check to google analytics inline script.
37+
if(strpos($vars['element']['#value'], 'GoogleAnalyticsObject')) {
38+
$value = $vars['element']['#value'];
39+
$vars['element']['#value'] = "window.addEventListener('CookieInformationConsentGiven', function (event) {
40+
if (CookieInformation.getConsentGivenFor('cookie_cat_marketing')) {" . $value . "}
41+
}, false);";
42+
}
43+
}
44+
}

modules/itk_siteimprove/itk_siteimprove.module

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ function itk_siteimprove_form(array $form, array &$form_state) {
7171
'#required' => FALSE,
7272
'#default_value' => variable_get('itk_siteimprove_excludes', 'admin/*'),
7373
];
74+
$form['cookieinformation'] = [
75+
'#type' => 'details',
76+
'#title' => t('Cookieinformation.com'),
77+
'#group' => 'tabs',
78+
];
79+
$form['cookieinformation']['itk_use_cookieinformation'] = [
80+
'#type' => 'checkbox',
81+
'#title' => t('Check this if the site uses cookieinformation.com'),
82+
'#required' => FALSE,
83+
'#default_value' => variable_get('itk_use_cookieinformation', FALSE),
84+
];
7485

7586
return system_settings_form($form);
7687
}
@@ -82,6 +93,7 @@ function itk_siteimprove_form(array $form, array &$form_state) {
8293
*/
8394
function itk_siteimprove_preprocess_page(&$variables) {
8495
$key = variable_get('itk_siteimprove_key', '');
96+
$use_cookie_information = variable_get('itk_use_cookieinformation', '');
8597

8698
// Ignore if key is not set.
8799
if (!$key) {
@@ -108,6 +120,7 @@ function itk_siteimprove_preprocess_page(&$variables) {
108120
drupal_add_js(array(
109121
'itk_siteimprove' => array(
110122
'key' => $key,
123+
'useCookieinformation' => boolval($use_cookie_information)
111124
),
112125
), 'setting');
113126
}

modules/itk_siteimprove/js/itk-siteimprove.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,35 @@
66
(function ($, Drupal) {
77
'use strict';
88

9-
Drupal.behaviors.itkSiteimprove = {
9+
Drupal.behaviors.cookieMessageBehavior = {
1010
attach: function (context, settings) {
11-
var key = settings.itk_siteimprove.key;
12-
11+
let key = settings.itk_siteimprove.key;
12+
let useCookieInformation = settings.itk_siteimprove.useCookieinformation;
1313
if (key) {
14-
(function () {
15-
var sz = document.createElement('script');
16-
sz.type = 'text/javascript';
17-
sz.async = true;
18-
sz.src = '//ssl.siteimprove.com/js/siteanalyze_' + key + '.js';
19-
var s = document.getElementsByTagName('script')[0];
20-
s.parentNode.insertBefore(sz, s);
21-
})();
14+
if (useCookieInformation) {
15+
window.addEventListener('CookieInformationConsentGiven', function (event) {
16+
if (CookieInformation.getConsentGivenFor('cookie_cat_statistic')) {
17+
insertSiteImprove(key);
18+
}
19+
}, false);
20+
}
21+
else {
22+
insertSiteImprove(key);
23+
}
24+
2225
}
2326
}
2427
};
25-
})(jQuery, Drupal);
28+
})(jQuery, Drupal);
29+
30+
31+
function insertSiteImprove(key) {
32+
(function () {
33+
var sz = document.createElement('script');
34+
sz.type = 'text/javascript';
35+
sz.async = true;
36+
sz.src = '//ssl.siteimprove.com/js/siteanalyze_' + key + '.js';
37+
var s = document.getElementsByTagName('script')[0];
38+
s.parentNode.insertBefore(sz, s);
39+
})();
40+
}

0 commit comments

Comments
 (0)