actions(); $this->filters(); } function install() { global $wpdb; $table_name = $wpdb->prefix . $this->table; $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, total mediumint(9) NOT NULL DEFAULT 0, keyword varchar(250) NOT NULL, details text NULL, UNIQUE KEY id (id) );"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } function actions(){ //ACTIONS add_action( 'admin_enqueue_scripts', array(&$this, "enqueue_scripts")); add_action( "admin_menu",array(&$this, "menu_pages")); add_action( 'wp_head', array(&$this, 'head')); add_action( 'admin_head', array(&$this, 'head')); add_action( 'wp_footer', array(&$this, 'footer')); add_action( 'admin_footer', array(&$this, 'footer')); add_action( 'admin_notices', array(&$this, 'admin_notice') ); $settings = $this->get_settings(); if(intval($settings->track_live_search) == 1){ add_action( 'sf_value_results', array(&$this, 'track_live_search'), 10, 2); } } function filters(){ //FILTERS $settings = $this->get_settings(); if(intval($settings->track_search) == 1){ add_filter('posts_results', array(&$this, 'track_search'), 10, 2); } } function overview(){ echo apply_filters('ajaxy-overview', 'main'); } function menu_pages(){ if(!$this->menu_page_exists('ajaxy-page')){ add_menu_page( _n( 'Ajaxy', 'Ajaxy', 1, 'ajaxy' ), _n( 'Ajaxy', 'Ajaxy', 1 ), 'Ajaxy', 'ajaxy-page', array(&$this, 'overview')); } add_submenu_page( 'ajaxy-page', __('Search Tracker'), __('Search Tracker'), 'manage_options', 'ajaxy_sft_admin', array(&$this, 'admin_page')); } function menu_page_exists( $menu_slug ) { global $menu; foreach ( $menu as $i => $item ) { if ( $menu_slug == $item[2] ) { return true; } } return false; } function get_data_by_date($dateFrom, $dateTo) { global $wpdb; $timeSpan_concat = "DATE(time)"; //this query is important, so that you can view all results $general_query = $wpdb->prepare(" SELECT $timeSpan_concat AS htime, GROUP_CONCAT( details) AS details FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s GROUP BY htime", $dateFrom, $dateTo); //first query, count all unique keywords per specified time and timepsan $count_unique_query = $wpdb->prepare("SELECT * , COUNT( * ) AS count_unique FROM ( SELECT $timeSpan_concat AS dtime FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s GROUP BY dtime, keyword ) AS d GROUP BY dtime", $dateFrom, $dateTo); $count_search_query = $wpdb->prepare(" SELECT $timeSpan_concat AS stime, COUNT( * ) AS count_search FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s GROUP BY stime", $dateFrom, $dateTo); $count_zero_query = $wpdb->prepare(" SELECT count(*) as count_zero ,$timeSpan_concat AS atime FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s AND total =0 GROUP BY atime ", $dateFrom, $dateTo); $sum_total_query = $wpdb->prepare(" SELECT SUM(total) as total_results ,$timeSpan_concat AS ttime FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s GROUP BY ttime ", $dateFrom, $dateTo); $results = array(); if(isset($_GET['keyword_count'])) { if(isset($_GET['keyword'])) { $general_query = $wpdb->prepare(" SELECT $timeSpan_concat AS htime, GROUP_CONCAT( details) AS details, keyword FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s AND total =%s AND keyword =%s GROUP BY htime", $dateFrom, $dateTo, intval($_GET['keyword_count']), urldecode($_GET['keyword'])); $count_search_query = $wpdb->prepare(" SELECT $timeSpan_concat AS stime, COUNT( * ) AS count_search FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s AND total =%s AND keyword =%s GROUP BY stime", $dateFrom, $dateTo, intval($_GET['keyword_count']), urldecode($_GET['keyword'])); $sql = "SELECT DATE(htime) as date, keyword, GROUP_CONCAT( details) AS details, count_search FROM ($general_query) as gq LEFT JOIN ($count_search_query) AS shq ON gq.htime = shq.stime GROUP BY date"; $results = $wpdb->get_results($sql); } } else{ $sql = "SELECT DATE(htime) as date, GROUP_CONCAT( details) AS details, count_unique, count_search, count_zero, total_results FROM ($general_query) as gq LEFT JOIN ($count_unique_query) AS uq ON gq.htime = uq.dtime LEFT JOIN ($count_search_query) AS shq ON gq.htime = shq.stime LEFT JOIN ($count_zero_query) AS zq ON zq.atime = gq.htime LEFT JOIN ($sum_total_query) AS sq ON sq.ttime = gq.htime GROUP BY date"; $wpdb->query('SET SESSION group_concat_max_len = 1000000;'); $results = $wpdb->get_results($sql); } return $results; } function get_data_by_time($dateFrom, $dateTo, $hourFrom, $minuteFrom, $hourTo, $minuteTo, $timeSpan) { global $wpdb; $timeFrom = ($hourFrom < 10 ? '0'.$hourFrom:$hourFrom).":".($minuteFrom < 10 ? '0'.$minuteFrom:$minuteFrom); $timeTo = ($hourTo < 10 ? '0'.$hourTo:$hourTo).":".($minuteTo < 10 ? '0'.$minuteTo:$minuteTo); $timeSpan_concat = "CONCAT_WS( ' - ', CONCAT( DATE_FORMAT( TIME, '%%H' ) , ':', LPAD( DATE_FORMAT( TIME, '%%i' ) - MOD( DATE_FORMAT( TIME, '%%i' ) , $timeSpan ) , 2, '0' ) ) , CONCAT( DATE_FORMAT( TIME, '%%H' ) , ':', LPAD( DATE_FORMAT( TIME, '%%i' ) - MOD( DATE_FORMAT( TIME, '%%i' ) , $timeSpan ) + $timeSpan, 2, '0' ) ) )"; if($timeSpan > 59) { $timeSpan = $timeSpan/60; $timeSpan_concat = "CONCAT_WS( ' - ', CONCAT( LPAD(DATE_FORMAT( TIME, '%%H' ) - MOD( DATE_FORMAT( TIME, '%%H' ) , $timeSpan ), 2, '0' ) , ':00' ) , CONCAT( LPAD(DATE_FORMAT( TIME, '%%H' ) - MOD( DATE_FORMAT( TIME, '%%H' ) , $timeSpan ) + $timeSpan, 2, '0' ), ':00' ) )"; } //this query is important, so that you can view all results $general_query = $wpdb->prepare(" SELECT $timeSpan_concat AS htime, total as results, GROUP_CONCAT( details) AS details FROM ".$wpdb->prefix.$this->table." WHERE TIME( TIME ) >=%s AND TIME( TIME ) <=%s AND DATE( TIME ) >=%s AND DATE( TIME ) <=%s GROUP BY htime", $timeFrom, $timeTo, $dateFrom, $dateTo); //first query, count all unique keywords per specified time and timepsan $count_unique_query = $wpdb->prepare("SELECT * , COUNT( * ) AS count_unique FROM ( SELECT $timeSpan_concat AS dtime FROM ".$wpdb->prefix.$this->table." WHERE TIME( TIME ) >=%s AND TIME( TIME ) <=%s AND DATE( TIME ) >=%s AND DATE( TIME ) <=%s GROUP BY dtime, keyword ) AS d GROUP BY dtime", $timeFrom, $timeTo, $dateFrom, $dateTo); $count_search_query = $wpdb->prepare(" SELECT $timeSpan_concat AS stime, COUNT( * ) AS count_search FROM ".$wpdb->prefix.$this->table." WHERE TIME( TIME ) >=%s AND TIME( TIME ) <=%s AND DATE( TIME ) >=%s AND DATE( TIME ) <=%s GROUP BY stime", $timeFrom, $timeTo, $dateFrom, $dateTo); $count_zero_query = $wpdb->prepare(" SELECT count(*) as count_zero_time ,$timeSpan_concat AS atime FROM ".$wpdb->prefix.$this->table." WHERE TIME( TIME ) >=%s AND TIME( TIME ) <=%s AND DATE( TIME ) >=%s AND DATE( TIME ) <=%s AND total =0 GROUP BY atime ", $timeFrom, $timeTo, $dateFrom, $dateTo); $sum_total_query = $wpdb->prepare(" SELECT SUM(total) as total_results ,$timeSpan_concat AS ttime FROM ".$wpdb->prefix.$this->table." WHERE TIME( TIME ) >=%s AND TIME( TIME ) <=%s AND DATE( TIME ) >=%s AND DATE( TIME ) <=%s GROUP BY ttime ", $timeFrom, $timeTo, $dateFrom, $dateTo); $sql = "SELECT htime, GROUP_CONCAT( details) AS details, count_unique, count_search, count_zero_time, total_results FROM ($general_query) as gq LEFT JOIN ($count_unique_query) AS uq ON gq.htime = uq.dtime LEFT JOIN ($count_search_query) AS shq ON gq.htime = shq.stime LEFT JOIN ($count_zero_query) AS zq ON zq.atime = gq.htime LEFT JOIN ($sum_total_query) AS sq ON sq.ttime = gq.htime GROUP BY htime ORDER BY htime"; $wpdb->query('SET SESSION group_concat_max_len = 1000000;'); $results = $wpdb->get_results($sql); return $results; } function count_data() { global $wpdb; $count = $wpdb->get_var(" SELECT count(*) FROM ".$wpdb->prefix.$this->table." "); return $count; } function get_data_by_keyword($dateFrom, $dateTo) { global $wpdb; //this query is important, so that you can view all results $general_query = $wpdb->prepare(" SELECT keyword, details FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s GROUP BY id", $dateFrom, $dateTo); //first query, count all unique keywords per specified time and timepsan $count_search_query = $wpdb->prepare("SELECT SUM(cnt_search) AS count_search, keyword as ckeyword FROM ( SELECT COUNT( * ) AS cnt_search, keyword FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s GROUP BY id) AS d GROUP BY ckeyword", $dateFrom, $dateTo); $count_zero_query = $wpdb->prepare(" SELECT count(*) as count_zero , keyword AS zkeyword FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s AND total =0 GROUP BY zkeyword ", $dateFrom, $dateTo); $sum_total_query = $wpdb->prepare(" SELECT SUM(total) as total_results, ROUND(AVG(total)) as average_total_results, keyword AS skeyword FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s GROUP BY skeyword ", $dateFrom, $dateTo); if(isset($_GET['keyword_count'])) { if(isset($_GET['keyword'])) { $results = $wpdb->get_results($wpdb->prepare('SELECT * FROM (SELECT keyword, count(*) as count_kzero, DATE(time) as date,SUM( total ) as total_results FROM '.$wpdb->prefix.$this->table.' WHERE DATE(time) >=%s AND DATE(time) <=%s GROUP BY keyword) as t WHERE total_results=%d AND keyword=%s',$dateFrom, $dateTo, intval($_GET['keyword_count']), urldecode($_GET['keyword']))); } else{ $results = $wpdb->get_results($wpdb->prepare('SELECT * FROM (SELECT keyword, count(*) as count_kzero, DATE(time) as date,SUM( total ) as total_results FROM '.$wpdb->prefix.$this->table.' WHERE DATE(time) >=%s AND DATE(time) <=%s GROUP BY keyword) as t WHERE total_results=%d',$dateFrom, $dateTo, intval($_GET['keyword_count']))); } } else{ $wpdb->query('SET SESSION group_concat_max_len = 1000000;'); $sql = "SELECT keyword, GROUP_CONCAT( details) AS details, count_search, count_zero, average_total_results, total_results FROM ($general_query) as gq LEFT JOIN ($count_search_query) AS shq ON gq.keyword = shq.ckeyword LEFT JOIN ($count_zero_query) AS zq ON zq.zkeyword = gq.keyword LEFT JOIN ($sum_total_query) AS sq ON sq.skeyword = gq.keyword GROUP BY keyword"; $results = $wpdb->get_results($sql); } return $results; } function get_data_by_keyword_time($dateFrom, $dateTo, $hourFrom, $minuteFrom, $hourTo, $minuteTo, $timeSpan) { global $wpdb; $timeFrom = ($hourFrom < 10 ? '0'.$hourFrom:$hourFrom).":".($minuteFrom < 10 ? '0'.$minuteFrom:$minuteFrom); $timeTo = ($hourTo < 10 ? '0'.$hourTo:$hourTo).":".($minuteTo < 10 ? '0'.$minuteTo:$minuteTo); //this query is important, so that you can view all results $results = $wpdb->get_results($wpdb->prepare('SELECT * FROM (SELECT keyword, count(*) as count_kzero, MIN(TIME(time)) as time, MAX(TIME(time)) as ttime,SUM( total ) as total_results FROM '.$wpdb->prefix.$this->table.' WHERE DATE(time) >=%s AND DATE(time) <=%s AND TIME( TIME ) >=%s AND TIME( TIME ) <=%s GROUP BY keyword) as t WHERE total_results=%d',$dateFrom, $dateTo, $timeFrom, $timeTo, intval($_GET['keyword_count']))); return $results; } function clear_data_by_date($dateFrom, $dateTo) { global $wpdb; $sql = $wpdb->prepare(" DELETE FROM ".$wpdb->prefix.$this->table." WHERE DATE( TIME ) >=%s AND DATE( TIME ) <=%s ", $dateFrom, $dateTo); $wpdb->query($sql); } function admin_page(){ $tab = (!empty($_REQUEST['tab']) ? trim($_REQUEST['tab']) : false); $message = (!empty($_REQUEST['message']) ? trim($_REQUEST['message']) : false); ?>
Data should be cleared from time to time for more performance, data might become huge if your blog is receiving a lot of searches daily.
_('keyword'), 'details' => _('Details'), 'count_search' => _('Search'), 'count_zero' => _('No Results'), 'average_total_results' => _('Average Results per keyword'), 'total_results' => _('Total results')); if(isset($_GET['keyword_count'])) : if(isset($_GET['keyword'])) : unset($charts['BubbleChart']); $results = $this->get_data_by_date($dateFrom, $dateTo); $columns = array('date' => _('Date'), 'keyword' => _('keyword'), 'details' => _('Details'), 'count_search' => _('Search')); ?>