From 61e527f223f77913acd34513bb9a5fdc12703fea Mon Sep 17 00:00:00 2001 From: Maurice Kherlakian <1141190+mkherlakian@users.noreply.github.com> Date: Thu, 11 Jul 2019 12:41:48 -0400 Subject: [PATCH] Added segment_name and last_active_since parameters to CSV export (#120) * Added segment_name and last_active_since parameters to CSV export * fixed style issues * removed nullable php7 question mark * make type hinting compatible with 5.6 * removed forgotten int * changed 2 is_null instances to null !==, and added @param to docblock as per code review * fixed style issues * fixed docblock formatting --- src/Devices.php | 17 ++++++++++++++--- tests/OneSignal/Tests/DevicesTest.php | 6 ++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Devices.php b/src/Devices.php index b64bff3..5830fc3 100644 --- a/src/Devices.php +++ b/src/Devices.php @@ -173,12 +173,15 @@ public function onFocus($id, array $data) * * Application auth key must be set. * - * @param array $extraFields Additional fields that you wish to include. - * Currently supports: "location", "country", "rooted" + * @param array $extraFields Additional fields that you wish to include. + * Currently supports: "location", "country", "rooted" + * @param string $segmentName A segment name to filter the scv export by. + * Only devices from that segment will make it into the export + * @param int $lastActiveSince An epoch to filter results to users active after this time * * @return array */ - public function csvExport(array $extraFields = []) + public function csvExport(array $extraFields = [], $segmentName = null, $lastActiveSince = null) { $url = '/players/csv_export?app_id='.$this->api->getConfig()->getApplicationId(); @@ -190,6 +193,14 @@ public function csvExport(array $extraFields = []) 'extra_fields' => $extraFields, ]; + if (null !== $segmentName) { + $body['segment_name'] = $segmentName; + } + + if (null !== $lastActiveSince) { + $body['last_active_since'] = (string) $lastActiveSince; + } + return $this->api->request('POST', $url, $headers, json_encode($body)); } } diff --git a/tests/OneSignal/Tests/DevicesTest.php b/tests/OneSignal/Tests/DevicesTest.php index 10549e1..a432452 100644 --- a/tests/OneSignal/Tests/DevicesTest.php +++ b/tests/OneSignal/Tests/DevicesTest.php @@ -113,13 +113,15 @@ public function testOnPurchase() public function testCsvExport() { + $lastActiveSince = time() - 30 * 86400; + $expectedRequest = [ 'POST', '/players/csv_export?app_id=fakeApplicationId', ['Authorization' => 'Basic fakeApplicationAuthKey'], - '{"extra_fields":["myField1","myField2"]}', + '{"extra_fields":["myField1","myField2"],"segment_name":"Active Users","last_active_since":"'.$lastActiveSince.'"}', ]; - $this->assertEquals($expectedRequest, $this->devices->csvExport(['myField1', 'myField2'])); + $this->assertEquals($expectedRequest, $this->devices->csvExport(['myField1', 'myField2'], 'Active Users', $lastActiveSince)); } }