From 141a832a6671369cc3cfb3e1a862c5608d4d43bb Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Mon, 27 Nov 2023 12:08:01 +0000 Subject: [PATCH 1/8] Remove try/catch --- src/Analytics/Adapter.php | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/src/Analytics/Adapter.php b/src/Analytics/Adapter.php index cbb3f79..e6493bb 100644 --- a/src/Analytics/Adapter.php +++ b/src/Analytics/Adapter.php @@ -95,16 +95,12 @@ public function setUserAgent(string $userAgent): self /** * Creates an Event on the remote analytics platform. + * + * @throws Exception */ public function createEvent(Event $event): bool { - try { - return $this->send($event); - } catch (\Exception $e) { - $this->logError($e); - - return false; - } + return $this->send($event); } /** @@ -212,20 +208,4 @@ protected function flatten(array $data, string $prefix = ''): array return $output; } - - /** - * Log Error - * - * @return void - */ - protected function logError(Exception $e) - { - Console::error('[Error] '.$this->getName().' Error: '); - Console::error('[Error] Type: '.get_class($e)); - Console::error('[Error] Message: '.$e->getMessage()); - Console::error('[Error] File: '.$e->getFile()); - Console::error('[Error] Line: '.$e->getLine()); - Console::error('[Error] Trace: '); - Console::error($e->getTraceAsString()); - } } From 0bc910338ee046148a5ce81cfe6840a92acdf25c Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Mon, 27 Nov 2023 12:17:27 +0000 Subject: [PATCH 2/8] Remove other instances of logError --- src/Analytics/Adapter/HubSpot.php | 213 +++++++++++------------------- 1 file changed, 80 insertions(+), 133 deletions(-) diff --git a/src/Analytics/Adapter/HubSpot.php b/src/Analytics/Adapter/HubSpot.php index 7677398..93cf20e 100644 --- a/src/Analytics/Adapter/HubSpot.php +++ b/src/Analytics/Adapter/HubSpot.php @@ -15,7 +15,7 @@ class HubSpot extends Adapter public function __construct(string $token) { $this->headers = [ - 'Authorization' => 'Bearer '.$token, + 'Authorization' => 'Bearer ' . $token, 'Content-Type' => '', ]; } @@ -25,7 +25,7 @@ public function __construct(string $token) */ public function send(Event $event): bool { - if (! $this->enabled) { + if (!$this->enabled) { return false; } @@ -35,7 +35,7 @@ public function send(Event $event): bool public function validate(Event $event): bool { - if (! $this->enabled) { + if (!$this->enabled) { return false; } @@ -56,31 +56,25 @@ public function getName(): string */ public function contactExists(string $email): bool|int { - try { - $result = $this->call('POST', '/crm/v3/objects/contacts/search', [ - 'Content-Type' => 'application/json', - ], [ - 'filterGroups' => [[ - 'filters' => [ - [ - 'value' => $email, - 'propertyName' => 'email', - 'operator' => 'EQ', - ], + $result = $this->call('POST', '/crm/v3/objects/contacts/search', [ + 'Content-Type' => 'application/json', + ], [ + 'filterGroups' => [[ + 'filters' => [ + [ + 'value' => $email, + 'propertyName' => 'email', + 'operator' => 'EQ', ], - ], ], - ]); + ], + ],], + ]); - $result = json_decode($result, true); - - if ($result && $result['total'] > 0 && count($result['results']) > 0) { - return $result['results'][0]['id']; - } else { - return false; - } - } catch (\Exception $e) { - $this->logError($e); + $result = json_decode($result, true); + if ($result && $result['total'] > 0 && count($result['results']) > 0) { + return $result['results'][0]['id']; + } else { return false; } } @@ -97,17 +91,11 @@ public function createContact(string $email, string $firstName = '', string $las 'phone' => $phone, ]]; - try { - $this->call('POST', '/crm/v3/objects/contacts', [ - 'Content-Type' => 'application/json', - ], $body); - - return true; - } catch (\Exception $e) { - $this->logError($e); + $this->call('POST', '/crm/v3/objects/contacts', [ + 'Content-Type' => 'application/json', + ], $body); - return false; - } + return true; } /** @@ -123,7 +111,7 @@ public function updateContact(string $contactId, string $email, string $firstNam ]; try { - $this->call('PATCH', '/crm/v3/objects/contacts/'.$contactId, [ + $this->call('PATCH', '/crm/v3/objects/contacts/' . $contactId, [ 'Content-Type' => 'application/json', ], $body); @@ -134,9 +122,7 @@ public function updateContact(string $contactId, string $email, string $firstNam return true; } - $this->logError($e); - - return false; + throw $e; } } @@ -147,21 +133,15 @@ public function deleteContact(string $email): bool { $contact = $this->contactExists($email); - if (! $contact) { + if (!$contact) { return false; } - try { - $this->call('DELETE', '/crm/v3/objects/contacts/'.$contact, [ - 'Content-Type' => 'application/json', - ]); - - return true; - } catch (\Exception $e) { - $this->logError($e); + $this->call('DELETE', '/crm/v3/objects/contacts/' . $contact, [ + 'Content-Type' => 'application/json', + ]); - return false; - } + return true; } /** @@ -169,31 +149,25 @@ public function deleteContact(string $email): bool */ public function accountExists(string $name): bool|int { - try { - $result = $this->call('POST', '/crm/v3/objects/companies/search', [ - 'Content-Type' => 'application/json', - ], [ - 'filterGroups' => [[ - 'filters' => [ - [ - 'value' => $name, - 'propertyName' => 'name', - 'operator' => 'EQ', - ], + $result = $this->call('POST', '/crm/v3/objects/companies/search', [ + 'Content-Type' => 'application/json', + ], [ + 'filterGroups' => [[ + 'filters' => [ + [ + 'value' => $name, + 'propertyName' => 'name', + 'operator' => 'EQ', ], - ]], - ]); - - $result = json_decode($result, true); + ], + ]], + ]); - if ($result && $result['total'] > 0 && count($result['results']) > 0) { - return $result['results'][0]['id']; - } else { - return false; - } - } catch (\Exception $e) { - $this->logError($e); + $result = json_decode($result, true); + if ($result && $result['total'] > 0 && count($result['results']) > 0) { + return $result['results'][0]['id']; + } else { return false; } } @@ -208,17 +182,11 @@ public function createAccount(string $name, string $url = ''): bool 'domain' => $url, ]]; - try { - $this->call('POST', '/crm/v3/objects/companies', [ - 'Content-Type' => 'application/json', - ], $body); - - return true; - } catch (\Exception $e) { - $this->logError($e); + $this->call('POST', '/crm/v3/objects/companies', [ + 'Content-Type' => 'application/json', + ], $body); - return false; - } + return true; } /** @@ -232,7 +200,7 @@ public function updateAccount(string $accountId, string $name, string $url = '', ]; try { - $this->call('PATCH', '/crm/v3/objects/companies/'.$accountId, [ + $this->call('PATCH', '/crm/v3/objects/companies/' . $accountId, [ 'Content-Type' => 'application/json', ], $body); @@ -243,9 +211,7 @@ public function updateAccount(string $accountId, string $name, string $url = '', return true; } - $this->logError($e); - - return false; + throw $e; } } @@ -254,17 +220,11 @@ public function updateAccount(string $accountId, string $name, string $url = '', */ public function deleteAccount(string $accountId): bool { - try { - $this->call('DELETE', '/crm/v3/objects/companies/'.$accountId, [ - 'Content-Type' => 'application/json', - ]); + $this->call('DELETE', '/crm/v3/objects/companies/' . $accountId, [ + 'Content-Type' => 'application/json', + ]); - return true; - } catch (\Exception $e) { - $this->logError($e); - - return false; - } + return true; } /** @@ -275,39 +235,32 @@ public function deleteAccount(string $accountId): bool public function syncAssociation(string $accountId, string $contactId, string $role = ''): bool { // See if the association already exists + $response = $this->call('GET', '/crm/v4/objects/contact/' . $accountId . '/associations/company'); - try { - $response = $this->call('GET', '/crm/v4/objects/contact/'.$accountId.'/associations/company'); - - $response = json_decode($response, true); + $response = json_decode($response, true); - $associationId = null; + $associationId = null; - foreach ($response['results'] as $association) { - if ($association['from']['id'] == $contactId) { - $associationId = $association['id']; - } + foreach ($response['results'] as $association) { + if ($association['from']['id'] == $contactId) { + $associationId = $association['id']; } + } - if (empty($associationId)) { - // Create the association - $this->call('PUT', '/crm/v4/objects/contact/'.$contactId.'/associations/default/company/'.$accountId, [ - 'Content-Type' => 'application/json', - ]); - } else { - // Delete and recreate the association - $this->call('DELETE', '/crm/v4/objects/contact/'.$contactId.'/associations/company/'.$accountId, [ - 'Content-Type' => 'application/json', - ]); - - $this->call('PUT', '/crm/v4/objects/contact/'.$contactId.'/associations/default/company/'.$accountId, [ - 'Content-Type' => 'application/json', - ]); - } - } catch (\Exception $e) { - $this->logError($e); + if (empty($associationId)) { + // Create the association + $this->call('PUT', '/crm/v4/objects/contact/' . $contactId . '/associations/default/company/' . $accountId, [ + 'Content-Type' => 'application/json', + ]); + } else { + // Delete and recreate the association + $this->call('DELETE', '/crm/v4/objects/contact/' . $contactId . '/associations/company/' . $accountId, [ + 'Content-Type' => 'application/json', + ]); - return false; + $this->call('PUT', '/crm/v4/objects/contact/' . $contactId . '/associations/default/company/' . $accountId, [ + 'Content-Type' => 'application/json', + ]); } return true; @@ -318,16 +271,10 @@ public function syncAssociation(string $accountId, string $contactId, string $ro */ public function addToList(int $listId, int $contactId): bool { - try { - $this->call('PUT', '/crm/v3/lists/'.$listId.'/memberships/add', [ - 'Content-Type' => 'application/json', - ], [$contactId]); + $this->call('PUT', '/crm/v3/lists/' . $listId . '/memberships/add', [ + 'Content-Type' => 'application/json', + ], [$contactId]); - return true; - } catch (\Exception $e) { - $this->logError($e); - - return false; - } + return true; } } From 509051dd8907c524d455af55b838e30758bc7343 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Mon, 27 Nov 2023 12:17:42 +0000 Subject: [PATCH 3/8] Run Linter --- src/Analytics/Adapter.php | 3 +-- src/Analytics/Adapter/HubSpot.php | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Analytics/Adapter.php b/src/Analytics/Adapter.php index e6493bb..f368c40 100644 --- a/src/Analytics/Adapter.php +++ b/src/Analytics/Adapter.php @@ -3,7 +3,6 @@ namespace Utopia\Analytics; use Exception; -use Utopia\CLI\Console; abstract class Adapter { @@ -95,7 +94,7 @@ public function setUserAgent(string $userAgent): self /** * Creates an Event on the remote analytics platform. - * + * * @throws Exception */ public function createEvent(Event $event): bool diff --git a/src/Analytics/Adapter/HubSpot.php b/src/Analytics/Adapter/HubSpot.php index 93cf20e..63ddbdb 100644 --- a/src/Analytics/Adapter/HubSpot.php +++ b/src/Analytics/Adapter/HubSpot.php @@ -15,7 +15,7 @@ class HubSpot extends Adapter public function __construct(string $token) { $this->headers = [ - 'Authorization' => 'Bearer ' . $token, + 'Authorization' => 'Bearer '.$token, 'Content-Type' => '', ]; } @@ -25,7 +25,7 @@ public function __construct(string $token) */ public function send(Event $event): bool { - if (!$this->enabled) { + if (! $this->enabled) { return false; } @@ -35,7 +35,7 @@ public function send(Event $event): bool public function validate(Event $event): bool { - if (!$this->enabled) { + if (! $this->enabled) { return false; } @@ -67,7 +67,7 @@ public function contactExists(string $email): bool|int 'operator' => 'EQ', ], ], - ],], + ], ], ]); $result = json_decode($result, true); @@ -111,7 +111,7 @@ public function updateContact(string $contactId, string $email, string $firstNam ]; try { - $this->call('PATCH', '/crm/v3/objects/contacts/' . $contactId, [ + $this->call('PATCH', '/crm/v3/objects/contacts/'.$contactId, [ 'Content-Type' => 'application/json', ], $body); @@ -133,11 +133,11 @@ public function deleteContact(string $email): bool { $contact = $this->contactExists($email); - if (!$contact) { + if (! $contact) { return false; } - $this->call('DELETE', '/crm/v3/objects/contacts/' . $contact, [ + $this->call('DELETE', '/crm/v3/objects/contacts/'.$contact, [ 'Content-Type' => 'application/json', ]); @@ -200,7 +200,7 @@ public function updateAccount(string $accountId, string $name, string $url = '', ]; try { - $this->call('PATCH', '/crm/v3/objects/companies/' . $accountId, [ + $this->call('PATCH', '/crm/v3/objects/companies/'.$accountId, [ 'Content-Type' => 'application/json', ], $body); @@ -220,7 +220,7 @@ public function updateAccount(string $accountId, string $name, string $url = '', */ public function deleteAccount(string $accountId): bool { - $this->call('DELETE', '/crm/v3/objects/companies/' . $accountId, [ + $this->call('DELETE', '/crm/v3/objects/companies/'.$accountId, [ 'Content-Type' => 'application/json', ]); @@ -235,7 +235,7 @@ public function deleteAccount(string $accountId): bool public function syncAssociation(string $accountId, string $contactId, string $role = ''): bool { // See if the association already exists - $response = $this->call('GET', '/crm/v4/objects/contact/' . $accountId . '/associations/company'); + $response = $this->call('GET', '/crm/v4/objects/contact/'.$accountId.'/associations/company'); $response = json_decode($response, true); @@ -249,16 +249,16 @@ public function syncAssociation(string $accountId, string $contactId, string $ro if (empty($associationId)) { // Create the association - $this->call('PUT', '/crm/v4/objects/contact/' . $contactId . '/associations/default/company/' . $accountId, [ + $this->call('PUT', '/crm/v4/objects/contact/'.$contactId.'/associations/default/company/'.$accountId, [ 'Content-Type' => 'application/json', ]); } else { // Delete and recreate the association - $this->call('DELETE', '/crm/v4/objects/contact/' . $contactId . '/associations/company/' . $accountId, [ + $this->call('DELETE', '/crm/v4/objects/contact/'.$contactId.'/associations/company/'.$accountId, [ 'Content-Type' => 'application/json', ]); - $this->call('PUT', '/crm/v4/objects/contact/' . $contactId . '/associations/default/company/' . $accountId, [ + $this->call('PUT', '/crm/v4/objects/contact/'.$contactId.'/associations/default/company/'.$accountId, [ 'Content-Type' => 'application/json', ]); } @@ -271,7 +271,7 @@ public function syncAssociation(string $accountId, string $contactId, string $ro */ public function addToList(int $listId, int $contactId): bool { - $this->call('PUT', '/crm/v3/lists/' . $listId . '/memberships/add', [ + $this->call('PUT', '/crm/v3/lists/'.$listId.'/memberships/add', [ 'Content-Type' => 'application/json', ], [$contactId]); From 9df647bc3dc02ddfbe03c0ac96eaafd977f9d877 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Mon, 27 Nov 2023 13:48:34 +0000 Subject: [PATCH 4/8] Update HubSpot.php --- src/Analytics/Adapter/HubSpot.php | 34 ++++++++----------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/src/Analytics/Adapter/HubSpot.php b/src/Analytics/Adapter/HubSpot.php index 63ddbdb..ddc0246 100644 --- a/src/Analytics/Adapter/HubSpot.php +++ b/src/Analytics/Adapter/HubSpot.php @@ -110,20 +110,11 @@ public function updateContact(string $contactId, string $email, string $firstNam 'phone' => $phone, ]; - try { - $this->call('PATCH', '/crm/v3/objects/contacts/'.$contactId, [ - 'Content-Type' => 'application/json', - ], $body); - - return true; - } catch (\Exception $e) { - if ($e->getCode() == 400) { - // No changes to make - return true; - } + $this->call('PATCH', '/crm/v3/objects/contacts/'.$contactId, [ + 'Content-Type' => 'application/json', + ], $body); - throw $e; - } + return true; } /** @@ -199,20 +190,11 @@ public function updateAccount(string $accountId, string $name, string $url = '', 'domain' => $url, ]; - try { - $this->call('PATCH', '/crm/v3/objects/companies/'.$accountId, [ - 'Content-Type' => 'application/json', - ], $body); - - return true; - } catch (\Exception $e) { - if ($e->getCode() == 400) { - // No changes to make - return true; - } + $this->call('PATCH', '/crm/v3/objects/companies/'.$accountId, [ + 'Content-Type' => 'application/json', + ], $body); - throw $e; - } + return true; } /** From 3db75ae28cd62367be2e499f9cc73a20b641ad75 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Mon, 11 Dec 2023 15:31:38 +0000 Subject: [PATCH 5/8] Re-add 400 checks for no changes made --- src/Analytics/Adapter/HubSpot.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Analytics/Adapter/HubSpot.php b/src/Analytics/Adapter/HubSpot.php index ddc0246..f26c000 100644 --- a/src/Analytics/Adapter/HubSpot.php +++ b/src/Analytics/Adapter/HubSpot.php @@ -110,9 +110,16 @@ public function updateContact(string $contactId, string $email, string $firstNam 'phone' => $phone, ]; - $this->call('PATCH', '/crm/v3/objects/contacts/'.$contactId, [ - 'Content-Type' => 'application/json', - ], $body); + try { + $this->call('PATCH', '/crm/v3/objects/contacts/'.$contactId, [ + 'Content-Type' => 'application/json', + ], $body); + } catch (\Exception $e) { + if ($e->getCode() == 400) { + // No changes to make + return true; + } + } return true; } @@ -190,9 +197,16 @@ public function updateAccount(string $accountId, string $name, string $url = '', 'domain' => $url, ]; - $this->call('PATCH', '/crm/v3/objects/companies/'.$accountId, [ - 'Content-Type' => 'application/json', - ], $body); + try { + $this->call('PATCH', '/crm/v3/objects/companies/'.$accountId, [ + 'Content-Type' => 'application/json', + ], $body); + } catch (\Exception $e) { + if ($e->getCode() == 400) { + // No changes to make + return true; + } + } return true; } From efd58fe5c16c05fad85e178835624e4385f7fce6 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 14 Dec 2023 11:15:16 +0000 Subject: [PATCH 6/8] Add Param Flattening for Orbit --- src/Analytics/Adapter/Orbit.php | 79 ++++++++++++++++++++++--------- tests/Analytics/AnalyticsTest.php | 8 ++-- 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/Analytics/Adapter/Orbit.php b/src/Analytics/Adapter/Orbit.php index 33477ce..4741665 100644 --- a/src/Analytics/Adapter/Orbit.php +++ b/src/Analytics/Adapter/Orbit.php @@ -35,17 +35,42 @@ public function getName(): string */ public function __construct(string $workspaceId, string $apiKey, string $dataOrigin) { - $this->endpoint = $this->endpoint.$workspaceId; + $this->endpoint = $this->endpoint . $workspaceId; $this->apiKey = $apiKey; $this->dataOrigin = $dataOrigin; } + + function cleanup(array $props): array + { + $props = array_filter($props, fn ($value) => !is_null($value) && $value !== ''); + + // Flatten arrays + $flatten = function ($array) use (&$flatten) { + $return = []; + + foreach ($array as $key => $value) { + if (is_array($value)) { + $return = array_merge($return, $flatten($value)); + } else { + $return[$key] = $value; + } + } + + return $return; + }; + + $props = $flatten($props); + + return $props; + } + /** * Creates an Event on the remote analytics platform. Requires email prop. */ public function send(Event $event): bool { - if (! $event->getProp('email')) { + if (!$event->getProp('email')) { return false; } @@ -59,6 +84,23 @@ public function send(Event $event): bool $tags[] = $event->getProp('code'); } + $props = $this->cleanup($event->getProps()); + + $properties = array_map(function ($value) { + if (is_array($value)) { + var_dump($value); + $value = implode(',', $value); + } + + // Allow only alphanumeric characters and commas + $value = preg_replace('/[^a-zA-Z0-9,]/', '', $value); + + return $value; + }, $props); + + unset($properties['email']); + unset($properties['name']); + $activity = [ 'title' => $event->getName(), 'activity_type_key' => $event->getType(), @@ -68,23 +110,14 @@ public function send(Event $event): bool 'name' => $event->getProp('name'), 'tags_to_add' => $tags, ], - 'properties' => array_map(function ($value) { - if (is_array($value)) { - return json_encode($value); - } - - return $value; - }, array_filter($event->getProps(), fn ($value) => ! is_null($value) && $value !== '')), + 'properties' => $properties ?? [], ]; - unset($activity['properties']['email']); - unset($activity['properties']['name']); - - $activity = array_filter($activity, fn ($value) => ! is_null($value) && $value !== ''); + $activity = array_filter($activity, fn ($value) => !is_null($value) && $value !== ''); - $this->call('POST', $this->endpoint.'/activities', [ + $this->call('POST', $this->endpoint . '/activities', [ 'Content-Type' => 'application/json', - 'Authorization' => 'Bearer '.$this->apiKey, + 'Authorization' => 'Bearer ' . $this->apiKey, ], [ 'activity' => $activity, ]); @@ -114,7 +147,7 @@ public function setUserAgent(string $userAgent): self public function validate(Event $event): bool { - if (! $this->enabled) { + if (!$this->enabled) { return false; } @@ -134,13 +167,13 @@ public function validate(Event $event): bool throw new \Exception('Event email is required'); } - if (! $this->send($event)) { + if (!$this->send($event)) { throw new \Exception('Failed to send event'); } // Check if event made it. $listMembers = $this->call('GET', '/members/find', [ - 'Authorization' => 'Bearer '.$this->apiKey, + 'Authorization' => 'Bearer ' . $this->apiKey, ], [ 'source' => 'email', 'email' => $event->getProp('email'), @@ -154,8 +187,8 @@ public function validate(Event $event): bool $member = $listMembers['data']; - $activities = $this->call('GET', '/members/'.$member['id'].'/activities', [ - 'Authorization' => 'Bearer '.$this->apiKey, + $activities = $this->call('GET', '/members/' . $member['id'] . '/activities', [ + 'Authorization' => 'Bearer ' . $this->apiKey, ], [ 'activity_type' => $event->getType(), ]); @@ -174,12 +207,12 @@ public function validate(Event $event): bool } } - if (! $foundActivity) { + if (!$foundActivity) { throw new \Exception('Failed to find event in Orbit'); } - $this->call('DELETE', '/members/'.$member['id'].'/activities/'.$foundActivity, [ - 'Authorization' => 'Bearer '.$this->apiKey, + $this->call('DELETE', '/members/' . $member['id'] . '/activities/' . $foundActivity, [ + 'Authorization' => 'Bearer ' . $this->apiKey, ], []); return true; diff --git a/tests/Analytics/AnalyticsTest.php b/tests/Analytics/AnalyticsTest.php index 5efd84c..1aa73de 100644 --- a/tests/Analytics/AnalyticsTest.php +++ b/tests/Analytics/AnalyticsTest.php @@ -30,9 +30,9 @@ class AnalyticsTest extends TestCase public function setUp(): void { - $this->ga = new GoogleAnalytics(App::getEnv('GA_TID'), App::getEnv('GA_CID')); - $this->pa = new Plausible(App::getEnv('PA_DOMAIN'), App::getEnv('PA_APIKEY'), 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36', '192.168.0.1'); - $this->orbit = new Orbit(App::getEnv('OR_WORKSPACEID'), App::getEnv('OR_APIKEY'), 'Utopia Testing Suite'); + // // $this->ga = new GoogleAnalytics(App::getEnv('GA_TID'), App::getEnv('GA_CID')); + // // $this->pa = new Plausible(App::getEnv('PA_DOMAIN'), App::getEnv('PA_APIKEY'), 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36', '192.168.0.1'); + // $this->orbit = new Orbit(App::getEnv('OR_WORKSPACEID'), App::getEnv('OR_APIKEY'), 'Utopia Testing Suite'); $this->mp = new Mixpanel(App::getEnv('MP_PROJECT_TOKEN')); $this->hs = new HubSpot(App::getEnv('HS_APIKEY')); } @@ -217,7 +217,7 @@ public function testOrbit(): void $event->setType('testEvent') ->setName('testEvent') ->setUrl('https://www.appwrite.io/docs/installation') - ->setProps(['category' => 'testEvent', 'email' => 'analytics@utopiaphp.com', 'tags' => ['test', 'test2']]); + ->setProps(['category' => 'testEvent', 'email' => 'analytics@utopiaphp.com', 'tags' => ['test', 'test2', ['test3' => 'test4', ['test5' => 'test6']]]]); $this->assertTrue($this->orbit->send($event)); $this->assertTrue($this->orbit->validate($event)); From 6a66bce298bdb3ebf3706a31efcad4fdd3995e79 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 14 Dec 2023 11:16:30 +0000 Subject: [PATCH 7/8] Re enable tests --- tests/Analytics/AnalyticsTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Analytics/AnalyticsTest.php b/tests/Analytics/AnalyticsTest.php index 1aa73de..bc2fdae 100644 --- a/tests/Analytics/AnalyticsTest.php +++ b/tests/Analytics/AnalyticsTest.php @@ -30,9 +30,9 @@ class AnalyticsTest extends TestCase public function setUp(): void { - // // $this->ga = new GoogleAnalytics(App::getEnv('GA_TID'), App::getEnv('GA_CID')); - // // $this->pa = new Plausible(App::getEnv('PA_DOMAIN'), App::getEnv('PA_APIKEY'), 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36', '192.168.0.1'); - // $this->orbit = new Orbit(App::getEnv('OR_WORKSPACEID'), App::getEnv('OR_APIKEY'), 'Utopia Testing Suite'); + $this->ga = new GoogleAnalytics(App::getEnv('GA_TID'), App::getEnv('GA_CID')); + $this->pa = new Plausible(App::getEnv('PA_DOMAIN'), App::getEnv('PA_APIKEY'), 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36', '192.168.0.1'); + $this->orbit = new Orbit(App::getEnv('OR_WORKSPACEID'), App::getEnv('OR_APIKEY'), 'Utopia Testing Suite'); $this->mp = new Mixpanel(App::getEnv('MP_PROJECT_TOKEN')); $this->hs = new HubSpot(App::getEnv('HS_APIKEY')); } From 7b9b2551009107ab5b40c696a8740731cfcb917e Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 14 Dec 2023 11:16:51 +0000 Subject: [PATCH 8/8] Update Orbit.php --- src/Analytics/Adapter/Orbit.php | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Analytics/Adapter/Orbit.php b/src/Analytics/Adapter/Orbit.php index 4741665..744cf3d 100644 --- a/src/Analytics/Adapter/Orbit.php +++ b/src/Analytics/Adapter/Orbit.php @@ -35,15 +35,14 @@ public function getName(): string */ public function __construct(string $workspaceId, string $apiKey, string $dataOrigin) { - $this->endpoint = $this->endpoint . $workspaceId; + $this->endpoint = $this->endpoint.$workspaceId; $this->apiKey = $apiKey; $this->dataOrigin = $dataOrigin; } - - function cleanup(array $props): array + public function cleanup(array $props): array { - $props = array_filter($props, fn ($value) => !is_null($value) && $value !== ''); + $props = array_filter($props, fn ($value) => ! is_null($value) && $value !== ''); // Flatten arrays $flatten = function ($array) use (&$flatten) { @@ -61,7 +60,7 @@ function cleanup(array $props): array }; $props = $flatten($props); - + return $props; } @@ -70,7 +69,7 @@ function cleanup(array $props): array */ public function send(Event $event): bool { - if (!$event->getProp('email')) { + if (! $event->getProp('email')) { return false; } @@ -113,11 +112,11 @@ public function send(Event $event): bool 'properties' => $properties ?? [], ]; - $activity = array_filter($activity, fn ($value) => !is_null($value) && $value !== ''); + $activity = array_filter($activity, fn ($value) => ! is_null($value) && $value !== ''); - $this->call('POST', $this->endpoint . '/activities', [ + $this->call('POST', $this->endpoint.'/activities', [ 'Content-Type' => 'application/json', - 'Authorization' => 'Bearer ' . $this->apiKey, + 'Authorization' => 'Bearer '.$this->apiKey, ], [ 'activity' => $activity, ]); @@ -147,7 +146,7 @@ public function setUserAgent(string $userAgent): self public function validate(Event $event): bool { - if (!$this->enabled) { + if (! $this->enabled) { return false; } @@ -167,13 +166,13 @@ public function validate(Event $event): bool throw new \Exception('Event email is required'); } - if (!$this->send($event)) { + if (! $this->send($event)) { throw new \Exception('Failed to send event'); } // Check if event made it. $listMembers = $this->call('GET', '/members/find', [ - 'Authorization' => 'Bearer ' . $this->apiKey, + 'Authorization' => 'Bearer '.$this->apiKey, ], [ 'source' => 'email', 'email' => $event->getProp('email'), @@ -187,8 +186,8 @@ public function validate(Event $event): bool $member = $listMembers['data']; - $activities = $this->call('GET', '/members/' . $member['id'] . '/activities', [ - 'Authorization' => 'Bearer ' . $this->apiKey, + $activities = $this->call('GET', '/members/'.$member['id'].'/activities', [ + 'Authorization' => 'Bearer '.$this->apiKey, ], [ 'activity_type' => $event->getType(), ]); @@ -207,12 +206,12 @@ public function validate(Event $event): bool } } - if (!$foundActivity) { + if (! $foundActivity) { throw new \Exception('Failed to find event in Orbit'); } - $this->call('DELETE', '/members/' . $member['id'] . '/activities/' . $foundActivity, [ - 'Authorization' => 'Bearer ' . $this->apiKey, + $this->call('DELETE', '/members/'.$member['id'].'/activities/'.$foundActivity, [ + 'Authorization' => 'Bearer '.$this->apiKey, ], []); return true;