I wondered: it would be a great help to query each visitor's history, to see how they arrived at your site (search engine, link, ad, etc.) I haven't tested if its possible to query the history object in this way yet, though.
But how ethical is it? I don't want to have my visitors think I'm breaching privacy!
Actually its sorta different. Capturing a persons history file, basically will tell you every website that that person has visited is a major privacy concern and is unethical in my opinion. However tracking the referrer is not unethical and is not a major concern because you dont so much figure out where they hangout online but just develop data that can be used to target advertising and such.
I agree with CinC. If someone accesses your site from a bookmark in their favorites, your referrer log will say "Bookmark" or something similar. Now if that is the case why would you need to know what the previous site was. By accessing the history log you would be getting access to that person's surfing habits, thereby becoming able to track where they go and figure out more targetted advertising for this person. Now if you have their permission then everything is ok. If you don't have permission then your violating their privacy. This sort of behind the scenes "trickery" is the same reason that the general public is paranoid about cookies today.
Now I am not saying your going to do this, nor do I wish to imply that your unethical but some people might not see it the same way. This would make your job more difficult in the future. The best way to get what you want is to use HTTP_REFERRER in CGI programs or to access your server's referrer log.