-
Notifications
You must be signed in to change notification settings - Fork 18
Add HubSpot adapter #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
016fca3
Remove AC tests and start working on adding HubSpot
PineappleIOnic 53246eb
Remove GA and finish hubspot tests
PineappleIOnic 5475dc9
Update HubSpot.php
PineappleIOnic 852c867
Fix Tests
PineappleIOnic be85efd
Run Linter and fix tests
PineappleIOnic fcafe7e
Add AddToList method
PineappleIOnic 134ccb8
Update src/Analytics/Adapter.php
PineappleIOnic 59bf461
Address Jake's Comments
PineappleIOnic dcf85e1
Run Linter
PineappleIOnic 2494ca5
Remove ActiveCampaign
PineappleIOnic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,314 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Analytics\Adapter; | ||
|
|
||
| use Utopia\Analytics\Adapter; | ||
| use Utopia\Analytics\Event; | ||
|
|
||
| class HubSpot extends Adapter | ||
| { | ||
| /** | ||
| * Endpoint for MixPanel Events | ||
| */ | ||
| public string $endpoint = 'https://api.hubapi.com'; | ||
|
|
||
| public function __construct(string $token) | ||
| { | ||
| $this->headers = [ | ||
| 'Authorization' => 'Bearer '.$token, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Creates an Event on the remote analytics platform. | ||
| */ | ||
| public function send(Event $event): bool | ||
| { | ||
| if (! $this->enabled) { | ||
| return false; | ||
| } | ||
|
|
||
| // HubSpot event tracking isn't possible due to their chrome based extention system | ||
| return true; | ||
|
PineappleIOnic marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public function validate(Event $event): bool | ||
| { | ||
| if (! $this->enabled) { | ||
| return false; | ||
| } | ||
|
|
||
| // HubSpot event tracking isn't possible due to their chrome based extention system | ||
| return true; | ||
|
PineappleIOnic marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * Gets the name of the adapter. | ||
| */ | ||
| public function getName(): string | ||
| { | ||
| return 'HubSpot'; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a contact exists by the email ID. Returns the User ID if it exists and false if it doesn't. | ||
| */ | ||
| 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 = 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); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create a contact | ||
| */ | ||
| public function createContact(string $email, string $firstName = '', string $lastName = '', string $phone = ''): bool | ||
| { | ||
| $body = ['properties' => [ | ||
| 'email' => $email, | ||
| 'firstname' => $firstName, | ||
| 'lastname' => $lastName, | ||
| 'phone' => $phone, | ||
| ]]; | ||
|
|
||
| try { | ||
| $this->call('POST', '/crm/v3/objects/contacts', [ | ||
| 'Content-Type' => 'application/json', | ||
| ], $body); | ||
|
|
||
| return true; | ||
| } catch (\Exception $e) { | ||
| $this->logError($e); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Update contact | ||
| */ | ||
| public function updateContact(string $contactId, string $email, string $firstName = '', string $lastName = '', string $phone = ''): bool | ||
| { | ||
| $body = [ | ||
| 'email' => $email, | ||
| 'firstname' => $firstName, | ||
| 'lastname' => $lastName, | ||
| '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->logError($e); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Delete a contact | ||
| */ | ||
| public function deleteContact(string $email): bool | ||
| { | ||
| $contact = $this->contactExists($email); | ||
|
|
||
| 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); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Account Exists | ||
| */ | ||
| 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 = 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); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create an account | ||
| */ | ||
| public function createAccount(string $name, string $url = ''): bool | ||
| { | ||
| $body = ['properties' => [ | ||
| 'name' => $name, | ||
| 'domain' => $url, | ||
| ]]; | ||
|
|
||
| try { | ||
| $this->call('POST', '/crm/v3/objects/companies', [ | ||
| 'Content-Type' => 'application/json', | ||
| ], $body); | ||
|
|
||
| return true; | ||
| } catch (\Exception $e) { | ||
| $this->logError($e); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Update an account | ||
| */ | ||
| public function updateAccount(string $accountId, string $name, string $url = '', int $ownerId = 1, array $fields = []): bool | ||
| { | ||
| $body = [ | ||
| 'name' => $name, | ||
| '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->logError($e); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Delete an account | ||
| */ | ||
| public function deleteAccount(string $accountId): bool | ||
| { | ||
| try { | ||
| $this->call('DELETE', '/crm/v3/objects/companies/'.$accountId, [ | ||
| 'Content-Type' => 'application/json', | ||
| ]); | ||
|
|
||
| return true; | ||
| } catch (\Exception $e) { | ||
| $this->logError($e); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sync an association | ||
| * | ||
| * Creates an association if it doesn't exist and updates it if it does | ||
| */ | ||
| public function syncAssociation(string $accountId, string $contactId, string $role = ''): bool | ||
| { | ||
| // See if the association already exists | ||
|
|
||
| try { | ||
| $response = $this->call('GET', '/crm/v4/objects/contact/'.$accountId.'/associations/company'); | ||
|
|
||
| $response = json_decode($response, true); | ||
|
|
||
| $associationId = null; | ||
|
|
||
| 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); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.