| 1 |
<?php |
| 2 |
|
| 3 |
namespace PostHog; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use PostHog\Consumer\File; |
| 7 |
use PostHog\Consumer\ForkCurl; |
| 8 |
use PostHog\Consumer\LibCurl; |
| 9 |
use PostHog\Consumer\Socket; |
| 10 |
|
| 11 |
class Client |
| 12 |
{ |
| 13 |
|
| 14 |
private const CONSUMERS = [ |
| 15 |
"socket" => Socket::class, |
| 16 |
"file" => File::class, |
| 17 |
"fork_curl" => ForkCurl::class, |
| 18 |
"lib_curl" => LibCurl::class, |
| 19 |
]; |
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $apiKey; |
| 26 |
|
| 27 |
/** |
| 28 |
* Consumer object handles queueing and bundling requests to PostHog. |
| 29 |
* |
| 30 |
* @var Consumer |
| 31 |
*/ |
| 32 |
protected $consumer; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var HttpClient |
| 36 |
*/ |
| 37 |
private $httpClient; |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Create a new posthog object with your app's API key |
| 42 |
* key |
| 43 |
* |
| 44 |
* @param string $apiKey |
| 45 |
* @param array $options array of consumer options [optional] |
| 46 |
* @param HttpClient|null $httpClient |
| 47 |
*/ |
| 48 |
public function __construct(string $apiKey, array $options = [], ?HttpClient $httpClient = null) |
| 49 |
{ |
| 50 |
$this->apiKey = $apiKey; |
| 51 |
$Consumer = self::CONSUMERS[$options["consumer"] ?? "lib_curl"]; |
| 52 |
$this->consumer = new $Consumer($apiKey, $options); |
| 53 |
$this->httpClient = $httpClient !== null ? $httpClient : new HttpClient( |
| 54 |
$options['host'] ?? "app.posthog.com", |
| 55 |
$options['ssl'] ?? true, |
| 56 |
10000, |
| 57 |
false, |
| 58 |
$options["debug"] ?? false |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
public function __destruct() |
| 63 |
{ |
| 64 |
$this->consumer->__destruct(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Captures a user action |
| 69 |
* |
| 70 |
* @param array $message |
| 71 |
* @return bool whether the capture call succeeded |
| 72 |
*/ |
| 73 |
public function capture(array $message) |
| 74 |
{ |
| 75 |
$message = $this->message($message); |
| 76 |
$message["type"] = "capture"; |
| 77 |
|
| 78 |
return $this->consumer->capture($message); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Tags properties about the user. |
| 83 |
* |
| 84 |
* @param array $message |
| 85 |
* @return bool whether the identify call succeeded |
| 86 |
*/ |
| 87 |
public function identify(array $message) |
| 88 |
{ |
| 89 |
if (isset($message['properties'])) { |
| 90 |
$message['$set'] = $message['properties']; |
| 91 |
} |
| 92 |
|
| 93 |
$message = $this->message($message); |
| 94 |
$message["type"] = "identify"; |
| 95 |
$message["event"] = '$identify'; |
| 96 |
|
| 97 |
return $this->consumer->identify($message); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* decide if the feature flag is enabled for this distinct id. |
| 102 |
* |
| 103 |
* @param string $key |
| 104 |
* @param string $distinctId |
| 105 |
* @param mixed $defaultValue |
| 106 |
* @param array $groups |
| 107 |
* @return bool |
| 108 |
* @throws Exception |
| 109 |
*/ |
| 110 |
public function isFeatureEnabled( |
| 111 |
string $key, |
| 112 |
string $distinctId, |
| 113 |
$defaultValue = false, |
| 114 |
array $groups = array() |
| 115 |
): bool { |
| 116 |
$flags = $this->fetchEnabledFeatureFlags($distinctId, $groups); |
| 117 |
|
| 118 |
$result = in_array($key, $flags); |
| 119 |
|
| 120 |
$this->capture([ |
| 121 |
"properties" => [ |
| 122 |
'$feature_flag' => $key, |
| 123 |
'$feature_flag_response' => $result, |
| 124 |
], |
| 125 |
"distinct_id" => $distinctId, |
| 126 |
"event" => '$feature_flag_called', |
| 127 |
]); |
| 128 |
|
| 129 |
if ($result) { |
| 130 |
return true; |
| 131 |
} |
| 132 |
return $defaultValue; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
/** |
| 137 |
* @param string $distinctId |
| 138 |
* @param array $groups |
| 139 |
* @return array of enabled feature flags |
| 140 |
* @throws Exception |
| 141 |
*/ |
| 142 |
public function fetchEnabledFeatureFlags(string $distinctId, array $groups = array()): array |
| 143 |
{ |
| 144 |
return json_decode($this->decide($distinctId, $groups), true)['featureFlags'] ?? []; |
| 145 |
} |
| 146 |
|
| 147 |
public function decide(string $distinctId, array $groups = array()) |
| 148 |
{ |
| 149 |
$payload = array( |
| 150 |
'api_key' => $this->apiKey, |
| 151 |
'distinct_id' => $distinctId, |
| 152 |
); |
| 153 |
|
| 154 |
if (!empty($groups)) { |
| 155 |
$payload["groups"] = $groups; |
| 156 |
} |
| 157 |
|
| 158 |
return $this->httpClient->sendRequest( |
| 159 |
'/decide/', |
| 160 |
json_encode($payload), |
| 161 |
[ |
| 162 |
// Send user agent in the form of {library_name}/{library_version} as per RFC 7231. |
| 163 |
"User-Agent: posthog-php/" . PostHog::VERSION, |
| 164 |
] |
| 165 |
)->getResponse(); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Aliases from one user id to another |
| 170 |
* |
| 171 |
* @param array $message |
| 172 |
* @return boolean whether the alias call succeeded |
| 173 |
*/ |
| 174 |
public function alias(array $message) |
| 175 |
{ |
| 176 |
$message = $this->message($message); |
| 177 |
$message["type"] = "alias"; |
| 178 |
$message["event"] = '$create_alias'; |
| 179 |
|
| 180 |
$message['properties']['distinct_id'] = $message['distinct_id']; |
| 181 |
$message['properties']['alias'] = $message['alias']; |
| 182 |
|
| 183 |
$message['distinct_id'] = null; |
| 184 |
unset($message['alias']); |
| 185 |
|
| 186 |
return $this->consumer->alias($message); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Queue a raw (prepared) message |
| 191 |
* |
| 192 |
* @param array $message |
| 193 |
* @return mixed whether the identify call succeeded |
| 194 |
*/ |
| 195 |
public function raw(array $message) |
| 196 |
{ |
| 197 |
return $this->consumer->enqueue($message); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Flush any async consumers |
| 202 |
* @return boolean true if flushed successfully |
| 203 |
*/ |
| 204 |
public function flush() |
| 205 |
{ |
| 206 |
if (method_exists($this->consumer, 'flush')) { |
| 207 |
return $this->consumer->flush(); |
| 208 |
} |
| 209 |
|
| 210 |
return true; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Formats a timestamp by making sure it is set |
| 215 |
* and converting it to iso8601. |
| 216 |
* |
| 217 |
* The timestamp can be time in seconds `time()` or `microseconds(true)`. |
| 218 |
* any other input is considered an error and the method will return a new date. |
| 219 |
* |
| 220 |
* Note: php's date() "u" format (for microseconds) has a bug in it |
| 221 |
* it always shows `.000` for microseconds since `date()` only accepts |
| 222 |
* ints, so we have to construct the date ourselves if microtime is passed. |
| 223 |
* |
| 224 |
* @param $ts |
| 225 |
* @return false|string |
| 226 |
*/ |
| 227 |
private function formatTime($ts) |
| 228 |
{ |
| 229 |
// time() |
| 230 |
if (null == $ts || !$ts) { |
| 231 |
$ts = time(); |
| 232 |
} |
| 233 |
if (false !== filter_var($ts, FILTER_VALIDATE_INT)) { |
| 234 |
return date("c", (int)$ts); |
| 235 |
} |
| 236 |
|
| 237 |
// anything else try to strtotime the date. |
| 238 |
if (false === filter_var($ts, FILTER_VALIDATE_FLOAT)) { |
| 239 |
if (is_string($ts)) { |
| 240 |
return date("c", strtotime($ts)); |
| 241 |
} |
| 242 |
|
| 243 |
return date("c"); |
| 244 |
} |
| 245 |
|
| 246 |
// fix for floatval casting in send.php |
| 247 |
$parts = explode(".", (string)$ts); |
| 248 |
if (!isset($parts[1])) { |
| 249 |
return date("c", (int)$parts[0]); |
| 250 |
} |
| 251 |
|
| 252 |
// microtime(true) |
| 253 |
$sec = (int)$parts[0]; |
| 254 |
$usec = (int)$parts[1]; |
| 255 |
$fmt = sprintf("Y-m-d\\TH:i:s%sP", $usec); |
| 256 |
|
| 257 |
return date($fmt, (int)$sec); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Add common fields to the given `message` |
| 262 |
* |
| 263 |
* @param array $msg |
| 264 |
* @return array |
| 265 |
*/ |
| 266 |
private function message($msg) |
| 267 |
{ |
| 268 |
if (!isset($msg["properties"])) { |
| 269 |
$msg["properties"] = array(); |
| 270 |
} |
| 271 |
|
| 272 |
$msg["library"] = 'posthog-php'; |
| 273 |
$msg["library_version"] = PostHog::VERSION; |
| 274 |
$msg["library_consumer"] = $this->consumer->getConsumer(); |
| 275 |
|
| 276 |
$msg["properties"]['$lib'] = 'posthog-php'; |
| 277 |
$msg["properties"]['$lib_version'] = PostHog::VERSION; |
| 278 |
$msg["properties"]['$lib_consumer'] = $this->consumer->getConsumer(); |
| 279 |
|
| 280 |
if (isset($msg["distinctId"])) { |
| 281 |
$msg["distinct_id"] = $msg["distinctId"]; |
| 282 |
unset($msg["distinctId"]); |
| 283 |
} |
| 284 |
|
| 285 |
if (!isset($msg["timestamp"])) { |
| 286 |
$msg["timestamp"] = null; |
| 287 |
} |
| 288 |
$msg["timestamp"] = $this->formatTime($msg["timestamp"]); |
| 289 |
|
| 290 |
return $msg; |
| 291 |
} |
| 292 |
} |
| 293 |
|