views:

1118

answers:

3
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 
{
    var pageTracker = _gat._getTracker("UA-XXXXXXX-1");
    pageTracker._trackPageview();
}
catch(err) {}

Would it be possible to call this script from an external JS file? I wanted to to something like:

<script type="text/javascript" src="googleanalytics.js" ></script>

and put one of these on each of my HTML pages.
The code I have above will be inside googleanalytics.js
Google's instructions was to put the code in each page. The problem with that is it makes it harder to change the tracking code. (We use different tracking codes for our DEV and PROD pages).
I've tried it out and it doesn't seem to be working.
Is there something wrong with doing that? Or is there something else causing the problem?

Important FYI Please note that we are using IE6 and 8 browsers (yes, I know, no need to tell me)

A: 

Wrap google code into function and execute on each page ;)

usoban
+2  A: 

Can you not use your server-side language to output the code at the bottom of each page? Have a function such as output_ga() and call that. That way you can change it in one place.

Draemon
The company I work for prefer that I don't use any server-side scripting for this, so this is out of the question. If it were up to me though, this would be exactly how I would do it.
Nikko
+7  A: 

Yes this is possible. If it is not working then there is something else going on.

Just a thought, Google Analytics is usually about a day behind in reporting so when you make changes it will take some time before you know it is working. What I like to do is hit a page that does not get traffic very often to assure me that I have my tracking set up correctly.

Also, you might try making the link an absolute link in your <script tag. It might just be looking in the wrong place for the analytics code.

Jason
Yes, I'm aware that GA is a day behind, the problem is that it's isn't registering any hits even though it has been 4 days since I applied the code and I've been surfing through the DEV site all day long for the whole 4 days, fixing bugs and stuff. Hopefully it was something wrong with the registration or something else. We will try to re-apply for a new tracking code and see what happens then. Thanks for the re-assurance!
Nikko
When you say it is not registering hits from your DEV site, is your DEV site url something like: http://mycomputer/If so, it appears to me that Google filters out hits coming from domain names that are not fully qualified. At least, that appears to be what is happening with my testing. Try accessing the DEV site using: http://mycomputer.mycompany.com/ and see if hits start registering.
slolife
The dev site is hosted elsewhere, with it's own domain and everything, and is not hosted locally on the machine I'm using. The problem is that the dev server where the files are hosted has already worked before with google analytics with another site, though that site didn't use the external JS script I am referring to here.
Nikko