You could use an Analytics tools like Piwik or Google Analytics. They give you great information on your users behavior and are easy to integrate. No need to code this at your own.
You can track, if people click on links to external sites - but you cannot track, if someone just leaves your site entering a new url to his address bar.
EDIT
You already use Google Analytics. Have a look into event tracking. This can help you to track people clicking external links on your site and leaving it that way. Tracking the back-button of your browser is also possible.
I think you are especially interested in people, visiting your site and directly clicking the back button, going back to Google (or any other referring site). This user behavior is called "bouncing" and it is a very important key metric to judge the quality of your site. Google Analytics shows you the bounce rate for every page on your site. 30% means, 30% of the users entering your site on this specific page leave it directly, w/o viewing another page of your site.