Web Analytics Embedded JavaScript Page Tracking Code: Place at the top or bottom of the page?


Hosted reporting systems commonly use code embedded in site pages to track user viewings of each website page (alternatively the systems download and report on data contained in web server log files).

Web Analytics system vendors provide instructions specifying where to place the JavaScript code in a site’s pages. Usually the tracking code is placed in the page heading section (<head>), or at the page bottom, right before the body closing tag, </body>.

What is the difference between tracking code placement in the page top and bottom?

The answer exposes one of the weaknesses in JavaScript embedded page tags as a web analytics data collection method. To track a page, the JavaScript code must contact the reporting server to say “This page is being viewed by visitor x who just viewed page y using browser z”. The reporting server adds this information to its database for a given web site in order to produce web analytics reports. If the JavaScript logic is at the beginning of a web page, the web browser pauses during loading the page when it encounters the JavaScript code. The browser must also download the tracking code if it hasn’t already down so, and must contact the reporting server as specified in the tracking code.

In the best of scenarios, this process is transparent to the end-user. A user on a dial-up connection will probably notice a slight delay on the first visit as the JavaScript code file is downloaded. Should the reporting server be sluggish, or down, a web page with tracking code will appear to stop as the browser awaits a non-responsive reporting server. Eventually, the request to the reporting server will timeout and the page will most likely load.

Place tracking code at the bottom of the page if you can

Tracking code placed at the bottom of the page will have nominal impact on a user’s browsing experience. Almost all of a web page loads before the reporting server is called. Even if the reporting server is sluggish or outright unavailable, the worst that happens is that page views may go unreported — a site’s visitors aren’t adversely impacted.

Thus, the best place for tracking code from a site point of view , is at the bottom of the page. Before you move your tracking code to the bottom of your pages, check with your analytics vendor to see if there are any problems with their tracking and reporting when tracking code is placed at the bottom of a site’s pages.

Sites deploying Google’s Google Analytics have probably noticed delays in page loading as Google has been working through teething pains due to greater than anticipated take-up of their free Web Analytics offer. Google originally specified placement of tracking code in the <head> section of site pages but is now specifying tag inclusion at the bottom of the page.

Placement at the bottom of the page improves web analytics statistics

One common report available from web analytics systems based on web server log files is user aborted page views — as determined by http server status 206. This status occurs when a user begins to download a page (or other object) from a site but then aborts the download by clicking on the browser stop button or by going to a new page (or site). Status 206 data can indicate the presence of quick acting, impatient site navigators (they see an enticing link and click on it before the current page is finished loading) — this report can also indicate slow content delivery — page sizes are too big for users on dial-up connections? Marketing launched a new promotion without insuring compatibility with current site capacity sizing?

Embedded page tag solutions are less precise. Unfortunately, embedded tag systems are not able to determine if partial content was delivered to the user — its all or nothing. Placing the tagging code at the beginning of the page will erroneously track as viewed pages which have been aborted by the user — a site will not know from their web analytics solution that there is a problem viewing pages — except for perhaps noting a high number of users abandoning the site after few page views. Placement of tracking code at the bottom of the page results in the opposite problem: partially viewed pages will not appear directly in web analytics statistics.

Should I save the JavaScript tracking code file on my server?

The first time a user visits your site, the reporting server is called twice — once to download the tracking JavaScript code and a second time to send data on the current page. Even if the tracking code JavaScript file is on your sever, a call will be made to the reporting sever to log the page view data — so storing the tracking code on your sever only solves part of the problem of a sluggish reporting server.

A new problem arises if the hosted solution vendor changes the JavaScript tracking code logic — normally, the update would happen automatically. However, if your web site pages are referring to an obsolete version stored on your servers, you could have reporting problems. For these reasons, we do not recommend you save the JavaScript file on your web server.

Similar Posts:

Registration is now open for the next SEO Course and Google Analytics Course in Milan. Don’t miss the opportunity!


About Sean Carlos

Sean Carlos is a digital marketing consultant & teacher, assisting companies with their Search (SEO + SEA = SEM), Social Media & Digital Media Analytics strategies. Sean first worked with text indexing in 1990 in a project for the Los Angeles County Museum of Art. Since then he worked for Hewlett-Packard Consulting and later as IT Manager of a real estate website before founding Antezeta in 2006. Sean is an official instructor of the Digital Analytics Association and collaborates with the Bocconi University. He is Chairman of the SMX Search and Social Media Conference, 13 & 14 November in Milan. He is also a co-author of the Treccani encyclopedic dictionary of computer science, ICT & digital media. Born in Providence, RI, USA, Sean received Honors in Physics from Bates College, Maine. He speaks English, Italian and German.

Leave a reply

Warning: Comments are very welcome insofar as they add something to the discussion. Spam and/or polemical comments without a rational justification of the author's position risk being mercilessly deleted at the sole discretion of the administrator. Yes, life is hard :-).