Google Analytics API如何检索下一页数据 [英] Google Analytics API how to retrieve next page of data
问题描述
我使用Google Analytics API检索我的某个配置文件的报告数据。如果报告中的行数超过1000,则响应包含1,000个结果以及一个名为 nextPage
的参数,其中包含下一页数据的URL。我很困惑如何实际使用这个URL来检索数据。我用什么API方法来实际获取下一页数据。这是我的代码:
$ client = new Google_Client();
$ client-> setApplicationName('Google Analytics'); //您的应用名称
//设置断言凭证
$ client-> setAssertionCredentials(
new Google_Auth_AssertionCredentials(
GOOGLE_ANALYTICS_SERVICE_EMAIL,// email您添加到GA
数组('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents(storage_path()。'/ keys /privatekey.p12')//你下载的密钥文件
));
$ client-> setClientId(GOOGLE_ANALYTICS_CLIENT_ID); //从API控制台
$ service = new Google_Service_Analytics($ client);
$ result = $ service-> data_ga-> get(
'ga:'。DEFAULT_VIEW_ID,
'2014-09-01',
' 2015-01-26',
'ga:uniquePageViews',
array(
'dimensions'=>'ga:dimension1',
'filters'=>' ga:dimension3 == product'
)
);
print_r($ result);
这个结果是 Google_Service_Analytics_GaData
其中包含1000行以及以下数据:
[nextLink] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:86454007&dimensions=ga:dimension1&metrics=ga:uniquePageViews&filters=ga:dimension3%3D%3Dproduct&start-date= 2014-09-01& end-date = 2015-01-26& start-index = 1001& max-results = 1000
如何使用这个 nextLink
来检索下一页数据?如果你看一下这个链接中的参数,你会发现这个链接中的参数会发现它与原始查询相同,但 start-index
值设置为 1001
: p>
https://www.googleapis.com/analytics/v3/data/ga?
ids = ga:86454007&
dimensions = ga:dimension1&
metrics = ga:uniquePageViews&
filters = ga:dimension3%3D%3Dproduct&
start-date = 2014-09-01&
end-date = 2015-01-26&
start-index = 1001&
max-results = 1000
所以基本上你必须继续查询,直到 start-index
+ itemsPerPage
> totalResults
。另外,如果你知道你将有一个大的数据集,你通常可以将 max-results
字段设置为像 10000
。
它不是PHP SDK的一部分,但这个模块显示了一个直到 totalResults
已到达。
I'm using the Google Analytics API to retrieve reporting data for one of my profiles. If the number of rows in the report exceeds 1000, the response contains 1,000 results plus a parameter called nextPage
, which contains a URL for the next page of data. I am confused how to actually use this URL in order to retrieve the data. What API method to I use to actually get the next page of data. Here's my code:
$client = new Google_Client();
$client->setApplicationName('Google Analytics'); // name of your app
// set assertion credentials
$client->setAssertionCredentials(
new Google_Auth_AssertionCredentials(
GOOGLE_ANALYTICS_SERVICE_EMAIL, // email you added to GA
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents(storage_path().'/keys/privatekey.p12') // keyfile you downloaded
));
$client->setClientId(GOOGLE_ANALYTICS_CLIENT_ID); // from API console
$service = new Google_Service_Analytics($client);
$result = $service->data_ga->get(
'ga:'.DEFAULT_VIEW_ID,
'2014-09-01',
'2015-01-26',
'ga:uniquePageViews',
array(
'dimensions'=>'ga:dimension1',
'filters'=>'ga:dimension3==product'
)
);
print_r($result);
The result of this is Google_Service_Analytics_GaData
object, which contains data for 1000 rows plus this:
[nextLink] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:86454007&dimensions=ga:dimension1&metrics=ga:uniquePageViews&filters=ga:dimension3%3D%3Dproduct&start-date=2014-09-01&end-date=2015-01-26&start-index=1001&max-results=1000
How can I use this nextLink
to retrieve the next page of data? There must be some mechanism for this built into the Google SDK, right?
If you look at the parameters in that link you'll notice it's the same as your original query, but the start-index
value is set to 1001
:
https://www.googleapis.com/analytics/v3/data/ga?
ids=ga:86454007&
dimensions=ga:dimension1&
metrics=ga:uniquePageViews&
filters=ga:dimension3%3D%3Dproduct&
start-date=2014-09-01&
end-date=2015-01-26&
start-index=1001&
max-results=1000
So basically you have to keep doing queries until start-index
+ itemsPerPage
> totalResults
. Also, if you know you're going to have a large dataset, you can often set the max-results
field to something higher like 10000
.
It's not part of the PHP SDK, but this module shows an example of making multiple requests until totalResults
is reached.
这篇关于Google Analytics API如何检索下一页数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!