Where Should the Analytics Tracking Code Be Placed in the HTML of a Webpage for Data Collection?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

You need to paste the entire analytics tracking code snippet into the HTML of your web pages right after the opening <head> tag to collect the information about visitors and track down your website performance. Google Analytics tracking code helps you analyze how users behave on your site. It is capable of collecting information about the following :

  • Observe the number of users your site gets from your organic efforts.
  • How long do the visitors stay on your website.
  • Where do the visitors come from, and which pages do they spend the most time on?
  • What page users are on when they choose to leave your website.

All this info is valuable in search engine optimization (SEO).

Syntax

Let’s say you have an analytics tracking code, and you want to use it on your website. All you have to put the code after opening the <head> tag.

The Tracking Code placement could differ for types of websites.

Importance Of Google Analytics Code Placement

Google Analytics allows you to track site visits and page how the website is performing daily, weekly, or monthly in terms of conversions, organic traffic, and growth. Correct placement of the tracking code is vital to collecting the correct information about visitors.

A high bounce rate also indicates that something is wrong with tracking. Even incorrect placement impacts data accuracy, as visitor count went down overnight.

If there's something wrong with the tracking code placement, you'll see a lot of red flags in the metrics reported.

Having the wrong placement of tracking code indicates so many things like :

Data corruption or wrong data

If your Google Analytics tracking code is incorrect or not added in the correct place, it may not activate at the right time or may be activated multiple times.

Your Google Analytics tracking code reports incorrect data about conversions and transactions because maybe your code does not fire while the transaction or conversion is happening on the website, which may lead to the manipulation of website matrics and not getting you the correct information about visitors.

Imagine a scenario where you've made 50+ transactions, and your Google Analytics report says you've made only 20 transactions.

Incorrect tracking

Correlated to the above, you could have “the right” Google Analytics tracking code, but it may still send the wrong information if you’ve placed the code in the wrong place.

For example, you entered the tracking code incorrectly on the page or at the bottom of the page. Before it loads, the visitor clicks on another link, and he will be redirected to another page. Since your tracking code is placed at the bottom, it did not load and did not track the visitor detail.

Sometimes, incorrect tracking becomes double or triple because of the wrong placement.

Attribution errors

Attribution is the act of assigning credit for conversions to different marketing channels such as ads, clicks, and factors along a user's path to completing a conversion. When someone visits your site (from paid ads or organic visits), they remain the same user but with a different referral for last-click attribution, or they may have zero history with your site. Become a brand new Direct User.

In the first case, you can still specify some values from multi-channel funnels and assisted conversions, but with the latter, not everything you do is responsible for bringing that user to your site. Google's analytics method of data collection depends upon the types of websites.

Let's talk about them.

For Static Website

A static website does not generate HTML using a programming language or interface like Python or any other.

Here's how you can add Google Analytics tracking code to your static website :

  • Locate the tracking code, and then copy the entire code snippet. Don’t modify the code snippet.
  • Paste the complete code snippet into the HTML on your web pages, right after the opening <head> HTML tag.

For dynamic website

A dynamic website generates HTML using a programming language or interface such as Python. To collect data, including the global site tag in the source code of each webpage you want to measure.

You can use a generic include or template to push the global site tag to every webpage dynamically.

  • Find and copy the code exactly without editing it.
  • Paste your global site tag code snippet into its own include file.
  • Link the include file so that the code snippet appears after the opening <head> tag on each page you want to measure.

PHP website

If you are adding google analytics tracking code to a PHP website, it is best to add it via a file because you may not have an option to add code directly to the website.

  • Find and copy the code precisely without editing it.
  • Create a file named analyticstracking.php and paste the google analytics tracking code into it.
  • Log in to your server and upload the file into your site's root directory.
  • Now Include the analyticstracking.php file on each PHP template page.
  • For each template page, immediately after the opening <body> Html tag, add the following code :

And you're done. You've successfully added the tracking code to a PHP website.

Web-hosting service

Some Internet service providers or hosting platforms simplify the installation of Google Analytics tracking code by offering plugins that automatically install it on your website. Go through your platform’s instructions to add your Google Analytics ID.

Here are instructions for implementing the Google Analytics tracking code for some common providers :

If you are using other hosting providers, I strongly suggest you check their setup manual to know how to add tracking code to their website.

Conclusion

  • Always place the Analytics tracking code after opening the head tag.
  • Do not modify the tracking code before pasting it.
  • Placement of tracking code plays an important role because incorrect placement causes many issues such as incorrect tracking, Data corruption or wrong Data, and even Attribution errors.