Hello! I am finishing my new site - TextOpus. Now, I am trying to add new Google Analytics code, but it makes my site not XHTML valid. I am placing it just before </body> tag, as Google suggests me to do. How can I place this code on my site in XHTML way?
Thanks, but there is only one post about my problem and it's about old Google Analytics code (I have no problems with it). The problem occures with new code.
if you replace the analytics code with this, it makes it XHTML valid: <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); </script> <script src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'></script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-290852-8"); pageTracker._initData(); pageTracker._trackPageview(); </script> Code (markup): not sure if your allowed to change it but essentially just writing the embedded script tag outside
Sorry, I've placed information that it works too soon. It validates, but doesn't work. There's a problem in this line: <script src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'></script> Code (markup): Web browser can't parse this url to script. Any suggestions?
There's an old Javascript coder trick of using HTML comment tags after the opening script tag and before the closing script tag. FOR EXAMPLE: <script type="text/javascript"> <!-- var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write("\<script src="'" type="'text/javascript'">\<\/script>" ); --> </script> <script type="text/javascript"> <!-- var pageTracker = _gat._getTracker("UA-XXXXXX-X"); pageTracker._initData(); pageTracker._trackPageview(); --> </script> That should allow the code to validate and permit it to keep working. Don't just copy and paste the above code, though. I've used UA-XXXXXX-X in the example above because I have no idea what your specific account info is. Hope that helps.
The comment tags will help your site validate...follow his advice. It should be done for all javascript.