[Resolved] How to put cookie banner script at the top of a

Home Forums Support How to put cookie banner script at the top of a

  • This topic has 5 replies, 2 voices, and was last updated 12 months ago by Tom.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1136343
    Artem

    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.

    #1136488
    Tom
    Lead Developer
    Lead Developer

    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 πŸ™‚

    #1136735
    Artem

    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.

    #1137312
    Tom
    Lead Developer
    Lead Developer

    I’m not seeing that code in the source of your website.

    Can you try clearing your browser cache?

    #1137527
    Artem

    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.

    #1138235
    Tom
    Lead Developer
    Lead Developer

    You’re very welcome πŸ™‚

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.