Where should the Analytics tracking code be placed in the HTML (Hypertext Markup Language) of a webpage for data collection? Generally, there are four options. Not all choices are suitable for you and you should pick one according to your condition. Read more related articles provided by MiniTool team.

Next, let’s analyze that question in detail.

About the Question

“Where should the Analytics tracking code be placed in the HTML of a webpage for data collection?” You may encounter this question when you try to set up your website with Google Analytics (GA) system to let GA monitor your webpage behaviors.

To enable the Google Analytics system to get control of the performance of your website, you have to place the GA tracking code on your website. In detail, you need to put the Google Analytics code into the HTML file of your webpages. Then, GA can collect data information on your web and create a traffic report of your pages for you.

Then, in the HTML of a webpage, where should the Analytics tracking code be placed for data collection?

[5 + 10] Best Free HTML to PDF / PDF to HTML Converters
[5 + 10] Best Free HTML to PDF / PDF to HTML Converters

How to convert HTML to PDF? What are the free HTML to PDF converters? How to convert PDF to HTML? What is the best PDF to HTML converter?

Read More

About the Answers

For the above question, there are usually four answers just listed in the above content. Let’s take a look at the 4 answers one by one.

The first answer means that you should put the Google Analytics tracking ID at the beginning of the <head> tag of your website, very after the “<head>”. The <head> tag is the very start part of an HTML file. Therefore, it means that you need to put the GA tracking code at the beginning of the HTML file.

The <head> tag in an HTML file usually includes the meta info (title, keywords, description, and URL), publish info (publish date, modify date, author name, etc.), and some other script details of the webpage. Those contents can’t be displayed on a normal page view.

The Opening Head Tag in HTML

The second answer says that you need to put your GA tracking code at the end of the <head> tag; which is right before the closing </head> tag.

The Closing Head Tag in HTML

The third answer for the question “Where should the Analytics tracking code be placed in the HTML of a webpage for data collection?” suggests you put the tracking code at the beginning of the <body> tag, right after “<body>” in the HTML.

The <body> tag contains the main contents of a webpage and those contents are visible on normal page view.

The final answer tells that you are recommended to put your GA tracking code before the closing </body> tag, at the end of the body part.

The Closing Body Tag in HTML

The above three screenshots are taken from one the of webpages of minitool.com, a website where you can get professional and reliable backup software, partition program, data recovery tool, and some multimedia applications.

After comparing those 3 pictures, you can find that the Google Analytics tracking code locates very before the closing </head> tag in the second image for the MiniTool website. Yet, for other websites, their GA tracking code maybe after the beginning of the <head> tag, after the beginning of the <body> tag, or before the closing of the </body> tag.  

Google Analytics Tracking Code in HTML

How to Sep up Google Analytics Tracking ID?

Moreover, which option should you choose to place your tracking ID? It depends on your situation.

For Static Website

If your website is a static one, you should put the global site tag, a Google Analytics property containing the Tracking ID, before the closing </head> tag on every page you want to measure. Just copy and paste the HTML ID there.

For Dynamic Website

If yours is a dynamic website that generates HTML using a programming languages or interfaces like Python or PHP. To collect data, you should include the global site data in the source code for every page you want to control.

Way 1. Use a Common Include or Template

  1. Copy your global site tag without editing and paste it into its include file.
  2. Link the include file so that the snippet appears before the closing </head> tag on each webpage you plan to monitor.

Way 2. Rely on Analyticstracking.php File

  1. Copy your global site tag without editing and paste it into a file named “analyticstracking.php”.
  2. Include the analyticstracking.php file on each PHP template page.
  3. Add this code right after the opening <body> tag for each template page: <?php include_once(“analyticstracking.php”) ?>.
How to Save a Webpage as a PDF Within Seconds on Your PC?
How to Save a Webpage as a PDF Within Seconds on Your PC?

How to save a webpage as a PDF? If you want to know, then you should read this post carefully. You can know how to do that on different browsers.

Read More

For Web-Hosting Service

If you are using a web-hosting service that you do not control the page code, you are able to rely on plug-ins provided by Internet service providers (ISP) or hosting platforms to automatically install Google Analytics on your webpages.

After setting up your global site tag. You may wonder whether it is working or not. To do so, just visit your website and check the real-time reports in GA to verify that your tracking is working. You can also make use of Google Tag Assistant to do the task.  

Also Read

  • linkedin
  • reddit