Search Meter Statistics page to see what your visitors are searching for on your blog. Version: 2.3 Author: Bennett McElwee Author URI: http://www.thunderguy.com/semicolon/ $Revision: 109 $ INSTRUCTIONS 1. Copy this file into the plugins directory in your WordPress installation (wp-content/plugins). 2. Log in to WordPress administration. Go to the Plugins page and Activate this plugin. * To see search statistics, log in to WordPress Admin, go to the Dashboard page and click Search Meter. * To control search statistics, log in to WordPress Admin, go to the Options page and click Search Meter. * To display recent and popular searches, use the Recent Searches and Popular Searches widgets, or the sm_list_popular_searches() and sm_list_recent_searches() template tags. * For full details, see http://www.thunderguy.com/semicolon/wordpress/search-meter-wordpress-plugin/ Thanks to Kaufman (http://www.terrik.com/wordpress/) and the many others who have for valuable coding suggestions. The many other users who have offered suggestions. Copyright (C) 2005-07 Bennett McElwee (bennett at thunderguy dotcom) This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details, available at http://www.gnu.org/copyleft/gpl.html or by writing to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ if (!is_plugin_page()) : // Parameters (you can change these if you know what you're doing) define('TGUY_SM_HISTORY_SIZE', 500); // The number of recent searches that will be saved. The table can // contain up to 100 more rows than this number define('TGUY_SM_ALLOW_EMPTY_REFERER', false); // Searches with an empty referer header are often bogus requests // from Google's AdSense crawler or something similar, so they are // excluded. Set this to true to record all such searches. define('TGUY_SM_ALLOW_DUPLICATE_SAVES', false); // It may be that the filter gets called more than once for a given // request. Search Meter ignores these duplicates. Set this to true // to record duplicates (the fact that it's a dupe will be recorded // in the details). This will mess up the stats, but could be useful // for troubleshooting. define('TGUY_SM_STATS_CAPABILITY_LEVEL', 1); // Minimum user capability level for users to be able to see stats. define('TGUY_SM_OPTIONS_CAPABILITY_LEVEL', 10); // Minimum user capability level for users to be able to set options. // Template Tags function sm_list_popular_searches($before = '', $after = '', $count = 5) { // List the most popular searches in the last month in decreasing order of popularity. global $wpdb, $table_prefix; $count = intval($count); // This is a simpler query than the report query, and may produce // slightly different results. This query returns searches if they // have ever had any hits, even if the last search yielded no hits. // This makes for a more efficient search -- important if this // function will be used in a sidebar. $results = $wpdb->get_results( "SELECT `terms`, SUM( `count` ) AS countsum FROM `{$table_prefix}searchmeter` WHERE DATE_SUB( CURDATE( ) , INTERVAL 30 DAY ) <= `date` AND 0 < `last_hits` GROUP BY `terms` ORDER BY countsum DESC, `terms` ASC LIMIT $count"); if (count($results)) { echo "$before\n
This widget shows the most popular successful search terms in the last month.
Powered by Search Meter
Recent Searches', '', $number); } function sm_list_recent_searches_control() { $options = get_option('tguy_search_meter'); if ($_POST["recent-searches-submit"]) { $options['recent-searches-number'] = (int) $_POST["recent-searches-number"]; update_option('tguy_search_meter', $options); } $number = sm_constrain_widget_search_count((int)$options['recent-searches-number']); ?>This widget shows the most recent successful search terms.
Powered by Search Meter
query_vars['s']; if (get_magic_quotes_gpc()) { $search_string = stripslashes($search_string); } // search terms is the words in the query $search_terms = $search_string; $search_terms = preg_replace('/[," ]+/', ' ', $search_terms); $search_terms = trim($search_terms); // This actually only returns a maximum of the number of posts per page $hit_count = count($posts); // Other useful details of the search $details = ''; $options = get_option('tguy_search_meter'); if ($options['sm_details_verbose']) { if (TGUY_SM_ALLOW_DUPLICATE_SAVES) { $details .= "Search Meter action count: $tguy_sm_action_count\n"; } foreach (array('REQUEST_URI','REQUEST_METHOD','QUERY_STRING','REMOTE_ADDR','HTTP_USER_AGENT','HTTP_REFERER') as $header) { $details .= $header . ': ' . $_SERVER[$header] . "\n"; } } // Sanitise as necessary $search_string = addslashes($search_string); $search_terms = addslashes($search_terms); $details = addslashes($details); // Save the individual search to the DB $query = "INSERT INTO `{$table_prefix}searchmeter_recent` (`terms`,`datetime`,`hits`,`details`) VALUES ('$search_string',NOW(),$hit_count,'$details')"; $success = mysql_query($query); // If it failed, maybe the table was never created. // Try to create it and then try again. if (!$success) { if (tguy_sm_create_recent_table()) { $success = mysql_query($query); } } if ($success) { // Ensure table never grows larger than TGUY_SM_HISTORY_SIZE + 100 $rowcount = $wpdb->get_var( "SELECT count(`datetime`) as rowcount FROM `{$table_prefix}searchmeter_recent`"); if ((TGUY_SM_HISTORY_SIZE + 100) < $rowcount) { // find time of (TGUY_SM_HISTORY_SIZE)th entry $dateZero = $wpdb->get_var( "SELECT `datetime` FROM `{$table_prefix}searchmeter_recent` ORDER BY `datetime` DESC LIMIT ".TGUY_SM_HISTORY_SIZE.", 1"); $query = "DELETE FROM `{$table_prefix}searchmeter_recent` WHERE `datetime` < '$dateZero'"; $success = mysql_query($query); } } // Save search summary into the DB. Usually this will be a new query, so try to insert first $query = "INSERT INTO `{$table_prefix}searchmeter` (`terms`,`date`,`count`,`last_hits`) VALUES ('$search_terms',CURDATE(),1,$hit_count)"; $success = mysql_query($query); if (!$success) { $query = "UPDATE `{$table_prefix}searchmeter` SET `count` = `count` + 1, `last_hits` = $hit_count WHERE `terms` = '$search_terms' AND `date` = CURDATE()"; $success = mysql_query($query); // Table should always exist, so don't try to create again } } return $posts; } function tguy_sm_create_summary_table() { // Create the table if not already there. global $wpdb, $table_prefix; $table_name = $table_prefix . "searchmeter"; if ($wpdb->get_var("show tables like '$table_name'") != $table_name) { if (file_exists(ABSPATH . 'wp-admin/includes/upgrade.php')) { require_once(ABSPATH . '/wp-admin/includes/upgrade.php'); } else { // Wordpress 2.2 or earlier require_once(ABSPATH . 'wp-admin/upgrade-functions.php'); } dbDelta("CREATE TABLE `{$table_name}` ( `terms` VARCHAR(50) NOT NULL, `date` DATE NOT NULL, `count` INT(11) NOT NULL, `last_hits` INT(11) NOT NULL, PRIMARY KEY (`terms`,`date`) ); "); } } function tguy_sm_create_recent_table() { // Create the table if not already there. global $wpdb, $table_prefix; $table_name = $table_prefix . "searchmeter_recent"; if ($wpdb->get_var("show tables like '$table_name'") != $table_name) { if (file_exists(ABSPATH . 'wp-admin/includes/upgrade.php')) { require_once(ABSPATH . '/wp-admin/includes/upgrade.php'); } else { // Wordpress 2.2 or earlier require_once(ABSPATH . 'wp-admin/upgrade-functions.php'); } dbDelta("CREATE TABLE `{$table_name}` ( `terms` VARCHAR(50) NOT NULL, `datetime` DATETIME NOT NULL, `hits` INT(11) NOT NULL, `details` TEXT NOT NULL, KEY `datetimeindex` (`datetime`) ); "); } } function tguy_sm_reset_stats() { global $wpdb, $table_prefix; tguy_sm_create_recent_table(); // Delete all records $wpdb->query("DELETE FROM `{$table_prefix}searchmeter`"); $wpdb->query("DELETE FROM `{$table_prefix}searchmeter_recent`"); } function tguy_sm_add_admin_pages() { add_submenu_page('index.php', 'Search Meter Statistics', 'Search Meter', TGUY_SM_STATS_CAPABILITY_LEVEL, __FILE__, 'tguy_sm_stats_page'); add_options_page('Search Meter', 'Search Meter', TGUY_SM_OPTIONS_CAPABILITY_LEVEL, __FILE__, 'tguy_sm_options_page'); } // Display information function tguy_sm_stats_css() { ?> query( "DELETE FROM `{$table_prefix}searchmeter` WHERE `date` < DATE_SUB( CURDATE() , INTERVAL 30 DAY)"); echo "\n"; ?>These tables show the most popular searches on your blog for the given time periods. Term is the text that was searched for; you can click it to see which posts contain that term. (This won't be counted as another search.) Searches is the number of times the term was searched for. Results is the number of posts that were returned from the last search for that term.
These tables show only the search terms for which the last search yielded no results. People are searching your blog for these terms; maybe you should give them what they want.
To manage your search statistics, go to your Search Meter Options page.
For information and updates, see the Search Meter home page. You can also offer suggestions, request new features or report problems.
| Term | Searches | Results |
|---|---|---|
| terms) ?> | countsum ?> | hits ?> |
No searches recorded for this period.
This table shows the last searches on this blog. Term is the text that was searched for; you can click it to see which posts contain that term. (This won't be counted as another search.) Results is the number of posts that were returned from the search.
| Date & time | Term | Results | Details | Show details |
|---|---|---|---|---|
| datetime ?> | terms) ?> | hits ?> | ", htmlspecialchars($result->details)) ?> |
No searches recorded.
To manage your search statistics, go to your Search Meter Options page.
For information and updates, see the Search Meter home page. You can also offer suggestions, request new features or report problems.
Plugin settings saved.
'; } else if (isset($_POST['tguy_sm_reset'])) { check_admin_referer('search-meter-reset-stats'); tguy_sm_reset_stats(); echo 'Statistics have been reset.
Click this button to reset all search statistics. This will delete all information about previous searches.
To see your search statistics, go to your Search Meter Statistics page.
For information and updates, see the Search Meter home page. At that page, you can also offer suggestions, request new features or report problems.
Do you find this plugin useful?
Do you find this plugin useful?
I write WordPress plugins because I enjoy doing it, but it does take up a lot
of my time. If you think this plugin is useful, please consider donating some appropriate
amount by clicking here. Thank you.