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

  1. Download the zip file.
  2. Drop the awstats.php file in your plugin directory.
  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.
  4. 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.

59 Responses to “Awstats plugin”

  1. Steve says:

    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 2

  2. Steve says:

    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?

  3. Leon says:

    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.

  4. Curioso says:

    Leon, thanks!
    Fixed.

  5. Steve says:

    Curioso - any ideas about my 2 posts? regarding showing only x categories and the plugin not working

  6. Curioso says:

    Steve, I have no immediate plans to add this, but I will keep it in mind

  7. Lucas says:

    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

  8. Curioso says:

    As far as I know, awstats strips off the ?-part

  9. Twisted world of Esden says:

    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 …

  10. char says:

    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!

  11. David says:

    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?

  12. Curioso says:

    David, maybe you can use AWstats totals, see http://www.telartis.nl/xcms/awstats

  13. Dustin says:

    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

  14. Jim says:

    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

  15. Curioso says:

    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/

  16. Lyndon says:

    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

  17. Curioso says:

    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!

  18. Lyndon says:

    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.

  19. Layout Changes and new plug-ins: Nirlog.com says:

    […] 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. […]

  20. groovehouse says:

    Does this plugin work with WP 2.0.1?

  21. Jakob Kruse says:

    I’ve tested it in WP 2.0.2, and it doesn’t work there. Haven’t investigated the issue further yet.

  22. Steve says:

    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

  23. Valter Nepomuceno says:

    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

  24. Curioso says:

    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.

  25. Valter Nepomuceno says:

    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 ;)

  26. Valter Nepomuceno says:

    Curioso, can’t you help me?

  27. joedoe says:

    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;
    
  28. joedoe says:

    damn, just change div instead of li on $r = $title ? line

  29. Alan Kellogg says:

    Ran into a problem. Here’s the message I got after editing awstat.php:

    Parse error: syntax error, unexpected ‘:’ in /home/mythmag/public_html/wp/wp-content/plugins/awstats.php on line 14

    Here’s the editing done. (No, I have no idea how lines are counted. :) )

    It is a shared server.

  30. Alan Kellogg says:

    Update: Corrected my “spelling”. :)

    (Remember children, it’s a semi-colon, not a colon. :D )

  31. SHRIKEE says:

    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

  32. Curioso says:

    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.

  33. SHRIKEE says:

    ahh.. hmm i was looking for the wrong files then.
    thanks :) ill try and find the right files soon.

  34. SHRIKEE says:

    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/

  35. iface thoughts » Blog Archive » Using AWStats says:

    […] 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. […]

  36. oz says:

    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

  37. Saima says:

    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.

  38. Saima says:

    I have it working now. I had to give both Read and Execute permissions for World+Others for the ‘tmp’ and ‘awstats’ folders.

  39. Mickey J Barczyk says:

    Hey. How about adding “Average hits per day/month” statistics and things like that? ^^

  40. Mickey J Barczyk says:

    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:

  41. Mickey J Barczyk says:

    My last post didnt worked out pretty well, here is a txt-file instead:
    http://mj.barczyk.se/blog/wp-content/additional.txt

  42. Marcus says:

    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,

  43. Ash Haque says:

    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!

  44. New Statistics Page - Bball City says:

    […] 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). […]

  45. Wordpress Plugins « TimeSavingExpert.com says:

    […] Awstats Plugin (http://www.curioso.org/2005/04/05/awstats-plugin/)  […]

  46. oz says:

    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: }

  47. Gas says:

    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, ‘’, ‘’)

  48. Wordpress Plugins Themes Download » Blog Archive » Awstats says:

    […] http://www.curioso.org/2005/04/05/awstats-plugin/ […]

  49. _ck_ says:

    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.

  50. Die besten WordPress Plugins - Statistik » Tipps zur Suchmaschinenoptimierung says:

    […] 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. […]

  51. Kham Tran says:

    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

  52. Kham Tran says:

    I’m also wanting not to display the count “(X)” at the end of each post in the lists. How can I do that?

  53. Spadge says:

    Hey CK, you manage to post Barczyk’s code yet?

  54. Ian Smith says:

    Thanks for the free plugin. It’s exactly what I want to display my host Powweb’s awstats.

  55. Ian Smith says:

    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?

  56. Curioso says:

    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.

  57. Ian Smith says:

    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.

  58. ivie says:

    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

  59. Sandro kensan says:

    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?

Leave a Reply