Archived topic
How to put cookie banner script at the top of a
5 replies · Started by Artem on January 18, 2020
Hi,
I need to put the script of my cookie banner at the top of the <head> section of my site. So it should be the very first.
I do it via Hook wp-head.
The problem is that the google analytics script is always on top.
<script type="text/javascript" async="" src="https://www.google-analytics.com/analytics.js"></script>
For analytics, I use Global Site Tag (gtag.js) tracking code. I put it in the Hook wp-head with a priority 15.
My cookie banner script I put in the Hook wp-head with a priority 1.
When I see html - Global Site Tag (gtag.js) tracking code
<script src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1" type="text/javascript"></script>
is below my cookie banner script. So, it's OK. But I don't know how to move google analytics script.
So, how can I put my script at the top?
Thank you.
Hi there,
Have you tried putting the analytics and gtag scripts in the same hook element? That way they should both appear above the cookie script.
Let me know :)
The thing is that I don't have any other analytics script. Just this:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX', { 'anonymize_ip': true });
</script>
When I put this in the Hook wp-head I automaticaly get <script type="text/javascript" async="" src="https://www.google-analytics.com/analytics.js"></script> as a first script in the head.
You can see it on my site.
Thank you.
I'm not seeing that code in the source of your website.
Can you try clearing your browser cache?
Tom,
Thank you very much!
I was struggling with this stuff for several days. And the solution was so simple.
The issue resolved by clearing browser cache.
Thanks again.
You're very welcome :)