Skip to content

Commit

Permalink
Support European and other servers
Browse files Browse the repository at this point in the history
  • Loading branch information
potsky authored Sep 2, 2022
1 parent 3cace0b commit d3d1b17
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Amplitude.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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());
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit d3d1b17

Please sign in to comment.