Google introduced yet another tracking feature, called GA4, that provides more insights than what is currently available with their universal tracker. GA4 is not yet supported out of the box by Backlight 4, but using phplugins, it is easy to add to your own site.
In order that this code works, you need to have set Google Analytics Web Property ID in the Backlight admin settings.
Following are the few lines that need to be added to your phplugins.php file.
function head( ) {
echo " <!-- Global site tag (gtag.js) - Google Analytics -->
<script async src='https://www.googletagmanager.com/gtag/js?id=G-xxx'></script>
<script>
if (document.cookie.indexOf(gaDisableId + '=true') > -1) {
window['ga-disable-G-xxx'] = true;
}
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-xxx');
</script>
";
}
You only need to replace the string ‘G-xxx’ with your GA4 tracking code three times.
If you already use the head() function, then you only need to add the code between the two curly quotes (starting from echo until the closing “;). If the user elects to disable Google tracking using the existing Backlight popup, then GA4 tracking is disabled as well!
Happy tracking!
Did you like this post? Did you use the given code? Please consider supporting me by buying me a coffee!
Thanks!