= $block->escapeHtml(__('We use cookies to make your experience better.')) ?>
= $block->escapeHtml(__('To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies.')) ?>
<span><?= $block->escapeHtml(__('Google Analytics is active, if you want to desactivate it click')) ?></span>
<a href="<?= $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]) ?>?ga=1" title="<?= $block->escapeHtml(__('Google Analytics')) ?>">
<?= $block->escapeHtml(__('here')) ?>.
</a>
</p>
<div class="actions">
<button id="btn-cookie-allow" class="action allow primary">
<span><?= $block->escapeHtml(__('Allow Cookies')) ?></span>
</button>
</div>
</div>
</div>
<script type="text/x-magento-init">
{
"#notice-cookie-block": {
"cookieNotices": {
"cookieAllowButtonSelector": "#btn-cookie-allow",
"cookieName": "<?= /* @noEscape */ \Magento\Cookie\Helper\Cookie::IS_USER_ALLOWED_SAVE_COOKIE ?>",
"cookieValue": <?= /* @noEscape */ $this->helper(\Magento\Cookie\Helper\Cookie::class)->getAcceptedSaveCookiesWebsiteIds() ?>,
"cookieLifetime": <?= /* @noEscape */ $this->helper(\Magento\Cookie\Helper\Cookie::class)->getCookieRestrictionLifetime() ?>,
"noCookiesUrl": "<?= $block->escapeJs($block->escapeUrl($block->getUrl('cookie/index/noCookies'))) ?>"
}
}
}
</script>
`
and in ga.phtml
<?php /** @var $block \Magento\GoogleAnalytics\Block\Ga */ ?>
<?php $accountId = $block->getConfig(\Magento\GoogleAnalytics\Helper\Data::XML_PATH_ACCOUNT) ?>
<!-- BEGIN GOOGLE ANALYTICS CODE -->
<script type="text/x-magento-init">
{
"*": {
"Magento_GoogleAnalytics/js/google-analytics": {
"isCookieRestrictionModeEnabled": <?= (int)$block->isCookieRestrictionModeEnabled() ?>,
"currentWebsite": <?= (int)$block->getCurrentWebsiteId() ?>,
"cookieName": "<?= /* @escapeNotVerified */ \Magento\Cookie\Helper\Cookie::IS_USER_ALLOWED_SAVE_COOKIE ?>",
"ordersTrackingData": <?= /* @escapeNotVerified */ json_encode($block->getOrdersTrackingData()) ?>,
"pageTrackingData": <?= /* @escapeNotVerified */ json_encode($block->getPageTrackingData($accountId)) ?>
}
}
}
</script>
<!-- END GOOGLE ANALYTICS CODE -->
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cookieManager = $objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface');
if($block->isCookieRestrictionModeEnabled() == 1 && $cookieManager->getCookie('user_allowed_save_cookie') === NULL): ?>
<script type="text/javascript">
require([
'jquery',
'jquery/jquery.cookie'
], function ($) {
$(document).ready(function(){
var check_cookie = $.cookie('ga_default'); // Get Cookie Value
if(!check_cookie || check_cookie != '2'){
var date = new Date();
var minutes = 60;
date.setTime(date.getTime() + (minutes * 60 * 1000));
$.cookie('ga_default', '', {path: '/', expires: -1}); // Expire Cookie
$.cookie('ga_default', 'bar', {expires: date}); // Set Cookie Expiry Time
$.cookie('ga_default', '1'); // Set Cookie Value
}
if(check_cookie == '1'){
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-44874885-2');
}
});
});
</script>
<?php if( $cookieManager->getCookie('ga_default') == 1): ?>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-44874885-2"></script>
<?php endif; ?>
<?php if($this->getRequest()->getParam('ga') == "1"): ?>
<script type="text/javascript">
require([
'jquery',
'jquery/jquery.cookie'
], function ($) {
$(document).ready(function(){
$.cookie('ga_default', '2');
});
});
</script>
<?php endif; ?>
<?php endif; ?>


Most helpful comment
Confirmed here. We need the cookie allow feature thats why using true doesn't work for us. The root cause for this problem lies in
module-cookie/view/frontend/web/js/notices.js, where the cookieValue is not saved as a JSON string. After confirming cookies the value just gets saved as "[object Object]", which cannot be interpreted by_getAcceptedSaveCookiesWebsitesinmodule-cookie/Helper/Cookie.php