Using JavaScript code efficiently when a page has tracking codes, and other useful tips.

Some tracking codes out there are quite useful, but as a draw back have a slower loading time. It's important to prioritize your javascript resources so things run as you would expect them to. When just using google analytics, this isn't really an issue.

If you're like me, you put most of your JavaScript code in the head section, usually in a $(document).ready for jquery or a window.onload for vanilla JavaScript. If there's code in there that you want to run right after the HTML and DOM load, you may want to place it not in a window.onload, but in a script tag that sits just after the HTML. This is usually right before the tracking code is loaded. This ensures it loads before the tracking, and that it has all the dom elements to utilize.

Performance best practices:

If you're using a library such as jquery or prototype, I recommend instead of hosting the file or files yourself, reference the one google has hosted. You can find the urls for the minified and uncompressed files here: http://code.google.com/apis/ajaxlibs/documentation/

The reason to use the library this way is that it helps with load times. This is achieved by the fact that many other sites utilize the documents hosted there, and therefore visitors of your site will benefit from having the resource cached already. Another thing is that it's google, and you can therefore count on it being up all the time. Be sure to use the minified files. They are compressed, and therefore load a bit faster.

I recommend looking into optimizing your code, which varies from different libraries. At the endo of the day, just keep your javascript in order of what you want to see happen first.