Search Meter Statistics page to see what your visitors are searching for on your blog.
Version: 1.1
Author: Bennett McElwee
Author URI: http://www.thunderguy.com/semicolon/
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.
TEMPLATE TAGS
sm_list_popular_searches()
Show a list of the search terms that have produced hits at your site during the last 30 days.
Readers can click the search term to repeat the search.
sm_list_popular_searches('
Popular Searches
')
Show the list as above, with the heading "popular Searches". If there have been no searches,
then this tag displays nothing.
sm_list_popular_searches('Popular Searches
', '')
Show the headed list as above; this form of the tag should be used in the default WordPress theme.
Put it in the sidebar.php file.
Thanks to Kaufman (http://www.terrik.com/wordpress/) for valuable coding suggestions.
Copyright (C) 2005 Bennett McElwee (bennett at thunderguy dotcom)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
The license is also available at http://www.gnu.org/copyleft/gpl.html
*/
// Template Tags
function sm_list_popular_searches($before = '', $after = '') {
global $wpdb, $table_prefix;
/* This is a simpler query that 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 5");
if (count($results)) {
echo "$before\n\n$after\n";
}
}
/* Use a "the_posts" filter to check if it's a search, and to count
the number of hits. The filter doesn't make any changes.
*/
function tguy_sm_save_search(&$posts) {
global $wp_query, $table_prefix;
if ($wp_query->is_search && ! $wp_query->is_admin) {
$search_string = $wp_query->query_vars['s'];
if (!get_magic_quotes_gpc()) {
$search_string = addslashes($search_string);
}
$search_string = preg_replace('/, +/', ' ', $search_string);
$search_string = str_replace(',', ' ', $search_string);
$search_string = str_replace('"', ' ', $search_string);
$search_string = trim($search_string);
$hit_count = count($posts);
// Save them 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_string',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_string' AND `date` = CURDATE()";
$success = mysql_query($query);
}
}
return $posts;
}
function tguy_sm_init() {
// Create the table if it's not already there
global $table_prefix;
if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
$result = mysql_list_tables(DB_NAME);
$tables = array();
while ($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
if (!in_array($table_prefix.'searchmeter', $tables)) {
tguy_sm_install();
}
}
}
function tguy_sm_install() {
global $wpdb, $table_prefix;
$result = mysql_query("
CREATE TABLE `{$table_prefix}searchmeter`
(
`terms` VARCHAR(50) NOT NULL,
`date` DATE NOT NULL,
`count` INT(11) NOT NULL,
`last_hits` INT(11) NOT NULL,
PRIMARY KEY (`terms`,`date`)
)
") or die(mysql_error());
if (!$result) {
return false;
}
return true;
}
function tguy_sm_stats_css() {
?>
get_results(
"SELECT `terms`,
SUM( `count` ) AS countsum,
SUBSTRING( MAX( CONCAT( `date` , ' ', `last_hits` ) ) , 12 ) AS hits
FROM `{$table_prefix}searchmeter`
WHERE DATE_SUB( CURDATE( ) , INTERVAL $days DAY ) <= `date`
GROUP BY `terms`
$hits_selector
ORDER BY countsum DESC, `terms` ASC
LIMIT 20");
if (count($results)) {
?>
| Termino | Busquedas |
Resultados |
| terms) ?> |
countsum ?> |
hits ?> |
No hubo busquedas durante este periodo.
query(
"DELETE FROM `{$table_prefix}searchmeter`
WHERE `date` < DATE_SUB( CURDATE() , INTERVAL 30 DAY)");
echo "\n";
?>
Todas las Busquedas
Estas tablas muestran las busquedas mas populares en los periodos determinados. Termino es el texto buscado; Busquedas es el numero de veces que el Termino fue buscado; y Resultados es el numero de articulos devueltos por la ultima busqueda para ese termino.
Ayer y Hoy
Ultimos 7 Dias
Ultimos 30 Dias
Busquedas Fracasadas
Estas tablas muestran las busquedas realizadas que no devolvieron resultados. Alguien ha buscado estos terminos en el blog; quiza deberias darles lo que quieren.
Ayer y Hoy
Ultimos 7 Dias
Ultimos 30 Dias
Notas
Para reiniciar las estadisticas, ves a la Pagina de Opciones de Search Meter Options.
Para informacion y actualizaciones, visita la pagina de Search Meter. En la pagina podras hacer sugerencias, pedir nuevas caracteristicas o informar sobre problemas.
Reiniciar Estadisticas
Pulsa este boton para reiniciar todas las estadisticas. Esto eliminara toda la informacion de las busquedas anteriores.
Notas
Para ver las estadisticas ves a la Pagina de Estadistica de Search Meter.
Para informacion y actualizaciones, visita la pagina de Search Meter. En la pagina podras hacer sugerencias, pedir nuevas caracteristicas o informar sobre problemas.
query("DELETE FROM `{$table_prefix}searchmeter`");
header('Location: '.get_bloginfo('wpurl').'/wp-admin/options-general.php?page=search-meter.php&updated=true');
exit();
}
if (function_exists('add_action') && function_exists('add_filter')) {
add_action('init', 'tguy_sm_init');
add_filter('the_posts', 'tguy_sm_save_search', 20); // run after other plugins
add_action('admin_head', 'tguy_sm_stats_css');
add_action('admin_menu', 'tguy_sm_add_admin_pages');
}