Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"require": {
"php": ">=8.0",
"utopia-php/cli": "^0.15.0"
"utopia-php/cli": "^0.15.0",
"sahils/utopia-fetch": "dev-main"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
Expand Down
146 changes: 94 additions & 52 deletions src/Analytics/Adapter/ActiveCampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Utopia\Analytics\Adapter;
use Utopia\Analytics\Event;
use Sahils\UtopiaFetch\Client;

class ActiveCampaign extends Adapter
{
Expand Down Expand Up @@ -44,9 +45,13 @@ public function getName(): string
public function contactExists(string $email): bool|int
{
try {
$result = $this->call('GET', '/api/3/contacts', [], [
'email' => $email,
]);
$result = Client::fetch(
url: $this->endpoint.'/api/3/contacts',
method: 'GET',
query: [
'email' => $email,
],
)->getBody();

$result = json_decode($result, true);

Expand Down Expand Up @@ -75,9 +80,11 @@ public function createContact(string $email, string $firstName = '', string $las
]];

try {
$this->call('POST', '/api/3/contacts', [
'Content-Type' => 'application/json',
], $body);
Client::fetch(
url: $this->endpoint.'/api/3/contacts',
method: 'POST',
body: $body,
);

return true;
} catch (\Exception $e) {
Expand All @@ -100,9 +107,11 @@ public function updateContact(string $contactId, string $email, string $firstNam
]];

try {
$this->call('PUT', '/api/3/contacts/'.$contactId, [
'Content-Type' => 'application/json',
], $body);
Client::fetch(
url: $this->endpoint.'/api/3/contacts'.$contactId,
method: 'PUT',
body: $body,
);

return true;
} catch (\Exception $e) {
Expand All @@ -124,7 +133,10 @@ public function deleteContact(string $email): bool
}

try {
$this->call('DELETE', '/api/3/contacts/'.$contact);
Client::fetch(
url: $this->endpoint.'/api/3/contacts'.$contact,
method: 'DELETE'
);

return true;
} catch (\Exception $e) {
Expand All @@ -140,9 +152,13 @@ public function deleteContact(string $email): bool
public function accountExists(string $name): bool|int
{
try {
$result = $this->call('GET', '/api/3/accounts', [], [
'search' => $name,
]);
$result = Client::fetch(
url: $this->endpoint.'/api/3/accounts',
method: 'GET',
query: [
'search' => $name
]
)->getBody();

if (intval(json_decode($result, true)['meta']['total']) > 0) {
return intval((json_decode($result, true))['accounts'][0]['id']);
Expand Down Expand Up @@ -171,9 +187,11 @@ public function createAccount(string $name, string $url = '', int $ownerId = 1,
]];

try {
$this->call('POST', '/api/3/accounts', [
'Content-Type' => 'application/json',
], $body);
Client::fetch(
url: $this->endpoint.'/api/3/accounts',
method: 'POST',
body: $body,
);

return true;
} catch (\Exception $e) {
Expand All @@ -198,9 +216,11 @@ public function updateAccount(string $accountId, string $name, string $url = '',
]];

try {
$this->call('PUT', '/api/3/accounts/'.$accountId, [
'Content-Type' => 'application/json',
], array_filter($body));
Client::fetch(
url: $this->endpoint.'/api/3/accounts/'.$accountId,
method: 'PUT',
body: array_filter($body),
);

return true;
} catch (\Exception $e) {
Expand All @@ -216,7 +236,10 @@ public function updateAccount(string $accountId, string $name, string $url = '',
public function deleteAccount(string $accountId): bool
{
try {
$this->call('DELETE', '/api/3/accounts/'.$accountId);
Client::fetch(
url: $this->endpoint.'/api/3/accounts/'.$accountId,
method: 'DELETE'
);

return true;
} catch (\Exception $e) {
Expand All @@ -236,10 +259,14 @@ public function syncAssociation(string $accountId, string $contactId, string $ro
// See if the association already exists

try {
$result = $this->call('GET', '/api/3/accountContacts', [], [
'filters[account]' => $accountId,
'filters[contact]' => $contactId,
]);
$result = Client::fetch(
url: $this->endpoint.'/api/3/accountContacts',
method: 'GET',
query: [
'filters[account]' => $accountId,
'filters[contact]' => $contactId,
]
)->getBody();
} catch (\Exception $e) {
$this->logError($e);

Expand All @@ -251,13 +278,15 @@ public function syncAssociation(string $accountId, string $contactId, string $ro
$associationId = intval((json_decode($result, true))['accountContacts'][0]['id']);

try {
$result = $this->call('PUT', '/api/3/accountContacts/'.$associationId, [
'Content-Type' => 'application/json',
], [
'accountContact' => [
'jobTitle' => $role,
],
]);
$result = Client::fetch(
url: $this->endpoint.'/api/3/accountContacts/'.$associationId,
method: 'PUT',
body: [
'accountContact' => [
'jobTitle' => $role,
],
]
)->getBody();

return true;
} catch (\Exception $e) {
Expand All @@ -267,13 +296,17 @@ public function syncAssociation(string $accountId, string $contactId, string $ro
}
} else {
// Create the association
$result = $this->call('POST', '/api/3/accountContacts', [
'Content-Type' => 'application/json',
], ['accountContact' => [
'account' => $accountId,
'contact' => $contactId,
'jobTitle' => $role,
]]);
$result = Client::fetch(
url: $this->endpoint.'/api/3/accountContacts',
method: 'POST',
body: [
'accountContact' => [
'account' => $accountId,
'contact' => $contactId,
'jobTitle' => $role,
],
]
)->getBody();

return true;
}
Expand Down Expand Up @@ -312,8 +345,11 @@ public function send(Event $event): bool
];

$query = array_filter($query, fn ($value) => ! is_null($value) && $value !== '');

$res = $this->call('POST', 'https://trackcmp.net/event', [], $query); // Active Campaign event URL, Refer to https://developers.activecampaign.com/reference/track-event/ for more details
$res = Client::fetch(
url: 'https://trackcmp.net/event',
method: 'POST',
body: $query
)->getBody();
if (json_decode($res, true)['success'] === 1) {
return true;
} else {
Expand Down Expand Up @@ -348,14 +384,16 @@ public function setTags(string $contactId, array $tags): bool
{
foreach ($tags as $tag) {
try {
$this->call('POST', '/api/3/contactTags', [
'Content-Type' => 'application/json',
], [
'contactTag' => [
'contact' => $contactId,
'tag' => $tag,
],
]);
Client::fetch(
url: $this->endpoint.'/api/3/contactTags',
method: 'POST',
body: [
'contactTag' => [
'contact' => $contactId,
'tag' => $tag,
],
]
);
} catch (\Exception $e) {
$this->logError($e);

Expand Down Expand Up @@ -386,10 +424,14 @@ public function validate(Event $event): bool
$foundLog = false;

// Get contact again, since AC doesn't refresh logs immediately
$response = $this->call('GET', '/api/3/activities', [], [
'contact' => $contactID,
'orders[tstamp]' => 'DESC',
]);
$response = Client::fetch(
url: $this->endpoint.'/api/3/activities',
method: 'GET',
query: [
'contact' => $contactID,
'orders[tstamp]' => 'DESC',
]
)->getBody();

$response = json_decode($response, true);

Expand Down
39 changes: 25 additions & 14 deletions src/Analytics/Adapter/GoogleAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Utopia\Analytics\Adapter;
use Utopia\Analytics\Event;
use Sahils\UtopiaFetch\Client;

class GoogleAnalytics extends Adapter
{
Expand Down Expand Up @@ -81,15 +82,18 @@ public function validate(Event $event): bool
];

$query = array_filter($query, fn ($value) => ! is_null($value) && $value !== '');

$validateResponse = $this->call('POST', $this->debugEndpoint, [], array_merge(
$query,
[
'tid' => $this->tid,
'cid' => $this->cid,
'v' => 1,
]
));
$validateResponse = Client::fetch(
url: $this->debugEndpoint,
method: 'POST',
body: array_merge(
$query,
[
'tid' => $this->tid,
'cid' => $this->cid,
'v' => 1,
]
)
)->getBody();

$validateResponse = json_decode($validateResponse, true);

Expand Down Expand Up @@ -140,11 +144,18 @@ public function send(Event $event): bool

$query = array_filter($query, fn ($value) => ! is_null($value) && $value !== '');

$result = $this->call('POST', $this->endpoint, [], array_merge([
'tid' => $this->tid,
'cid' => $this->cid,
'v' => 1,
], $query));
$result = Client::fetch(
url: $this->endpoint,
method: 'POST',
body: array_merge(
$query,
[
'tid' => $this->tid,
'cid' => $this->cid,
'v' => 1,
]
)
)->getBody();

// Parse Debug data
if ($this->endpoint == 'https://www.google-analytics.com/debug/collect') {
Expand Down
23 changes: 19 additions & 4 deletions src/Analytics/Adapter/Mixpanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Utopia\Analytics\Adapter;
use Utopia\Analytics\Event;
use Sahils\UtopiaFetch\Client;

class Mixpanel extends Adapter
{
Expand Down Expand Up @@ -74,8 +75,12 @@ public function send(Event $event): bool
'Content-Type' => 'application/json',
'accept' => 'text/plain',
];

$res = $this->call('POST', '/track', $headers, $payload);
$res = Client::fetch(
url: $this->endpoint.'/track',
method: 'POST',
headers: $headers,
body: $payload
)->getBody();

if ($res !== '1') {
throw new Exception('Failed to send event for '.$event->getProp('email'));
Expand Down Expand Up @@ -105,7 +110,12 @@ public function createProfile(string $distinctId, string $ip, array $properties
'accept' => 'text/plain',
];

$res = $this->call('POST', '/engage#profile-set', $headers, $payload);
$res = Client::fetch(
method: 'POST',
url: $this->endpoint.'/engage#profile-set',
headers: $headers,
body: $payload
);

if ($res !== '1') {
return false;
Expand All @@ -131,7 +141,12 @@ public function appendProperties(string $distinctId, array $properties): bool
'accept' => 'text/plain',
];

$res = $this->call('POST', '/engage#profile-union', $headers, $payload);
$res = Client::fetch(
method: 'POST',
url: $this->endpoint.'/engage#profile-union',
headers: $headers,
body: $payload
);

if ($res !== '1') {
return false;
Expand Down
Loading