CREATE TABLE `art_stat` (
`art_id` INT NOT NULL ,
`day_stat` INT DEFAULT '0' ,
`week_stat` INT DEFAULT '0' ,
`month_stat` INT DEFAULT '0' ,
`year_stat` INT DEFAULT '0' ,
UNIQUE (
`art_id`
)
);
* This source code was highlighted with Source Code Highlighter .
<?php
set_time_limit(0);
require_once 'start.php' ; //
require_once 'gapi.class.php' ; //
// -
define( 'ga_email' , '...' );
define( 'ga_password' , '...' );
define( 'ga_profile_id' , '...' );
//
// ,
// d -
// w -
// m -
// y -
set_stat( 'd' );
function set_stat( $time_limiter = 'd' ) {
switch ( $time_limiter ) {
case 'd' :
// Day
$start_stamp = mktime( date( "H" ), date( "i" ), date( "s" ), date( "n" ), date( "j" )-1, date( "Y" ) );
$field = 'day_stat' ;
break ;
case 'w' :
// Week
$start_stamp = mktime( date( "H" ), date( "i" ), date( "s" ), date( "n" ), date( "j" )-7, date( "Y" ) );
$field = 'week_stat' ;
break ;
case 'm' :
// Month
$start_stamp = mktime( date( "H" ), date( "i" ), date( "s" ), date( "n" )-1, date( "j" ), date( "Y" ) );
$field = 'month_stat' ;
break ;
case 'y' :
// Year
$start_stamp = mktime( date( "H" ), date( "i" ), date( "s" ), date( "n" ), date( "j" ), date( "Y" )-1 );
$field = 'year_stat' ;
break ;
} // End switch
$end_date = date( 'Ym-d' );
$start_date = date( 'Ym-d' , $start_stamp );
echo "From " . $start_date . " to " . $end_date . "<br>" ;
$ga = new gapi(ga_email,ga_password); //
$start_index = 1;
$max_results = 500;
$filter = null ; // . , ,
// GA
while ( $ga->requestReportData(ga_profile_id, array( 'pagePath' ), array( 'pageviews' , 'uniquePageviews' ), array( '-Pageviews' ), $filter, $start_date, $end_date, $start_index, $max_results) ) {
$request = $ga->getResults();
foreach ( $request as $page ) {
if ( preg_match( "/^\/(\d+)\.html$/is" , $page, $matches ) ) { // URL
$art_id = $matches[1]; // , ID URL ID
//
mysql_query( "UPDATE art_stat
SET " . $field . " = " . $page->getuniquePageviews() . "
WHERE art_id = " . $art_id
);
if ( mysql_affected_rows() == 0 ) {
mysql_query( "INSERT INTO art_stat ( art_id, " . $field . " )
VALUES ( " . $art_id . ", " . $page->getuniquePageviews() . " )"
);
} // End if
} // End if
} // End foreach
$start_index += $max_results;
} // End while
} // End function set_stat
require_once 'end.php' ; //
?>
* This source code was highlighted with Source Code Highlighter .
SELECT a.id, a.name, s.week_stat AS cnt
FROM art a LEFT JOIN art_stat s ON ( s.art_id = a.id )
ORDER BY cnt DESC
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/69651/
All Articles