1

This is likely a very simple question with hopefully a simple answer. I'm using a CMS (TeamSite) and trying to add Google Analytics to a site. The problem is, as the CMS generates the HTML I can't add the Google Analytics code just before the closing </head> tag as Google tells you to. The other method of adding GA to your site is to add some JavaScript before the closing </body> tag. Now I have done this but TeamSite seems to put HTML comments around the JavaScript. Now without sounding like a complete fool, does this mean that the browser will ignore the JavaScript and not execute it? Code is below:

<script type="text/javascript"><!--
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
try{
// --></script>
<script type="text/javascript"><!--
var pageTracker = _gat._getTracker("UA-20657322-12");
pageTracker._trackPageview();
} catch(err) {}
// --></script>

Is there another way for me to add GA to the site without having to take the file from the production server and manually add the script before the closing </head> tag? Any help would be much appreciated.

Thanks

zik
  • 3,035
  • 10
  • 41
  • 62

2 Answers2

2

If the HTML comment tags are inserted before and after your and tags, the Javascript will not run. If it is inside the script tag, everything should work fine.

See for yourself:

<html>
<head><title>test</title></head>
<body>

<script>
alert('not commented');
</script>

<!--
<script>
alert('outside commented');
</script>
-->

<script>
//<!--
alert('inside commented');
//-->
</script>

</body></html>

The first and third alert will fire, but the second one will not. Like the poster below me mentions, this has to do with backwards compatibility so older browsers that do not support Javascript don't get confused.

mrbellek
  • 2,300
  • 16
  • 20
  • 2
    Same asnwer as mine but a definite +1 for good visual examples. :) I will note that you don't need the // on the first comment since – Chris Jan 24 '11 at 12:16
  • Yup, duly noted for next time! Great thanks for this...every day is a schoolday :) – zik Jan 24 '11 at 12:17
0

In a script block HTML comments are treated slightly differently. In a script block single line comment.

The reason for this is so that in really old browsers that don't know about script tags you could use this sort of markup and if it didn't understand script tags it would not render the javascript to page (because it thinks its in a comment) and if it does understand script tags it will just treat teh opening one as a single line comment and then the closing tag is normally marked as a commetn using the //.

So in summary these comment tags shouldn't be causing you a problem that I can see.

Is the script not being run on your page are are you just uncertain where your issue lies? Sticking an "alert('test');" into that block should allow you to confirm that it is being run.

Chris
  • 27,210
  • 6
  • 71
  • 92
  • Awesome and thanks to you too. Well I think the problem at the moment is that I don't think that the script was being run. Under Google Analytics the site shows up as "Google Analytics is gathering data" which is fine, but it's been doing this for a few days which almost leads me to believe that the script is not being executed, although this script I only added to the page today. I will wait 24 hours to see if this works. Thanks to you both. – zik Jan 24 '11 at 12:13