|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AlexWestergaard\PhpGa4; |
| 4 | + |
| 5 | +use GuzzleHttp\Client as Guzzle; |
| 6 | +use AlexWestergaard\PhpGa4\Helper; |
| 7 | +use AlexWestergaard\PhpGa4\Facade; |
| 8 | +use AlexWestergaard\PhpGa4\Exception\Ga4Exception; |
| 9 | + |
| 10 | +class Firebase extends Helper\IOHelper implements Facade\Type\FirebaseType |
| 11 | +{ |
| 12 | + private Guzzle $guzzle; |
| 13 | + |
| 14 | + private Helper\ConsentHelper $consent; |
| 15 | + private Helper\UserDataHelper $userdata; |
| 16 | + |
| 17 | + protected null|bool $non_personalized_ads = false; |
| 18 | + protected null|int $timestamp_micros; |
| 19 | + protected null|string $app_instance_id; |
| 20 | + protected null|string $user_id; |
| 21 | + protected array $user_properties = []; |
| 22 | + protected array $events = []; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + private string $firebase_app_id, |
| 26 | + private string $api_secret, |
| 27 | + private bool $debug = false |
| 28 | + ) { |
| 29 | + parent::__construct(); |
| 30 | + $this->guzzle = new Guzzle(); |
| 31 | + $this->consent = new Helper\ConsentHelper(); |
| 32 | + $this->userdata = new Helper\UserDataHelper(); |
| 33 | + } |
| 34 | + |
| 35 | + public function getParams(): array |
| 36 | + { |
| 37 | + return [ |
| 38 | + 'non_personalized_ads', |
| 39 | + 'timestamp_micros', |
| 40 | + 'app_instance_id', |
| 41 | + 'user_id', |
| 42 | + 'user_properties', |
| 43 | + 'events', |
| 44 | + ]; |
| 45 | + } |
| 46 | + |
| 47 | + public function getRequiredParams(): array |
| 48 | + { |
| 49 | + return ['app_instance_id']; |
| 50 | + } |
| 51 | + |
| 52 | + public function setAppInstanceId(string $id) |
| 53 | + { |
| 54 | + $this->app_instance_id = $id; |
| 55 | + return $this; |
| 56 | + } |
| 57 | + |
| 58 | + public function setUserId(string $id) |
| 59 | + { |
| 60 | + $this->user_id = $id; |
| 61 | + return $this; |
| 62 | + } |
| 63 | + |
| 64 | + public function setTimestampMicros(int|float $microOrUnix) |
| 65 | + { |
| 66 | + $min = Helper\ConvertHelper::timeAsMicro(strtotime('-3 days') + 10); |
| 67 | + $max = Helper\ConvertHelper::timeAsMicro(time() + 3); |
| 68 | + |
| 69 | + $time = Helper\ConvertHelper::timeAsMicro($microOrUnix); |
| 70 | + |
| 71 | + if ($time < $min || $time > $max) { |
| 72 | + throw Ga4Exception::throwMicrotimeExpired(); |
| 73 | + } |
| 74 | + |
| 75 | + $this->timestamp_micros = $time; |
| 76 | + return $this; |
| 77 | + } |
| 78 | + |
| 79 | + public function addUserProperty(Facade\Type\UserPropertyType ...$props) |
| 80 | + { |
| 81 | + foreach ($props as $prop) { |
| 82 | + $this->user_properties = array_replace($this->user_properties, $prop->toArray()); |
| 83 | + } |
| 84 | + |
| 85 | + return $this; |
| 86 | + } |
| 87 | + |
| 88 | + public function addEvent(Facade\Type\EventType ...$events) |
| 89 | + { |
| 90 | + foreach ($events as $event) { |
| 91 | + $this->events[] = $event->toArray(); |
| 92 | + } |
| 93 | + |
| 94 | + return $this; |
| 95 | + } |
| 96 | + |
| 97 | + public function consent(): Helper\ConsentHelper |
| 98 | + { |
| 99 | + return $this->consent; |
| 100 | + } |
| 101 | + |
| 102 | + public function userdata(): Helper\UserDataHelper |
| 103 | + { |
| 104 | + return $this->userdata; |
| 105 | + } |
| 106 | + |
| 107 | + public function post(): void |
| 108 | + { |
| 109 | + if (empty($this->firebase_app_id)) { |
| 110 | + throw Ga4Exception::throwMissingFirebaseAppId(); |
| 111 | + } |
| 112 | + |
| 113 | + if (empty($this->api_secret)) { |
| 114 | + throw Ga4Exception::throwMissingApiSecret(); |
| 115 | + } |
| 116 | + |
| 117 | + if (empty($this->app_instance_id)) { |
| 118 | + throw Ga4Exception::throwMissingAppInstanceId(); |
| 119 | + } |
| 120 | + |
| 121 | + $url = $this->debug ? Facade\Type\AnalyticsType::URL_DEBUG : Facade\Type\AnalyticsType::URL_LIVE; |
| 122 | + $url .= '?' . http_build_query(['firebase_app_id' => $this->firebase_app_id, 'api_secret' => $this->api_secret]); |
| 123 | + |
| 124 | + $body = array_replace_recursive( |
| 125 | + $this->toArray(), |
| 126 | + ["app_instance_id" => $this->app_instance_id], |
| 127 | + ["user_data" => !empty($this->user_id) ? $this->userdata->toArray() : []], // Only accepted if user_id is passed too |
| 128 | + ["user_properties" => $this->user_properties], |
| 129 | + ["consent" => $this->consent->toArray()], |
| 130 | + ); |
| 131 | + |
| 132 | + if (count($body["user_data"]) < 1) unset($body["user_data"]); |
| 133 | + if (count($body["user_properties"]) < 1) unset($body["user_properties"]); |
| 134 | + |
| 135 | + $chunkEvents = array_chunk($this->events, 25); |
| 136 | + |
| 137 | + if (count($chunkEvents) < 1) { |
| 138 | + throw Ga4Exception::throwMissingEvents(); |
| 139 | + } |
| 140 | + |
| 141 | + $this->userdata->reset(); |
| 142 | + $this->user_properties = []; |
| 143 | + $this->events = []; |
| 144 | + |
| 145 | + foreach ($chunkEvents as $events) { |
| 146 | + $body['events'] = $events; |
| 147 | + |
| 148 | + $kB = 1024; |
| 149 | + if (($size = mb_strlen(json_encode($body))) > ($kB * 130)) { |
| 150 | + Ga4Exception::throwRequestTooLarge(intval($size / $kB)); |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + $jsonBody = json_encode($body); |
| 155 | + $jsonBody = strtr($jsonBody, [':[]' => ':{}']); |
| 156 | + |
| 157 | + $res = $this->guzzle->request('POST', $url, [ |
| 158 | + 'headers' => [ |
| 159 | + 'content-type' => 'application/json;charset=utf-8' |
| 160 | + ], |
| 161 | + 'body' => $jsonBody, |
| 162 | + ]); |
| 163 | + |
| 164 | + if (!in_array(($code = $res?->getStatusCode() ?? 0), Facade\Type\AnalyticsType::ACCEPT_RESPONSE_HEADERS)) { |
| 165 | + Ga4Exception::throwRequestWrongResponceCode($code); |
| 166 | + } |
| 167 | + |
| 168 | + if ($code !== 204) { |
| 169 | + $callback = @json_decode($res->getBody()->getContents(), true); |
| 170 | + |
| 171 | + if (json_last_error() != JSON_ERROR_NONE) { |
| 172 | + Ga4Exception::throwRequestInvalidResponse(); |
| 173 | + } elseif (empty($callback)) { |
| 174 | + Ga4Exception::throwRequestEmptyResponse(); |
| 175 | + } elseif (!empty($callback['validationMessages'])) { |
| 176 | + foreach ($callback['validationMessages'] as $msg) { |
| 177 | + Ga4Exception::throwRequestInvalidBody($msg); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if (Ga4Exception::hasThrowStack()) { |
| 184 | + throw Ga4Exception::getThrowStack(); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + public static function new(string $firebase_app_id, string $api_secret, bool $debug = false): static |
| 189 | + { |
| 190 | + return new static($firebase_app_id, $api_secret, $debug); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Deprecated references |
| 195 | + */ |
| 196 | + |
| 197 | + /** @deprecated 1.1.9 Please use `Analytics->consent->setAdPersonalizationPermission()` instead */ |
| 198 | + public function setNonPersonalizedAds(bool $exclude) |
| 199 | + { |
| 200 | + $this->consent->setAdPersonalizationPermission(!$exclude); |
| 201 | + return $this; |
| 202 | + } |
| 203 | + |
| 204 | + /** @deprecated 1.1.1 Please use `Analytics->consent->setAdPersonalizationPermission()` instead */ |
| 205 | + public function allowPersonalisedAds(bool $allow) |
| 206 | + { |
| 207 | + $this->consent->setAdPersonalizationPermission($allow); |
| 208 | + } |
| 209 | + |
| 210 | + /** @deprecated 1.1.1 Please use `Analytics->setTimestampMicros()` instead */ |
| 211 | + public function setTimestamp(int|float $microOrUnix) |
| 212 | + { |
| 213 | + $this->setTimestampMicros($microOrUnix); |
| 214 | + } |
| 215 | +} |
0 commit comments