我想获得一些使用ga:pagePath
过滤的Google Analytics数据(页面浏览量)(API V3)。
首先,我使用以下代码连接到API:
$token = "some token";
$client = new Google_Client();
$client->setApplicationName("***");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setDeveloperKey($developer_key);
$client->setScopes(array("https://www.googleapis.com/auth/analytics.readonly"));
$client->setApprovalPrompt('force');
$client->setAccessType('offline');
$client->setAccessToken($token);
然后我用各自的个人资料(观点)获得所有帐户:
$service = new Google_Service_Analytics($client);
$accounts = $service->management_accountSummaries->listManagementAccountSummaries();
$profiles = new stdClass();
foreach ($accounts->getItems() as $item) {
foreach($item->getWebProperties() as $wp) {
$views = $wp->getProfiles();
if (!is_null($views)) {
foreach($wp->getProfiles() as $view) {
$profiles->{$view['id']} = $view['name'];
}
}
}
}
获得所有个人资料(观点)后,我得到了数据:
$path = "/example-path/path-example";
$host = "www.example.com";
$optParams = array(
'dimensions' => 'ga:pagePath,ga:source,ga:keyword',
'sort' => '-ga:sessions,ga:source',
'filters' => 'ga:medium==organic,ga:pagePath=='.$path.',ga:hostname=='.$host,
'max-results' => '25'
);
$totalSessions = 0;
$today = date('Y-m-d', time());
$monthbefore = date('Y-m-d', (time() - (30 * 24 * 60 * 60)));
foreach ($profiles as $profile_id => $profile) {
$data = $service->data_ga->get(
"ga:".$profile_id,
$monthbefore,
$today,
'ga:sessions',
$optParams
);
$totalSessions += intval($data->totalsForAllResults['ga:sessions']);
}
if (!isset($analytics_data)) {
$analytics_data = new stdClass();
}
if (!isset($analytics_data->$ii)) {
$analytics_data->$ii = new stdClass();
}
$analytics_data->$ii->{'total_page_views'} = $totalSessions;
现在我得到了数据。但问题是我得到整个网站的页面浏览总数,而我需要具有特定路径的页面的页面浏览量" / example-path / path-example"。我无法弄清楚我到底在哪里。提前谢谢。