@@ -47,6 +47,7 @@ function run_report_with_pagination(string $propertyId)
47
47
// Create an instance of the Google Analytics Data API client library.
48
48
$ client = new BetaAnalyticsDataClient ();
49
49
50
+ // [START analyticsdata_run_report_with_pagination_page1]
50
51
// Make an API call.
51
52
$ request = (new RunReportRequest ())
52
53
->setProperty ('properties/ ' . $ propertyId )
@@ -68,19 +69,42 @@ function run_report_with_pagination(string $propertyId)
68
69
])
69
70
->setLimit (100000 )
70
71
->setOffset (0 );
72
+
73
+ $ requestCount = 1 ;
74
+ printf ('Sending request #%d ' . PHP_EOL , $ requestCount );
75
+
71
76
$ response = $ client ->runReport ($ request );
77
+ # [END analyticsdata_run_report_with_pagination_page1]
78
+
79
+ printRunReportResponseWithPagination ($ response , $ requestCount );
72
80
73
- printRunReportResponseWithPagination ($ response );
81
+ // [START analyticsdata_run_report_with_pagination_page2]
82
+ $ rowsReceived = count ($ response ->getRows ());
83
+ $ totalRows = $ response ->getRowCount ();
84
+
85
+ // Run the same report with an increased offset value to retrieve each additional
86
+ // page until all rows are received.
87
+ while ($ rowsReceived < $ totalRows ) {
88
+ $ request = $ request ->setOffset ($ rowsReceived );
89
+ $ requestCount ++;
90
+ printf ('Sending request #%d ' . PHP_EOL , $ requestCount );
91
+
92
+ $ response = $ client ->runReport ($ request );
93
+ $ rowsReceived += count ($ response ->getRows ());
94
+ printRunReportResponseWithPagination ($ response , $ requestCount );
95
+ }
96
+ // [END analyticsdata_run_report_with_pagination_page2]
74
97
}
75
98
76
99
/**
77
100
* Print results of a runReport call.
78
101
* @param RunReportResponse $response
102
+ * @param int $requestCount
79
103
*/
80
- function printRunReportResponseWithPagination (RunReportResponse $ response )
104
+ function printRunReportResponseWithPagination (RunReportResponse $ response, int $ requestCount )
81
105
{
82
106
// [START analyticsdata_print_run_report_response_header]
83
- printf ('%s rows received% s ' , $ response ->getRowCount () , PHP_EOL );
107
+ printf ('%s rows received for request #%d% s ' , count ( $ response ->getRows ()), $ requestCount , PHP_EOL );
84
108
foreach ($ response ->getDimensionHeaders () as $ dimensionHeader ) {
85
109
printf ('Dimension header name: %s%s ' , $ dimensionHeader ->getName (), PHP_EOL );
86
110
}
0 commit comments