Skip to content

Commit 6ec22b3

Browse files
authored
Merge pull request #121 from keboola/CM-765-ondra
profiles paging
2 parents 358eca3 + 622c04d commit 6ec22b3

3 files changed

Lines changed: 20 additions & 9 deletions

File tree

src/GoogleAnalytics/Client.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,22 @@ public function getWebProperties(): array
9999
return $body['items'] ?? [];
100100
}
101101

102-
public function getAccountProfiles(): array
102+
public function getAccountProfiles(int $pageSize = self::PAGE_SIZE): array
103103
{
104-
$response = $this->api->request(self::ACCOUNT_PROFILES_URL);
105-
$body = json_decode($response->getBody()->getContents(), true);
106-
return $body['items'] ?? [];
104+
$items = [];
105+
do {
106+
$url = sprintf('%s?max-results=%d', self::ACCOUNT_PROFILES_URL, $pageSize);
107+
if (isset($body['nextLink'])) {
108+
$url = $body['nextLink'];
109+
}
110+
$response = $this->api->request($url);
111+
$body = json_decode($response->getBody()->getContents(), true);
112+
if (isset($body['items'])) {
113+
$items[] = $body['items'];
114+
}
115+
} while (isset($body['nextLink']));
116+
117+
return array_merge([], ...$items);
107118
}
108119

109120
public function getCustomMetrics(int $accountId, string $webPropertyId): array

tests/Keboola/GoogleAnalyticsExtractor/Extractor/ExtractorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function testGetProfilesProperties(): void
199199
->with($this->logicalOr(
200200
sprintf('%s?pageSize=%d', Client::ACCOUNT_PROPERTIES_URL, Client::PAGE_SIZE),
201201
Client::ACCOUNT_WEB_PROPERTIES_URL,
202-
Client::ACCOUNT_PROFILES_URL,
202+
sprintf('%s?max-results=%d', Client::ACCOUNT_PROFILES_URL, Client::PAGE_SIZE),
203203
Client::ACCOUNTS_URL
204204
))
205205
->will($this->returnCallback(array($this, 'returnMockServerRequest')))
@@ -257,8 +257,8 @@ public function testGetEmptyProfilesProperties(): void
257257
->method('request')
258258
->with($this->logicalOr(
259259
sprintf('%s?pageSize=%d', Client::ACCOUNT_PROPERTIES_URL, Client::PAGE_SIZE),
260+
sprintf('%s?max-results=%d', Client::ACCOUNT_PROFILES_URL, Client::PAGE_SIZE),
260261
Client::ACCOUNT_WEB_PROPERTIES_URL,
261-
Client::ACCOUNT_PROFILES_URL,
262262
Client::ACCOUNTS_URL
263263
))
264264
->will($this->returnCallback(array($this, 'returnMockServerRequestEmptyResponse')))
@@ -366,7 +366,7 @@ public function returnMockServerRequest(string $url): Response
366366
[],
367367
'{"accountSummaries":[{"name":"accountSummaries/128209249","account":"accounts/128209249","displayName":"Keboola Website"},{"name":"accountSummaries/185283969","account":"accounts/185283969","displayName":"Ondřej Jodas","propertySummaries":[{"property":"properties/255885884","displayName":"users"}]},{"name":"accountSummaries/52541130","account":"accounts/52541130","displayName":"Keboola Status Blog"}]}'
368368
);
369-
case Client::ACCOUNT_PROFILES_URL:
369+
case sprintf('%s?max-results=%d', Client::ACCOUNT_PROFILES_URL, Client::PAGE_SIZE):
370370
return new Response(
371371
200,
372372
[],

tests/Keboola/GoogleAnalyticsExtractor/Extractor/ValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function returnMockServerRequest(string $url): Response
158158
[],
159159
'{"accountSummaries":[{"name":"accountSummaries/128209249","account":"accounts/128209249","displayName":"Keboola Website"},{"name":"accountSummaries/185283969","account":"accounts/185283969","displayName":"Ondřej Jodas","propertySummaries":[{"property":"properties/255885884","displayName":"users"}]},{"name":"accountSummaries/52541130","account":"accounts/52541130","displayName":"Keboola Status Blog"}]}'
160160
);
161-
case Client::ACCOUNT_PROFILES_URL:
161+
case sprintf('%s?max-results=%d', Client::ACCOUNT_PROFILES_URL, Client::PAGE_SIZE):
162162
return new Response(
163163
200,
164164
[],
@@ -176,7 +176,7 @@ private function getMockRestApi(): RestApi
176176
->method('request')
177177
->with($this->logicalOr(
178178
sprintf('%s?pageSize=%d', Client::ACCOUNT_PROPERTIES_URL, Client::PAGE_SIZE),
179-
Client::ACCOUNT_PROFILES_URL
179+
sprintf('%s?max-results=%d', Client::ACCOUNT_PROFILES_URL, Client::PAGE_SIZE)
180180
))
181181
->will($this->returnCallback([$this, 'returnMockServerRequest']));
182182

0 commit comments

Comments
 (0)