Jump to content

Javascript from Google


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 post
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 post
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 post
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 post
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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...