Jump to content

Javascript from Google


kurt.santo

Recommended Posts

I have a code given by Google to include in website code, but I would like to understand first what it does. The code is:

<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"));</script><script type="text/javascript">var pageTracker = _gat._getTracker("UA-1982198-7");pageTracker._initData();pageTracker._trackPageview();</script>

Does someone know what it does and is this to be included in all pages of that website?Thanks,Kurt

Link to comment
Share on other sites

It includes another bit of JavaScript from Google's site and then runs commands that use that code.From the URL it seems that code is for Google Analytics. You'd need it on all page because, well, the service collects data on all pages.

Link to comment
Share on other sites

It includes another bit of JavaScript from Google's site and then runs commands that use that code.From the URL it seems that code is for Google Analytics. You'd need it on all page because, well, the service collects data on all pages.
Do you know why they wrap it up in two <script /> tags?Kurt
Link to comment
Share on other sites

the first script includes a remote script from their servers and the second one uses the included script. They could have done it all in one but I am not sure why they didn't. Makes no difference either way.

Link to comment
Share on other sites

It actually does make a difference, in order for the second script to have access to the _gat object the first script that writes an "include" with document.write needs to be closed. If the line using _gat comes right after the document.write line it will be undefined. I'm not sure which subtlety in the browser makes that happen, but that's the way it works.

Link to comment
Share on other sites

It actually does make a difference, in order for the second script to have access to the _gat object the first script that writes an "include" with document.write needs to be closed. If the line using _gat comes right after the document.write line it will be undefined. I'm not sure which subtlety in the browser makes that happen, but that's the way it works.
Thanks. I didn't know that. I always include scripts with src="". Is there a benfit to using document.write in this case?
Link to comment
Share on other sites

I'm not sure why they did that, the only reason I know that is because I tried to combine them in one of my pages and it wasn't working. They do say to add that script before the </body> tag, so the document isn't going to be closed by the time it runs, but I'm not sure why they chose to do it that way instead of creating a script element and attaching it to the head, for example.

Link to comment
Share on other sites

I'm not sure why they did that, the only reason I know that is because I tried to combine them in one of my pages and it wasn't working. They do say to add that script before the </body> tag, so the document isn't going to be closed by the time it runs, but I'm not sure why they chose to do it that way instead of creating a script element and attaching it to the head, for example.
Thats good to know. Thanks for all inputs...Kurt
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...