From: garvinhicking Date: Sun, 21 Aug 2005 15:54:24 +0000 (+0000) Subject: Better and individual statistical tracking (optional) by Frederik Sandberg. X-Git-Tag: 0.9~193 X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=eff8c3d43dd2f3e7b27867ea98dc3b7f8676007e;p=s9y.git Better and individual statistical tracking (optional) by Frederik Sandberg. Thanks a lot! --- diff --git a/docs/NEWS b/docs/NEWS index ce6839d..2a40241 100644 --- a/docs/NEWS +++ b/docs/NEWS @@ -3,6 +3,9 @@ Version 0.9 () ------------------------------------------------------------------------ + * Updated statistics plugin to track seperate visitor/referrer statistics. + Patch by Fredrik Sandberg, thanks a lot! (garvinhicking) + * Make category selector be nicer to Opera. Load the handling functions only after the DOM load is completed. Add new "addLoadEvent" functionality for future use to stack onload events. (garvinhicking) diff --git a/plugins/serendipity_event_statistics/serendipity_event_statistics.php b/plugins/serendipity_event_statistics/serendipity_event_statistics.php index 52999e9..9d47e74 100644 --- a/plugins/serendipity_event_statistics/serendipity_event_statistics.php +++ b/plugins/serendipity_event_statistics/serendipity_event_statistics.php @@ -7,7 +7,7 @@ if (file_exists($probelang)) { } @define('PLUGIN_EVENT_STATISTICS_NAME', 'Statistics'); -@define('PLUGIN_EVENT_STATISTICS_DESC', 'Adds a link to interesting statistics in your entries panel'); +@define('PLUGIN_EVENT_STATISTICS_DESC', 'Adds a link to interesting statistics in your entries panel, including a visitors counter'); @define('PLUGIN_EVENT_STATISTICS_OUT_STATISTICS', 'Statistics'); @define('PLUGIN_EVENT_STATISTICS_OUT_FIRST_ENTRY', 'First entry'); @define('PLUGIN_EVENT_STATISTICS_OUT_LAST_ENTRY', 'Last entry'); @@ -55,6 +55,24 @@ if (file_exists($probelang)) { @define('PLUGIN_EVENT_STATISTICS_MAX_ITEMS', 'Maximum items'); @define('PLUGIN_EVENT_STATISTICS_MAX_ITEMS_DESC', 'How many items to display per statistical value? (Default: 20)'); +//Language constants for the Extended Visitors feature +@define('PLUGIN_EVENT_STATISTICS_EXT_ADD', 'Extended Visitors Statistics'); +@define('PLUGIN_EVENT_STATISTICS_EXT_ADD_DESC', 'Add Extended Visitors Statistics feature? (default: no)'); +@define('PLUGIN_EVENT_STATISTICS_EXT_OPT1', 'No!'); +@define('PLUGIN_EVENT_STATISTICS_EXT_OPT2', 'Yes, at the bottom of the page'); +@define('PLUGIN_EVENT_STATISTICS_EXT_OPT3', 'Yes, at the top of the page'); +@define('PLUGIN_EVENT_STATISTICS_EXT_VISITORS', 'Nr of vistors'); +@define('PLUGIN_EVENT_STATISTICS_EXT_VISTODAY', 'Nr of vistors today'); +@define('PLUGIN_EVENT_STATISTICS_EXT_VISTOTAL', 'Total nr of vistors'); +@define('PLUGIN_EVENT_STATISTICS_EXT_VISSINCE', 'The extended visitor“s statistic feature has collected data since'); +@define('PLUGIN_EVENT_STATISTICS_EXT_VISLATEST', 'Latest Visitors'); +@define('PLUGIN_EVENT_STATISTICS_EXT_TOPREFS', 'Top Referrers'); +@define('PLUGIN_EVENT_STATISTICS_EXT_TOPREFS_NONE', 'No referrers has yet been registered.'); +@define('PLUGIN_EVENT_STATISTICS_OUT_EXT_STATISTICS', 'Extended Visitor Statistics'); +@define('PLUGIN_EVENT_STATISTICS_BANNED_HOSTS', 'Ban browsers from beeing counted'); +@define('PLUGIN_EVENT_STATISTICS_BANNED_HOSTS_DESC', 'Insert browsers that should be excluded from counting, separated by "|"'); + + class serendipity_event_statistics extends serendipity_event { var $title = PLUGIN_EVENT_STATISTICS_NAME; @@ -66,8 +84,8 @@ class serendipity_event_statistics extends serendipity_event $propbag->add('name', PLUGIN_EVENT_STATISTICS_NAME); $propbag->add('description', PLUGIN_EVENT_STATISTICS_DESC); $propbag->add('stackable', false); - $propbag->add('author', 'Garvin Hicking'); - $propbag->add('version', '1.1'); + $propbag->add('author', 'Garvin Hicking, Fredrik Sandberg'); + $propbag->add('version', '1.21'); $propbag->add('requirements', array( 'serendipity' => '0.8', 'smarty' => '2.6.7', @@ -76,10 +94,11 @@ class serendipity_event_statistics extends serendipity_event $propbag->add('groups', array('STATISTICS')); $propbag->add('event_hooks', array( 'backend_sidebar_entries' => true, - 'backend_sidebar_entries_event_display_statistics' => true + 'backend_sidebar_entries_event_display_statistics' => true, + 'frontend_configure' => true )); - $propbag->add('configuration', array('max_items')); + $propbag->add('configuration', array('max_items', 'ext_vis_stat','banned_bots')); } function introspect_config_item($name, &$propbag) @@ -91,6 +110,24 @@ class serendipity_event_statistics extends serendipity_event $propbag->add('description', PLUGIN_EVENT_STATISTICS_MAX_ITEMS_DESC); $propbag->add('default', 20); break; + + + case 'ext_vis_stat': + $select = array('no' => PLUGIN_EVENT_STATISTICS_EXT_OPT1, 'yesBot' => PLUGIN_EVENT_STATISTICS_EXT_OPT2, 'yesTop' => PLUGIN_EVENT_STATISTICS_EXT_OPT3); + $propbag->add('type', 'select'); + $propbag->add('name', PLUGIN_EVENT_STATISTICS_EXT_ADD); + $propbag->add('description', PLUGIN_EVENT_STATISTICS_EXT_ADD_DESC); + $propbag->add('select_values', $select); + $propbag->add('default', 'no'); + + break; + + case 'banned_bots': + $propbag->add('type', 'string'); + $propbag->add('name', PLUGIN_EVENT_STATISTICS_BANNED_HOSTS); + $propbag->add('description', PLUGIN_EVENT_STATISTICS_BANNED_HOSTS_DESC); + $propbag->add('default', 'msnbot.msn.com'); + break; } return true; @@ -107,6 +144,37 @@ class serendipity_event_statistics extends serendipity_event if (isset($hooks[$event])) { switch($event) { + + case 'frontend_configure': + if ($this->get_config('ext_vis_stat') == 'no') { + return; + } + + //checking if db tables exists, otherwise install them + $tableChecker = serendipity_db_query("SELECT counter_id FROM {$serendipity['dbPrefix']}visitors LIMIT 1", true); + if (!is_array($tableChecker)){ + $this->createTables(); + } + + //Unique visitors are beeing registered and counted here. Calling function below. + $sessionChecker = serendipity_db_query("SELECT count(sessID) FROM {$serendipity['dbPrefix']}visitors WHERE '".session_id()."' = sessID GROUP BY sessID", true); + if ((is_array($sessionChecker)) && ($sessionChecker[0] == 0)) { + + // avoiding banned browsers + $banned_bots = $this->get_config('banned_bots'); + $tmp_array = explode('|', $banned_bots); + $found = 'no'; + for ($i=0; $icountVisitor(); + } + } + + break; case 'backend_sidebar_entries': ?>
  • @@ -116,11 +184,17 @@ class serendipity_event_statistics extends serendipity_event case 'backend_sidebar_entries_event_display_statistics': $max_items = $this->get_config('max_items'); + $ext_vis_stat = $this->get_config('ext_vis_stat'); if (!$max_items || !is_numeric($max_items) || $max_items < 1) { $max_items = 20; } + + if ($ext_vis_stat == 'yesTop') { + $this->extendedVisitorStatistics($max_items); + } + $first_entry = serendipity_db_query("SELECT timestamp FROM {$serendipity['dbPrefix']}entries ORDER BY timestamp ASC limit 1", true); $last_entry = serendipity_db_query("SELECT timestamp FROM {$serendipity['dbPrefix']}entries ORDER BY timestamp DESC limit 1", true); $total_count = serendipity_db_query("SELECT count(id) FROM {$serendipity['dbPrefix']}entries", true); @@ -421,7 +495,13 @@ class serendipity_event_statistics extends serendipity_event
    $max_items)); ?> -extendedVisitorStatistics($max_items); + } + return true; break; @@ -433,7 +513,197 @@ class serendipity_event_statistics extends serendipity_event return false; } } + + function countVisitor(){ + + global $serendipity; + + $referer = $_SERVER['HTTP_REFERER']; + $values = array( + 'sessID' => session_id(), + 'day' => date('Y-m-d'), + 'time' => date('H:i'), + 'ref' => strip_tags($referer), + 'browser'=> strip_tags($_SERVER['HTTP_USER_AGENT']), + 'ip' => strip_tags($_SERVER['REMOTE_ADDR']) + ); + + serendipity_db_insert('visitors', $values); + + // updating the referrer-table + if (strlen($referer) >= 1) { + + //retrieving the referrer base URL + $temp_array = explode('?', $referer); + $urlA = $temp_array[0]; + + //removing "http://" & trailing subdirectories + $temp_array3 = explode('//', $urlA); + $urlB = $temp_array3[1]; + $temp_array4 = explode('/', $urlB); + $urlB = $temp_array4[0]; + + //removing www + $urlC = serendipity_db_escape_string(str_replace('www.', '', $urlB)); + + //updating db + $q = serendipity_db_query("SELECT count(refs) FROM {$serendipity['dbPrefix']}refs WHERE refs = '$urlC' GROUP BY refs", true); + if (!is_array($q) || $q[0] >= 1){ + serendipity_db_query("UPDATE {$serendipity['dbPrefix']}refs SET count=count+1 WHERE (refs = '$urlC')"); + } else { + serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}refs (refs, count) VALUES ('$urlC', 1)"); + } + } + + } //end of function countVisitor + + + function extendedVisitorStatistics($max_items){ + + global $serendipity; + + // ---------------QUERIES for Viewing statistics ---------------------------------------------- + $day = date('Y-m-d'); + $visitors_count_today = serendipity_db_query("SELECT count(counter_id) FROM {$serendipity['dbPrefix']}visitors WHERE day = '".$day."'", true); + $visitors_count_firstday = serendipity_db_query("SELECT day FROM {$serendipity['dbPrefix']}visitors ORDER BY counter_id ASC LIMIT 1", true); + $visitors_count = serendipity_db_query("SELECT count(counter_id) FROM {$serendipity['dbPrefix']}visitors", true); + $visitors_latest = serendipity_db_query("SELECT counter_id, day, time, ref, browser, ip FROM {$serendipity['dbPrefix']}visitors ORDER BY counter_id DESC LIMIT ".$max_items.""); + $top_refs = serendipity_db_query("SELECT refs, count FROM {$serendipity['dbPrefix']}refs ORDER BY count DESC LIMIT ".$max_items.""); + + // ---------------STYLES for Viewing statistics ---------------------------------------------- + echo ""; + + ?> +

    +
    +
    +


    \n"?>
    + +
    +
    +
    + ".$visitors_count_today[0]."
    \n"; + echo PLUGIN_EVENT_STATISTICS_EXT_VISTOTAL.": ".$visitors_count[0]."
    \n"; + ?> +
    +
    +
    + +
    + +
    +
    + + $row) { + if ($color == "col1"){$color ="col2";}else{$color ="col1";} + echo ""; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + } + } + ?> +
    ".$row['day']." ".$row['time']."".wordwrap($row['ref'], 25, "\n", 1)."".wordwrap($row['browser'], 25, "\n", 1).""; + if ($row['ip']){ + echo wordwrap(gethostbyaddr($row['ip']), 25, "\n", 1); + } else { + echo "--"; + } + echo "

    +
    +
    + +
    +
    + +
    +
    + + $row) { + if ($color == "col4"){$color ="col5";}else{$color ="col4";} + echo ""; + } + } else { + echo PLUGIN_EVENT_STATISTICS_EXT_TOPREFS_NONE; + } + ?> +
    ".$i++.". ".$row['refs']." (".$row['count'].")
    +
    +
    +
    + createTables(); + + } + + function uninstall(){ + + $this->dropTables(); + + } + } /* vim: set sts=4 ts=4 expandtab : */ -?>