Title: GA php code not working
Last modified: September 9, 2022

---

# GA php code not working

 *  Resolved [renevdpk](https://wordpress.org/support/users/renevdpk/)
 * (@renevdpk)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/ga-php-code-not-working/)
 * 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](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fga-php-code-not-working%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Aakash](https://wordpress.org/support/users/aakash8/)
 * (@aakash8)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/ga-php-code-not-working/#post-15998105)
 * 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](https://pastebin.com/).
 * Here’s a plugin to help view PHP errors: [https://wordpress.org/plugins/error-log-viewer/](https://wordpress.org/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](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [3 years, 8 months ago](https://wordpress.org/support/topic/ga-php-code-not-working/#post-16036957)
 * 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.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [3 years, 8 months ago](https://wordpress.org/support/topic/ga-php-code-not-working/#post-16036957)
 * Status: resolved