Files
ltsecurityinc/app/code/Plumrocket/CookieConsent/view/frontend/templates/gtm.phtml
T

58 lines
2.2 KiB
PHTML

<?php
/**
* @package Plumrocket_CookieConsent
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
* @license https://plumrocket.com/license End-user License Agreement
*/
?>
<?php
/** @var \Plumrocket\CookieConsent\Block\Gtm $block */
/** @var \Plumrocket\Base\ViewModel\CspNonce $cspNonceViewModel */
$cspNonceViewModel = $block->getData('cspNonceViewModel');
?>
<!-- Google Tag Manager -->
<script type="text/javascript" <?= /* @noEscape */ $cspNonceViewModel->getNonce() ?>>
(function (config) {
function runGtm(config)
{
(function (w,d,s,l,i) {
w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'<?= $block->escapeJs($block->getGtmServerBaseUrl()) ?>/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer',config.containerId);
}
if (! config.consentMode) {
document.addEventListener('pr-cookie-consent-api:configured', () => {
prCookieConsentApi.isAllowedCategory(config.categoryKey) && runGtm(config);
});
return;
}
window.dataLayer = window.dataLayer || [];
function gtag() {dataLayer.push(arguments);}
gtag('consent', 'default', config.defaultConsentTypes);
runGtm(config);
document.addEventListener('pr-cookie-consent-api:configured', () => {
const updateGtmConsents = () => {
let updatedConsentTypes = { ...config.defaultConsentTypes };
for (const [key, categoryKey] of Object.entries(config.consentTypesCategories)) {
updatedConsentTypes[key] = prCookieConsentApi.isAllowedCategory(categoryKey) ? 'granted' : 'denied'
}
gtag('consent', 'update', updatedConsentTypes);
}
updateGtmConsents();
prCookieConsentApi.consent.addCallback(updateGtmConsents);
});
})(<?= /* @noEscape */ $block->getJsonConfig() ?>);
</script>
<!-- End Google Tag Manager -->