Db
6 years ago
Handler
6 years ago
TableLogAction
6 years ago
Visit
6 years ago
Action.php
6 years ago
ActionPageview.php
6 years ago
Cache.php
6 years ago
Db.php
6 years ago
Failures.php
6 years ago
FingerprintSalt.php
6 years ago
GoalManager.php
6 years ago
Handler.php
6 years ago
IgnoreCookie.php
6 years ago
LogTable.php
6 years ago
Model.php
6 years ago
PageUrl.php
6 years ago
Request.php
5 years ago
RequestProcessor.php
6 years ago
RequestSet.php
6 years ago
Response.php
6 years ago
ScheduledTasksRunner.php
6 years ago
Settings.php
5 years ago
TableLogAction.php
6 years ago
TrackerCodeGenerator.php
6 years ago
TrackerConfig.php
6 years ago
Visit.php
5 years ago
VisitExcluded.php
6 years ago
VisitInterface.php
6 years ago
Visitor.php
6 years ago
VisitorNotFoundInDb.php
6 years ago
VisitorRecognizer.php
6 years ago
TrackerCodeGenerator.php
295 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | |
| 9 | namespace Piwik\Tracker; |
| 10 | |
| 11 | use Piwik\Common; |
| 12 | use Piwik\DbHelper; |
| 13 | use Piwik\Option; |
| 14 | use Piwik\Piwik; |
| 15 | use Piwik\Plugins\CustomVariables\CustomVariables; |
| 16 | use Piwik\Plugins\SitesManager\API as APISitesManager; |
| 17 | use Piwik\SettingsPiwik; |
| 18 | use Piwik\View; |
| 19 | |
| 20 | /** |
| 21 | * Generates the Javascript code to be inserted on every page of the website to track. |
| 22 | */ |
| 23 | class TrackerCodeGenerator |
| 24 | { |
| 25 | /** |
| 26 | * whether matomo.js|php should be forced over piwik.js|php |
| 27 | * @var bool |
| 28 | */ |
| 29 | private $shouldForceMatomoEndpoint = false; |
| 30 | |
| 31 | public function forceMatomoEndpoint() |
| 32 | { |
| 33 | $this->shouldForceMatomoEndpoint = true; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param int $idSite |
| 38 | * @param string $piwikUrl http://path/to/piwik/site/ |
| 39 | * @param bool $mergeSubdomains |
| 40 | * @param bool $groupPageTitlesByDomain |
| 41 | * @param bool $mergeAliasUrls |
| 42 | * @param array $visitorCustomVariables |
| 43 | * @param array $pageCustomVariables |
| 44 | * @param string $customCampaignNameQueryParam |
| 45 | * @param string $customCampaignKeywordParam |
| 46 | * @param bool $doNotTrack |
| 47 | * @param bool $disableCookies |
| 48 | * @param bool $trackNoScript |
| 49 | * @return string Javascript code. |
| 50 | */ |
| 51 | public function generate( |
| 52 | $idSite, |
| 53 | $piwikUrl, |
| 54 | $mergeSubdomains = false, |
| 55 | $groupPageTitlesByDomain = false, |
| 56 | $mergeAliasUrls = false, |
| 57 | $visitorCustomVariables = null, |
| 58 | $pageCustomVariables = null, |
| 59 | $customCampaignNameQueryParam = null, |
| 60 | $customCampaignKeywordParam = null, |
| 61 | $doNotTrack = false, |
| 62 | $disableCookies = false, |
| 63 | $trackNoScript = false, |
| 64 | $crossDomain = false |
| 65 | ) { |
| 66 | // changes made to this code should be mirrored in plugins/CoreAdminHome/javascripts/jsTrackingGenerator.js var generateJsCode |
| 67 | |
| 68 | if (substr($piwikUrl, 0, 4) !== 'http') { |
| 69 | $piwikUrl = 'http://' . $piwikUrl; |
| 70 | } |
| 71 | preg_match('~^(http|https)://(.*)$~D', $piwikUrl, $matches); |
| 72 | $piwikUrl = rtrim(@$matches[2], "/"); |
| 73 | |
| 74 | // Build optional parameters to be added to text |
| 75 | $options = ''; |
| 76 | $optionsBeforeTrackerUrl = ''; |
| 77 | if ($groupPageTitlesByDomain) { |
| 78 | $options .= ' _paq.push(["setDocumentTitle", document.domain + "/" + document.title]);' . "\n"; |
| 79 | } |
| 80 | if ($crossDomain) { |
| 81 | // When enabling cross domain, we also need to call `setDomains` |
| 82 | $mergeAliasUrls = true; |
| 83 | } |
| 84 | if ($mergeSubdomains || $mergeAliasUrls) { |
| 85 | $options .= $this->getJavascriptTagOptions($idSite, $mergeSubdomains, $mergeAliasUrls); |
| 86 | } |
| 87 | |
| 88 | if ($crossDomain) { |
| 89 | $options .= ' _paq.push(["enableCrossDomainLinking"]);' . "\n"; |
| 90 | } |
| 91 | |
| 92 | $maxCustomVars = CustomVariables::getNumUsableCustomVariables(); |
| 93 | |
| 94 | if ($visitorCustomVariables && count($visitorCustomVariables) > 0) { |
| 95 | $options .= ' // you can set up to ' . $maxCustomVars . ' custom variables for each visitor' . "\n"; |
| 96 | $index = 1; |
| 97 | foreach ($visitorCustomVariables as $visitorCustomVariable) { |
| 98 | if (empty($visitorCustomVariable)) { |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | $options .= sprintf( |
| 103 | ' _paq.push(["setCustomVariable", %d, %s, %s, "visit"]);%s', |
| 104 | $index++, |
| 105 | json_encode($visitorCustomVariable[0]), |
| 106 | json_encode($visitorCustomVariable[1]), |
| 107 | "\n" |
| 108 | ); |
| 109 | } |
| 110 | } |
| 111 | if ($pageCustomVariables && count($pageCustomVariables) > 0) { |
| 112 | $options .= ' // you can set up to ' . $maxCustomVars . ' custom variables for each action (page view, download, click, site search)' . "\n"; |
| 113 | $index = 1; |
| 114 | foreach ($pageCustomVariables as $pageCustomVariable) { |
| 115 | if (empty($pageCustomVariable)) { |
| 116 | continue; |
| 117 | } |
| 118 | $options .= sprintf( |
| 119 | ' _paq.push(["setCustomVariable", %d, %s, %s, "page"]);%s', |
| 120 | $index++, |
| 121 | json_encode($pageCustomVariable[0]), |
| 122 | json_encode($pageCustomVariable[1]), |
| 123 | "\n" |
| 124 | ); |
| 125 | } |
| 126 | } |
| 127 | if ($customCampaignNameQueryParam) { |
| 128 | $options .= ' _paq.push(["setCampaignNameKey", ' |
| 129 | . json_encode($customCampaignNameQueryParam) . ']);' . "\n"; |
| 130 | } |
| 131 | if ($customCampaignKeywordParam) { |
| 132 | $options .= ' _paq.push(["setCampaignKeywordKey", ' |
| 133 | . json_encode($customCampaignKeywordParam) . ']);' . "\n"; |
| 134 | } |
| 135 | if ($doNotTrack) { |
| 136 | $options .= ' _paq.push(["setDoNotTrack", true]);' . "\n"; |
| 137 | } |
| 138 | if ($disableCookies) { |
| 139 | $options .= ' _paq.push(["disableCookies"]);' . "\n"; |
| 140 | } |
| 141 | |
| 142 | $codeImpl = array( |
| 143 | 'idSite' => $idSite, |
| 144 | // TODO why sanitizeInputValue() and not json_encode? |
| 145 | 'piwikUrl' => Common::sanitizeInputValue($piwikUrl), |
| 146 | 'options' => $options, |
| 147 | 'optionsBeforeTrackerUrl' => $optionsBeforeTrackerUrl, |
| 148 | 'protocol' => '//', |
| 149 | 'loadAsync' => true, |
| 150 | 'trackNoScript' => $trackNoScript, |
| 151 | 'matomoJsFilename' => $this->getJsTrackerEndpoint(), |
| 152 | 'matomoPhpFilename' => $this->getPhpTrackerEndpoint(), |
| 153 | ); |
| 154 | |
| 155 | if (SettingsPiwik::isHttpsForced()) { |
| 156 | $codeImpl['protocol'] = 'https://'; |
| 157 | } |
| 158 | |
| 159 | $parameters = compact('mergeSubdomains', 'groupPageTitlesByDomain', 'mergeAliasUrls', 'visitorCustomVariables', |
| 160 | 'pageCustomVariables', 'customCampaignNameQueryParam', 'customCampaignKeywordParam', |
| 161 | 'doNotTrack'); |
| 162 | |
| 163 | /** |
| 164 | * Triggered when generating JavaScript tracking code server side. Plugins can use |
| 165 | * this event to customise the JavaScript tracking code that is displayed to the |
| 166 | * user. |
| 167 | * |
| 168 | * @param array &$codeImpl An array containing snippets of code that the event handler |
| 169 | * can modify. Will contain the following elements: |
| 170 | * |
| 171 | * - **idSite**: The ID of the site being tracked. |
| 172 | * - **piwikUrl**: The tracker URL to use. |
| 173 | * - **options**: A string of JavaScript code that customises |
| 174 | * the JavaScript tracker. |
| 175 | * - **optionsBeforeTrackerUrl**: A string of Javascript code that customises |
| 176 | * the JavaScript tracker inside of anonymous function before |
| 177 | * adding setTrackerUrl into paq. |
| 178 | * - **protocol**: Piwik url protocol. |
| 179 | * - **loadAsync**: boolean whether piwik.js should be loaded syncronous or asynchronous |
| 180 | * |
| 181 | * The **httpsPiwikUrl** element can be set if the HTTPS |
| 182 | * domain is different from the normal domain. |
| 183 | * @param array $parameters The parameters supplied to `TrackerCodeGenerator::generate()`. |
| 184 | */ |
| 185 | Piwik::postEvent('Piwik.getJavascriptCode', array(&$codeImpl, $parameters)); |
| 186 | |
| 187 | $setTrackerUrl = 'var u="' . $codeImpl['protocol'] . '{$piwikUrl}/";'; |
| 188 | |
| 189 | if (!empty($codeImpl['httpsPiwikUrl'])) { |
| 190 | $setTrackerUrl = 'var u=((document.location.protocol === "https:") ? "https://{$httpsPiwikUrl}/" : "http://{$piwikUrl}/");'; |
| 191 | $codeImpl['httpsPiwikUrl'] = rtrim($codeImpl['httpsPiwikUrl'], "/"); |
| 192 | } |
| 193 | $codeImpl = array('setTrackerUrl' => htmlentities($setTrackerUrl, ENT_COMPAT | ENT_HTML401, 'UTF-8')) + $codeImpl; |
| 194 | |
| 195 | $view = new View('@Morpheus/javascriptCode'); |
| 196 | $view->disableCacheBuster(); |
| 197 | $view->loadAsync = $codeImpl['loadAsync']; |
| 198 | $view->trackNoScript = $codeImpl['trackNoScript']; |
| 199 | $jsCode = $view->render(); |
| 200 | $jsCode = htmlentities($jsCode, ENT_COMPAT | ENT_HTML401, 'UTF-8'); |
| 201 | |
| 202 | foreach ($codeImpl as $keyToReplace => $replaceWith) { |
| 203 | $jsCode = str_replace('{$' . $keyToReplace . '}', $replaceWith, $jsCode); |
| 204 | } |
| 205 | |
| 206 | return $jsCode; |
| 207 | } |
| 208 | |
| 209 | public function getJsTrackerEndpoint() |
| 210 | { |
| 211 | $name = 'matomo.js'; |
| 212 | if ($this->shouldPreferPiwikEndpoint()) { |
| 213 | $name = 'piwik.js'; |
| 214 | } |
| 215 | return $name; |
| 216 | } |
| 217 | |
| 218 | public function getPhpTrackerEndpoint() |
| 219 | { |
| 220 | $name = 'matomo.php'; |
| 221 | if ($this->shouldPreferPiwikEndpoint()) { |
| 222 | $name = 'piwik.php'; |
| 223 | } |
| 224 | return $name; |
| 225 | } |
| 226 | |
| 227 | public function shouldPreferPiwikEndpoint() |
| 228 | { |
| 229 | if ($this->shouldForceMatomoEndpoint) { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | // only since 3.7.0 we use the default matomo.js|php... for all other installs we need to keep BC |
| 234 | return DbHelper::wasMatomoInstalledBeforeVersion('3.7.0-b1'); |
| 235 | } |
| 236 | |
| 237 | private function getJavascriptTagOptions($idSite, $mergeSubdomains, $mergeAliasUrls) |
| 238 | { |
| 239 | try { |
| 240 | $websiteUrls = APISitesManager::getInstance()->getSiteUrlsFromId($idSite); |
| 241 | } catch (\Exception $e) { |
| 242 | return ''; |
| 243 | } |
| 244 | // We need to parse_url to isolate hosts |
| 245 | $websiteHosts = array(); |
| 246 | $firstHost = null; |
| 247 | foreach ($websiteUrls as $site_url) { |
| 248 | if (empty($site_url)) { |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | $referrerParsed = parse_url($site_url); |
| 253 | |
| 254 | if (!isset($firstHost) && isset($referrerParsed['host'])) { |
| 255 | $firstHost = $referrerParsed['host']; |
| 256 | } |
| 257 | |
| 258 | if (isset($referrerParsed['host'])) { |
| 259 | $url = $referrerParsed['host']; |
| 260 | } else { |
| 261 | $url = ''; |
| 262 | } |
| 263 | if (!empty($referrerParsed['path'])) { |
| 264 | $url .= $referrerParsed['path']; |
| 265 | } |
| 266 | |
| 267 | if (!empty($url)) { |
| 268 | $websiteHosts[] = $url; |
| 269 | } |
| 270 | } |
| 271 | $options = ''; |
| 272 | if ($mergeSubdomains && !empty($firstHost)) { |
| 273 | $options .= ' _paq.push(["setCookieDomain", "*.' . $firstHost . '"]);' . "\n"; |
| 274 | } |
| 275 | if ($mergeAliasUrls && !empty($websiteHosts)) { |
| 276 | $urls = '["*.' . implode('","*.', $websiteHosts) . '"]'; |
| 277 | $options .= ' _paq.push(["setDomains", ' . $urls . ']);' . "\n"; |
| 278 | } |
| 279 | return $options; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * When including the JS tracking code in a mailto link, we need to strip the surrounding HTML tags off. This |
| 284 | * ensures consistent behaviour between mail clients that render the mailto body as plain text (as in the |
| 285 | * spec), and those which try to render it as HTML and therefore hide the tags. |
| 286 | * @param string $jsTrackingCode JS tracking code as returned from the generate() function. |
| 287 | * @return string |
| 288 | */ |
| 289 | public static function stripTags($jsTrackingCode) |
| 290 | { |
| 291 | // Strip off open and close <script> tag and comments so that JS will be displayed in ALL mail clients |
| 292 | return trim(strip_tags(html_entity_decode($jsTrackingCode))); |
| 293 | } |
| 294 | } |
| 295 |