[英]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'); // 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);
其結果是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
檢索下一頁數據? Google SDK必須內置某種機制,對嗎?
如果您查看該鏈接中的參數,您會注意到它與原始查詢相同,但是start-index
值設置為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
因此,基本上,您必須繼續進行查詢,直到start-index
+ itemsPerPage
> totalResults
。 另外,如果您知道將要有一個大型數據集,則通常可以將max-results
字段設置為更高的值,例如10000
。
它不是PHP SDK的一部分,但是此模塊顯示了一個示例,該示例提出多個請求,直到達到totalResults
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.