Optimizations in the Google Analytics tracking snippet

This is not an instructional post on how to use Google Analytics, but a quick teardown to highlight some clever things the script does to optimize page load speed and work around network latency issues.

How does the Google Analytics tracking snippet work? The code they tell you to paste into your site is an opaque mess:

1
2
3
4
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

Lets clean up the function contents a little bit.

Read More

Code Rainbow

Oh the joys of writing CRUD methods. Found this gem in one of the applications I helped maintain. Its been replaced with the much more terse null-coalescing operator by now.

Who doesn’t love 1000 lines of CRUD methods?

?? anyone?

Its crud like this that makes an ORM look so appealing.

SSH to same directory on remote server

My local dev environment matches the file structure of our dev server. Its convenient to be able to ssh to the same directory on the development server.

I added this to my ~/.profile.

1
2
3
function dev01 {
ssh dev01 -t "cd $PWD; bash --login"
}

Then all I have to do is type dev01 on my local terminal and it opens a shell on the server in the same directory. If the directory is not found, it just drops you back into ~/