|
<?php |
|
/** |
|
* BEGIN: Check if Cookies are accepted from the User (Compatible with Cookies Policy Notification Bar - Joomla! plugin by Web357.com) |
|
* GOOGLE CONSENT MODE |
|
* https://developers.google.com/tag-platform/devguides/consent |
|
*/ |
|
$cookies_accepted = 0; |
|
|
|
// get cookies from the browser |
|
$jcookies = JFactory::getApplication()->input->cookie->getArray(); // $_COOKIE |
|
|
|
// get plugin's parameters |
|
jimport('joomla.plugin.helper'); |
|
$cookiespolicynotificationbar = JPluginHelper::getPlugin('system', 'cookiespolicynotificationbar'); |
|
$cpnb_params = new JRegistry($cookiespolicynotificationbar->params); |
|
$cpnb_cookie_name = $cpnb_params->get('cookie_name', 'cookiesDirective'); |
|
|
|
// Block cookies from specific categories |
|
if (isset($jcookies['cpnb_cookiesSettings'])) |
|
{ |
|
$cpnb_specific_cookie_category_name_to_be_blocked = 'analytical-cookies'; // Note that you can replace the category ID (analytical-cookies) with another one. The category id should be same with that one you have created in the plugin settings. |
|
$cpnb_cookiesSettings = $jcookies['cpnb_cookiesSettings']; |
|
$cpnb_cookiesSettings_arr = json_decode($cpnb_cookiesSettings, true); |
|
$cpnb_specific_cookies_category_accepted = $cpnb_cookiesSettings_arr[$cpnb_specific_cookie_category_name_to_be_blocked]; |
|
|
|
if (isset($jcookies[$cpnb_cookie_name]) && $cpnb_specific_cookies_category_accepted === 1) |
|
{ |
|
$cookies_accepted = 1; // Granted |
|
} |
|
elseif ((isset($jcookies['cpnbCookiesDeclined']) || isset($jcookies[$cpnb_cookie_name])) && $cpnb_specific_cookies_category_accepted === 0) |
|
{ |
|
$cookies_accepted = -1; // Denied |
|
} |
|
} |
|
/** |
|
* END: Check if Cookies are accepted from the User (Compatible with Cookies Policy Notification Bar - Joomla! plugin by Web357.com) |
|
*/ |
|
?> |
|
|
|
<!-- DEFAULT GOOGLE CONSENT --> |
|
<!-- Global site tag (gtag.js) - Google Analytics --> |
|
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXX"></script> |
|
<script> |
|
window.dataLayer = window.dataLayer || []; |
|
function gtag(){dataLayer.push(arguments);} |
|
|
|
// Default to 'denied'. |
|
gtag('consent', 'default', { |
|
'ad_storage': 'denied', |
|
'analytics_storage': 'denied' |
|
}); |
|
|
|
gtag('js', new Date()); |
|
gtag('config', 'G-XXXXXX'); |
|
</script> |
|
|
|
<?php |
|
/** |
|
* Here you will place the gtag based on user's choice (Accept, Decline) |
|
*/ |
|
if ($cookies_accepted === 1): ?> |
|
<!-- GOOGLE CONSENT GRANTED --> |
|
<!-- When users specify consent or their consent status is known from a previous page load, update measurement behavior with the gtag('consent', 'update', ...) command. Call update as soon as possible on every page. After a user grants consent, persist their choice and call the update command accordingly on subsequent pages. |
|
For example, the following code grants consent when user agrees to allow advertising cookies: --> |
|
<script> |
|
gtag('consent', 'update', { |
|
'ad_storage': 'granted', |
|
'analytics_storage': 'granted' |
|
}); |
|
</script> |
|
<?php elseif ($cookies_accepted === -1): ?> |
|
<!-- GOOGLE CONSENT DENIED --> |
|
<script> |
|
gtag('consent', 'update', { |
|
'ad_storage': 'denied', |
|
'analytics_storage': 'denied' |
|
}); |
|
</script> |
|
<?php endif; ?> |
|
|
|
</body> |
|
</html> |
@Yiannistaos I have not had luck with this code and re-written this for Joomla3. I am in the process of doing this for Joomla4/5.
Have you relooked at this since to see if this is still actual.
BTW, I am not sure that you actually need the categories, as this can be built in the code to check for 1 or 0 in the json output.
Your thoughts ?
M