使用GAPI返回包含视图百分比的页面列表

时间:2011-03-28 14:23:53

标签: php api google-analytics

任何人都知道如何使用GAPI从我的Google分析帐户中返回以下信息?

GA account

1 个答案:

答案 0 :(得分:3)

您需要使用pagePath维度和pageviews指标。尝试以下内容:

<?php
require 'gapi.class.php';

$gaEmail = 'youremail@email.com';
$gaPassword = 'your password';
$profileId = 'your profile id';

$dimensions = array('pagePath');
$metrics = array('pageviews');
$sortMetric=null;
$filter=null;
$startDate='2011-02-01';
$endDate='2011-02-28';
$startIndex=1;
$maxResults=10000;

$ga = new gapi($gaEmail, $gaPassword);

$ga->requestReportData($profileId, $dimensions, $metrics, $sortMetric, $filter, $startDate, $endDate, $startIndex, $maxResults);

$totalPageviews = $ga->getPageviews();

foreach ($ga->getResults() as $result) {
    $pageviews = $result->getPageviews();
    $percentPageviews = round(($result->getPageviews()/$totalPageviews)*100,2);
    print "$result:$pageviews:$percentPageviews\n";
}

?>