2 // TODO: make things cleaner. This file is an awful HTML/PHP soup.
\r
3 define('YOURLS_INFOS', true);
\r
4 require_once( dirname(__FILE__).'/includes/load-yourls.php' );
\r
5 require_once( YOURLS_INC.'/functions-infos.php' );
\r
6 yourls_maybe_require_auth();
\r
8 // Variables should be defined in yourls-loader.php, if not try GET request (old behavior of yourls-infos.php)
\r
9 if( !isset( $keyword ) && isset( $_GET['id'] ) )
\r
10 $keyword = $_GET['id'];
\r
11 if( !isset( $aggregate ) && isset( $_GET['all'] ) && $_GET['all'] == 1 && yourls_allow_duplicate_longurls() )
\r
14 if ( !isset( $keyword ) ) {
\r
15 yourls_do_action( 'infos_no_keyword' );
\r
16 yourls_redirect( YOURLS_SITE, 302 );
\r
19 // Get basic infos for this shortened URL
\r
20 $keyword = yourls_sanitize_string( $keyword );
\r
21 $longurl = yourls_get_keyword_longurl( $keyword );
\r
22 $clicks = yourls_get_keyword_clicks( $keyword );
\r
23 $timestamp = yourls_get_keyword_timestamp( $keyword );
\r
24 $title = yourls_get_keyword_title( $keyword );
\r
26 // Update title if it hasn't been stored yet
\r
27 if( $title == '' ) {
\r
28 $title = yourls_get_remote_title( $longurl );
\r
29 yourls_edit_link_title( $keyword, $title );
\r
32 if ( $longurl === false ) {
\r
33 yourls_do_action( 'infos_keyword_not_found' );
\r
34 yourls_redirect( YOURLS_SITE, 302 );
\r
37 yourls_do_action( 'pre_yourls_infos', $keyword );
\r
39 if( yourls_do_log_redirect() ) {
\r
41 // Duplicate keywords, if applicable
\r
42 $keyword_list = yourls_get_duplicate_keywords( $longurl );
\r
44 // Fetch all information from the table log
\r
45 $table = YOURLS_DB_TABLE_LOG;
\r
48 // Fetch information for all keywords pointing to $longurl
\r
49 $keywords = join( "', '", $keyword_list );
\r
50 $query = "SELECT `shorturl`, `click_time`, `referrer`, `user_agent`, `country_code` FROM `$table` WHERE `shorturl` IN ( '$keywords' );";
\r
52 // Fetch information for current keyword only
\r
53 $query = "SELECT `click_time`, `referrer`, `user_agent`, `country_code` FROM `$table` WHERE `shorturl` = '$keyword'";
\r
56 $hits = $ydb->get_results( yourls_apply_filter( 'stat_query_all', $query ) );
\r
58 $referrers = array();
\r
59 $direct = $notdirect = 0;
\r
60 $countries = array();
\r
62 $list_of_days = array();
\r
63 $list_of_months = array();
\r
64 $list_of_years = array();
\r
65 $last_24h = array();
\r
67 // Loop through all results and build list of referrers, countries and hits per day
\r
68 foreach( (array)$hits as $hit ) {
\r
69 extract( (array)$hit );
\r
71 if ( isset( $country_code ) && $country_code ) {
\r
72 if( !array_key_exists( $country_code, $countries ) )
\r
73 $countries[$country_code] = 0;
\r
74 $countries[$country_code]++;
\r
77 if( isset( $referrer ) ) {
\r
78 if ( $referrer == 'direct' ) {
\r
82 $host = yourls_get_domain( $referrer );
\r
83 if( !array_key_exists( $host, $referrers ) )
\r
84 $referrers[$host] = array( );
\r
85 if( !array_key_exists( $referrer, $referrers[$host] ) )
\r
86 $referrers[$host][$referrer] = 0;
\r
87 $referrers[$host][$referrer]++;
\r
91 if( isset( $click_time ) ) {
\r
92 $now = intval( date('U') );
\r
94 preg_match('/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/', $click_time, $matches);
\r
95 list( $temp, $year, $month, $day, $hour, $min, $sec ) = $matches;
\r
98 // Build array of $dates[$year][$month][$day] = number of clicks
\r
99 if( !array_key_exists( $year, $dates ) )
\r
100 $dates[$year] = array();
\r
101 if( !array_key_exists( $month, $dates[$year] ) )
\r
102 $dates[$year][$month] = array();
\r
103 if( !array_key_exists( $day, $dates[$year][$month] ) )
\r
104 $dates[$year][$month][$day] = 0;
\r
105 $dates[$year][$month][$day]++;
\r
107 // Build array of last 24 hours $last_24h[$hour] = number of click
\r
108 $then = strtotime( $click_time);
\r
109 if( ( $now >= $then ) && ( ( $now - $then ) < ( 24 * 60 * 60 ) ) ) {
\r
110 $year = sprintf( "%02d", substr($year, -2) ); // 2009 -> 09
\r
111 $diff = $now - strtotime( $click_time);
\r
112 if( !array_key_exists( "$year-$month-$day-$hour", $last_24h ) )
\r
113 $last_24h["$year-$month-$day-$hour"] = 0;
\r
114 $last_24h["$year-$month-$day-$hour"]++;
\r
120 // Sort dates, chronologically from [2007][12][24] to [2009][02][19]
\r
122 foreach( $dates as $year=>$months ) {
\r
123 ksort( $dates[$year] );
\r
124 foreach( $months as $month=>$day ) {
\r
125 ksort( $dates[$year][$month] );
\r
129 // Get $list_of_days, $list_of_months, $list_of_years
\r
131 $first_year = key( $dates );
\r
132 $last_year = end( array_keys($dates) );
\r
134 extract( yourls_build_list_of_days( $dates ) );
\r
136 // If the $last_24h doesn't have all the hours, insert missing hours with value 0
\r
137 for ($i = 23; $i >= 0; $i--) {
\r
138 $h = date('y-m-d-H', $now - ($i * 60 * 60) );
\r
139 if( !array_key_exists( $h, $last_24h ) ) {
\r
143 ksort( $last_24h );
\r
146 // Sort countries, most frequent first
\r
148 arsort( $countries );
\r
150 // Sort referrers. $referrer_sort is a array of most frequent domains
\r
151 arsort( $referrers );
\r
152 $referrer_sort = array();
\r
153 $number_of_sites = count( array_keys( $referrers ) );
\r
154 foreach( $referrers as $site => $urls ) {
\r
155 if( count($urls) > 1 || $number_of_sites == 1 )
\r
156 $referrer_sort[$site] = array_sum( $urls );
\r
158 arsort($referrer_sort);
\r
160 // Filter all this junk if applicable. Be warned, some are possibly huge datasets.
\r
161 $referrers = yourls_apply_filter( 'pre_yourls_info_referrers', $referrers );
\r
162 $referrer_sort = yourls_apply_filter( 'pre_yourls_info_referrer_sort', $referrer_sort );
\r
163 $direct = yourls_apply_filter( 'pre_yourls_info_direct', $direct );
\r
164 $notdirect = yourls_apply_filter( 'pre_yourls_info_notdirect', $notdirect );
\r
165 $dates = yourls_apply_filter( 'pre_yourls_info_dates', $dates );
\r
166 $list_of_days = yourls_apply_filter( 'pre_yourls_info_list_of_days', $list_of_days );
\r
167 $list_of_months = yourls_apply_filter( 'pre_yourls_info_list_of_months', $list_of_months );
\r
168 $list_of_years = yourls_apply_filter( 'pre_yourls_info_list_of_years', $list_of_years );
\r
169 $last_24h = yourls_apply_filter( 'pre_yourls_info_last_24h', $last_24h );
\r
170 $countries = yourls_apply_filter( 'pre_yourls_info_countries', $countries );
\r
174 echo "referrers: "; print_r( $referrers );
\r
175 echo "referrer sort: "; print_r( $referrer_sort );
\r
176 echo "direct: $direct\n";
\r
177 echo "notdirect: $notdirect\n";
\r
178 echo "dates: "; print_r( $dates );
\r
179 echo "list of days: "; print_r( $list_of_days );
\r
180 echo "list_of_months: "; print_r( $list_of_months );
\r
181 echo "list_of_years: "; print_r( $list_of_years );
\r
182 echo "last_24h: "; print_r( $last_24h );
\r
183 echo "countries: "; print_r( $countries );
\r
189 yourls_html_head( 'infos', 'Statistics for '.YOURLS_SITE.'/'.$keyword );
\r
190 yourls_html_logo();
\r
191 yourls_html_menu();
\r
194 <h2 id="informations"><?php echo $title; ?></h2>
\r
196 <h3><span class="label">Short URL:</span> <img src="<?php echo yourls_match_current_protocol( YOURLS_SITE ); ?>/images/favicon.gif"/>
\r
197 <?php if( $aggregate ) {
\r
199 foreach( $keyword_list as $k ) {
\r
202 yourls_html_link( yourls_link($k) );
\r
204 yourls_html_link( yourls_link($k), "/$k" );
\r
206 if ( $i < count( $keyword_list ) )
\r
210 yourls_html_link( yourls_link($keyword) );
\r
211 if( count( $keyword_list ) > 1 )
\r
212 echo ' <a href="'. yourls_link($keyword).'+all" title="Aggregate stats for duplicate short URLs"><img src="' . yourls_match_current_protocol( YOURLS_SITE ) . '/images/chart_bar_add.png" border="0" /></a>';
\r
214 <h3 id="longurl"><span class="label">Long URL:</span> <img class="fix_images" src="<?php echo yourls_get_favicon_url( $longurl );?>" /> <?php yourls_html_link( $longurl, yourls_trim_long_string( $longurl ), 'longurl' ); ?></h3>
\r
217 <div class="wrap_unfloat">
\r
218 <ul id="headers" class="toggle_display stat_tab">
\r
219 <?php if( yourls_do_log_redirect() ) { ?>
\r
220 <li class="selected"><a href="#stat_tab_stats"><h2>Traffic statistics</h2></a></li>
\r
221 <li><a href="#stat_tab_location"><h2>Traffic location</h2></a></li>
\r
222 <li><a href="#stat_tab_sources"><h2>Traffic sources</h2></a></li>
\r
224 <li><a href="#stat_tab_share"><h2>Share</h2></a></li>
\r
229 <?php if( yourls_do_log_redirect() ) { ?>
\r
230 <div id="stat_tab_stats" class="tab">
\r
231 <h2>Traffic statistics</h2>
\r
233 <?php yourls_do_action( 'pre_yourls_info_stats', $keyword ); ?>
\r
235 <?php if ( $list_of_days ) { ?>
\r
239 '24' => 'Last 24 hours',
\r
240 '7' => 'Last 7 days',
\r
241 '30' => 'Last 30 days',
\r
245 // Which graph to generate ?
\r
246 $do_all = $do_30 = $do_7 = $do_24 = false;
\r
247 $hits_all = array_sum( $list_of_days );
\r
248 $hits_30 = array_sum( array_slice( $list_of_days, -30 ) );
\r
249 $hits_7 = array_sum( array_slice( $list_of_days, -7 ) );
\r
250 $hits_24 = array_sum( $last_24h );
\r
251 if( $hits_all > 0 )
\r
252 $do_all = true; // graph for all days range
\r
253 if( $hits_30 > 0 && count( array_slice( $list_of_days, -30 ) ) == 30 )
\r
254 $do_30 = true; // graph for last 30 days
\r
255 if( $hits_7 > 0 && count( array_slice( $list_of_days, -7 ) ) == 7 )
\r
256 $do_7 = true; // graph for last 7 days
\r
258 $do_24 = true; // graph for last 24 hours
\r
260 // Which graph to display ?
\r
261 $display_all = $display_30 = $display_7 = $display_24 = false;
\r
263 $display_24 = true;
\r
264 } elseif ( $do_7 ) {
\r
266 } elseif ( $do_30 ) {
\r
267 $display_30 = true;
\r
268 } elseif ( $do_all ) {
\r
269 $display_all = true;
\r
273 <table border="0" cellspacing="2">
\r
276 <ul id="stats_lines" class="toggle_display stat_line">
\r
278 if( $do_24 == true )
\r
279 echo "<li><a href='#stat_line_24'>Last 24 hours</a>";
\r
280 if( $do_7 == true )
\r
281 echo "<li><a href='#stat_line_7'>Last 7 days</a>";
\r
282 if( $do_30 == true )
\r
283 echo "<li><a href='#stat_line_30'>Last 30 days</a>";
\r
284 if( $do_all == true )
\r
285 echo "<li><a href='#stat_line_all'>All time</a>";
\r
289 // Generate, and display if applicable, each needed graph
\r
290 foreach( $graphs as $graph => $graphtitle ) {
\r
291 if( ${'do_'.$graph} == true ) {
\r
292 $display = ( ${'display_'.$graph} === true ? 'display:block' : 'display:none' );
\r
293 echo "<div id='stat_line_$graph' class='stats_line line' style='$display'>";
\r
294 $labels_1 = $labels_2 = array();
\r
297 // each key of $last_24h is of type "yy-mm-dd-hh"
\r
298 $first_key = current( array_keys( $last_24h ) );
\r
299 $last_key = end( array_keys( $last_24h ) );
\r
300 // Get "dd/mm" of first and last key
\r
301 $first_label = preg_replace( '/\d\d-(\d\d)-(\d\d)-\d\d/', '$2/$1', $first_key );
\r
302 $last_label = preg_replace( '/\d\d-(\d\d)-(\d\d)-\d\d/', '$2/$1', $last_key );
\r
303 $labels_2 = array( $first_label, $last_label);
\r
304 // Get hh of each key
\r
305 foreach( $last_24h as $k=>$v ) {
\r
306 $labels_1[] = end( explode( '-', $k ) ); // 'hh'
\r
309 echo "<h3>Number of hits : $graphtitle</h3>";
\r
310 yourls_stats_line( $last_24h, $labels_1, $labels_2 );
\r
315 // each key of $list_of_days is of type "yyyy-mm-dd"
\r
316 $slice = array_slice( $list_of_days, intval( $graph ) * -1 );
\r
317 foreach( $slice as $k=>$v ) {
\r
319 $labels_1[] = preg_replace( '/\d\d\d\d-\d\d-(\d\d)/', '$1', $k );
\r
321 $first_key = current( array_keys( $slice ) );
\r
322 $last_key = end( array_keys( $slice ) );
\r
323 // Get "dd/mm" of first and last key
\r
324 $first_label = preg_replace( '/\d\d\d\d-(\d\d)-(\d\d)/', '$2/$1', $first_key );
\r
325 $last_label = preg_replace( '/\d\d\d\d-(\d\d)-(\d\d)/', '$2/$1', $last_key );
\r
326 $labels_2 = array( $first_label, $last_label);
\r
328 echo "<h3>Number of hits : $graphtitle</h3>";
\r
329 yourls_stats_line( $slice, $labels_1, $labels_2 );
\r
335 foreach( $list_of_days as $k=>$v ) {
\r
336 $labels_1[] = preg_replace( '/\d\d(\d\d)-(\d\d)-\d\d/', '$1-$2', $k );
\r
338 // take out duplicates
\r
339 $labels_1 = array_unique( $labels_1 );
\r
340 // now get "mm" only so we have all different month
\r
341 foreach( $labels_1 as $k=>$v ) {
\r
342 $labels_1[$k] = preg_replace( '/\d\d-(\d\d)/', '$1', $v );
\r
345 echo "<h3>Number of hits : $graphtitle</h3>";
\r
346 $labels_1 = yourls_array_granularity( $labels_1, 30, false );
\r
347 $labels_2 = yourls_array_granularity( $list_of_years, 30, false );
\r
348 yourls_stats_line( $list_of_days, $labels_1, $labels_2 );
\r
357 <h3>Historical click count</h3>
\r
359 $ago = round( (date('U') - strtotime($timestamp)) / (24* 60 * 60 ) );
\r
363 $daysago = '(about '.$ago .' '.yourls_plural( ' day', $ago ).' ago)';
\r
366 <p>Short URL created on <?php echo date("F j, Y @ g:i a", strtotime($timestamp)); ?> <?php echo $daysago; ?></p>
\r
367 <div class="wrap_unfloat">
\r
368 <ul class="no_bullet toggle_display stat_line" id="historical_clicks">
\r
370 foreach( $graphs as $graph => $graphtitle ) {
\r
371 if ( ${'do_'.$graph} ) {
\r
372 $link = "<a href='#stat_line_$graph'>$graphtitle</a>";
\r
374 $link = $graphtitle;
\r
377 if( ${'do_'.$graph} ) {
\r
381 $stat = round( ( ${'hits_'.$graph} / intval( $graph ) ) * 100 ) / 100 . ' per day';
\r
384 $stat = round( ( ${'hits_'.$graph} / 24 ) * 100 ) / 100 . ' per hour';
\r
388 $stat = round( ( ${'hits_'.$graph} / $ago ) * 100 ) / 100 . ' per day';
\r
391 $hits = yourls_plural( 'hit', ${'hits_'.$graph} );
\r
392 echo "<li><span class='historical_link'>$link</span> <span class='historical_count'>${'hits_'.$graph} $hits</span> $stat</li>\n";
\r
399 <?php $best = yourls_stats_get_best_day( $list_of_days ); ?>
\r
400 <p><strong><?php echo $best['max'];?></strong> <?php echo yourls_plural( 'hit', $best['max'] ); ?> on <?php echo date("F j, Y", strtotime($best['day'])); ?>.
\r
401 <a href="" class='details hide-if-no-js' id="more_clicks">Click for more details</a></p>
\r
402 <ul id="details_clicks" style="display:none">
\r
404 foreach( $dates as $year=>$months ) {
\r
405 if( count( $list_of_years ) > 1 ) {
\r
406 $li = "<a href='' class='details' id='more_year$year'>Year $year</a>";
\r
409 $li = "Year $year";
\r
410 $display = 'block';
\r
413 echo "<ul style='display:$display' id='details_year$year'>";
\r
414 foreach( $months as $month=>$days ) {
\r
415 $monthname = date("F", mktime(0, 0, 0, $month,1));
\r
416 if( count( $list_of_months ) > 1 ) {
\r
417 $li = "<a href='' class='details' id='more_month$year$month'>$monthname</a>";
\r
420 $li = "$monthname";
\r
421 $display = 'block';
\r
424 echo "<ul style='display:$display' id='details_month$year$month'>";
\r
425 foreach( $days as $day=>$hits ) {
\r
426 $class = ( $hits == $best['max'] ? 'class="bestday"' : '' );
\r
427 echo "<li $class>$day: $hits". yourls_plural( ' hit', $hits) ."</li>\n";
\r
441 <?php yourls_do_action( 'post_yourls_info_stats', $keyword ); ?>
\r
444 echo "<p>No traffic yet. Get some clicks first!</p>";
\r
449 <div id="stat_tab_location" class="tab">
\r
450 <h2>Traffic location</h2>
\r
452 <?php yourls_do_action( 'pre_yourls_info_location', $keyword ); ?>
\r
454 <?php if ( $countries ) { ?>
\r
456 <table border="0" cellspacing="2">
\r
459 <h3>Top 5 countries</h3>
\r
460 <?php yourls_stats_pie( $countries, 5 ); ?>
\r
461 <p><a href="" class='details hide-if-no-js' id="more_countries">Click for more details</a></p>
\r
462 <ul id="details_countries" style="display:none" class="no_bullet">
\r
464 foreach( $countries as $code=>$count ) {
\r
465 echo "<li><img src='".yourls_geo_get_flag( $code )."' /> $code (".yourls_geo_countrycode_to_countryname( $code ).") : $count ".yourls_plural('hit', $count)."</li>\n";
\r
472 <h3>Overall traffic</h3>
\r
473 <?php yourls_stats_countries_map( $countries ); ?>
\r
478 <?php yourls_do_action( 'post_yourls_info_location', $keyword ); ?>
\r
481 echo "<p>No country data.</p>";
\r
486 <div id="stat_tab_sources" class="tab">
\r
487 <h2>Traffic Sources</h2>
\r
489 <?php yourls_do_action( 'pre_yourls_info_sources', $keyword ); ?>
\r
491 <?php if ( $referrers ) { ?>
\r
493 <table border="0" cellspacing="2">
\r
496 <h3>Referrer shares</h3>
\r
498 if ( $number_of_sites > 1 )
\r
499 $referrer_sort['Others'] = count( $referrers );
\r
500 yourls_stats_pie( $referrer_sort, 5, '440x220' );
\r
501 unset( $referrer_sort['Others'] );
\r
504 <ul class="no_bullet">
\r
507 foreach( $referrer_sort as $site => $count ) {
\r
509 $favicon = yourls_get_favicon_url( $site );
\r
510 echo "<li class='sites_list'><img src='$favicon' class='fix_images'/> $site: <strong>$count</strong> <a href='' class='details hide-if-no-js' id='more_url$i'>(details)</a></li>\n";
\r
511 echo "<ul id='details_url$i' style='display:none'>";
\r
512 foreach( $referrers[$site] as $url => $count ) {
\r
513 echo "<li>"; yourls_html_link($url); echo ": <strong>$count</strong></li>\n";
\r
516 unset( $referrers[$site] );
\r
518 // Any referrer left? Group in "various"
\r
519 if ( $referrers ) {
\r
520 echo "<li id='sites_various'>Various: <strong>". count( $referrers ). "</strong> <a href='' class='details hide-if-no-js' id='more_various'>(details)</a></li>\n";
\r
521 echo "<ul id='details_various' style='display:none'>";
\r
522 foreach( $referrers as $url ) {
\r
523 echo "<li>"; yourls_html_link(key($url)); echo ": 1</li>\n";
\r
534 <h3>Direct vs Referrer Traffic</h3>
\r
536 yourls_stats_pie( array('Direct'=>$direct, 'Referrers'=> $notdirect), 5, '440x220' );
\r
538 <p>Direct traffic: <strong><?php echo $direct; ?></strong> <?php echo yourls_plural( 'hit', $direct ); ?> </p>
\r
539 <p>Referrer traffic: <strong><?php echo $notdirect; ?></strong> <?php echo yourls_plural( 'hit', $notdirect ); ?> </p>
\r
545 <?php yourls_do_action( 'post_yourls_info_sources', $keyword ); ?>
\r
548 echo "<p>No referrer data.</p>";
\r
553 <?php } // endif do log redirect ?>
\r
556 <div id="stat_tab_share" class="tab">
\r
559 <?php yourls_share_box( $longurl, yourls_link($keyword), $title, '', '<h3>Short link</h3>', '<h3>Quick Share</h3>'); ?>
\r
566 <?php yourls_html_footer(); ?>
\r