Skip to content

Commit

Permalink
fix assignment tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Dec 30, 2023
1 parent 1e5d2ce commit ba3ca2b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/Amplitude/Amplitude.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ private function post(string $url, array $payload): void
$this->logger->error('[Amplitude] Failed to encode payload: ' . json_last_error());
return;
}
$request = $this->httpClient->createRequest('POST', $url)->withHeader('json', $payloadJson);
$request = $this->httpClient
->createRequest('POST', $url, $payloadJson)
->withHeader('Content-Type', 'application/json');
try {
$response = $fetchClient->sendRequest($request);
if ($response->getStatusCode() != 200) {
echo json_encode($payload) . "\n";
$this->logger->error('[Amplitude] Failed to send event: ' . $payloadJson . ', ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase());
} else {
$this->logger->debug("[Amplitude] Event sent successfully: " . $payloadJson);
$this->queue = [];
return;
}
$this->logger->debug("[Amplitude] Event sent successfully: " . $payloadJson);
$this->queue = [];

} catch (ClientExceptionInterface $e) {
$this->logger->error('[Amplitude] Failed to send event: ' . $payloadJson . ', ' . $e->getMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/FetchClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public function getClient(): ClientInterface;
/**
* return a Psr Request to be sent by the client
*/
public function createRequest(string $method, string $uri) : RequestInterface;
public function createRequest(string $method, string $uri, ?string $body = null) : RequestInterface;
}
8 changes: 6 additions & 2 deletions src/Http/GuzzleFetchClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AmplitudeExperiment\Http;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function __construct(array $config)
$handlerStack->push(Middleware::retry(
function ($retries, Request $request, $response = null, $exception = null) {
// Retry if the maximum number of retries is not reached and an exception occurred
return $retries < $this->config['fetchRetries'] && $exception instanceof \Exception;
return $retries < $this->config['fetchRetries'] && $exception instanceof Exception;
},
function ($retries) {
// Calculate delay
Expand All @@ -78,8 +79,11 @@ public function getClient(): ClientInterface
return $this->client;
}

public function createRequest(string $method, string $uri): Request
public function createRequest(string $method, string $uri, ?string $body = null): Request
{
if ($body !== null) {
return new Request($method, $uri, [], $body);
}
return new Request($method, $uri);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Local/LocalEvaluationClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function evaluate(User $user, array $flagKeys = []): array
}
$this->logger->debug('[Experiment] Evaluate - user: ' . json_encode($user->toArray()) . ' with flags: ' . json_encode($flags));
$results = array_map('AmplitudeExperiment\Variant::convertEvaluationVariantToVariant', $this->evaluation->evaluate($user->toEvaluationContext(), $flags));
$this->logger->debug('[Experiment] Evaluate - variants:', $results);
$this->logger->debug('[Experiment] Evaluate - variants:' . json_encode($results));
if ($this->assignmentService) {
$this->assignmentService->track(new Assignment($user, $results));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Local/LocalEvaluationConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LocalEvaluationConfig
public ?FetchClientInterface $fetchClient;
/**
* @var array<string, mixed>
* The configuration for the underlying default Guzzle client.
* The configuration for the underlying default Guzzle client. See {@link GUZZLE_DEFAULTS} for defaults.
*/
public array $guzzleClientConfig;

Expand Down
2 changes: 1 addition & 1 deletion src/Remote/RemoteEvaluationConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RemoteEvaluationConfig
public ?FetchClientInterface $fetchClient;
/**
* @var array<string, mixed>
* The configuration for the underlying default Guzzle client.
* The configuration for the underlying default Guzzle client. See {@link GUZZLE_DEFAULTS} for defaults.
*/
public array $guzzleClientConfig;

Expand Down

0 comments on commit ba3ca2b

Please sign in to comment.