• Resolved renevdpk

    (@renevdpk)


    I just don’t see what I’m doing wrong.

    I bought Code Snippets so I don’t have to use all kind of plugins anymore (and ofcourse no child theme). But I can’t get the Google Analytics code to work.

    I added a new php snippet with this code.

    add_action( 'wp_head', 'wpb_add_googleanalytics' );
    function wpb_add_googleanalytics() {
    ?>
      <!-- Global site tag (gtag.js) - Google Analytics -->
      <script async src="https://www.googletagmanager.com/gtag/js?id=G-Y2ZYXN458M"></script>
      <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'G-Y2ZYXN458M');
      </script>
    <?php
    }

    why isn’t it working and show up information in analytics.google.com

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Code looks fine after a quick glance, hmm.

    Can you try checking your PHP log for any errors, and share it with us by posting it to a site like PasteBin.

    Here’s a plugin to help view PHP errors: https://ww.wp.xz.cn/plugins/error-log-viewer/
    You can also Google “how to view WordPress PHP error log” for additional methods on how to view it.

    Note: I am seeing one line loading a GTM script on your site head here:
    <script src="https://www.googletagmanager.com/gtag/js?id=G-Y2ZYXN458M" type="text/javascript"></script>

    Plugin Author Shea Bunge

    (@bungeshea)

    It might be possible that you’re not seeing immediate changes do to site caching. I recommend double-checking that you have purged any cache on the server or plugin level after enabling this snippet.

    As a precautionary measure, I recommend using anonymous functions over named functions in snippets whenever possible:

    add_action( 'wp_head', function () {
    	?>
    	<!-- Global site tag (gtag.js) - Google Analytics -->
    	<script async src="https://www.googletagmanager.com/gtag/js?id=G-Y2ZYXN458M"></script>
    	<script>
    		window.dataLayer = window.dataLayer || [];
    		function gtag(){dataLayer.push(arguments);}
    		gtag('js', new Date());
    		gtag('config', 'G-Y2ZYXN458M');
    	</script>
    	<?php
    } );
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘GA php code not working’ is closed to new replies.