How to include google analytics code the right way?

Whenever we include the code given by google analytics in the static web page, it does not validate…
Is their any way it can be put in an external javascript file & called from <head>

If I remember correctly, it has to be placed just before the closing </ body> tag.

mine is also right above the </body> tag and it validates.

I had a validation problem with analytics code that was puzzling. I had copied the code exactly as google provided it, and yet the validation problem was occurring.

Finally, I wondered whether it could be the line-break problem in Notepad, to which I was copying the google code and then copying it from there to my webpage code. So I changed the Wordwrap option off in Notepad, copied the code from google once again, and then created an shtml file that was placed in the page template just above the closing </body> tag.

And the problem disappeared.

Hmmm Yes… It validates, but,

Isn’t there a better, intelligent options if we can keep the code as external js file? That way it’ll load in cache

Did you notice that an “shtml” file was mentioned in my post?

an shtml file is an external file that is called by any html page that wants it using the #include call.

Just like an external js file, shtml also need to be written only once, and once uploaded, it can be called by the regular pages.

Initially I also used SSI (shtml) but then discovered php and also that most hosting companies have php but fewer have support for SSI.

I have therefore placed my Google Analytics code into a seperate html file that I call via a php include between the </head> and <body> tags.

I use these includes for anything that will be common to all or many of my pages.

I checked your code, but it uses the <script> code just above the body…

there’s no shtml code… Or am I looking somewhere else?

Hmmm, how to do this?? can you please show any site where you used this or give an example of the code?

Hi guys,

the thing is that you need to print into the client’s browser code given by google in any way you can.
You can put it into static html page or statically put into .php script and
can include from php also.



<?php
include_once(“code.php”); // this will print anything inside code.php
?>
</body>
</html>

First you create a file named, say, ‘analytics-tracker.shtml’ with the tracker code:

[INDENT]<script src=“http://www.google-analytics.com/urchin.js” type=“text/javascript”>
</script>
<script type=“text/javascript”>
_uacct = “…”;
urchinTracker();
</script>[/INDENT]

You upload the above .shtml file into your server.

Next you call it from your pages’ html code as below:

[INDENT]<head>

</head>

<body>
<div class=“main”>
<div class=“leftcol”>

</div>
</div><!–#include file=“analytics-tracker.shtml” –>
</body>
</html>
[/INDENT]

You upload the pages to the server as they are created.

If you view the source code of the pages, you won’t find the #include call. Instead you will find the tracker code expanded and included at the relevant place, as below:

[INDENT]… </div><script src=“http://www.google-analytics.com/urchin.js” type=“text/javascript”>
</script>
<script type=“text/javascript”>
_uacct = “…”;
urchinTracker();
</script>
</body>
</html>
[/INDENT]

All correct but Google says that the actual code must be placed between the </head> and the <body> tags and not in the actual body section as noted above.

  1. If I remember correctly, what Google says is to insert the code just before the ending </body> tag. I’ll check again.
  2. The code I had given were all taken from my website with only the ID dotted out. It has been been working for long

This is true. So persons comfortable with PHP should use that option.

Also, some webhosts do not automatically include the expanded shtml code in the html file. They might require some directives to be included in the .htaccess file before .shtml files are processed.

I’m not a php dev., but i think it soes like this:

  1. First create a .php file, ex: analytics.php and put the google’s js code into it:

<script src=“http://www.google-analytics.com/urchin.js” type=“text/javascript”>
</script>
<script type=“text/javascript”>
_uacct = “…”;
urchinTracker();
</script>

  1. call the analytics.php file just above </body>

<?php
include_once(“analytics.php”);
?>

Is this right?

The question is: Will this page ex: index.html will change into index.php? or .html will work ?

Google asks to put the code “just above </body>”

Nothing may appear between </head> and <body> in an HTML document except whitespace.

http://www.w3.org/TR/html401/struct/global.html

hello, can you answer that? Anybody here? :slight_smile:

My apologies, I have checked again and see that it must be immediately before the </body> tag and not immediately before the <body> as I thought - I now need to change the location of all my google scripts. Interesting because they have been working fine between the </head> and <body> tags!?!?

You might have better luck if you try at the PHP forum.