there are programs that do this that are written in php. Heres some free ones which I have used in the past. Your site may become slower if your not using paid hosting due to bandwidth. Install it and add the javascript snippet in your pages.

http://piwik.org/

http://www.openwebanalytics.com/