-
Notifications
You must be signed in to change notification settings - Fork 0
/
liveramp_integration.module
119 lines (101 loc) · 2.66 KB
/
liveramp_integration.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* @file
* Module file.
*/
use Drupal\Core\Render\Markup;
/**
* Implements hook_page_attachments().
*/
function liveramp_integration_page_attachments(array &$page) {
$config = \Drupal::config('liveramp_integration.configuration');
$appId = $config->get('app_id');
$async = $config->get('async_mode');
$defer = $config->get('defer_mode');
$disableConsent = \Drupal::request()->query->get('disable_gdpr_consent');
$isDisabled = (1 === (int) $disableConsent) ? TRUE : FALSE;
$currentRoute = \Drupal::routeMatch()->getRouteName();
// Script must be also included on configuration form.
if (\Drupal::service('router.admin_context')
->isAdminRoute() && 'liveramp_integration.configuration_form' !== $currentRoute) {
return;
}
if (empty($appId) || $isDisabled) {
return;
}
// Attach liveramp script.
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'src' => 'https://gdpr-wrapper.privacymanager.io/gdpr/' . $appId . '/gdpr-liveramp.js',
'async' => ($async) ? TRUE : FALSE,
'defer' => ($defer) ? TRUE : FALSE,
],
'#weight' => -1,
],
'liveramp_integration-gdpr-liveramp',
];
$vendors = _liveramp_integration_create_json_list($config->get('vendor_ids'), 'vendorId');
$script =
<<< JS
(function () {
let handle = setInterval(function () {
if (undefined !== window.__tcfapi) {
checkConsent();
clearInterval(handle);
}
}, 50);
let killId = setTimeout(function () {
for (let i = killId; i > 0; i--) {
clearInterval(i);
}
}, 2000);
})();
function checkConsent() {
window.__tcfapi('checkConsent', null, function (data, success) {
const vendorsWithConsentHc = [];
if (data && data.length) {
data.forEach((item) => {
if (item.hasConsent && item.vendorName) {
vendorsWithConsentHc.push(item);
}
});
}
window.consentlayer = window.consentlayer || [];
window.consentlayer.push({
vendorsWithConsentHc
});
}, {
data: $vendors,
recheckConsentOnChange: true,
perVendor: true
});
}
JS;
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#value' => Markup::create($script),
'#weight' => -1,
],
'liveramp_integration-check-consent',
];
}
/**
* Create json list.
*
* @param array $values
* Value.
* @param string $key
* Key.
*
* @return false|string
*/
function _liveramp_integration_create_json_list(array $values, $key) {
$data = [];
foreach ($values as $value) {
$data[][$key] = (int) $value;
}
return json_encode($data);
}