Google Analytics/Google Tag Manager code is loaded too late - this is not recommended by Google

206 views
Skip to first unread message

Dragomir Denev

unread,
Jun 5, 2018, 2:43:07 AM6/5/18
to Cookie Control Support
As far as I understand, when accepting the Analytics cookie, Cookie Control executes the JS code that enables Google Analytics (Google Tag Manager in my case). This JS code is executed in the footer, after page is loaded.

Google recommends that GTM JS code is put as high as possible in the head of the page (https://www.lunametrics.com/blog/2016/11/22/google-tag-manager-snippet-placement/) and that it is not recommended to execute that code late (i.e. in footer) since we will lose some tracking opportunities (for example users that leave the page before it is fully loaded).

As I see it, this is a problem with all GDPR Cookie consent solutions that rely on JS to allow/disallow GA/GTM code and cookies.

What are your thoughts on this? Is there a way to work this around?

Best regards

Alex Asigno

unread,
Jun 5, 2018, 2:58:58 AM6/5/18
to Cookie Control Support
Keep GTM at top of code. Set a function to fire an event to the dataLayer on acceptance of the cookie consent. Then set the tags to fire off triggers in GTM using the custom events.

Alex Asigno

unread,
Jun 5, 2018, 3:00:53 AM6/5/18
to Cookie Control Support
E.g.
 
onAccept: function() {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'marketingConsentGiven'
});

Then in GTM create new trigger, custom event, then event name as marketingConsentGiven 

Dragomir Denev

unread,
Jun 5, 2018, 6:53:30 AM6/5/18
to Cookie Control Support
Thank you, Alex, for the answer!

So, what you are saying is that I should add the new trigger (based on the new event described above) to all my current UA tags in GTM, so that the tags are fired only if the consent event was fired, right?

But IMHO this still does not resolve the main problem - that the consent is given (and the event is fired) after the whole page has been loaded (because CookieControl.load() is called at the end of the page). In this case the UA tags will fail to track the visit if the user (who has previously already given his/her consent) leaves the page before it is loaded. In this case we have the situation where I am allowed to track the user but unable to do so. This will not be a problem if the GTM is loaded in the head (as Google recommends).

Any chance to extract the onAccept callback to be fired in the head and not in the CookieControl.load() at the bottom?

Dragomir Denev

unread,
Jun 5, 2018, 7:44:13 AM6/5/18
to Cookie Control Support
I should maybe add that I am using the WordPress plugin version.

Alex Asigno

unread,
Jun 5, 2018, 8:47:24 AM6/5/18
to Cookie Control Support
Sorry, my comment was around preventing you firing the entire GTM container on accept and to allow you to individually fire tags based off various consents.

The GDPR only requires explicit opt in consent for cookies which store private data. Implicit consent is fine for other cookies. As long as you have advertising features turned off in GA and use the anonymise IP function you can fire the cookie with out explicit opt in.

Check Simo Ahava blog for turning on off advertising functions in GA if you need them.

Reply all
Reply to author
Forward
0 new messages