Title: Code Snippet for Google Analytics
Last modified: December 19, 2017

---

# Code Snippet for Google Analytics

 *  Resolved [momadvisorblog](https://wordpress.org/support/users/momadvisorblog/)
 * (@momadvisorblog)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/code-snippet-for-google-analytics/)
 * Hi!
 * I´m trying to add Google Analytics to the header of my site using Code Snippets,
   but can´t seem to get it right.
 * It´s just not counting all the visits. Can you help?
 * I´m using the example you gave to someone earlier.
 * I changed the GA Tracking ID in the example here, but I´m using the right one
   on teh site.
 * add_action( ‘wp_head’, function () { ?>
 * <script>
 * // <!– Global Site Tag (gtag.js) – Google Analytics –>
    <script async src=”[https://www.googletagmanager.com/gtag/js?id=My_GA_TRACKING_ID”></script&gt](https://www.googletagmanager.com/gtag/js?id=My_GA_TRACKING_ID”></script&gt);
   <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.
   push(arguments)}; gtag(‘js’, new Date());
 *  gtag(‘config’, ‘My_GA_TRACKING_ID’);
    </script>
 * </script>
 * <?php } );
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcode-snippet-for-google-analytics%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/code-snippet-for-google-analytics/#post-9795029)
 * It looks like you have an extra set of `<script>` tags surrounding the code. 
   This is what would be causing the issue.
 * Here’s a fixed version of the tracking code snippet:
 *     ```
       add_action( 'wp_head', function () { ?>
   
       <!-- Global site tag (gtag.js) - Google Analytics -->
       <script async src="https://www.googletagmanager.com/gtag/js?id=My_GA_TRACKING_ID"></script>
       <script>
         window.dataLayer = window.dataLayer || [];
         function gtag(){dataLayer.push(arguments);}
         gtag('js', new Date());
   
         gtag('config', 'My_GA_TRACKING_ID');
       </script>
   
       <?php });
       ```
   
 *  [pjcoombes](https://wordpress.org/support/users/pjcoombes/)
 * (@pjcoombes)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/code-snippet-for-google-analytics/#post-10032967)
 * Is this snippet “Run snippet everywhere” or “Only run on site front-end”?
 * Thanks!
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/code-snippet-for-google-analytics/#post-10034228)
 * It can be either – with both options, the code will only be added on the front-
   end.
 * Choosing the “Only run on site front-end” option will just make the code a slight
   bit more efficient as it will be excluded completely from the admin area.
 *  Thread Starter [momadvisorblog](https://wordpress.org/support/users/momadvisorblog/)
 * (@momadvisorblog)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/code-snippet-for-google-analytics/#post-10377174)
 * Hi Shea!
 * Me again 🙂
 * I´m trying to set Google Tag Manager instead of google analytics.
 * But I´m not sure how to do that, I allready have the code but how do I specify
   the header and body part?
 * This is the code Google tag manager gave me
 * Add in header:
 * <!– Google Tag Manager –>
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({‘
   gtm.start’: new Date().getTime(),event:’gtm.js’});var f=d.getElementsByTagName(
   s)[0], j=d.createElement(s),dl=l!=’dataLayer’?’&l=’+l:”;j.async=true;j.src= ‘
   [https://www.googletagmanager.com/gtm.js?id=’+i+dl;f.parentNode.insertBefore(j,f)](https://www.googletagmanager.com/gtm.js?id=’+i+dl;f.parentNode.insertBefore(j,f));})(
   window,document,’script’,’dataLayer’,’GTM-XXXXXX’);</script> <!– End Google Tag
   Manager –>
 * Add ater body:
 * <!– Google Tag Manager (noscript) –>
    <noscript><iframe src=”[https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX&#8221](https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX&#8221);
   height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript
   > <!– End Google Tag Manager (noscript) –>
 * Could you please help me with the correct way to set it up?
 * Best!
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/code-snippet-for-google-analytics/#post-10377194)
 * Hey,
 * All you need to do is wrap each section of code in the appropriate WordPress 
   action filter hook, so that the first one outputs in the page header and the 
   second in the page footer:
 *     ```
       add_action( 'wp_head', function () { ?>
   
       	<!-- Google Tag Manager -->
       	<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
       	new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
       	j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
       	'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
       	})(window,document,'script','dataLayer','GTM-XXXXXX');</script>
       	<!-- End Google Tag Manager -->
   
       <?php }, 1 );
   
       add_action( 'wp_footer', function () { ?>
   
       	<!-- Google Tag Manager (noscript) -->
       	<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX"
       	height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
       	<!-- End Google Tag Manager (noscript) -->
   
       <?php }, 1 );
       ```
   
 *  Thread Starter [momadvisorblog](https://wordpress.org/support/users/momadvisorblog/)
 * (@momadvisorblog)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/code-snippet-for-google-analytics/#post-10379172)
 * Thank you Shea!
 *  Thread Starter [momadvisorblog](https://wordpress.org/support/users/momadvisorblog/)
 * (@momadvisorblog)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/code-snippet-for-google-analytics/#post-10416728)
 * Shea I think I need to insert it in the body,
 * would it be then something like this?
 * add_action( ‘wp_head’, function () { ?>
 *  <!– Google Tag Manager –>
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({‘
   gtm.start’: new Date().getTime(),event:’gtm.js’});var f=d.getElementsByTagName(
   s)[0], j=d.createElement(s),dl=l!=’dataLayer’?’&l=’+l:”;j.async=true;j.src= ‘
   [https://www.googletagmanager.com/gtm.js?id=’+i+dl;f.parentNode.insertBefore(j,f)](https://www.googletagmanager.com/gtm.js?id=’+i+dl;f.parentNode.insertBefore(j,f));})(
   window,document,’script’,’dataLayer’,’GTM-XXXXXX’);</script> <!– End Google Tag
   Manager –>
 * <?php }, 1 );
 * add_action( ‘wp_body’, function () { ?>
 *  <!– Google Tag Manager (noscript) –>
    <noscript><iframe src=”[https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX&#8221](https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX&#8221);
   height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript
   > <!– End Google Tag Manager (noscript) –>
 * <?php }, 1 );

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

The topic ‘Code Snippet for Google Analytics’ 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/)

 * 7 replies
 * 3 participants
 * Last reply from: [momadvisorblog](https://wordpress.org/support/users/momadvisorblog/)
 * Last activity: [7 years, 10 months ago](https://wordpress.org/support/topic/code-snippet-for-google-analytics/#post-10416728)
 * Status: resolved