| Total Complexity | 40 |
| Total Lines | 345 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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 $_request = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var Service|null|false |
||
| 69 | */ |
||
| 70 | private $_service = null; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string|null |
||
| 74 | */ |
||
| 75 | private $_affiliation = null; |
||
| 76 | |||
| 77 | private $_shouldSendAnalytics = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Component factory for creating events. |
||
| 81 | * |
||
| 82 | * @return ComponentFactory |
||
| 83 | */ |
||
| 84 | public function create(): ComponentFactory |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Add an event to be sent to Google |
||
| 91 | * |
||
| 92 | * @param AbstractEvent $event |
||
| 93 | * @return BaseRequest |
||
| 94 | */ |
||
| 95 | public function addEvent(AbstractEvent $event): BaseRequest |
||
| 96 | { |
||
| 97 | $clientId = $this->request()->getClientId(); |
||
| 98 | |||
| 99 | if (strpos($clientId, '.') !== false) { |
||
| 100 | [$sessionId, $sessionNumber] = explode('.', $clientId); |
||
| 101 | $event->setSessionId($sessionId); |
||
| 102 | $event->setSessionNumber($sessionNumber); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $this->request()->addEvent($event); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Send the events collected so far. |
||
| 110 | * |
||
| 111 | * @return BaseResponse|null |
||
| 112 | * @throws HydrationException |
||
| 113 | * @throws ValidationException |
||
| 114 | */ |
||
| 115 | public function sendCollectedEvents(): ?BaseResponse |
||
| 116 | { |
||
| 117 | if ($this->_shouldSendAnalytics === null) { |
||
| 118 | $this->_shouldSendAnalytics = AnalyticsHelper::shouldSendAnalytics(); |
||
| 119 | } |
||
| 120 | |||
| 121 | if (!$this->_shouldSendAnalytics) { |
||
| 122 | return null; |
||
| 123 | } |
||
| 124 | |||
| 125 | $service = $this->service(); |
||
| 126 | |||
| 127 | if (!$service) { |
||
| 128 | return null; |
||
| 129 | } |
||
| 130 | |||
| 131 | $request = $this->request(); |
||
| 132 | $eventCount = count($request->getEvents()->getEventList()); |
||
| 133 | |||
| 134 | if (!InstantAnalytics::$settings->sendAnalyticsData) { |
||
| 135 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 136 | 'Analytics not enabled - skipped sending {count} events', |
||
| 137 | ['count' => $eventCount], |
||
| 138 | __METHOD__ |
||
| 139 | ); |
||
| 140 | |||
| 141 | return null; |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($eventCount === 0) { |
||
| 145 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 146 | 'No events collected to send', |
||
| 147 | [], |
||
| 148 | __METHOD__ |
||
| 149 | ); |
||
| 150 | |||
| 151 | return null; |
||
| 152 | } |
||
| 153 | |||
| 154 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 155 | 'Sending {count} analytics events', |
||
| 156 | ['count' => $eventCount], |
||
| 157 | __METHOD__ |
||
| 158 | ); |
||
| 159 | |||
| 160 | $response = $service->send($request); |
||
| 161 | |||
| 162 | // Clear events already sent from the list. |
||
| 163 | $request->getEvents()->setEventList([]); |
||
| 164 | |||
| 165 | return $response; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Set affiliation for all the events that incorporate Commerce Product info for the remaining duration of request. |
||
| 170 | * |
||
| 171 | * @param string $affiliation |
||
| 172 | * @return $this |
||
| 173 | */ |
||
| 174 | public function setAffiliation(string $affiliation): self |
||
| 175 | { |
||
| 176 | $this->_affiliation = $affiliation; |
||
| 177 | return $this; |
||
| 178 | } |
||
| 179 | |||
| 180 | public function getAffiliation(): ?string |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Add a commerce item list impression. |
||
| 187 | * |
||
| 188 | * @param Product|Variant $productVariant |
||
| 189 | * @param int $index |
||
| 190 | * @param string $listName |
||
| 191 | * @throws InvalidConfigException |
||
| 192 | */ |
||
| 193 | public function addCommerceProductImpression($productVariant, int $index = 0, string $listName = 'default') { |
||
| 194 | InstantAnalytics::$plugin->commerce->addCommerceProductImpression($productVariant); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Begin checkout. |
||
| 199 | * |
||
| 200 | * @param Order $cart |
||
| 201 | */ |
||
| 202 | public function beginCheckout(Order $cart) { |
||
| 203 | InstantAnalytics::$plugin->commerce->triggerBeginCheckoutEvent($cart); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Add a commerce item list impression. |
||
| 208 | * |
||
| 209 | * @param Product|Variant $productVariant |
||
| 210 | * @param int $index |
||
| 211 | * @param string $listName |
||
| 212 | * @deprecated `Analytics::addCommerceProductDetailView()` is deprecated. Use `Analytics::addCommerceProductImpression()` instead. |
||
| 213 | * @throws InvalidConfigException |
||
| 214 | */ |
||
| 215 | public function addCommerceProductDetailView($productVariant, int $index = 0, string $listName = 'default') { |
||
| 216 | Craft::$app->getDeprecator()->log('Analytics::addCommerceProductDetailView()', '`Analytics::addCommerceProductDetailView()` is deprecated. Use `Analytics::addCommerceProductImpression()` instead.'); |
||
| 217 | $this->addCommerceProductImpression($productVariant); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Add a commerce product list impression. |
||
| 222 | * |
||
| 223 | * @param array $products |
||
| 224 | * @param $listName |
||
| 225 | */ |
||
| 226 | public function addCommerceProductListImpression(array $products, $listName) { |
||
| 227 | InstantAnalytics::$plugin->commerce->addCommerceProductListImpression($products, $listName); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set the measurement id. |
||
| 232 | * |
||
| 233 | * @param string $measurementId |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | public function setMeasurementId(string $measurementId): self |
||
| 237 | { |
||
| 238 | $service = $this->service(); |
||
| 239 | |||
| 240 | if (!$service) { |
||
| 241 | throw new InvalidConfigException('instant-analytics-ga4', 'Unable to create GA4 service object'); |
||
| 242 | } |
||
| 243 | |||
| 244 | $service->setMeasurementId($measurementId); |
||
| 245 | return $this; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Set the API secret. |
||
| 250 | * |
||
| 251 | * @param string $apiSecret |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function setApiSecret(string $apiSecret): self |
||
| 255 | { |
||
| 256 | $service = $this->service(); |
||
| 257 | |||
| 258 | if (!$service) { |
||
| 259 | throw new InvalidConfigException('instant-analytics-ga4', 'Unable to create GA4 service object'); |
||
| 260 | } |
||
| 261 | |||
| 262 | return $this; |
||
| 263 | } |
||
| 264 | |||
| 265 | public function __call(string $methodName, array $arguments): ?self |
||
| 266 | { |
||
| 267 | $knownProperties = [ |
||
| 268 | 'allowGoogleSignals' => 'allow_google_signals', |
||
| 269 | 'allowAdPersonalizationSignals' => 'allow_ad_personalization_signals', |
||
| 270 | 'campaignContent' => 'campaign_content', |
||
| 271 | 'campaignId' => 'campaign_id', |
||
| 272 | 'campaignMedium' => 'campaign_medium', |
||
| 273 | 'campaignName' => 'campaign_name', |
||
| 274 | 'campaignSource' => 'campaign_source', |
||
| 275 | 'campaignTerm' => 'campaign_term', |
||
| 276 | 'campaign' => 'campaign', |
||
| 277 | 'clientId' => 'client_id', |
||
| 278 | 'contentGroup' => 'content_group', |
||
| 279 | 'cookieDomain' => 'cookie_domain', |
||
| 280 | 'cookieExpires' => 'cookie_expires', |
||
| 281 | 'cookieFlags' => 'cookie_flags', |
||
| 282 | 'cookiePath' => 'cookie_path', |
||
| 283 | 'cookiePrefix' => 'cookie_prefix', |
||
| 284 | 'cookieUpdate' => 'cookie_update', |
||
| 285 | 'language' => 'language', |
||
| 286 | 'pageLocation' => 'page_location', |
||
| 287 | 'pageReferrer' => 'page_referrer', |
||
| 288 | 'pageTitle' => 'page_title', |
||
| 289 | 'sendPageView' => 'send_page_view', |
||
| 290 | 'screenResolution' => 'screen_resolution', |
||
| 291 | 'userId' => 'user_id' |
||
| 292 | ]; |
||
| 293 | |||
| 294 | if (str_starts_with($methodName, 'set')) { |
||
| 295 | $methodName = lcfirst(substr($methodName, 3)); |
||
| 296 | |||
| 297 | $service = $this->service(); |
||
| 298 | if ($service && !empty($knownProperties[$methodName])) { |
||
| 299 | $service->setAdditionalQueryParam($knownProperties[$methodName], $arguments[0]); |
||
| 300 | |||
| 301 | return $this; |
||
| 302 | } |
||
| 303 | |||
| 304 | } |
||
| 305 | |||
| 306 | return null; |
||
| 307 | } |
||
| 308 | |||
| 309 | protected function request(): BaseRequest |
||
| 310 | { |
||
| 311 | if ($this->_request === null) { |
||
| 312 | $this->_request = new BaseRequest(); |
||
| 313 | |||
| 314 | $this->_request->setClientId(AnalyticsHelper::getClientId()); |
||
| 315 | |||
| 316 | if (InstantAnalytics::$settings->sendUserId) { |
||
| 317 | $userId = AnalyticsHelper::getUserId(); |
||
| 318 | |||
| 319 | if ($userId) { |
||
| 320 | $this->request()->setUserId($userId); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | |||
| 326 | return $this->_request; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Init the service used to send events |
||
| 331 | */ |
||
| 332 | public function init(): void |
||
| 336 | } |
||
| 337 | |||
| 338 | protected function service(): ?Service |
||
| 339 | { |
||
| 340 | if ($this->_service === null) { |
||
| 341 | $settings = InstantAnalytics::$settings; |
||
| 342 | $apiSecret = Craft::parseEnv($settings->googleAnalyticsMeasurementApiSecret); |
||
| 343 | $measurementId = Craft::parseEnv($settings->googleAnalyticsMeasurementId); |
||
| 405 | } |
||
| 406 | } |
||
| 407 |