| Total Complexity | 52 |
| Total Lines | 324 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 2 | 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 |
||
| 29 | class Analytics |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * If SEOmatic is installed, set the page title from it |
||
| 33 | */ |
||
| 34 | public static function getTitleFromSeomatic(): ?string |
||
| 35 | { |
||
| 36 | if (!InstantAnalytics::$seomaticPlugin) { |
||
| 37 | return null; |
||
| 38 | } |
||
| 39 | if (!Seomatic::$settings->renderEnabled) { |
||
| 40 | return null; |
||
| 41 | } |
||
| 42 | $titleTag = Seomatic::$plugin->title->get('title'); |
||
| 43 | |||
| 44 | if ($titleTag === null) { |
||
| 45 | return null; |
||
| 46 | } |
||
| 47 | |||
| 48 | $titleArray = $titleTag->renderAttributes(); |
||
| 49 | |||
| 50 | if (empty($titleArray['title'])) { |
||
| 51 | return null; |
||
| 52 | } |
||
| 53 | |||
| 54 | return $titleArray['title']; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Return a sanitized documentPath from a URL |
||
| 59 | * |
||
| 60 | * @param string $url |
||
| 61 | * |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public static function getDocumentPathFromUrl(string $url = ''): string |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get a PageView tracking URL |
||
| 102 | * |
||
| 103 | * @param $url |
||
| 104 | * @param $title |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | * @throws Exception |
||
| 108 | */ |
||
| 109 | public static function getPageViewTrackingUrl($url, $title): string |
||
| 110 | { |
||
| 111 | $urlParams = compact('url', 'title'); |
||
| 112 | |||
| 113 | $path = parse_url($url, PHP_URL_PATH); |
||
| 114 | $pathFragments = explode('/', rtrim($path, '/')); |
||
| 115 | $fileName = end($pathFragments); |
||
| 116 | $trackingUrl = UrlHelper::siteUrl('instantanalytics/pageViewTrack/' . $fileName, $urlParams); |
||
| 117 | |||
| 118 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 119 | 'Created pageViewTrackingUrl for: {trackingUrl}', |
||
| 120 | [ |
||
| 121 | 'trackingUrl' => $trackingUrl |
||
| 122 | ], |
||
| 123 | __METHOD__ |
||
| 124 | ); |
||
| 125 | |||
| 126 | return $trackingUrl; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get an Event tracking URL |
||
| 131 | * |
||
| 132 | * @param string $url |
||
| 133 | * @param string $eventName |
||
| 134 | * @param array $params |
||
| 135 | * @return string |
||
| 136 | * @throws Exception |
||
| 137 | */ |
||
| 138 | public static function getEventTrackingUrl( |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * _shouldSendAnalytics determines whether we should be sending Google |
||
| 162 | * Analytics data |
||
| 163 | * |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public static function shouldSendAnalytics(): bool |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * getClientId handles the parsing of the _ga cookie or setting it to a |
||
| 264 | * unique identifier |
||
| 265 | * |
||
| 266 | * @return string the cid |
||
| 267 | */ |
||
| 268 | public static function getClientId(): string |
||
| 269 | { |
||
| 270 | $cid = ''; |
||
| 271 | if (isset($_COOKIE['_ga'])) { |
||
| 272 | $parts = explode(".", $_COOKIE['_ga'], 4); |
||
| 273 | if ($parts !== false) { |
||
| 274 | $cid = implode('.', array_slice($parts, 2)); |
||
| 275 | } |
||
| 276 | } elseif (isset($_COOKIE['_ia']) && $_COOKIE['_ia'] !== '') { |
||
| 277 | $cid = $_COOKIE['_ia']; |
||
| 278 | } else { |
||
| 279 | // Generate our own client id, otherwise. |
||
| 280 | $cid = static::gaGenUUID() . '.1'; |
||
| 281 | } |
||
| 282 | |||
| 283 | if (InstantAnalytics::$settings->createGclidCookie && !empty($cid)) { |
||
| 284 | setcookie('_ia', $cid, strtotime('+2 years'), '/'); // Two years |
||
| 285 | } |
||
| 286 | |||
| 287 | return $cid; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get the Google Analytics session string from the cookie. |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public static function getSessionString(): string |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get the user id. |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public static function getUserId(): string |
||
| 317 | { |
||
| 318 | $userId = Craft::$app->getUser()->getId(); |
||
| 319 | |||
| 320 | if (!$userId) { |
||
| 321 | return ''; |
||
| 322 | } |
||
| 323 | |||
| 324 | return $userId; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * gaGenUUID Generate UUID v4 function - needed to generate a CID when one |
||
| 329 | * isn't available |
||
| 330 | * |
||
| 331 | * @return string The generated UUID |
||
| 332 | */ |
||
| 333 | protected static function gaGenUUID() |
||
| 353 | ); |
||
| 354 | } |
||
| 355 | } |
||
| 356 |