SiteHome :: Categories :: Index :: Consulting :: Training :: News :: Login


INSTALLING GOOGLE ANALYTICS TRACKING CODE


Installing Google Analytics tracking code on your site is a relatively simple thing, assuming that you have access to alter content on your site and are not dependent on a webmaster hiding somewhere in cyberspace.

You have a few options, depending on the type of pages your site serves.

1. If your site is flat HTML pages, you might be forced to add the code to the bottom of each page (a pain).

An extremely simplified example of an HTML page (which leaves out most things, but gives you an idea where the Google Analytics code goes) is:

<HTML>
<HEAD>
META details and other information go here . . . 
</HEAD>
<BODY>
Visible site text, images, menus, etc go here . . . 

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
  _uacct="UA-xxxx-x";
  urchinTracker();
</script>

</BODY>
</HTML>


Note that the example Analytics code used above comes from the Google site here.

If using flat HTML pages, you will need to go through each page that you want tracked on your server and add the code before the ending Body tag of </BODY>.

2. If your site serves pages in PHP, or similar, you might be able to add an include to the bottom of page(s).

An alternative to the above is to add a PHP call to a common page at the bottom of each page you want tracked. How do you know if your pages are PHP? You will see the page extensions (end of the page names) ending in .php (that is, unless your server rewrites URLs).

Taking the example above and altering it for PHP:

<HTML>
<HEAD>
META details and other information goes here . . . 
</HEAD>
<BODY>
Visible site text, images, menus, etc go here . . . 

<?php include '/analytics-code.php'; ?>

</BODY>
</HTML>


Note that the location of the include file and how you reference it will depend on where you put it on your server. Also, it is highly recommended to use "include" rather than "require", as if you use require and the file can't be found for some reason, your pages will not show, but PHP will shoot an error message to the screen (not exactly the thing you want your site visitors to see).

With the page of analytics_code.php that is being called having the following code:

<?php

echo 

"<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
  _uacct="UA-xxxx-x";
  urchinTracker();
</script>"

?>


The preceding example is slightly desirable to option one because if you have to alter the Analytics code for any reason, you only have to alter it once (i.e. in the include file named analytics_code.php) BUT you still have to go through each page on the site you want tracked and add the include code to call the analytics_code.php file in the first place.

3. If your site uses a ContentManagementSystem to deliver pages, you might be able to add the code to the bottom of the site template and when the page is rendered using this template, the Analytics code will appear on the bottom of each page.

It is impossible to give examples of all site template configurations, but a common way (that is also used in the site template that served the page you are now reading) is to have separate header, body and footer files. An example footer file with the Analytics code added would look something like this:

/*
Footer file in site template. Served after head and page content pages.
*/

<?php 

Any php file includes here . . .

Footer Text here . . . 

?>

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
  _uacct="UA-xxxx-x";
  urchinTracker();
</script>

</BODY>
<HTML>


The above example could be a page called footer.php that is called by the server after the header.php and body.php files.

Option three is most desirable, as if you use either of the first two options, you will have to go through and modify every page on your server that you want to track.

For more information, see this page.

Categories

CategoryGoogleAnalyticsHowTo



Copyright © 2008 Jordan Consulting Group | Powered by Wikka Wakka Wiki 1.1.6.3