Ads by ProfitSence
Close

Where should the analytics tracking code be placed in the html of a webpage for data collection?

Last Updated on Wednesday 5th Oct 2022

Google Analytics code

You need to paste the whole snippet into the HTML on your web pages just before the closing tag.

  • Means after the opening <head> tag of your website or before the closing
  • The main advantage of placing it in the <head> is that you will track the user's pageviews.
  • Even if they leave the page before it has been fully loaded.
  • The advantage is to catch a pageView call if your page fails to load completely
  • For Example, - if the user aborts loading or quickly closes the page, etc.

google analytics tracking id

Google's official recommendation for adding the Google Analytics tracking code is to place the tracking code at the website's head.

you should use the property id google analytics tracking code

			
					<!DOCTYPE html>
<html>
  <head>
    <title>Page Title</title>

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456789" ></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'UA-123456789');
    </script>
  </head>
  <body>
     <h2>Just add the Your Google Analytics Code like above</h2>
  </body>
</html>

			
	
  • Once Configured the required Javascript for Google Analytics in your head tag.
  • It will take approx 24-48 hours before you can see the results in your Google Analytics account.