Skip to content

Commit 6fc0826

Browse files
authored
Merge pull request #119 from keboola/CM-713-ondra
New sync action - get properties metadata
2 parents 1dc4f14 + c61f8ac commit 6fc0826

10 files changed

Lines changed: 82 additions & 3 deletions

File tree

src/Component.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Component extends BaseComponent
2929
private const ACTION_SEGMENTS = 'segments';
3030
private const ACTION_CUSTOM_METRICS = 'customMetrics';
3131
private const ACTION_GET_PROFILES_PROPERTIES = 'getProfilesProperties';
32+
private const ACTION_GET_PROPERTIES_METADATA = 'getPropertiesMetadata';
3233

3334
public function getConfig(): Config
3435
{
@@ -192,6 +193,21 @@ protected function runCustomMetricsAction(): array
192193
return $result;
193194
}
194195

196+
protected function getPropertiesMetadata(): array
197+
{
198+
$properties = $this->getConfig()->getProperties();
199+
$viewId = $this->getConfig()->getQuery()['viewId'] ?? null;
200+
201+
$result = [];
202+
try {
203+
$result = $this->getExtractor()->getPropertiesMetadata($properties, $viewId);
204+
} catch (RequestException $e) {
205+
$this->handleException($e);
206+
}
207+
208+
return $result;
209+
}
210+
195211
protected function getProfilesPropertiesAction(): array
196212
{
197213
return $this->getExtractor()->getProfilesPropertiesAction();
@@ -222,6 +238,7 @@ protected function getSyncActions(): array
222238
{
223239
return [
224240
self::ACTION_GET_PROFILES_PROPERTIES => 'getProfilesPropertiesAction',
241+
self::ACTION_GET_PROPERTIES_METADATA => 'getPropertiesMetadata',
225242
self::ACTION_SAMPLE => 'runSampleAction',
226243
self::ACTION_SAMPLE_JSON => 'runSampleJsonAction',
227244
self::ACTION_SEGMENTS => 'runSegmentsAction',

src/Extractor/Extractor.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,31 @@ private function getErrorMessage(string $message): string
404404
$reason
405405
);
406406
}
407+
408+
public function getPropertiesMetadata(array $properties, ?string $viewId = null): array
409+
{
410+
$metadata = [
411+
'dimensions' => [],
412+
'metrics' => [],
413+
];
414+
foreach ($properties as $property) {
415+
if ($viewId && $property['propertyKey'] !== $viewId) {
416+
continue;
417+
}
418+
$propertyMetadata = $this->gaApi->getPropertyMetadata($property['propertyKey']);
419+
foreach (['dimensions', 'metrics'] as $type) {
420+
$metadataTypeExists = array_map(fn(array $item) => $item['apiName'], $metadata[$type]);
421+
$propertyMetadataType = $propertyMetadata[$type];
422+
423+
$propertyMetadataType = array_filter($propertyMetadataType, function ($item) use ($metadataTypeExists) {
424+
if (in_array($item['apiName'], $metadataTypeExists)) {
425+
return false;
426+
}
427+
return true;
428+
});
429+
$metadata[$type] = array_merge($propertyMetadataType, $metadata[$type]);
430+
}
431+
}
432+
return $metadata;
433+
}
407434
}

src/GoogleAnalytics/Client.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Client
2222
private const MCF_URL = 'https://www.googleapis.com/analytics/v3/data/mcf';
2323
private const SEGMENTS_URL = 'https://www.googleapis.com/analytics/v3/management/segments';
2424
private const CUSTOM_METRICS_URL = 'https://www.googleapis.com/analytics/v3/management/accounts/%s/webproperties/%s/customMetrics';
25+
private const PROPERTY_METADATA_URL = 'https://analyticsdata.googleapis.com/v1beta/%s/metadata';
2526
/** @phpcs:enable */
2627

2728
protected GoogleApi $api;
@@ -76,6 +77,14 @@ public function getAccountProperties(int $pageSize = self::PAGE_SIZE): array
7677
return array_filter(array_merge([], ...$accountSummaries), fn(array $v) => isset($v['propertySummaries']));
7778
}
7879

80+
public function getPropertyMetadata(string $propertyId): array
81+
{
82+
$url = sprintf(self::PROPERTY_METADATA_URL, $propertyId);
83+
$response = $this->api->request($url);
84+
85+
return json_decode($response->getBody()->getContents(), true);
86+
}
87+
7988
public function getAccounts(): array
8089
{
8190
$response = $this->api->request(self::ACCOUNTS_URL);

tests/Keboola/GoogleAnalyticsExtractor/GoogleAnalytics/ClientTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public function testRetryOnUnknownMetric(): void
124124
],
125125
];
126126

127+
$this->client->getApi()->setBackoffsCount(3);
127128
try {
128129
$this->client->getBatch($query);
129130
} catch (ClientException $e) {
@@ -132,7 +133,7 @@ public function testRetryOnUnknownMetric(): void
132133

133134
/** @var TestHandler $testHandler */
134135
$testHandler = $this->logger->getHandlers()[0];
135-
for ($i = 1; $i <= 6; $i++) {
136+
for ($i = 1; $i < 3; $i++) {
136137
/** @var \Monolog\LogRecord $record */
137138
$record = $testHandler->getRecords()[$i];
138139
$this->assertEquals(

tests/functional/DatadirTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public function __construct(?string $name = null, array $data = [], string $data
1515
'access_token' => getenv('ACCESS_TOKEN'),
1616
'refresh_token' => getenv('REFRESH_TOKEN'),
1717
];
18-
1918
putenv(sprintf(
2019
'CREDENTIALS_DATA=%s',
2120
json_encode($credentialsData)

tests/functional/get-profile-properties-sync-action-error-handling/source/data/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"credentials": {
55
"appKey": "1025566943657-k9nvvskd1jbet47h0cuhmqktha05mf4s.apps.googleusercontent.com",
66
"#appSecret": "%env(string:CLIENT_SECRET)%",
7-
"#data": "%env(string:CREDENTIALS_DATA)%"
7+
"#data": "{\"access_token\": \"invalid_access_token\", \"refresh_token\": \"invalid_refresh_token\"}"
88
}
99
}
1010
},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"dimensions":[{%s}],"metrics":[{%s}]}

tests/functional/get-properties-metadata-sync-action/expected/data/out/files/.gitkeep

Whitespace-only changes.

tests/functional/get-properties-metadata-sync-action/expected/data/out/tables/.gitkeep

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"authorization": {
3+
"oauth_api": {
4+
"credentials": {
5+
"appKey": "%env(string:CLIENT_ID)%",
6+
"#appSecret": "%env(string:CLIENT_SECRET)%",
7+
"#data": "%env(string:CREDENTIALS_DATA)%"
8+
}
9+
}
10+
},
11+
"parameters": {
12+
"outputBucket": "in.c-ex-google-analytics-cfg",
13+
"outputTable": "test",
14+
"retriesCount": 1,
15+
"properties": [
16+
{
17+
"accountKey": "accounts/185283969",
18+
"accountName": "Keboola",
19+
"propertyKey": "properties/255885884",
20+
"propertyName": "users"
21+
}
22+
]
23+
},
24+
"action": "getPropertiesMetadata"
25+
}

0 commit comments

Comments
 (0)