From d3d1b17a947e5be68efb2e76358bbba1481d2c3b Mon Sep 17 00:00:00 2001 From: Potsky Date: Fri, 2 Sep 2022 17:22:27 +0200 Subject: [PATCH] Support European and other servers --- src/Amplitude.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Amplitude.php b/src/Amplitude.php index 317e0fd..318c274 100644 --- a/src/Amplitude.php +++ b/src/Amplitude.php @@ -20,6 +20,14 @@ class Amplitude */ protected $apiKey; + /** + * The API URL to use for all events generated by this instance + * Default will be const AMPLITUDE_API_URL + * + * @var string + */ + protected $apiUrl; + /** * The event that will be used for the next event being tracked * @@ -118,13 +126,16 @@ public static function getInstance($instanceName = 'default') * Constructor, optionally sets the api key * * @param string $apiKey - * @param \Psr\Log $logger + * @param string $apiUrl */ - public function __construct($apiKey = null) + public function __construct($apiKey = null, $apiUrl = null) { if (!empty($apiKey)) { $this->apiKey = (string)$apiKey; } + if (!empty($apiUrl)) { + $this->apiUrl = (string)$apiUrl; + } // Initialize logger to be null logger $this->setLogger(new Log\NullLogger()); } @@ -136,11 +147,13 @@ public function __construct($apiKey = null) * * @param string $apiKey Amplitude API key * @param string $userId + * @param string $apiUrl Amplitude API URL * @return \Zumba\Amplitude\Amplitude */ - public function init($apiKey, $userId = null) + public function init($apiKey, $userId = null, $apiUrl = null) { $this->apiKey = (string)$apiKey; + $this->apiUrl = (string)$apiUrl; if ($userId !== null) { $this->setUserId($userId); } @@ -487,11 +500,12 @@ protected function sendEvent() if (empty($this->event) || empty($this->apiKey)) { throw new \InternalErrorException('Event or api key not set, cannot send event'); } - $ch = curl_init(static::AMPLITUDE_API_URL); + $url = empty($this->apiUrl) ? static::AMPLITUDE_API_URL : $this->apiUrl; + $ch = curl_init($url); if (!$ch) { // Could be a number of PHP environment problems, log a critical error $this->logger->critical( - 'Call to curl_init(' . static::AMPLITUDE_API_URL . ') failed, unable to send Amplitude event' + 'Call to curl_init(' . $url . ') failed, unable to send Amplitude event' ); return; }