| Total Complexity | 41 |
| Total Lines | 351 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Analytics often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Analytics, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class Analytics |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * @var BaseRequest|null |
||
| 64 | */ |
||
| 65 | private ?BaseRequest $_request = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var Service|null|false |
||
| 69 | */ |
||
| 70 | private mixed $_service = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string|null |
||
| 74 | */ |
||
| 75 | private ?string $_affiliation = null; |
||
| 76 | |||
| 77 | private ?bool $_shouldSendAnalytics = null; |
||
| 78 | |||
| 79 | private ?string $_sessionString = null; |
||
| 80 | |||
| 81 | private array $eventList = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Component factory for creating events. |
||
| 85 | * |
||
| 86 | * @return ComponentFactory |
||
| 87 | */ |
||
| 88 | public function create(): ComponentFactory |
||
| 89 | { |
||
| 90 | return new ComponentFactory(); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Add an event to be sent to Google |
||
| 95 | * |
||
| 96 | * @param AbstractEvent $event |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public function addEvent(AbstractEvent $event): void |
||
| 100 | { |
||
| 101 | if ($this->_sessionString === null) { |
||
| 102 | $this->_sessionString = AnalyticsHelper::getSessionString(); |
||
| 103 | } |
||
| 104 | |||
| 105 | if (str_contains($this->_sessionString, '.')) { |
||
| 106 | [$sessionId, $sessionNumber] = explode('.', $this->_sessionString); |
||
| 107 | $event->setParamValue('sessionId', $sessionId); |
||
| 108 | $event->setParamValue('sessionNumber', $sessionNumber); |
||
| 109 | } |
||
| 110 | |||
| 111 | $this->eventList[] = $event; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Send the events collected so far. |
||
| 116 | * |
||
| 117 | * @return ?array |
||
| 118 | * @throws HydrationException |
||
| 119 | * @throws ValidationException |
||
| 120 | */ |
||
| 121 | public function sendCollectedEvents(): ?array |
||
| 122 | { |
||
| 123 | if ($this->_shouldSendAnalytics === null) { |
||
| 124 | $this->_shouldSendAnalytics = AnalyticsHelper::shouldSendAnalytics(); |
||
| 125 | } |
||
| 126 | |||
| 127 | if (!$this->_shouldSendAnalytics) { |
||
| 128 | return null; |
||
| 129 | } |
||
| 130 | |||
| 131 | $service = $this->service(); |
||
| 132 | |||
| 133 | if (!$service) { |
||
| 134 | return null; |
||
| 135 | } |
||
| 136 | |||
| 137 | $request = $this->request(); |
||
| 138 | $eventCount = count($this->eventList); |
||
| 139 | |||
| 140 | if (!InstantAnalytics::$settings->sendAnalyticsData) { |
||
| 141 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 142 | 'Analytics not enabled - skipped sending {count} events', |
||
| 143 | ['count' => $eventCount], |
||
| 144 | __METHOD__ |
||
| 145 | ); |
||
| 146 | |||
| 147 | return null; |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($eventCount === 0) { |
||
| 151 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 152 | 'No events collected to send', |
||
| 153 | [], |
||
| 154 | __METHOD__ |
||
| 155 | ); |
||
| 156 | |||
| 157 | return null; |
||
| 158 | } |
||
| 159 | |||
| 160 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 161 | 'Sending {count} analytics events', |
||
| 162 | ['count' => $eventCount], |
||
| 163 | __METHOD__ |
||
| 164 | ); |
||
| 165 | |||
| 166 | // Batch into groups of 25 |
||
| 167 | $responses = []; |
||
| 168 | |||
| 169 | foreach (array_chunk($this->eventList, 25) as $chunk) { |
||
| 170 | $request->getEvents()->setEventList([]); |
||
| 171 | |||
| 172 | /** @var AbstractEvent $event */ |
||
| 173 | foreach ($chunk as $event) { |
||
| 174 | $request->addEvent($event); |
||
| 175 | } |
||
| 176 | |||
| 177 | $responses[] = $service->send($request); |
||
| 178 | } |
||
| 179 | |||
| 180 | |||
| 181 | return $responses; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function getAffiliation(): ?string |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set affiliation for all the events that incorporate Commerce Product info for the remaining duration of request. |
||
| 191 | * |
||
| 192 | * @param string $affiliation |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | public function setAffiliation(string $affiliation): self |
||
| 196 | { |
||
| 197 | $this->_affiliation = $affiliation; |
||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Add a commerce item list impression. |
||
| 203 | * |
||
| 204 | * @param Product|Variant $productVariant |
||
| 205 | * @param int $index |
||
| 206 | * @param string $listName |
||
| 207 | * @throws InvalidConfigException |
||
| 208 | */ |
||
| 209 | public function addCommerceProductImpression(Product|Variant $productVariant, int $index = 0, string $listName = 'default') |
||
| 210 | { |
||
| 211 | InstantAnalytics::$plugin->commerce->addCommerceProductImpression($productVariant); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Begin checkout. |
||
| 216 | * |
||
| 217 | * @param Order $cart |
||
| 218 | */ |
||
| 219 | public function beginCheckout(Order $cart) |
||
| 220 | { |
||
| 221 | InstantAnalytics::$plugin->commerce->triggerBeginCheckoutEvent($cart); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Add a commerce item list impression. |
||
| 226 | * |
||
| 227 | * @param Product|Variant $productVariant |
||
| 228 | * @param int $index |
||
| 229 | * @param string $listName |
||
| 230 | * @throws InvalidConfigException |
||
| 231 | * @deprecated `Analytics::addCommerceProductDetailView()` is deprecated. Use `Analytics::addCommerceProductImpression()` instead. |
||
| 232 | */ |
||
| 233 | public function addCommerceProductDetailView(Product|Variant $productVariant, int $index = 0, string $listName = 'default') |
||
| 234 | { |
||
| 235 | Craft::$app->getDeprecator()->log('Analytics::addCommerceProductDetailView()', '`Analytics::addCommerceProductDetailView()` is deprecated. Use `Analytics::addCommerceProductImpression()` instead.'); |
||
| 236 | $this->addCommerceProductImpression($productVariant); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Add a commerce product list impression. |
||
| 241 | * |
||
| 242 | * @param array $products |
||
| 243 | * @param $listName |
||
| 244 | */ |
||
| 245 | public function addCommerceProductListImpression(array $products, string $listName = 'default') |
||
| 246 | { |
||
| 247 | InstantAnalytics::$plugin->commerce->addCommerceProductListImpression($products, $listName); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set the measurement id. |
||
| 252 | * |
||
| 253 | * @param string $measurementId |
||
| 254 | * @return $this |
||
| 255 | */ |
||
| 256 | public function setMeasurementId(string $measurementId): self |
||
| 257 | { |
||
| 258 | $this->service()?->setMeasurementId($measurementId); |
||
| 259 | return $this; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set the API secret. |
||
| 264 | * |
||
| 265 | * @param string $apiSecret |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function setApiSecret(string $apiSecret): self |
||
| 269 | { |
||
| 270 | $this->service()?->setApiSecret($apiSecret); |
||
| 271 | return $this; |
||
| 272 | } |
||
| 273 | |||
| 274 | public function __call(string $methodName, array $arguments): ?self |
||
| 275 | { |
||
| 276 | $knownProperties = [ |
||
| 277 | 'allowGoogleSignals' => 'allow_google_signals', |
||
| 278 | 'allowAdPersonalizationSignals' => 'allow_ad_personalization_signals', |
||
| 279 | 'campaignContent' => 'campaign_content', |
||
| 280 | 'campaignId' => 'campaign_id', |
||
| 281 | 'campaignMedium' => 'campaign_medium', |
||
| 282 | 'campaignName' => 'campaign_name', |
||
| 283 | 'campaignSource' => 'campaign_source', |
||
| 284 | 'campaignTerm' => 'campaign_term', |
||
| 285 | 'campaign' => 'campaign', |
||
| 286 | 'clientId' => 'client_id', |
||
| 287 | 'contentGroup' => 'content_group', |
||
| 288 | 'cookieDomain' => 'cookie_domain', |
||
| 289 | 'cookieExpires' => 'cookie_expires', |
||
| 290 | 'cookieFlags' => 'cookie_flags', |
||
| 291 | 'cookiePath' => 'cookie_path', |
||
| 292 | 'cookiePrefix' => 'cookie_prefix', |
||
| 293 | 'cookieUpdate' => 'cookie_update', |
||
| 294 | 'language' => 'language', |
||
| 295 | 'pageLocation' => 'page_location', |
||
| 296 | 'pageReferrer' => 'page_referrer', |
||
| 297 | 'pageTitle' => 'page_title', |
||
| 298 | 'sendPageView' => 'send_page_view', |
||
| 299 | 'screenResolution' => 'screen_resolution', |
||
| 300 | 'userId' => 'user_id', |
||
| 301 | ]; |
||
| 302 | |||
| 303 | if (str_starts_with($methodName, 'set')) { |
||
| 304 | $methodName = lcfirst(substr($methodName, 3)); |
||
| 305 | |||
| 306 | $service = $this->service(); |
||
| 307 | if ($service && !empty($knownProperties[$methodName])) { |
||
| 308 | $service->setAdditionalQueryParam($knownProperties[$methodName], $arguments[0]); |
||
| 309 | |||
| 310 | return $this; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | return null; |
||
| 315 | } |
||
| 316 | |||
| 317 | public function request(): BaseRequest |
||
| 318 | { |
||
| 319 | if ($this->_request === null) { |
||
| 320 | $this->_request = new BaseRequest(); |
||
| 321 | |||
| 322 | $this->_request->setClientId(AnalyticsHelper::getClientId()); |
||
| 323 | |||
| 324 | if (InstantAnalytics::$settings->sendUserId) { |
||
| 325 | $userId = AnalyticsHelper::getUserId(); |
||
| 326 | |||
| 327 | if ($userId) { |
||
| 328 | $this->request()->setUserId($userId); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | |||
| 334 | return $this->_request; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Init the service used to send events |
||
| 339 | */ |
||
| 340 | public function init(): void |
||
| 344 | } |
||
| 345 | |||
| 346 | protected function service(): ?Service |
||
| 347 | { |
||
| 348 | if ($this->_service === null) { |
||
| 349 | $settings = InstantAnalytics::$settings; |
||
| 350 | $apiSecret = App::parseEnv($settings->googleAnalyticsMeasurementApiSecret); |
||
| 351 | $measurementId = App::parseEnv($settings->googleAnalyticsMeasurementId); |
||
| 411 | } |
||
| 412 | } |
||
| 413 |