Site analytics and stats without Google and cookies

Written by
Date: 2013-01-12 11:24:00 00:00

With companies like Google, Facebook and Twitter getting more and more info about us while browsing the web. And with them even being able to identify us directly and record our browsing history. There are more and more people that do not like this, so, if you are hosting your own site, and you think you need analytics stats (who doesn't) but, at the same time you do not want to expose your visitors to those companies there are still options to get the stats info you need to improve your site, while not exposing or "selling" your visitor's privacy.

Get info from web server logs

The most common and oldest way to get statistical information about your visits is to parse the web server logs, trough some tool that will give you the statistical info you need. Some of the best options are:

Awstats:

AWStats is an open source Web analytics reporting tool, suitable for analyzing data from Internet services such as web, streaming media, mail and FTP servers. AWStats parses and analyzes server log files, producing HTML reports. Data is visually presented within reports by tables and bar graphs. Static reports can be created through a command line interface, and on-demand reporting is supported through a web browser CGI program. – Wikipedia

Webalizer:

The Webalizer is a GPL application that generates web pages of analysis, from access and usage logs, i.e. it is web log analysis software. It is one of the most commonly used web server administration tools. It was initiated by Bradford L. Barrett in 1997. Statistics commonly reported by Webalizer include: hits; visits; referrers; the visitors' countries; and the amount of data downloaded. These statistics can be viewed graphically and presented by different time frames, such as per day, hour, or month. – Wikipedia

Most web hosting providers will offer you access to one or both awstats and webalizer through CPanel. If you are hosting your site, on your own server or VPS you will need to install one of them on the same server, or on your personal computer, and transfer the logs to your PC to analize the info.

As you see, you need access to the logs of the server, that is only possible if you are hosting your site on own server.

Run your own analytics solution

Another option you have, is to run your own analytics solution, there are a lot of options, but the only one I have tested so far is, Piwik. Piwik offers you reports as good as the ones you could see on Google Analytics, with the plus that you own the data.

Piwik offers you basically two ways to track your visitors, you can add a java script code to all your pages (just like with Google Analytics) or you can use a picture tracking system. The last method will use PHP instead of JavaScript. You can combine both of them too, for those visitors who have JavaScript disabled, or those who are visiting your site with a browser not able to run Java scripts.

Cookies

If you want to stop setting cookies on the browser of your visitors, you will be happy to know that Piwik lets you track the traffic of your site without the need of cookies. You will not have the info about returning visitors, but maybe that is not a really important metric for you.

The basic JS code to insert in all your pages is:

<!-- Piwik --> <script type="text/javascript"> 
var pkBaseURL = (("https:" == document.location.protocol) ? "https://{$PIWIK_URL}/" : "http://{$PIWIK_URL}/"); 
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E")); 
</script><script type="text/javascript"> 
try { 
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", {$IDSITE}); 
piwikTracker.trackPageView(); 
piwikTracker.enableLinkTracking(); 
} catch( err ) {} 
</script> 
<!-- End Piwik Code -->

This code will set cookies, they are first party cookies, and the tracked info, will remain in your control, so your visitors shouldn't have a problem with this, you have the server logs anyway right?

But if you do not want to set any cookies, you can use this code:

    <!-- Piwik --> <script type="text/javascript"> 
    var pkBaseURL = (("https:" == document.location.protocol) ? "https://{$PIWIK_URL}/" : "http://{$PIWIK_URL}/"); 
    document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E")); 
    </script><script type="text/javascript"> 
    try { 
    var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", {$IDSITE}); 
piwikTracker.disableCookies();
    piwikTracker.trackPageView(); 
    piwikTracker.enableLinkTracking(); 
    } catch( err ) {} 
    </script> 
    <!-- End Piwik Code -->

Note the line piwikTracker.disableCookies();. That line will disable cookies in Piwik. To respect even more your visitors' privacy, you may want to enable the IP anonymization. To do that go to: Settings > Privacy to enable IP anonymization and enable it with at least 2 bytes masked.

Final words

Analytics info about visitors to your site, is extremely important. Knowing from where the come for example, helps you set your server nearer to the visitors thus giving them a better service. Also, knowing the OS and browser and screen resolution they are using helps you improve the layout of your site for a user's better experience.

Knowing this, it is also important to know that the visitors' privacy is also very important, and should try to help them keep that private. In my case I have disabled all sharing buttons, that way facebook, twitter and the rest are not able to track the visitors to my site, just because they are using the browser they use to log into those services accounts.

You now know, how to protect people's privacy and still get the important statistics information you need to better run your site.