This may be a stupid question but…
If I save google analytics “snippet” as a .JS file, will it still work or do i need to paste it in it’s entirety into my header?
This may be a stupid question but…
If I save google analytics “snippet” as a .JS file, will it still work or do i need to paste it in it’s entirety into my header?
On a side note if you are going to paste it into your HTML code, do so at the end as opposed to your header.
it will work… even if i paste the code at the end of my document
I believe you can do it that way.
As far as I’m aware (based on your question), you’re not able to export the tracking data for Google Analytics into a separate JavaScript file, it has to be embedded directly into the page. I did a bit of experimentation with my own site out of interest of the same question and it wouldn’t track or find the tracker if it wasn’t at the base of the HTML. Therefore I would say keep it in-tact within the page and place it as the Google FAQ says just above the </body> element.
As damage said it then makes an additional HTTP request, just add it to your HTML right above the </body> tag.
so is it faster putting it in a separate file? I imagine it is IF it works.
I’ve successfully used it in a separate file, the head and the body.
Hmmm… it’s currently working in the <head> of all of my own sites (and Google’s still telling you to put it there). It’s also working in a separate file for one of my clients.
Not sure what happened with your code… were you using the async version? I can confirm that if set up properly it works in a separate file, in the header, or at the bottom of the page.
It was less than a month ago I tested it, and it didn’t work in a separate file or in the head
Well does analytics need to be in the HTML?
That’s what I needed to know! Thank you
The latest GA code (asynchronous) can be placed at the beginning of the page.
No, it wouldn’t be faster in a separate file. You’re making an additional HTTP request. That’s an extra file to load for your server.
Alex, how long ago did you check that?
The new asynchronous code certainly works in the header, and should also work in a seperate JS file.
Google now gives instructions to put it just above </head> within the UI, though the docs still say before </body>. A few weeks ago the UI was telling people to put it after the opening <body> tag.
The thing about the async code is that it doesn’t really matter as much.
Have you taken a look here: http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=66983
If your pasting the code into your HTML then yes it needs to be within the body tag.
so if I save the code as a .js file, and I should put it at the end of my code - it DOES have to be put in before I end the body tag correct?