3 require_once( dirname(__FILE__).'/includes/load-yourls.php' );
\r
4 require_once( dirname(__FILE__).'/includes/functions-infos.php' );
\r
5 yourls_maybe_require_auth();
\r
7 if ( !isset( $_GET['id'] ) )
\r
8 yourls_redirect( YOURLS_SITE, 307 );
\r
10 // Get basic infos for this shortened URL
\r
11 $keyword = yourls_sanitize_string( $_GET['id'] );
\r
12 $longurl = yourls_get_keyword_longurl( $keyword );
\r
13 $clicks = yourls_get_keyword_clicks( $keyword );
\r
14 $timestamp = yourls_get_keyword_timestamp( $keyword );
\r
15 if ( $longurl === false )
\r
16 yourls_redirect( YOURLS_SITE, 307 );
\r
18 // Fetch all information from the table log
\r
19 $table = YOURLS_DB_TABLE_LOG;
\r
20 $hits = $ydb->get_results( "SELECT `click_time`, `referrer`, `user_agent`, `country_code` FROM `$table` WHERE `shorturl` = '$keyword';" );
\r
22 $referrers = array();
\r
24 $countries = array();
\r
26 $list_of_days = array();
\r
27 $list_of_months = array();
\r
28 $list_of_years = array();
\r
29 $last_24h = array();
\r
31 // Loop through all results and build list of referrers, countries and hits per day
\r
32 foreach( (array)$hits as $hit ) {
\r
33 extract( (array)$hit );
\r
35 if ( isset( $country_code ) && $country_code ) {
\r
36 if( !array_key_exists( $country_code, $countries ) )
\r
37 $countries[$country_code] = 0;
\r
38 $countries[$country_code]++;
\r
41 if( isset( $referrer ) ) {
\r
42 if ( $referrer == 'direct' ) {
\r
45 $host = yourls_get_domain( $referrer );
\r
46 if( !array_key_exists( $host, $referrers ) )
\r
47 $referrers[$host] = array( );
\r
48 if( !array_key_exists( $referrer, $referrers[$host] ) )
\r
49 $referrers[$host][$referrer] = 0;
\r
50 $referrers[$host][$referrer]++;
\r
54 if( isset( $click_time ) ) {
\r
55 $now = intval( date('U') );
\r
57 preg_match('/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/', $click_time, $matches);
\r
58 list( $temp, $year, $month, $day, $hour, $min, $sec ) = $matches;
\r
61 // Build array of $dates[$year][$month][$day] = number of clicks
\r
62 if( !array_key_exists( $year, $dates ) )
\r
63 $dates[$year] = array();
\r
64 if( !array_key_exists( $month, $dates[$year] ) )
\r
65 $dates[$year][$month] = array();
\r
66 if( !array_key_exists( $day, $dates[$year][$month] ) )
\r
67 $dates[$year][$month][$day] = 0;
\r
68 $dates[$year][$month][$day]++;
\r
70 // Build array of last 24 hours $last_24h[$hour] = number of click
\r
71 $then = strtotime( $click_time);
\r
72 if( ( $now >= $then ) && ( ( $now - $then ) < ( 24 * 60 * 60 ) ) ) {
\r
73 $year = sprintf( "%02d", substr( $year, -1, 2 ) ); // 2009 -> 09
\r
74 $diff = $now - strtotime( $click_time);
\r
75 if( !array_key_exists( "$year-$month-$day-$hour", $last_24h ) )
\r
76 $last_24h["$year-$month-$day-$hour"] = 0;
\r
77 $last_24h["$year-$month-$day-$hour"]++;
\r
83 // Sort dates, chronologically from [2007][12][24] to [2009][02][19]
\r
85 foreach( $dates as $year=>$months ) {
\r
86 ksort( $dates[$year] );
\r
87 foreach( $months as $month=>$day ) {
\r
88 ksort( $dates[$year][$month] );
\r
92 // Get $list_of_days, $list_of_months, $list_of_years
\r
94 extract( yourls_build_list_of_days( $dates ) );
\r
96 // If the $last_24h doesn't have all the hours, insert missing hours with value 0
\r
97 for ($i = 23; $i >= 0; $i--) {
\r
98 $h = date('y-m-d-H', $now - ($i * 60 * 60) );
\r
99 if( !array_key_exists( $h, $last_24h ) ) {
\r
103 ksort( $last_24h );
\r
106 // Sort countries, most frequent first
\r
108 arsort( $countries );
\r
110 // Sort referrers. $referrer_sort is a array of most frequent domains
\r
111 arsort( $referrers );
\r
112 $referrer_sort = array();
\r
113 $number_of_sites = count( array_keys( $referrers ) );
\r
114 foreach( $referrers as $site => $urls ) {
\r
115 if( count($urls) > 1 || $number_of_sites == 1 )
\r
116 $referrer_sort[$site] = array_sum( $urls );
\r
118 arsort($referrer_sort);
\r
122 //echo "referrers: "; print_r( $referrers );
\r
123 //echo "referrer sort: "; print_r( $referrer_sort );
\r
124 echo "dates: "; print_r( $dates );
\r
125 echo "list of days: "; print_r( $list_of_days );
\r
126 echo "list_of_months: "; print_r( $list_of_months );
\r
127 echo "list_of_years: "; print_r( $list_of_years );
\r
128 echo "last_24h: "; print_r( $last_24h );
\r
129 //echo "countries: "; print_r( $countries );
\r
135 yourls_html_head( 'infos' );
\r
139 <a href="<?php echo YOURLS_SITE; ?>/admin/index.php" title="YOURLS"><span>YOURLS</span>: <span>Y</span>our <span>O</span>wn <span>URL</span> <span>S</span>hortener<br/>
\r
140 <img src="<?php echo YOURLS_SITE; ?>/images/yourls-logo.png" alt="YOURLS" title="YOURLS" style="border: 0px;" /></a>
\r
142 <?php if ( yourls_is_private() ) { ?>
\r
143 <p>Your are logged in as: <strong><?php echo YOURLS_USER; ?></strong>. <a href="?mode=logout" title="Logout">Logout</a></p>
\r
146 <h2 id="informations">Informations</h2>
\r
148 <h3>Short URL: <img class="fix_images" src="<?php echo YOURLS_SITE; ?>/images/favicon.gif"/> <?php yourls_html_link( YOURLS_SITE."/$keyword" ); ?></h3>
\r
149 <h3 id="longurl">Long URL: <img class="fix_images" src="<?php echo yourls_get_domain( $longurl, true );?>/favicon.ico"/> <?php yourls_html_link( $longurl, '', 'longurl' ); ?></h3>
\r
152 <div class="wrap_unfloat">
\r
153 <ul id="headers" class="toggle_display stat_tab">
\r
154 <li class="selected"><a href="#stat_tab_stats"><h2>Traffic statistics</h2></a></li>
\r
155 <li><a href="#stat_tab_location"><h2>Traffic location</h2></a></li>
\r
156 <li><a href="#stat_tab_sources"><h2>Traffic sources</h2></a></li>
\r
157 <li><a href="#stat_tab_share"><h2>Share</h2></a></li>
\r
162 <div id="stat_tab_stats" class="tab">
\r
163 <h2>Traffic statistics</h2>
\r
165 <?php if ( $list_of_days ) { ?>
\r
169 '24' => 'Last 24 hours',
\r
170 '7' => 'Last 7 days',
\r
171 '30' => 'Last 30 days',
\r
175 // Which graph to generate ?
\r
176 $do_all = $do_30 = $do_7 = $do_24 = false;
\r
177 $hits_all = array_sum( $list_of_days );
\r
178 $hits_30 = array_sum( array_slice( $list_of_days, -30 ) );
\r
179 $hits_7 = array_sum( array_slice( $list_of_days, -7 ) );
\r
180 $hits_24 = array_sum( $last_24h );
\r
181 if( $hits_all > 0 )
\r
182 $do_all = true; // graph for all days range
\r
183 if( $hits_30 > 0 && count( array_slice( $list_of_days, -30 ) ) == 30 )
\r
184 $do_30 = true; // graph for last 30 days
\r
185 if( $hits_7 > 0 && count( array_slice( $list_of_days, -7 ) ) == 7 )
\r
186 $do_7 = true; // graph for last 7 days
\r
188 $do_24 = true; // graph for last 24 hours
\r
190 // Which graph to display ?
\r
191 $display_all = $display_30 = $display_7 = $display_24 = false;
\r
193 $display_24 = true;
\r
194 } elseif ( $do_7 ) {
\r
196 } elseif ( $do_30 ) {
\r
197 $display_30 = true;
\r
198 } elseif ( $do_all ) {
\r
199 $display_all = true;
\r
203 <table border="0" cellspacing="2">
\r
206 <ul id="stats_lines" class="toggle_display stat_line">
\r
208 if( $do_24 == true )
\r
209 echo "<li><a href='#stat_line_24'>Last 24 hours</a>";
\r
210 if( $do_7 == true )
\r
211 echo "<li><a href='#stat_line_7'>Last 7 days</a>";
\r
212 if( $do_30 == true )
\r
213 echo "<li><a href='#stat_line_30'>Last 30 days</a>";
\r
214 if( $do_all == true )
\r
215 echo "<li><a href='#stat_line_all'>All time</a>";
\r
219 // Generate, and display if applicable, each needed graph
\r
220 foreach( $graphs as $graph => $title ) {
\r
221 if( ${'do_'.$graph} == true ) {
\r
222 $display = ( ${'display_'.$graph} === true ? 'display:block' : 'display:none' );
\r
223 echo "<div id='stat_line_$graph' class='stats_line line' style='$display'>";
\r
224 $labels_1 = $labels_2 = array();
\r
227 // each key of $last_24h is of type "yy-mm-dd-hh"
\r
228 $first_key = current( array_keys( $last_24h ) );
\r
229 $last_key = end( array_keys( $last_24h ) );
\r
230 // Get "dd/mm" of first and last key
\r
231 $first_label = preg_replace( '/\d\d-(\d\d)-(\d\d)-\d\d/', '$2/$1', $first_key );
\r
232 $last_label = preg_replace( '/\d\d-(\d\d)-(\d\d)-\d\d/', '$2/$1', $last_key );
\r
233 $labels_2 = array( $first_label, $last_label);
\r
234 // Get hh of each key
\r
235 foreach( $last_24h as $k=>$v ) {
\r
236 $labels_1[] = end( explode( '-', $k ) ); // 'hh'
\r
239 echo "<h3>Number of hits : $title</h3>";
\r
240 yourls_stats_line( $last_24h, $labels_1, $labels_2 );
\r
245 // each key of $list_of_days is of type "yyyy-mm-dd"
\r
246 $slice = array_slice( $list_of_days, intval( $graph ) * -1 );
\r
247 foreach( $slice as $k=>$v ) {
\r
249 $labels_1[] = preg_replace( '/\d\d\d\d-\d\d-(\d\d)/', '$1', $k );
\r
251 $first_key = current( array_keys( $slice ) );
\r
252 $last_key = end( array_keys( $slice ) );
\r
253 // Get "dd/mm" of first and last key
\r
254 $first_label = preg_replace( '/\d\d\d\d-(\d\d)-(\d\d)/', '$2/$1', $first_key );
\r
255 $last_label = preg_replace( '/\d\d\d\d-(\d\d)-(\d\d)/', '$2/$1', $last_key );
\r
256 $labels_2 = array( $first_label, $last_label);
\r
258 echo "<h3>Number of hits : $title</h3>";
\r
259 yourls_stats_line( $slice, $labels_1, $labels_2 );
\r
265 foreach( $list_of_days as $k=>$v ) {
\r
266 $labels_1[] = preg_replace( '/\d\d(\d\d)-(\d\d)-\d\d/', '$1-$2', $k );
\r
268 // take out duplicates
\r
269 $labels_1 = array_unique( $labels_1 );
\r
270 // now get "mm" only so we have all different month
\r
271 foreach( $labels_1 as $k=>$v ) {
\r
272 $labels_1[$k] = preg_replace( '/\d\d-(\d\d)/', '$1', $v );
\r
275 echo "<h3>Number of hits : $title</h3>";
\r
276 $labels_1 = yourls_array_granularity( $labels_1, 30, false );
\r
277 $labels_2 = yourls_array_granularity( $list_of_years, 30, false );
\r
278 yourls_stats_line( $list_of_days, $labels_1, $labels_2 );
\r
287 <h3>Historical click count</h3>
\r
289 $ago = round( (date('U') - strtotime($timestamp)) / (24* 60 * 60 ) );
\r
293 $ago = '(about '.$ago .' '.yourls_plural( ' day', $ago ).' ago)';
\r
296 <p>Short URL created on <?php echo date("F j, Y @ g:i a", strtotime($timestamp)); ?> <?php echo $ago; ?></p>
\r
297 <div class="wrap_unfloat">
\r
298 <ul class="no_bullet toggle_display stat_line" id="historical_clicks">
\r
300 foreach( $graphs as $graph => $title ) {
\r
301 if ( ${'do_'.$graph} ) {
\r
302 $link = "<a href='#stat_line_$graph'>$title</a>";
\r
306 $hits = yourls_plural( 'hit', ${'hits_'.$graph} );
\r
307 echo "<li><span class='historical_link'>$link</span> ${'hits_'.$graph} $hits</li>\n";
\r
314 <?php $best = yourls_stats_get_best_day( $list_of_days ); ?>
\r
315 <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
316 <a href="" class='details' id="more_clicks">Click for more details</a></p>
\r
317 <ul id="details_clicks" style="display:none">
\r
319 foreach( $dates as $year=>$months ) {
\r
320 if( count( $list_of_years ) > 1 ) {
\r
321 $li = "<a href='' class='details' id='more_year$year'>Year $year</a>";
\r
324 $li = "Year $year";
\r
325 $display = 'block';
\r
328 echo "<ul style='display:$display' id='details_year$year'>";
\r
329 foreach( $months as $month=>$days ) {
\r
330 $monthname = date("F", mktime(0, 0, 0, $month,1));
\r
331 if( count( $list_of_months ) > 1 ) {
\r
332 $li = "<a href='' class='details' id='more_month$year$month'>$monthname</a>";
\r
335 $li = "$monthname";
\r
336 $display = 'block';
\r
339 echo "<ul style='display:$display' id='details_month$year$month'>";
\r
340 foreach( $days as $day=>$hits ) {
\r
341 $class = ( $hits == $best['max'] ? 'class="bestday"' : '' );
\r
342 echo "<li $class>$day: $hits". yourls_plural( ' hit', $hits) ."</li>\n";
\r
359 echo "<p>No traffic yet. Get some clicks first!</p>";
\r
364 <div id="stat_tab_location" class="tab">
\r
365 <h2>Traffic location</h2>
\r
367 <?php if ( $countries ) { ?>
\r
369 <table border="0" cellspacing="2">
\r
372 <h3>Top 5 countries</h3>
\r
373 <?php yourls_stats_pie( $countries, 5 ); ?>
\r
374 <p><a href="" class='details' id="more_countries">Click for more details</a></p>
\r
375 <ul id="details_countries" style="display:none" class="no_bullet">
\r
377 foreach( $countries as $code=>$count ) {
\r
378 echo "<li><img src='".yourls_geo_get_flag( $code )."' /> $code (".yourls_geo_countrycode_to_countryname( $code ).") : $count ".yourls_plural('hit', $count)."</li>\n";
\r
385 <h3>Overall traffic</h3>
\r
386 <?php yourls_stats_countries_map( $countries ); ?>
\r
392 echo "<p>No country data.</p>";
\r
397 <div id="stat_tab_sources" class="tab">
\r
398 <h2>Traffic Sources</h2>
\r
400 <?php if ( $referrers ) { ?>
\r
402 <table border="0" cellspacing="2">
\r
405 <h3>Referrer shares</h3>
\r
407 if ( $number_of_sites > 1 )
\r
408 $referrer_sort['Others'] = count( $referrers );
\r
409 yourls_stats_pie( $referrer_sort, 5, '440x220', '902020,FF6060' );
\r
410 unset( $referrer_sort['Others'] );
\r
413 <ul class="no_bullet">
\r
416 foreach( $referrer_sort as $site => $count ) {
\r
418 echo "<li class='sites_list'><img src='http://$site/favicon.ico' class='fix_images'/> $site: <strong>$count</strong> <a href='' class='details' id='more_url$i'>(details)</a></li>\n";
\r
419 echo "<ul id='details_url$i' style='display:none'>";
\r
420 foreach( $referrers[$site] as $url => $count ) {
\r
421 echo "<li>"; yourls_html_link($url); echo ": <strong>$count</strong></li>\n";
\r
424 unset( $referrers[$site] );
\r
426 // Any referrer left? Group in "various"
\r
427 if ( $referrers ) {
\r
428 echo "<li id='sites_various'>Various: <strong>". count( $referrers ). "</strong> <a href='' class='details' id='more_various'>(details)</a></li>\n";
\r
429 echo "<ul id='details_various' style='display:none'>";
\r
430 foreach( $referrers as $url ) {
\r
431 echo "<li>"; yourls_html_link(key($url)); echo ": 1</li>\n";
\r
442 <h3>Direct vs Referrer Traffic</h3>
\r
444 $ref_traffic = count($referrer_sort) + count($referrers);
\r
445 yourls_stats_pie( array('Direct'=>$direct, 'Referrers'=> $ref_traffic), 5, '440x220', '902020,FF6060' );
\r
447 <p>Direct traffic: <strong><?php echo $direct; ?></strong> <?php echo yourls_plural( 'hit', $direct ); ?> </p>
\r
448 <p>Referrer traffic: <strong><?php echo $ref_traffic; ?></strong> <?php echo yourls_plural( 'hit', $ref_traffic ); ?> </p>
\r
455 echo "<p>No referrer data.</p>";
\r
460 <div id="stat_tab_share" class="tab">
\r
463 <?php yourls_share_box( $longurl, YOURLS_SITE.'/'.$keyword, '', '', '<h3>Short link</h3>', '<h3>Quick Share</h3>'); ?>
\r
470 <?php yourls_html_footer(); ?>