Awstats plugin
This is a simple plugin that retrieves information from Awstats log files, currently only popular pages, search phrases, referrers and browser statistics. It provides template functions for including this information on your WordPress site.
Awstats is a web server statistics package that maintains a set of monthly statistics summaries (default in /var/lib/awstats). These files are used by this plugin to extract information that can be included in your web pages.
Installation
- Download the zip file.
- Drop the
awstats.phpfile in your plugin directory. - If necessary change the few lines at the top of the file to reflect the location of your awstats log files and the site string in the log filenames.
- Activate the plugin.
Usage
Insert something like
<li id=\"popular\">Popular:
<?php awstats_popular(); ?>
</li>
in your template where you would like to have an overview of popular pages.
Insert something like
<li id=\"searchwords\">Search words:
<?php awstats_searchwords(); ?>
</li>
in your template where you would like to have a search phrase overview.
Insert something like
<li id=\"referrers\">Referrers:
<?php awstats_referrers(); ?>
</li>
in your template where you would like to have an overview of referrers.
Insert something like
<li id=\"browsers\">Browsers:
<?php awstats_browsers(); ?>
</li>
in your template where you would like to have browser statistics.
The month for which the information is retrieved is the same as the month used in the get_calendar template function.
Update (12 May 2005)
I bumped the version to 1.0.
You can also add the heading string as parameter and omit the <li>-headers, e.g.
<?php awstats_popular(10, 'Popular'); ?>
<?php awstats_searchwords(10, 'Search phrases'); ?>
<?php awstats_browsers('Browsers'); ?>
<?php awstats_referrers(10, 'Referrers'); ?>
where 10 is the maximum number of items to be shown.
And you can invoke the template functions via do_action like:
<?php do_action('awstats_popular', 10, 'Popular'); ?>
<?php do_action('awstats_searchwords', 10, 'Search phrases'); ?>
<?php do_action('awstats_browsers', 'Browsers'); ?>
<?php do_action('awstats_referrers', 10, 'Referrers'); ?>
The extra parameters only work in WordPress 1.5.1 and higher.
May 12th, 2005 at 12:05
Would it be possible to add a parameter limiting popular to a category or categories? something like
awstats_popular(10, 'Popular', '1,2');would limit the list to cat_ID’s 1 and 2May 12th, 2005 at 16:05
Sorry for second post…I can’t seem to get the plug in to work. I know I have the right directory set up, but nothing appears. Ideas?
May 31st, 2005 at 05:05
I have looked into this briefly, it *seems* that in awstats_read_history():
} else {
$yyyy = gmdate('Y', current_time('timestamp') +
get_settings('gmt_offset') * 3600);
$mm = gmdate('m', current_time('timestamp') +
get_settings('gmt_offset') * 3600);
}
, it does not need to add gmt_offset*3600 again, as the current_time() already did that:
function current_time($type, $gmt = 0) {
switch ($type) {
case 'mysql':
if ($gmt) $d = gmdate('Y-m-d H:i:s');
else $d = gmdate('Y-m-d H:i:s', (time() + (get_settings('gmt_offset') * 3600)));
return $d;
break;
case 'timestamp':
if ($gmt) $d = time();
else $d = time() + (get_settings('gmt_offset') * 3600);
return $d;
break;
}
}
So changing those two gmdate() function calls to:
$yyyy = gmdate('Y', current_time('timestamp'));
$mm = gmdate('m', current_time('timestamp'));
should work better. This issue becomes more visible as my timezone is GMT+10.
May 31st, 2005 at 08:05
Leon, thanks!
Fixed.
June 1st, 2005 at 04:06
Curioso - any ideas about my 2 posts? regarding showing only x categories and the plugin not working
June 1st, 2005 at 06:06
Steve, I have no immediate plans to add this, but I will keep it in mind
June 1st, 2005 at 15:06
Do you know if awstats works with cgi’s? I want to analyse the traffic on a web site written in PHP where all pages have the same URL with different parameters. IE:
http://www.domain.com/index.php?id=1
http://www.domain.com/index.php?id=2
thanks in advance
June 1st, 2005 at 18:06
As far as I know, awstats strips off the ?-part
July 6th, 2005 at 23:07
Statistiken
Ich habe gerade ein Statistik Tool fuer diese Webseite eingerichtet namens awstats. Es ist unheimlich maechtig auch wenn man keine Plugins installiert. Ausser den standard Statistiken ueber, wer welche url am meisten geklickt hat und wann die meisten …
July 17th, 2005 at 03:07
I’d really like to use your plugin but I am hosted with livingdot and with my web hosting comes Awstats is there a way that I can still use the plugin? I’m not sure if I can.
Thanks!
September 16th, 2005 at 15:09
Is it possible to use this to grab on the bandwidth used on the site.. Also, we are using awstats for capturing mail stats, and would like to customize it to get the per domain send and receive totals from the server. What would be required to make that happen?
September 17th, 2005 at 14:09
David, maybe you can use AWstats totals, see http://www.telartis.nl/xcms/awstats
December 8th, 2005 at 20:12
Very cool plugin. I have used AWStats expensively for my clients. For being a free tool its extremely useful. I have a longer review of AWStats on my wordpress blog listed below:
Atlas-Tuesday - AWStats Review
February 2nd, 2006 at 14:02
I’ve tried to get the plugin to work but unsuccessfully so far. And it is due to me not knowing exactly where my log files are stored. Can you help me find out where they are so I can get your plugin to work?
Thanks, jim
February 3rd, 2006 at 09:02
Jim, this location depends on your host. It is specified as DirData in the awstats config file, which normally is at /etc/awstats/awstats.sitename.conf.
I know that for CPanel hosts the awstats files are at /home/user/tmp/awstats/
February 13th, 2006 at 04:02
I am trying to modify your script so that it might be included on any web page that is using awstats and php. Not quite sure how to go about this but thought you might make some suggestions.
Here’s what I’ve done so far:
Placed your awstats.php file in the root of the directory my web pages are served from. I am in a shared hosting environment (dreamhost) so awstats is installed directly in that same folder.
Inside your file I updated the location to the $awstats_dir (in this case the folder ‘aw’, I also tried an absolute path like /home/me/mywebsite/aw)
Next I included the script in a test php file like and tried calling one of the functions in that same file, not sure this would work, but figured it’s a start.
So I call http://hopewell.tv/test.php and I get this error:
Fatal error: Call to undefined function: get_settings() in /home/me/hopewell.tv/awstats.php on line 14
Any suggestions? Am I on the right path? Not sure why it can’t read a function that is defined just below it. Perhaps Wordpress has some kind of extra support for plug-ins that you are building on?
Any help would be greatly appreciated.
Thanks in advance,
Lyndon
February 13th, 2006 at 09:02
Lyndon, I cannot judge how useful this would be, as it currently is, this plugin is heavily bound to WordPress.
Some things you should adapt:
1. Set awstats_site to your site name (hostname), then you do not need awstats_getsite and get_settings (which is a WordPress function).
2. Adapt awstats_read_history to determine your own year/month numbers, they are currently determined based on WordPress parameters.
3. Adapt awstats_popular to do some other transform on the URLs, it now translates URLs back to WordPress posts.
On first sight, the other functions look independent of WordPress, good luck!
February 14th, 2006 at 06:02
Thanks, that’s a big help. I’m going to work on it more soon and will let you know how it goes.
I think I will focus on a couple of the simpler functions and get those going first.
Lyndon.
February 26th, 2006 at 08:02
[…] Popular: This could be a good place to start with for new visitors. Also regular readers can see which articles are actually getting more attention in this blog. And for myself this gives me a better idea about what my readers are finding interesting. I’m using a very cool Awstats plug-in to show the popular posts. This plug-in utilizes the data from web server log analyzer Awstats which I’ve been using for a long time. […]
March 11th, 2006 at 23:03
Does this plugin work with WP 2.0.1?
March 14th, 2006 at 19:03
I’ve tested it in WP 2.0.2, and it doesn’t work there. Haven’t investigated the issue further yet.
March 16th, 2006 at 19:03
Running WP 2.0.1 and it works great! Thanks for a great plugin! You can see the popular posts running on my homepage http://krazykoolkrap.com
March 22nd, 2006 at 19:03
Hello, my blog is http://cinexl.net/blog, and I’d like to apply to my blog the awstat plugin, but I couldn’t. I made all that steps, and I edited the top of the document to:
$awstats_dir = ‘/var/lib/awstats’;
$awstats_site = awstats_getsite(get_settings(’http://cinexl.net/blog’));
(I don’t know if the first line of that code is to change (?!?!))
And when I apply the codes to the pages, nothing appear, someone can help me?
Thanks
March 23rd, 2006 at 08:03
Valter, you should just leave the seconds line as it was:
$awstats_site = awstats_getsite(get_settings(’siteurl’));
Unless that does not work, but then you should specify the hostname as it appears in the awstats logfiles, e.g.
$awstats_site = ‘cinexl.net’;
It all depends on how you have awstats set up.
March 23rd, 2006 at 17:03
Sorry, but I think I mistaked the step nr 3 “If necessary change the few lines at the top of the file to reflect the location of your awstats log files and the site string in the log filenames.”
Didn’t get, what is necessary to do.
It’s needed to do something else after this?
Thanks for the help
March 27th, 2006 at 10:03
Curioso, can’t you help me?
March 30th, 2006 at 17:03
Valter, he already told you that it depends on your setup, for example: if you are on a shared hosting plan using cpanel, then your awstats logs are in tmp/awstats/awstats*.txt
and if in addition you’re on a subdomain, meaning you cant access tmp, then you will have to write a cron script to copy the log files to your location(my case)
btw: i modified the browser function to work with OS:
function awstats_os ($title = '') { $abb = array ( /* Simple version */ 'win' => 'Windows', 'linux' => 'Linux', 'freebsd' => 'FreeBSD', 'openbsd' => 'OpenBSD', 'netbsd' => 'NetBSD', 'mac' => 'MacOS', 'Unknown' => 'Unknown', '' => 'Other' ...(the same as the browser() )and changed this: instead of , cause it wasnt validating
arsort($stats, SORT_NUMERIC); $r = $title ? \"<strong>$title</strong>\" : \"\"; $r .= \"\n\"; foreach ($stats as $key => $val) { $percent = round(100 * $val / $total); $r .= \"$key ($percent%) [$val hits]\n\"; } $r .= $title ? \"\n\" : \"\n\"; echo $r;March 30th, 2006 at 17:03
damn, just change div instead of li on $r = $title ? line
April 1st, 2006 at 02:04
Ran into a problem. Here’s the message I got after editing awstat.php:
Here’s the editing done. (No, I have no idea how lines are counted.
)
April 1st, 2006 at 02:04
Update: Corrected my “spelling”.
(Remember children, it’s a semi-colon, not a colon.
)
August 5th, 2006 at 23:08
It doesnt work for me.
WP2.0.4
Altered the path to /var/log/httpd/access_log*
nothing shows.
Im using clarkconnect 3.2 with awstats.
As i read through your code i notice the plugin looks for a txt file as log? is this correct?
My log files are named access_log, access_log.1 etc etc
Thanks
August 7th, 2006 at 07:08
This plugin does not use the httpd log file, but the awstats summary files that awstats creates for each month. Where these are located depends on how awstats is setup.
August 8th, 2006 at 09:08
ahh.. hmm i was looking for the wrong files then.
ill try and find the right files soon.
thanks
August 8th, 2006 at 10:08
Hi there, me again. I got it working with the right files not.
Also wrote a small function to show this months visitor counters:
function awstats_general ($title = '') {$list = awstats_read_section('GENERAL');
if (!$list || count($list) == 0) return;
$n = 0;
$r = $title ? "$title" : "";
$r .= "\n";
list($pageviews, $uniques) = array_slice($list, 4, 5);
$r .= "Unique visits: $uniques[1]\n";
$r .= "Pageviews: $pageviews[1]\n";
$r .= "\n";
echo $r;
}
http://www.sothq.net/statistics/
August 25th, 2006 at 15:08
[…] I have started to use AWStats offered by my host, and I am using the AWStats Wordpress plugin by Curioso! to extract and display the popular posts. I will be testing it for a while in tandem with Google Analytics that I have already started to use. Feel free to point out any discrepancies that you see. asidesasidesLiked it? Tell othersThese icons link to social bookmarking sites where readers can share and discover new web pages. […]
September 5th, 2006 at 21:09
Get errors:
1. On clicking “Activate”, I get a white page with this:
Warning: Cannot modify header information - headers already sent by (output started at /home/ozdesi/public_html/passionforcinema-com/wp-content/plugins/awstats.php:224) in /home/ozdesi/public_html/passionforcinema-com/wp-includes/pluggable-functions.php on line 272
2. Next I clicked the back button. (blank page with error -> back to plugin page where I clicked the “Activate” link)… Refresh the plugin page. See this on top of the page:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/ozdesi/public_html/passionforcinema-com/wp-content/plugins/awstats.php:224) in /home/ozdesi/public_html/passionforcinema-com/wp-content/plugins/dd-formmailer-plugin.php on line 2
3. Now I try clicking “Deactivate”. I’m sent to a blank page with this message:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/ozdesi/public_html/passionforcinema-com/wp-content/plugins/awstats.php:224) in /home/ozdesi/public_html/passionforcinema-com/wp-content/plugins/dd-formmailer-plugin.php on line 2
Warning: Cannot modify header information - headers already sent by (output started at /home/ozdesi/public_html/passionforcinema-com/wp-content/plugins/awstats.php:224) in /home/ozdesi/public_html/passionforcinema-com/wp-includes/pluggable-functions.php on line 272
September 16th, 2006 at 14:09
Hi there,
I’m using Wordpress 2.0.4 on a cPanel web host. Here are the details:
awstats.php
$awstats_dir = ‘/home/saima/tmp/awstats’;
$awstats_site = awstats_getsite(get_settings(’siteurl’));
I have echoed $awstats_site. It returns the domain name which I believe is correct.
I have entered the following in my sidebar.php file
The only thing that appears is “Popular: “. Any ideas how I can get this working? I would like to have it.
September 16th, 2006 at 15:09
I have it working now. I had to give both Read and Execute permissions for World+Others for the ‘tmp’ and ‘awstats’ folders.
September 27th, 2006 at 19:09
Hey. How about adding “Average hits per day/month” statistics and things like that? ^^
September 27th, 2006 at 23:09
Here is some additional code for “average per dag hits/pages” and monthly stats.
function awstats_barczyk ($what = '') {// by Mille
// "number of visits - total/average"
// "pages - total/average"
$list = awstats_read_section('DAY');
if (!$list || count($list) == 0) return;
$total_pages = 0;
$total_visits = 0;
foreach ($list as $line) {
$total_pages += array_slice($line, 2, 1);
$total_visits += array_slice($line, -1);
}
// what's requested?
if ($what == 'total_visits')
return $total_visits;
elseif ($what == 'total_pageviews')
return $total_visits;
elseif ($what == 'avg_visits')
return ( $total_pages / sizeof($list) );
elseif ($what == 'avg_pageviews')
return ( $total_visits / sizeof($list) );
}
if (!isset($wpdb->posts)) {
$wpdb->posts = $table_prefix . 'posts';
$wpdb->users = $table_prefix . 'users';
$wpdb->categories = $table_prefix . 'categories';
$wpdb->post2cat = $table_prefix . 'post2cat';
$wpdb->comments = $table_prefix . 'comments';
$wpdb->links = $table_prefix . 'links';
$wpdb->linkcategories = $table_prefix . 'linkcategories';
$wpdb->options = $table_prefix . 'options';
$wpdb->optiontypes = $table_prefix . 'optiontypes';
$wpdb->optionvalues = $table_prefix . 'optionvalues';
$wpdb->postmeta = $table_prefix . 'postmeta';
}
awstats_read_history();
add_action('awstats_browsers', 'awstats_browsers', 10, 1);
add_action('awstats_popular', 'awstats_popular', 10, 2);
add_action('awstats_referrers', 'awstats_referrers', 10, 2);
add_action('awstats_searchwords', 'awstats_searchwords', 10, 2);
// added by Mille
add_action('awstats_barczyk', 'awstats_barczyk', 10, 1);
——————
to present them use:
September 27th, 2006 at 23:09
My last post didnt worked out pretty well, here is a txt-file instead:
http://mj.barczyk.se/blog/wp-content/additional.txt
September 30th, 2006 at 04:09
It doesn’t seem to work for me somehow…..the plugin never showed up for me to activate it…dont’ know why.
Does anyone have complete installation guide for these plugins: awstats and awstatstotals?
Thanks,
October 14th, 2006 at 06:10
Awesome plugin! Exactly what I’ve been looking for
Just one thing, the plugin never worked for me originally because my url always has a www. in front (WWW Redirect plugin) so when it was trying to get the log file it never worked.
I just had to do a quick fix in the $path bit in the plugin and manually enter the url, other than that, works exactly as promised!
October 15th, 2006 at 04:10
[…] I just finished putting together a decent statistics page tonight. The page was been put together using a combination of Mint, Awstats, a corresponding Awstats wordpress plugin, Sparkstats, and a combination of three different plugins from GaMerZ (WP-Stats, WP-UserOnline, WP-PostViews). […]
November 5th, 2006 at 22:11
[…] Awstats Plugin (http://www.curioso.org/2005/04/05/awstats-plugin/) […]
November 20th, 2006 at 08:11
Mickey, I added the function and tried using both total and average page views - but get an error:
Fatal error: Unsupported operand types in /home/public_html/passionforcinema-com/wp-content/plugins/awstats.php on line 217
Line 217 points to this line:
216: foreach ($list as $line) {
217: $total_pages += array_slice($line, 2, 1);
218: $total_visits += array_slice($line, -1);
219: }
February 12th, 2007 at 15:02
Hi ,
the check_comment function got nastier,don’t know if it s php or wordpress itself that causes the issue either way you must pass all the variables to it.
awstats.php line 154, there’s an if { something } , that something should be check_comment('’, ‘’, $u, ‘’, ‘’, ‘’,'’) rather than check_comment('’, ‘’, $u, ‘’, ‘’)
February 21st, 2007 at 08:02
[…] http://www.curioso.org/2005/04/05/awstats-plugin/ […]
March 29th, 2007 at 10:03
I’ve hacked Barczyk’s code addition to work properly and modified it to work properly with php4 (that array_slice is returning an array and needs to become an integer)
I’ll post an entire code update on my wordpress.com blog over the weekend once I add a few more enhancements.
Great work on this plugin, lots of great things we can tap into now with it like visits per country, top 25 countries, etc. without bogging down wordpress without additional stats keeping mysql stuff.
April 23rd, 2007 at 14:04
[…] Awstats Plugin This is a simple plugin that retrieves information from Awstats log files. It provides template functions for including this information on your WordPress site. […]
May 15th, 2007 at 07:05
Mad plugin! It took me a while to figure out as nothing was being displayed. I changed both the default values and now it works a treat.
$awstats_dir = ‘/home/khamtran/tmp/awstats’;
$awstats_site = ‘khamtran.com’;
I’m running cPanel and WordPress version 2.1.3
May 15th, 2007 at 07:05
I’m also wanting not to display the count “(X)” at the end of each post in the lists. How can I do that?
June 7th, 2007 at 18:06
Hey CK, you manage to post Barczyk’s code yet?
June 29th, 2007 at 11:06
Thanks for the free plugin. It’s exactly what I want to display my host Powweb’s awstats.
June 29th, 2007 at 12:06
I instructed my host Powweb to send Awstats log files which arrived in the form of a gz file. access_log_20070629.gz.
I tried every combination of amendment to the Awstats plugin file:
$awstats_dir = ‘/’;
$awstats_site = ‘holditupforridicule.com’;
and,
$awstats_dir = ‘/’;
$awstats_site = awstats_getsite(get_settings(’siteurl’));
and,
$awstats_dir = ‘/access_log_20070629.gz’;
$awstats_site = awstats_getsite(get_settings(’siteurl’));
and so on. I want browser stats displayed in my sidebar using:
Browsers:
I just get the text Browsers: every time:
I guess access_log_20070629.gz isn’t the correct Awstat summary file I need to use the plugin. Any idea how I get the summary file I need? Or is it something else I’m doing wrong?
June 29th, 2007 at 14:06
Normally awstats log files are named like awstats062007.www.curioso.org.txt, where 06 is the month, 2007 is the year, and www.curioso.org is the name of the site. There is one file generated for each month. On my server they are stored in /var/lib/awstats.
The contents looks like:
AWSTATS DATA FILE 6.5 (build 1.857)
# If you remove this file, all statistics for date 200706 will be lost/reset.
# Position (offset in bytes) in this file of beginning of each section for
# direct I/O access. If you made changes somewhere in this file, you should
# also remove completely the MAP section (AWStats will rewrite it at next
# update).
BEGIN_MAP 27
POS_GENERAL 1864
POS_TIME 2548
POS_VISITOR 76701
POS_DAY 346798
POS_DOMAIN 3570
etc.
access_log_20070629.gz looks more like a compressed raw web server log file.
awstats.pl processes such files and generates the monthly files I described above.
June 29th, 2007 at 16:06
Thanks for the reply. Gz are Gzip files which require Winzip ($25). Powweb are pretty inflexible about downloading the files I need in Gzip. I don’t even know if I have the right files off them yet. Maybe there’s away of generating my own Awstats Summary File. I’m pretty sure there is.
July 19th, 2007 at 03:07
hello. I use your plugin for one of my blog and it’s so cool. but btw, i want to know how to show the monthlly and total hit? thanks for your help
March 10th, 2008 at 00:03
Hi Curioso,
I have found a bug in your plugin, if the file is
AWSTATS DATA FILE 6.5 (build 1.857)
# If you remove this file, all statistics for date 200706 will be lost/reset.
# Position (offset in bytes) in this file of beginning of each section for
# direct I/O access. If you made changes somewhere in this file, you should
# also remove completely the MAP section (AWStats will rewrite it at next
# update).
BEGIN_MAP 27
POS_GENERAL 1864
POS_TIME 2548
etc, etc
you must use the offset for found the correct section. You found the section serching, for example, the words “BEGIN_SIDER” and the words “END_SIDER” that they can into other section.
For example:
AWSTATS DATA FILE 6.5 (build 1.857)
BEGIN_MAP 2
POS_AAA xxx
POS_BBB yyy
END_MAP
BEGIN_AAA 2
BEGIN_BBB.html gfsdgsdfgh ghgfhf
END_BBB.html fdghhdgfhfd hfghf
END_AAA
BEGIN_BBB 2
wqerqwe
sfsafsad
END_BBB
I think the plugin cant found the correct BBB section.
The bug is in this serching:
preg_match(’/\nBEGIN_’.$section.’ \d+\n(.*)\nEND_’.$section.’\n/s’,
$awstats_text, $match)
do you agree?