views:

776

answers:

4

Just to confirm...

file: google.js

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-11510668-1");
pageTracker._trackPageview();
} catch(err) {}

And then linking the file like:

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

That works, right?

+1  A: 

I can't think of a reason it wouldn't.

Have you tried it and encountered any issues?

Edit: I just tried this and inspected it in firebug, as far as I can see, the second script tag is written correctly.

This should work.

Neil Aitken
i can't tell at the moment since it takes a day for google to update stats.
n00b
+2  A: 

I think you still need to execute that script somehow. If you simply include it then it won't be executed. If you put it inside of a wrapper function:

function googleCode(){
// google code goes here
}

then you can use jQuery and execute it when a page is fully loaded

 $(document).ready(function() {
           googleCode();
        });

Just an idea.

Pawel Pabich
+3  A: 

Add that script to your page, then if you're using FireFox w/ FireBug (or other debugger) or Chrome, you can Inspect Element to see what's been written to the page. If you see...

<script src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>

...then you should be good to go!

hunter
alright great stuff... seems to work! thanks!
n00b
+2  A: 

My testing shows that this works.

The sample page provided runs a document.write() in an external script, and then also runs document.write() to print out yet another script tag, to make sure that that functionality works, also. I got expected output (two lines of text) on all browsers I tested, including Internet Explorer (even 6), Firefox, Safari, and Chrome.

I think they just say to do it inline because they think it's easier for newbies.

You should be fine.

Matchu
thanks for testing other browsers!
n00b
Glad you finally got me to do it. Would have been upset if I ended up losing all IE traffic or something when that project went live.
Matchu