| Total Complexity | 49 |
| Total Lines | 304 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 |
||
| 28 | class Analytics |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * If SEOmatic is installed, set the page title from it |
||
| 32 | */ |
||
| 33 | public static function getTitleFromSeomatic(): ?string |
||
| 34 | { |
||
| 35 | if (!InstantAnalytics::$seomaticPlugin) { |
||
| 36 | return null; |
||
| 37 | } |
||
| 38 | if (!Seomatic::$settings->renderEnabled) { |
||
| 39 | return null; |
||
| 40 | } |
||
| 41 | $titleTag = Seomatic::$plugin->title->get('title'); |
||
| 42 | |||
| 43 | if ($titleTag === null) { |
||
| 44 | return null; |
||
| 45 | } |
||
| 46 | |||
| 47 | $titleArray = $titleTag->renderAttributes(); |
||
| 48 | |||
| 49 | if (empty($titleArray['title'])) { |
||
| 50 | return null; |
||
| 51 | } |
||
| 52 | |||
| 53 | return $titleArray['title']; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Return a sanitized documentPath from a URL |
||
| 58 | * |
||
| 59 | * @param string $url |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public static function getDocumentPathFromUrl(string $url = ''): string |
||
| 64 | { |
||
| 65 | if ($url === '') { |
||
| 66 | $url = Craft::$app->getRequest()->getFullPath(); |
||
| 67 | } |
||
| 68 | |||
| 69 | // We want to send just a path to GA for page views |
||
| 70 | if (UrlHelper::isAbsoluteUrl($url)) { |
||
| 71 | $urlParts = parse_url($url); |
||
| 72 | $url = $urlParts['path'] ?? '/'; |
||
| 73 | if (isset($urlParts['query'])) { |
||
| 74 | $url .= '?' . $urlParts['query']; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | // We don't want to send protocol-relative URLs either |
||
| 79 | if (UrlHelper::isProtocolRelativeUrl($url)) { |
||
| 80 | $url = substr($url, 1); |
||
| 81 | } |
||
| 82 | |||
| 83 | // Strip the query string if that's the global config setting |
||
| 84 | if (InstantAnalytics::$settings) { |
||
| 85 | if (InstantAnalytics::$settings->stripQueryString !== null |
||
| 86 | && InstantAnalytics::$settings->stripQueryString) { |
||
| 87 | $url = UrlHelper::stripQueryString($url); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | // We always want the path to be / rather than empty |
||
| 92 | if ($url === '') { |
||
| 93 | $url = '/'; |
||
| 94 | } |
||
| 95 | |||
| 96 | return $url; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get a PageView tracking URL |
||
| 101 | * |
||
| 102 | * @param $url |
||
| 103 | * @param $title |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | * @throws Exception |
||
| 107 | */ |
||
| 108 | public static function getPageViewTrackingUrl($url, $title): string |
||
| 109 | { |
||
| 110 | $urlParams = compact('url', 'title'); |
||
| 111 | |||
| 112 | $path = parse_url($url, PHP_URL_PATH); |
||
| 113 | $pathFragments = explode('/', rtrim($path, '/')); |
||
| 114 | $fileName = end($pathFragments); |
||
| 115 | $trackingUrl = UrlHelper::siteUrl('instantanalytics/pageViewTrack/' . $fileName, $urlParams); |
||
| 116 | |||
| 117 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 118 | 'Created pageViewTrackingUrl for: {trackingUrl}', |
||
| 119 | [ |
||
| 120 | 'trackingUrl' => $trackingUrl |
||
| 121 | ], |
||
| 122 | __METHOD__ |
||
| 123 | ); |
||
| 124 | |||
| 125 | return $trackingUrl; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get an Event tracking URL |
||
| 130 | * |
||
| 131 | * @param string $url |
||
| 132 | * @param string $eventName |
||
| 133 | * @param array $params |
||
| 134 | * @return string |
||
| 135 | * @throws Exception |
||
| 136 | */ |
||
| 137 | public static function getEventTrackingUrl( |
||
| 138 | string $url, |
||
| 139 | string $eventName, |
||
| 140 | array $params = [] |
||
| 141 | ): string |
||
| 142 | { |
||
| 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 | { |
||
| 172 | if (InstantAnalytics::$settings->logExcludedAnalytics) { |
||
| 173 | $request = Craft::$app->getRequest(); |
||
| 174 | $requestIp = $request->getUserIP(); |
||
| 175 | InstantAnalytics::$plugin->logAnalyticsEvent( |
||
| 176 | 'Analytics excluded for:: {requestIp} due to: `{setting}`', |
||
| 177 | compact('requestIp', 'setting'), |
||
| 178 | __METHOD__ |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | }; |
||
| 182 | |||
| 183 | if (!InstantAnalytics::$settings->sendAnalyticsData) { |
||
| 184 | $logExclusion('sendAnalyticsData'); |
||
| 185 | return false; |
||
| 186 | } |
||
| 187 | |||
| 188 | if (!InstantAnalytics::$settings->sendAnalyticsInDevMode && Craft::$app->getConfig()->getGeneral()->devMode) { |
||
| 189 | $logExclusion('sendAnalyticsInDevMode'); |
||
| 190 | return false; |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($request->getIsConsoleRequest()) { |
||
| 194 | $logExclusion('Craft::$app->getRequest()->getIsConsoleRequest()'); |
||
| 195 | return false; |
||
| 196 | } |
||
| 197 | |||
| 198 | if ($request->getIsCpRequest()) { |
||
| 199 | $logExclusion('Craft::$app->getRequest()->getIsCpRequest()'); |
||
| 200 | return false; |
||
| 201 | } |
||
| 202 | |||
| 203 | if ($request->getIsLivePreview()) { |
||
| 204 | $logExclusion('Craft::$app->getRequest()->getIsLivePreview()'); |
||
| 205 | return false; |
||
| 206 | } |
||
| 207 | |||
| 208 | // Check the $_SERVER[] super-global exclusions |
||
| 209 | if (InstantAnalytics::$settings->serverExcludes !== null |
||
| 210 | && is_array(InstantAnalytics::$settings->serverExcludes)) { |
||
| 211 | foreach (InstantAnalytics::$settings->serverExcludes as $match => $matchArray) { |
||
| 212 | if (isset($_SERVER[$match])) { |
||
| 213 | foreach ($matchArray as $matchItem) { |
||
| 214 | if (preg_match($matchItem, $_SERVER[$match])) { |
||
| 215 | $logExclusion('serverExcludes'); |
||
| 216 | |||
| 217 | return false; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | // Filter out bot/spam requests via UserAgent |
||
| 225 | if (InstantAnalytics::$settings->filterBotUserAgents) { |
||
| 226 | $crawlerDetect = new CrawlerDetect; |
||
| 227 | // Check the user agent of the current 'visitor' |
||
| 228 | if ($crawlerDetect->isCrawler()) { |
||
| 229 | $logExclusion('filterBotUserAgents'); |
||
| 230 | |||
| 231 | return false; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | // Filter by user group |
||
| 236 | $userService = Craft::$app->getUser(); |
||
| 237 | /** @var UserElement $user */ |
||
| 238 | $user = $userService->getIdentity(); |
||
| 239 | if ($user) { |
||
| 240 | if (InstantAnalytics::$settings->adminExclude && $user->admin) { |
||
| 241 | $logExclusion('adminExclude'); |
||
| 242 | |||
| 243 | return false; |
||
| 244 | } |
||
| 245 | |||
| 246 | if (InstantAnalytics::$settings->groupExcludes !== null |
||
| 247 | && is_array(InstantAnalytics::$settings->groupExcludes)) { |
||
| 248 | foreach (InstantAnalytics::$settings->groupExcludes as $matchItem) { |
||
| 249 | if ($user->isInGroup($matchItem)) { |
||
| 250 | $logExclusion('groupExcludes'); |
||
| 251 | |||
| 252 | return false; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | return $result; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * getClientId handles the parsing of the _ga cookie or setting it to a |
||
| 263 | * unique identifier |
||
| 264 | * |
||
| 265 | * @return string the cid |
||
| 266 | */ |
||
| 267 | public static function getClientId(): string |
||
| 268 | { |
||
| 269 | $cid = ''; |
||
| 270 | $cookieName = '_ga_' . StringHelper::removeLeft(InstantAnalytics::$settings->googleAnalyticsMeasurementId, 'G-'); |
||
| 271 | if (isset($_COOKIE[$cookieName])) { |
||
| 272 | $parts = explode(".", $_COOKIE[$cookieName], 5); |
||
| 273 | if ($parts !== false) { |
||
| 274 | $cid = implode('.', array_slice($parts, 2, 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('+' . InstantAnalytics::$settings->sessionDuration . ' minutes'), '/'); // Two years |
||
| 285 | } |
||
| 286 | |||
| 287 | return $cid; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get the user id. |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public static function getUserId(): string |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * gaGenUUID Generate UUID v4 function - needed to generate a CID when one |
||
| 308 | * isn't available |
||
| 309 | * |
||
| 310 | * @return string The generated UUID |
||
| 311 | */ |
||
| 312 | protected static function gaGenUUID() |
||
| 332 | ); |
||
| 333 | } |
||
| 334 | } |
||
| 335 |