1(function ($, Drupal, drupalSettings, once) { 2 3 "use strict"; 4 5 // Sep up the namespace as a function to store list of arguments in a queue. 6 Drupal.eu_cookie_compliance = Drupal.eu_cookie_compliance || function() { 7 (Drupal.eu_cookie_compliance.queue = Drupal.eu_cookie_compliance.queue || []).push(arguments) 8 }; 9 10 // Initialize the object with some data. 11 Drupal.eu_cookie_compliance.a = +new Date; 12 13 // A shorter name to use when accessing the namespace. 14 const self = Drupal.eu_cookie_compliance; 15 16 Drupal.behaviors.euCookieComplianceGTM = { 17 attach: function (context) { 18 19 const prettyPrint = function(e) { 20 let textarea = e.currentTarget; 21 let ugly = $(textarea).val(); 22 try { 23 let obj = JSON.parse(ugly); 24 let pretty = JSON.stringify(obj, undefined, 4); 25 $(textarea).val(pretty); 26 } catch (e) { 27 // Oh well, but whatever... 28 } 29 } 30 31 $(once('eu_cookie_compliance_gtm_pretty_json_processed', 'textarea.eu_cookie_compliance_gtm_pretty_json')).each(function () { 32 $(this).on('blur', prettyPrint); 33 }); 34 } 35 }; 36 37 /** 38 * Replaces tokens in the GTM values. 39 * @private 40 */ 41 const _replaceTokens = function(response, value, replacements) { 42 43 var details = drupalSettings.eu_cookie_compliance.cookie_categories_details; 44 replacements = replacements || {}; 45 46 // Build replacements for all categories' status. 47 for (let category in details) { 48 let key = '@' + category + '_status'; 49 replacements[key] = (response.currentCategories.indexOf(category) > -1) ? "1" : "0"; 50 } 51 52 // Process the replacements. 53 for (let key in replacements) { 54 value = value.replace(new RegExp(key, 'g'), replacements[key]); 55 } 56 57 // Return the result. 58 return value; 59 } 60 61 /** 62 * Push one event with status of all categories 63 * and any additional data configured in the backend for categories. 64 * @private 65 */ 66 const _pushAllCategoriesToDataLayer = function(response, event) { 67 let data = { 68 'event': event 69 } 70 71 let details = drupalSettings.eu_cookie_compliance.cookie_categories_details; 72 73 for (let category in details) { 74 if ('third_party_settings' in details[category]) { 75 for (let prop in details[category].third_party_settings.eu_cookie_compliance_gtm.gtm_data) { 76 let value = '' + details[category].third_party_settings.eu_cookie_compliance_gtm.gtm_data[prop]; 77 let status = response.currentCategories.includes(category) ? "granted" : "denied"; 78 data[prop] = _replaceTokens(response, value, {'@status': status}); 79 } 80 } 81 } 82 83 window.dataLayer = window.dataLayer || []; 84 85 function gtag() { window.dataLayer.push(arguments); } 86 87 if (event === "cookie_consent_post_pref_load") { 88 gtag('consent', 'default', data); 89 } else { 90 gtag('consent', 'update', data); 91 } 92 93 window.dataLayer.push(data); 94 } 95 96 /** 97 * Push individual events for each active category. 98 * @private 99 */ 100 const _pushIndividualCategoriesToDataLayer = function(response) { 101 window.dataLayer = window.dataLayer || []; 102 103 // Push generic update event. 104 window.dataLayer.push({ 105 'event': 'cookie_consent_update' 106 }); 107 108 // Push one event for each active category. 109 for (let index in response.currentCategories) { 110 window.dataLayer.push({ 111 'event': 'cookie_consent_' + response.currentCategories[index] 112 }); 113 } 114 } 115 116 const postPreferencesLoadHandler = function(response) { 117 // Push one event with status of all categories 118 // and any additional data configured in the backend for categories. 119 _pushAllCategoriesToDataLayer(response, 'cookie_consent_post_pref_load'); 120 121 // Push individual events for each active category. 122 _pushIndividualCategoriesToDataLayer(response); 123 }; 124 Drupal.eu_cookie_compliance('postPreferencesLoad', postPreferencesLoadHandler); 125 126 const postPreferencesSaveHandler = function(response) { 127 // Push one event with status of all categories 128 // and any additional data configured in the backend for categories. 129 _pushAllCategoriesToDataLayer(response, 'cookie_consent_post_pref_save'); 130 131 // Push individual events for each active category. 132 _pushIndividualCategoriesToDataLayer(response); 133 }; 134 Drupal.eu_cookie_compliance('postPreferencesSave', postPreferencesSaveHandler); 135 136})(jQuery, Drupal, drupalSettings, once);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.