From 4dea5f84d8fd6bf18bca0f14f13003d062d3954a Mon Sep 17 00:00:00 2001 From: ozhozh Date: Sun, 6 Sep 2009 22:03:30 +0000 Subject: [PATCH] Big commit: 1st attempt at stat page & functions git-svn-id: http://yourls.googlecode.com/svn/trunk@132 12232710-3e20-11de-b438-597f59cd7555 --- includes/functions-infos.php | 139 ++++++++++++++++++++++++++ yourls-infos.php | 185 +++++++++++++++++++++++++++++++++++ 2 files changed, 324 insertions(+) create mode 100644 includes/functions-infos.php create mode 100644 yourls-infos.php diff --git a/includes/functions-infos.php b/includes/functions-infos.php new file mode 100644 index 0000000..3fd9764 --- /dev/null +++ b/includes/functions-infos.php @@ -0,0 +1,139 @@ + 'number of visits' (sort by DESC) +function yourls_stats_countries_map( $countries ) { + arsort( $countries ); + $map = array( + 'cht' => 't', + 'chs' => '440x220', + 'chtm'=> 'world', + 'chco'=> 'FFFFFF,9090AA,202040', + 'chld'=> join('' , array_keys( $countries ) ), + 'chd' => 't:'. join(',' , $countries ), + 'chf' => 'bg,s,EAF7FE' + ); + $map_src = 'http://chart.apis.google.com/chart?' . http_build_query( $map ); + echo ""; +} + +// Echoes an image tag of Google Charts pie from sorted array of 'country_code' => 'number of visits' (sort by DESC). Optional $limit = (integer) limit list of X first countries, sorted by most visits +function yourls_stats_countries_pie( $countries, $limit = 10 ) { + if ( count( $countries ) > $limit ) { + $i= 0; + $trim_countries = array('Others' => 0); + foreach( $countries as $country_code=>$visits ) { + $i++; + if( $i <= $limit ) { + $trim_countries[$country_code] = $visits; + } else { + $trim_countries['Others'] += $visits; + } + } + $countries = $trim_countries; + } + $pie = array( + 'cht' => 'p', + 'chs' => '340x220', + 'chd' => 't:'.( join(',' , $countries ) ), + 'chco'=> '202040,9090AA', + 'chl' => join('|' , array_keys( $countries ) ) + ); + $pie_src = 'http://chart.apis.google.com/chart?' . http_build_query( $pie ); + echo ""; +} + +// Echoes an image tag of Google Charts bar graph from list of chronologically sorted array of [year][month][day] => 'number of clicks' +function yourls_stats_clicks_bar( $dates ) { + /* Say we have an array like: + $dates = array ( + 2009 => array ( + '08' => array ( + 29 => 15, + 30 => 5, + ), + '09' => array ( + '02' => 3, + '03' => 5, + '04' => 2, + '05' => 99, + ), + ), + ) + */ + + // Get first & last years from our range. In our example: 2009 & 2009 + $first_year = key( $dates ); + $last_year = end( array_keys($dates) ); + reset( $dates ); + + // Get first & last months from our range. In our example: 08 & 09 + $first_month = key( $dates[$first_year] ); + $last_month = end( array_keys($dates[$last_year]) ); + reset( $dates ); + + // Get first & last days from our range. In our example: 29 & 05 + $first_day = key( $dates[$first_year][$first_month] ); + $last_day = end( array_keys($dates[$last_year][$last_month]) ); + + // Now build a list of all years (2009), month (08 & 09) and days (all from 2009-08-29 to 2009-09-05) + $list_of_years = array(); + $list_of_months = array(); + $list_of_days = array(); + for ( $year = $first_year; $year <= $last_year; $year++ ) { + $_year = sprintf('%04d', $year); + $list_of_years[$_year] = $_year; + $current_first_month = ( $year == $first_year ? $first_month : '01' ); + $current_last_month = ( $year == $last_year ? $last_month : '12' ); + for ( $month = $current_first_month; $month <= $current_last_month; $month++ ) { + $_month = sprintf('%02d', $month); + $list_of_months[$_month] = $_month; + $current_first_day = ( $year == $first_year && $month == $first_month ? $first_day : '01' ); + $current_last_day = ( $year == $last_year && $month == $last_month ? $last_day : yourls_days_in_month($month, $year) ); + for ( $day = $current_first_day; $day <= $current_last_day; $day++ ) { + $day = sprintf('%02d', $day); + $list_of_days[] = isset( $dates[$_year][$_month][$day] ) ? $dates[$_year][$_month][$day] : 0; + } + } + } + + // Make the chart + $label_years = $first_year != $last_year ? join('|', $list_of_years ) : $first_year.'|'.$last_year; + $label_months = count( $list_of_months ) > 1 ? join('|', $list_of_months) : $first_month.'|'.$first_year; + $max = max( $list_of_days ); + $label_clicks = '0|'.intval( $max / 4 ).'|'.intval( $max / 2 ).'|'.intval( $max / 1.5 ).'|'.$max; + $line = array( + 'cht' => 'lc', + 'chs' => '1000x300', + 'chs' => '440x220', + 'chxt'=> 'x,x,y', + 'chd' => 't:'.( join(',' , $list_of_days ) ), + 'chxl'=> '0:|'. $label_years .'|1:|'. $label_months .'|2:|'. $label_clicks + ); + $line_src = 'http://chart.apis.google.com/chart?' . http_build_query( $line ); + echo ""; +} + +// Return the number of days in a month. From php.net, used if PHP built without calendar functions +function yourls_days_in_month($month, $year) { + // calculate number of days in a month + return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31); +} + +// Get max value from date array of [year][month][day] = 'hits' +function yourls_stats_get_best_day( $dates ) { + $max = 0; $day = 0; + foreach( $dates as $year=>$months ) { + foreach( $months as $month=>$days ) { + foreach( $days as $day=>$visits ) { + if( $visits > $max ) { + $max = intval($visits); + $day = "$year-$month-$day"; + } + } + } + } + + return array( 'day' => $day, 'max' => $max ); + +} + diff --git a/yourls-infos.php b/yourls-infos.php new file mode 100644 index 0000000..63f5a6c --- /dev/null +++ b/yourls-infos.php @@ -0,0 +1,185 @@ +get_results( "SELECT `click_time`, `referrer`, `user_agent`, `country_code` FROM `$table` WHERE `shorturl` = '$keyword';" ); + +$referrers = array(); +$direct = 0; +$countries = array(); +$dates = array(); + +// Loop through all results and build list of referrers, countries and hits per day +foreach( $hits as $hit ) { + extract( (array)$hit ); + + if ( isset( $country_code ) && $country_code ) { + if( !array_key_exists( $country_code, $countries ) ) + $countries[$country_code] = 0; + $countries[$country_code]++; + } + + if( isset( $referrer ) ) { + if ( $referrer == 'direct' ) { + $direct++; + } else { + $parse = parse_url( $referrer ); + $host = $parse['host']; + unset( $parse ); + if( !array_key_exists( $host, $referrers ) ) + $referrers[$host] = array( ); + if( !array_key_exists( $referrer, $referrers[$host] ) ) + $referrers[$host][$referrer] = 0; + $referrers[$host][$referrer]++; + } + } + + if( isset( $click_time ) ) { + preg_match('/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/', $click_time, $matches); + list( $temp, $year, $month, $day ) = $matches; + unset( $matches ); + if( !array_key_exists( $year, $dates ) ) + $dates[$year] = array(); + if( !array_key_exists( $month, $dates[$year] ) ) + $dates[$year][$month] = array(); + if( !array_key_exists( $day, $dates[$year][$month] ) ) + $dates[$year][$month][$day] = 0; + $dates[$year][$month][$day]++; + } +} + +// Sort dates, chronologically from [2007][12][24] to [2009][02][19] +ksort( $dates ); +foreach( $dates as $year=>$months ) { + ksort( $dates[$year] ); + foreach( $months as $month=>$day ) { + ksort( $dates[$year][$month] ); + } +} + +// Sort countries, most frequent first +if ( $countries ) + arsort( $countries ); + +// Sort referrers. $referrer_sort is a array of most frequent domains +arsort( $referrers ); +$referrer_sort = array(); +foreach( $referrers as $site => $urls ) { + if( count($urls) > 1 ) + $referrer_sort[$site] = array_sum( $urls ); +} +arsort($referrer_sort); + +/** +echo "
";
+echo "referrers: "; print_r( $referrers );
+echo "referrer sort: "; print_r( $referrer_sort );
+echo "dates: "; print_r( $dates );
+echo "countries: "; print_r( $countries );
+die();
+/**/
+
+
+yourls_html_head( 'infos' );
+?>
+
+

+ YOURLS: Your Own URL Shortener
+ YOURLS
+

+ +

Your are logged in as: . Logout

+ + +

Informations

+ +

Short URL:

+

Long URL:

+

Number of hits since : hit 1 ? 's' : ''); ?>

+ +

Traffic statistics

+ +

+ + +

Best day: hit 1 ? 's' : ''); ?> on . +Click for more details

+ + +

Traffic location

+ + + + + + +
+

Top 5 countries. Click for more details

+ + + +
+

Overall traffic

+ +
+ +

Traffic sources

+ + + + + + \ No newline at end of file -- 2.45.0