Ezoic - Setup Google analytics tracking based on consent using Ezoic privacy

By enabling Privacy and Consent management in Ezoic you can gather consent from your users:

ezoic-privacy-in-menu

You enable consent management here:

ezoic-consent-management

When this is enabled your users will be presented with a consent dialog upon visiting your site:

ezoic-privacy-dialog-1

Usually you would simply continue with recommended cookies, however users are given the right to consent to how their data is used and give no consent:

ezoic-all-unchecked

I have previously used google analytics 4 using gtag on my site. I wanted to combine this with the consent from Ezoic. Ezoic has a simple javascript callback for when users give consent:

<script>
    function EzConsentCallback(consent) {
        // consent contains the necessary, preferences, statistics, and marketing properties with boolean values 
        if(consent.marketing) {
            do marketing stuff here - like insert ads. 
        }
    }
</script>

The consent object is much simpler than the UI and tells you whether consent for preferences, statistics and marketing were given:

{necessary: true, preferences: false, statistics: false, marketing: false}

Using gtag you can set the consent for google analytics, for example in the below we deny ad and analytics storage:

gtag('consent', 'default', {
	'ad_storage': 'denied',
	'analytics_storage': 'denied',
});

Combining the ezoic and gtag code, we can write something like the following:

var tagId = 'YOURTAGID';

function EzConsentCallback(consent) {
	var url = 'https://www.googletagmanager.com/gtag/js?id=' + tagId;
	var script = document.createElement('script');
	script.onload = function () {
		window.dataLayer = window.dataLayer || [];

		window.gtag = function () { dataLayer.push(arguments); };

		gtag('consent', 'default', {
			'ad_storage': (consent.marketing ? 'granted' : 'denied'),
			'analytics_storage': (consent.statistics ? 'granted' : 'denied'),
		});

		window.gtag('config', tagId);
		window.gtag('js', new Date());
	};
	script.src = url;
	document.head.appendChild(script);
}

This will set ad_storage and analytics_storage based on marketing and statistics from the Ezoic consent. When using the above it seems like no tracking request is sent if statistics is not enabled. The above will only load google analytics scripts once the consent is given. I am in no way an expert on consent, I have tied Ezoic consent and gtag consent together with best effort. If you know a better or more suitable way, please leave a comment down below.

Important NOTE: If no consent is given this is never triggered! Non-europeans are not presented with the dialog per default and therefore give no consent. This will be apparent by you suddenly only tracking Europeans in Google Analytics. I have found no easy way to distinguish between the two, so I have enabled the consent dialog for all users:

ezoic-show-consent-worldwide

Enabling this for all users will likely hurt your revenue. But then again, everyone gets to choose to consent to how their data is used.

That is it

I hope you found this helpful, it took me a couple of hours to figure this out and I hope you can do it faster with this help! As always please leave a comment down below!