| Total Complexity | 52 |
| Total Lines | 322 |
| 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 |
||
| 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( |
||
| 139 | string $url, |
||
| 140 | string $eventName, |
||
| 141 | array $params = [], |
||
| 142 | ): string { |
||
| 143 | $urlParams = compact('url', 'eventName', 'params'); |
||
| 144 | |||
| 145 | $fileName = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_BASENAME); |
||
| 146 | $trackingUrl = UrlHelper::siteUrl('instantanalytics/eventTrack/' . $fileName, $urlParams); |
||
| 147 | |||
| 148 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 149 | 'Created eventTrackingUrl for: {trackingUrl}', |
||
| 150 | [ |
||
| 151 | 'trackingUrl' => $trackingUrl, |
||
| 152 | ], |
||
| 153 | __METHOD__ |
||
| 154 | ); |
||
| 155 | |||
| 156 | return $trackingUrl; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * _shouldSendAnalytics determines whether we should be sending Google |
||
| 161 | * Analytics data |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public static function shouldSendAnalytics(): bool |
||
| 166 | { |
||
| 167 | $result = true; |
||
| 168 | $request = Craft::$app->getRequest(); |
||
| 169 | |||
| 170 | $logExclusion = static function(string $setting) { |
||
| 171 | if (InstantAnalytics::$settings->logExcludedAnalytics) { |
||
| 172 | $request = Craft::$app->getRequest(); |
||
| 173 | $requestIp = $request->getUserIP(); |
||
| 174 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 175 | 'Analytics excluded for:: {requestIp} due to: `{setting}`', |
||
| 176 | compact('requestIp', 'setting'), |
||
| 177 | __METHOD__ |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | }; |
||
| 181 | |||
| 182 | if (!InstantAnalytics::$settings->sendAnalyticsData) { |
||
| 183 | $logExclusion('sendAnalyticsData'); |
||
| 184 | return false; |
||
| 185 | } |
||
| 186 | |||
| 187 | if (!InstantAnalytics::$settings->sendAnalyticsInDevMode && Craft::$app->getConfig()->getGeneral()->devMode) { |
||
| 188 | $logExclusion('sendAnalyticsInDevMode'); |
||
| 189 | return false; |
||
| 190 | } |
||
| 191 | |||
| 192 | if ($request->getIsConsoleRequest()) { |
||
| 193 | $logExclusion('Craft::$app->getRequest()->getIsConsoleRequest()'); |
||
| 194 | return false; |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($request->getIsCpRequest()) { |
||
| 198 | $logExclusion('Craft::$app->getRequest()->getIsCpRequest()'); |
||
| 199 | return false; |
||
| 200 | } |
||
| 201 | |||
| 202 | if ($request->getIsLivePreview()) { |
||
| 203 | $logExclusion('Craft::$app->getRequest()->getIsLivePreview()'); |
||
| 204 | return false; |
||
| 205 | } |
||
| 206 | |||
| 207 | // Check the $_SERVER[] super-global exclusions |
||
| 208 | if (InstantAnalytics::$settings->serverExcludes !== null |
||
| 209 | && !empty(InstantAnalytics::$settings->serverExcludes)) { |
||
| 210 | foreach (InstantAnalytics::$settings->serverExcludes as $match => $matchArray) { |
||
| 211 | if (isset($_SERVER[$match])) { |
||
| 212 | foreach ($matchArray as $matchItem) { |
||
| 213 | if (preg_match($matchItem, $_SERVER[$match])) { |
||
| 214 | $logExclusion('serverExcludes'); |
||
| 215 | |||
| 216 | return false; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | // Filter out bot/spam requests via UserAgent |
||
| 224 | if (InstantAnalytics::$settings->filterBotUserAgents) { |
||
| 225 | $crawlerDetect = new CrawlerDetect(); |
||
| 226 | // Check the user agent of the current 'visitor' |
||
| 227 | if ($crawlerDetect->isCrawler()) { |
||
| 228 | $logExclusion('filterBotUserAgents'); |
||
| 229 | |||
| 230 | return false; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | // Filter by user group |
||
| 235 | $userService = Craft::$app->getUser(); |
||
| 236 | /** @var ?UserElement $user */ |
||
| 237 | $user = $userService->getIdentity(); |
||
| 238 | if ($user) { |
||
| 239 | if (InstantAnalytics::$settings->adminExclude && $user->admin) { |
||
| 240 | $logExclusion('adminExclude'); |
||
| 241 | |||
| 242 | return false; |
||
| 243 | } |
||
| 244 | |||
| 245 | if (InstantAnalytics::$settings->groupExcludes !== null |
||
| 246 | && !empty(InstantAnalytics::$settings->groupExcludes)) { |
||
| 247 | foreach (InstantAnalytics::$settings->groupExcludes as $matchItem) { |
||
| 248 | if ($user->isInGroup($matchItem)) { |
||
| 249 | $logExclusion('groupExcludes'); |
||
| 250 | |||
| 251 | return false; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | return $result; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * getClientId handles the parsing of the _ga cookie or setting it to a |
||
| 262 | * unique identifier |
||
| 263 | * |
||
| 264 | * @return string the cid |
||
| 265 | */ |
||
| 266 | public static function getClientId(): string |
||
| 267 | { |
||
| 268 | $cid = ''; |
||
| 269 | if (isset($_COOKIE['_ga'])) { |
||
| 270 | $parts = explode(".", $_COOKIE['_ga'], 4); |
||
| 271 | if ($parts !== false) { |
||
| 272 | $cid = implode('.', array_slice($parts, 2)); |
||
| 273 | } |
||
| 274 | } elseif (isset($_COOKIE['_ia']) && $_COOKIE['_ia'] !== '') { |
||
| 275 | $cid = $_COOKIE['_ia']; |
||
| 276 | } else { |
||
| 277 | // Generate our own client id, otherwise. |
||
| 278 | $cid = static::gaGenUUID() . '.1'; |
||
| 279 | } |
||
| 280 | |||
| 281 | if (InstantAnalytics::$settings->createGclidCookie && !empty($cid)) { |
||
| 282 | setcookie('_ia', $cid, strtotime('+2 years'), '/'); // Two years |
||
| 283 | } |
||
| 284 | |||
| 285 | return $cid; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get the Google Analytics session string from the cookie. |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public static function getSessionString(): string |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get the user id. |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public static function getUserId(): string |
||
| 315 | { |
||
| 316 | $userId = Craft::$app->getUser()->getId(); |
||
| 317 | |||
| 318 | if (!$userId) { |
||
| 319 | return ''; |
||
| 320 | } |
||
| 321 | |||
| 322 | return $userId; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * gaGenUUID Generate UUID v4 function - needed to generate a CID when one |
||
| 327 | * isn't available |
||
| 328 | * |
||
| 329 | * @return string The generated UUID |
||
| 330 | */ |
||
| 331 | protected static function gaGenUUID() |
||
| 351 | ); |
||
| 352 | } |
||
| 353 | } |
||
| 354 |