| Total Complexity | 52 |
| Total Lines | 324 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 65 | { |
||
| 66 | if ($url === '') { |
||
| 67 | $url = Craft::$app->getRequest()->getFullPath(); |
||
| 68 | } |
||
| 69 | |||
| 70 | // We want to send just a path to GA for page views |
||
| 71 | if (UrlHelper::isAbsoluteUrl($url)) { |
||
| 72 | $urlParts = parse_url($url); |
||
| 73 | $url = $urlParts['path'] ?? '/'; |
||
| 74 | if (isset($urlParts['query'])) { |
||
| 75 | $url .= '?' . $urlParts['query']; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | // We don't want to send protocol-relative URLs either |
||
| 80 | if (UrlHelper::isProtocolRelativeUrl($url)) { |
||
| 81 | $url = substr($url, 1); |
||
| 82 | } |
||
| 83 | |||
| 84 | // Strip the query string if that's the global config setting |
||
| 85 | if (InstantAnalytics::$settings) { |
||
| 86 | if (InstantAnalytics::$settings->stripQueryString !== null |
||
| 87 | && InstantAnalytics::$settings->stripQueryString) { |
||
| 88 | $url = UrlHelper::stripQueryString($url); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | // We always want the path to be / rather than empty |
||
| 93 | if ($url === '') { |
||
| 94 | $url = '/'; |
||
| 95 | } |
||
| 96 | |||
| 97 | return $url; |
||
| 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( |
||
| 139 | string $url, |
||
| 140 | string $eventName, |
||
| 141 | array $params = [] |
||
| 142 | ): string |
||
| 143 | { |
||
| 144 | $urlParams = compact('url', 'eventName', 'params'); |
||
| 145 | |||
| 146 | $fileName = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_BASENAME); |
||
| 147 | $trackingUrl = UrlHelper::siteUrl('instantanalytics/eventTrack/' . $fileName, $urlParams); |
||
| 148 | |||
| 149 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 150 | 'Created eventTrackingUrl for: {trackingUrl}', |
||
| 151 | [ |
||
| 152 | 'trackingUrl' => $trackingUrl |
||
| 153 | ], |
||
| 154 | __METHOD__ |
||
| 155 | ); |
||
| 156 | |||
| 157 | return $trackingUrl; |
||
| 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 |
||
| 167 | { |
||
| 168 | $result = true; |
||
| 169 | $request = Craft::$app->getRequest(); |
||
| 170 | |||
| 171 | $logExclusion = static function (string $setting) |
||
| 172 | { |
||
| 173 | if (InstantAnalytics::$settings->logExcludedAnalytics) { |
||
| 174 | $request = Craft::$app->getRequest(); |
||
| 175 | $requestIp = $request->getUserIP(); |
||
| 176 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 177 | 'Analytics excluded for:: {requestIp} due to: `{setting}`', |
||
| 178 | compact('requestIp', 'setting'), |
||
| 179 | __METHOD__ |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | }; |
||
| 183 | |||
| 184 | if (!InstantAnalytics::$settings->sendAnalyticsData) { |
||
| 185 | $logExclusion('sendAnalyticsData'); |
||
| 186 | return false; |
||
| 187 | } |
||
| 188 | |||
| 189 | if (!InstantAnalytics::$settings->sendAnalyticsInDevMode && Craft::$app->getConfig()->getGeneral()->devMode) { |
||
| 190 | $logExclusion('sendAnalyticsInDevMode'); |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | |||
| 194 | if ($request->getIsConsoleRequest()) { |
||
| 195 | $logExclusion('Craft::$app->getRequest()->getIsConsoleRequest()'); |
||
| 196 | return false; |
||
| 197 | } |
||
| 198 | |||
| 199 | if ($request->getIsCpRequest()) { |
||
| 200 | $logExclusion('Craft::$app->getRequest()->getIsCpRequest()'); |
||
| 201 | return false; |
||
| 202 | } |
||
| 203 | |||
| 204 | if ($request->getIsLivePreview()) { |
||
| 205 | $logExclusion('Craft::$app->getRequest()->getIsLivePreview()'); |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | |||
| 209 | // Check the $_SERVER[] super-global exclusions |
||
| 210 | if (InstantAnalytics::$settings->serverExcludes !== null |
||
| 211 | && is_array(InstantAnalytics::$settings->serverExcludes)) { |
||
| 212 | foreach (InstantAnalytics::$settings->serverExcludes as $match => $matchArray) { |
||
| 213 | if (isset($_SERVER[$match])) { |
||
| 214 | foreach ($matchArray as $matchItem) { |
||
| 215 | if (preg_match($matchItem, $_SERVER[$match])) { |
||
| 216 | $logExclusion('serverExcludes'); |
||
| 217 | |||
| 218 | return false; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | // Filter out bot/spam requests via UserAgent |
||
| 226 | if (InstantAnalytics::$settings->filterBotUserAgents) { |
||
| 227 | $crawlerDetect = new CrawlerDetect; |
||
| 228 | // Check the user agent of the current 'visitor' |
||
| 229 | if ($crawlerDetect->isCrawler()) { |
||
| 230 | $logExclusion('filterBotUserAgents'); |
||
| 231 | |||
| 232 | return false; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | // Filter by user group |
||
| 237 | $userService = Craft::$app->getUser(); |
||
| 238 | /** @var UserElement $user */ |
||
| 239 | $user = $userService->getIdentity(); |
||
| 240 | if ($user) { |
||
| 241 | if (InstantAnalytics::$settings->adminExclude && $user->admin) { |
||
| 242 | $logExclusion('adminExclude'); |
||
| 243 | |||
| 244 | return false; |
||
| 245 | } |
||
| 246 | |||
| 247 | if (InstantAnalytics::$settings->groupExcludes !== null |
||
| 248 | && is_array(InstantAnalytics::$settings->groupExcludes)) { |
||
| 249 | foreach (InstantAnalytics::$settings->groupExcludes as $matchItem) { |
||
| 250 | if ($user->isInGroup($matchItem)) { |
||
| 251 | $logExclusion('groupExcludes'); |
||
| 252 | |||
| 253 | return false; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | return $result; |
||
| 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 |
||
| 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 |