问题描述
我能够从谷歌分析 api 运行hello"应用程序:
I was able to run de "hello" app from google analytics api:
function getFirstProfileId($analytics) {
// Get the user's first view (profile) ID.
// Get the list of accounts for the authorized user.
$accounts = $analytics->management_accounts->listManagementAccounts();
if (count($accounts->getItems()) > 0) {
$items = $accounts->getItems();
$firstAccountId = $items[0]->getId();
// Get the list of properties for the authorized user.
$properties = $analytics->management_webproperties
->listManagementWebproperties($firstAccountId);
if (count($properties->getItems()) > 0) {
$items = $properties->getItems();
$firstPropertyId = $items[0]->getId();
// Get the list of views (profiles) for the authorized user.
$profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstPropertyId);
if (count($profiles->getItems()) > 0) {
$items = $profiles->getItems();
// Return the first view (profile) ID.
return $items[0]->getId();
} else {
throw new Exception('No views (profiles) found for this user.');
}
} else {
throw new Exception('No properties found for this user.');
}
} else {
throw new Exception('No accounts found for this user.');
}
}
function getResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
return $analytics->data_ga->get(
'ga:' . $profileId,
'7daysAgo',
'today',
'ga:sessions'
);
}
这会返回(整个)帐户的页面浏览量.但是我想获得页面浏览量(针对整个范围)针对特定网址,
That returns the page views for the (whole) account. But I would like to get the page views ( for the whole range ) for a specific url,
我正在尝试寻找方法,但最类似页面 似乎是用不同的语法编写的.所以我不知道如何在当前应用中输入这些参数.
I'm trying to find how to do so, but the most similar page that I found, seems to be written in a different syntax. So I don't know how to enter those params in the current app.
有什么想法吗?
--编辑--
更多信息,前面的代码,打印:
For more information, the previous code, prints:
但是这段代码(带有我之前提供的链接的语法")
But this code ( with the "syntax" of the link I provided before )
function getReport($analytics) {
// Replace with your view ID, for example XXXX.
$VIEW_ID = "40xxxyyy9"; // I got it from The Google Account Explorer
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("7daysAgo");
$dateRange->setEndDate("today");
// Create the Metrics object.
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($sessions));
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analytics->reports->batchGet( $body );
}
function printResults($reports) {
for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
$report = $reports[ $reportIndex ];
$header = $report->getColumnHeader();
$dimensionHeaders = $header->getDimensions();
$metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
$rows = $report->getData()->getRows();
for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
$row = $rows[ $rowIndex ];
$dimensions = $row->getDimensions();
$metrics = $row->getMetrics();
for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
}
for ($j = 0; $j < count( $metricHeaders ) && $j < count( $metrics ); $j++) {
$entry = $metricHeaders[$j];
$values = $metrics[$j];
print("Metric type: " . $entry->getType() . "\n" );
for ( $valueIndex = 0; $valueIndex < count( $values->getValues() ); $valueIndex++ ) {
$value = $values->getValues()[ $valueIndex ];
print($entry->getName() . ": " . $value . "\n");
}
}
}
}
}
打印出来:
Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 403, "message": "Google Analytics Reporting API has not been used in project api-project-395xxxxxxx2105 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview?project=api-project-395xxxxxxx2105 then retry.
如果我按照控制台链接访问我的项目.它让我知道已经启用(否则问题中的第一个代码应该不起作用)
And if I follow the console link to my project. It let me know that is already enabled ( otherwise the first code in the question shouldn't work )
推荐答案
您的示例实际上返回的是会话数,而不是浏览量.因此,要获得 URL 的综合浏览量,您需要 a) 将 ga:sessions 更改为 ga:pageViews 和 b) 应用将数据限制为预期 Url 的过滤器.
Your example actually return the number of sessions, not pageviews. So to get the number of pageviews for an Url you would need to a) change ga:sessions to ga:pageViews and b) apply a filter that limits the data to the expected Url.
过滤器可以通过将它们作为可选参数数组的一部分传递来应用,因此您需要稍微更改您的函数(这基本上取自文档):
Filters can by applied by passing them as part of an array of optional parameters, so you'd need to change your function a bit (this basically taken from the documentation):
function getResults($analytics, $profileId) {
$optParams = array(
'filters' => 'ga:pagePath=@/my/url'
);
return $analytics->data_ga->get(
'ga:' . $profileId,
'7daysAgo',
'today',
'ga:pageViews',
$optParams
);
}
IIRC =@
应该是过滤器中的部分匹配(否则请参阅文档),当然您需要将/my/url"更改为您的实际网址.
IIRC =@
should be a partial match in a filter (else consult the documentation) and of course you need to change "/my/url" to your actual url.
另请注意我对不同 API 版本的评论.
Also note my comment about different API versions.
这篇关于Google Analytics api - 通过 URL 获取页面浏览量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!