• Resolved gruff578

    (@gruff578)


    Hi,
    I am having problems with getting this gtag.js to work (I have removed my personal account ID numbers)
    <!– Global site tag (gtag.js) – Google Analytics –>
    <script async src=”https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X”></script&gt;
    <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag(‘js’, new Date());

    gtag(‘config’, ‘UA-UA-XXXXXXXX-X’);
    </script>

    When I remove all the script tags it’s not doing anything – doubtless my lack of knowledge. Could you just show me what it should be without the script tags. I don’t think I am understanding correctly.

    By the way the other updates were fantastic and it all works great.

    Thanks a lot for all the work.

    Gruff

    • This topic was modified 6 years, 5 months ago by gruff578.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,
    Indeed there is a problem with the “async src=’…’ ” part that can’t be loaded properly.
    In my theme functions.php I came up with this loader:

    add_action( 'wp_footer', 'my_footer_scripts' );
      function my_footer_scripts(){
      echo '<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-1"></script>';
    }

    Then, in your snippet, just add the :

      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-XXXXXXXX-1');

    Modify your track code as needed. If you want to load the java script in the header use wp_head instead of wp_footer.

    As a suggestion for the developer, in the Google Analytics Snippet, a field with the track code can be added, so one can put his track code and then the snippet generate all the stuff needed.

    • This reply was modified 6 years, 5 months ago by rivmanbx.
    • This reply was modified 6 years, 5 months ago by rivmanbx.
    Thread Starter gruff578

    (@gruff578)

    Thanks a lot,
    I took the top part and made a functional plugin with the top add_action, and then put the rest in the snippet window and all is working well.

    The tracking is on and off as per the settings.

    Thanks a lot for you help.
    Gruff

    • This reply was modified 6 years, 5 months ago by gruff578.

    @rivmanbx; Thanks for the temporary fix. I’ll make sure you’ll be able to add your <script> tags when necessary, for example for Google Analytics.

    Thread Starter gruff578

    (@gruff578)

    The nice thing with this GA script is that you can really easily anonymise the IP addresses as well, by adding one tiny bit in the very last line of the snippet, and I checked it with your plugin and it is all working.

    – so if people want to use the GA global gtag and anonymise the IP addresses they just have to do this script in the snippet window

    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag(‘js’, new Date());

    gtag(‘config’, ‘UA-XXXXXXXXX-X’, { ‘anonymize_ip’: true });

    and the bit you gave me for the functions.php –

    add_action( ‘wp_footer’, ‘my_footer_scripts’ );
    function my_footer_scripts(){
    echo ‘<script async src=”https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-1″></script>&#8217;;
    }

    For beginners it’s easier than learning general tag manager and anonymising from the filters.

    Maybe you could get people to send you the scripts they have that function well, and you could just have a list on your support page where people could come and find the scripts they need.

    Anyhow it is working really well, so well done with all the great work.
    Gruff

    • This reply was modified 6 years, 5 months ago by gruff578.
    • This reply was modified 6 years, 5 months ago by gruff578.
    • This reply was modified 6 years, 5 months ago by gruff578.

    • This reply was modified 6 years, 5 months ago by Roberto. Reason: type
    • This reply was modified 6 years, 5 months ago by Roberto.
    • This reply was modified 6 years, 5 months ago by Roberto. Reason: my code have error
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Global site tag (gtag.js) – Google Analytics’ is closed to new replies.