ソース

require_once 'gapi.class.php' ;

$startDate = date( 'Y-m-d',strtotime( "-8 day" ,time()));
$endDate   = date( 'Y-m-d',strtotime( "-1 day" ,time()));

$setting = array( 'EMAIL'       => 'youremailaddress' ,
                 'PASSWORD'    => 'yourgmailpassword' ,
                 'PROFILE_ID'  => 'XXXXXXXX' ,
                 'DIMENSIONS'  => array ('pageTitle' ,'pagePath' ),
                 'METRICS'     => array ('pageviews' ),
                 'SORT_METRIC' => array ('-pageviews' ),
                 'FILTER'      => 'keyword!=(not set)||pagePath==^\/$' ,
                 'START_DATE'  => $startDate,
                 'END_DATE'    => $endDate,
                 'START_INDEX' => 1,
                 'MAX_RESULTS' => 20
    );
$ga = new gapi($setting[ 'EMAIL'],$setting[ 'PASSWORD' ]);
$ga->requestReportData($setting[ 'PROFILE_ID'],
                       $setting[ 'DIMENSIONS' ],
                       $setting[ 'METRICS'],
                       $setting[ 'SORT_METRIC' ],
                       $setting[ 'FILTER'],
                       $setting[ 'START_DATE' ],
                       $setting[ 'END_DATE' ],
                       $setting[ 'START_INDEX' ],
                       $setting[ 'MAX_RESULTS' ]);
$rankingList = array();
$rankingNum = 1;
foreach ($ga->getResults() as $result) {
        if ($rankingNum > 10) {
               break ;
       }
        if (preg_match('/^\/$/' , $result->getPagePath())) {
               continue ;
       }
        if (preg_match('/tag/' , $result->getPagePath())) {
               continue ;
       }
        elseif (preg_match('/category/' , $result->getPagePath())) {
               continue ;
       }

       $rankingList[$rankingNum][ 'pageview' ] = $result->getPageviews();
       $rankingList[$rankingNum][ 'title'] = $result->getPageTitle();
       $rankingList[$rankingNum][ 'url']   = $result->getPagePath();
       $rankingNum++;
}

$str = null;

/* ここで出力用のデータを加工 */

if (file_put_contents( '/path/to/template/ranking.html' , $str, LOCK_EX)) {
       print_r($rankingList);
}
else {
        echo 'ランキング生成エラー' ;
}

  • gapi
  • API
  • xmlパーサー