Skip to content

Commit

Permalink
Create analytics_google4.html5
Browse files Browse the repository at this point in the history
  • Loading branch information
mstefan44 committed May 26, 2023
1 parent 6ea482a commit e5d6853
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Resources/contao/templates/analytics_google4.html5
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* To use this script, please fill in your Google Analytics ID below
*/
$GoogleAnalyticsId = 'UA-XXXXX-X';

/**
* DO NOT EDIT ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!
* if ($GoogleAnalyticsId != 'UA-XXXXX-X' && !BE_USER_LOGGED_IN && sha1(session_id() . (!Config::get('disableIpCheck') ? Environment::get('ip') : '') . 'BE_USER_AUTH') != Input::cookie('BE_USER_AUTH')):
*/
if ('XXXXX' != $GoogleAnalyticsId && !$this->hasAuthenticatedBackendUser()): ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
if (localStorage.getItem('contaoPrivacy.enabledAnalytics')) {
// Create the GA4 script element
var ga4Script = document.createElement('script');
ga4Script.async = true;
ga4Script.src = 'https://www.googletagmanager.com/gtag/js?id=<?= $GoogleAnalyticsId ?>';
// Append the GA4 script to the placeholder element
document.body.appendChild(ga4Script);

// Insert the GA4 initialization code
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', '<?= $GoogleAnalyticsId ?>', {'anonymize_ip': true});
}
});
</script>
<?php endif; ?>

0 comments on commit e5d6853

Please sign in to comment.