• Resolved momadvisorblog

    (@momadvisorblog)


    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;
    <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 to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    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

    (@pjcoombes)

    Is this snippet “Run snippet everywhere” or “Only run on site front-end”?

    Thanks!

    Plugin Author Shea Bunge

    (@bungeshea)

    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

    (@momadvisorblog)

    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);
    })(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;
    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

    (@bungeshea)

    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

    (@momadvisorblog)

    Thank you Shea!

    Thread Starter momadvisorblog

    (@momadvisorblog)

    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);
    })(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;
    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.