所以我所做的是,我设置 Request.MaxResult = 10000 和 Response.ItemsPerPage = 10000。至关重要的角色由具有分页机制的 Request.StartIndex 扮演,例如在第一个循环中,我们将获得 1-10000 行,然后是 10001-20000。 ...20001-30000 等等。
在循环下,对特定日期数据的请求最初设置为 Request.StartIndex = 1,然后在行数达到第 10000 行时连续增加到 MaxResult + 1 例如:- Request.StartIndex = 1 然后 10001 然后 20001 ... .30001...40001 依此类推,直到 Response.Rows == null。
这是我的代码:-
StringBuilder sbrCSVData = new StringBuilder();
int intStartIndex = 1;
int intIndexCnt = 0;
int intMaxRecords = 10000;
AnalyticsService objAnaSrv = new AnalyticsService(objInit);
string metrics = "ga:visitors,ga:visits,ga:visitBounceRate,ga:pageviews,ga:pageviewsPerVisit,ga:uniquePageviews,ga:avgTimeOnPage,ga:exits,ga:avgPageLoadTime,ga:goal1ConversionRate";
DataResource.GaResource.GetRequest objRequest = objAnaSrv.Data.Ga.Get(strProfId, startDate,endDate, metrics);
objRequest.Dimensions = "ga:visitCount,ga:browser,ga:browserVersion,ga:IsMobile,ga:screenResolution,ga:date,ga:hour";
objRequest.MaxResults = 10000;
while (true)
{
objRequest.StartIndex = intStartIndex;
GaData objResponse = objRequest.Fetch();
objResponse.ItemsPerPage = intMaxRecords;
if (objResponse.Rows != null)
{
intIndexCnt++;
for (int intRows = 0; intRows < objResponse.Rows.Count; intRows++)
{
IList<string> lstRow = objResponse.Rows[intRows];
for (int intCols = 0; intCols <= lstRow.Count - 1; intCols++)
{
strRowData += lstRow[intCols].ToString() + "|";
}
strRowData = strRowData.Remove(strRowData.Length - 1, 1);
sbrCSVData.Append(strRowData + "\r\n");
strRowData = "";
}
intStartIndex = intIndexCnt * intMaxRecords + 1;
}
else break;
}