Sunday, March 17, 2013

AWStats API v0.9 (First Look)

The AWStats API is now starting to work.

It is a simple php script that you place somewhere on you website and this script will return the AWStats data as json.

As an example say that you place the script getawstats.php on your site lets call it example.com, then when you access http://example.com/awstats/getawstat.php you can get the following data

{"summary":{"year":"2013","month":"04","lastupdate":"20130410075001","uniques":"27423","visits":"60031","pages":152305,"hits":708106,"bandwidth":118969674567},"days":[{"day":1,"pages":10662,"hits":50642,"bandwidth":4577570523,"visits":5086},{"day":2,"pages":11969,"hits":46479,"bandwidth":3942706134,"visits":6325},{"day":3,"pages":14303,"hits":55780,"bandwidth":8044779500,"visits":6415},{"day":4,"pages":34091,"hits":217563,"bandwidth":47562300844,"visits":9528},{"day":5,"pages":21942,"hits":109844,"bandwidth":18326884056,"visits":7771},{"day":6,"pages":16759,"hits":63628,"bandwidth":11206302251,"visits":6731},{"day":7,"pages":12302,"hits":48495,"bandwidth":8123735971,"visits":4993},{"day":8,"pages":12060,"hits":43136,"bandwidth":7246989597,"visits":5119},{"day":9,"pages":15968,"hits":64832,"bandwidth":8986186671,"visits":6940},........ and so on


This json data can easily be turned into data objects in many languages. C#, Java, Php.
That makes it very easy to work with.

By default it return stats for the current month. If you want to get stats from the past you can pass it statsmonth and statsyear parameters as POST data in the request..

I run into some strange issue with php and post data. The first post parameter always failed to be parse into the $_POST variable in php. I'm not a php expert so I probably have done something wrong. So as a workaround I always send an extra dummy parameter first.

Sending the following in the body of a POST request
dummy=none&statsyear=2010&statsmonth=2

And it will return stats for February of 2010 (If you now have stats for that month)

getawstats.php needs to know where the AWStats Data files are located and also how you name them.
This is configured by editing the file and at the begining of the script there is two variables that need to be modified.

// Path to the AWStats Data File.
$awstatsDataPath = "/var/lib/awstats/example/";
      
// Filename of the stat files. DO NOT INCLUDE the DATETIME part of the filenames
$statfilename = ".example.txt";

This script is using a modified version of the AWFile class, I had to do some minor adjustment for it so that the data generated is placed in array in a different way and also the AWFile class did not parse some of the data I needed, so also added that.

To do

There are a couple of things missing. The parse need to be modified to aggregate the browser stats. Having one entry for every chrome version or firefox version is not a good idea since there come out new version of them every week.

OS version data also needs some post processing.

Some support for authentication is needed. Some simple login mechanism like digest login or something like that.

Download

There is no download of my AWStats API yet. I will release it here and on my webpage when it is done.
If you want my current alpha version you can drop me a mail.

1 comment:

  1. hello, can i look at your api?

    Michaelsermoneta@gmail.com

    ReplyDelete