getJavascriptTagOptions($idSite, $mergeSubdomains, $mergeAliasUrls); } $maxCustomVars = CustomVariables::getMaxCustomVariables(); if ($visitorCustomVariables && count($visitorCustomVariables) > 0) { $options .= ' // you can set up to ' . $maxCustomVars . ' custom variables for each visitor' . PHP_EOL; $index = 1; foreach ($visitorCustomVariables as $visitorCustomVariable) { if (empty($visitorCustomVariable)) { continue; } $options .= sprintf( ' _paq.push(["setCustomVariable", %d, %s, %s, "visit"]);%s', $index++, json_encode($visitorCustomVariable[0]), json_encode($visitorCustomVariable[1]), PHP_EOL ); } } if ($pageCustomVariables && count($pageCustomVariables) > 0) { $options .= ' // you can set up to ' . $maxCustomVars . ' custom variables for each action (page view, download, click, site search)' . PHP_EOL; $index = 1; foreach ($pageCustomVariables as $pageCustomVariable) { if (empty($pageCustomVariable)) { continue; } $options .= sprintf( ' _paq.push(["setCustomVariable", %d, %s, %s, "page"]);%s', $index++, json_encode($pageCustomVariable[0]), json_encode($pageCustomVariable[1]), PHP_EOL ); } } if ($customCampaignNameQueryParam) { $options .= ' _paq.push(["setCampaignNameKey", ' . json_encode($customCampaignNameQueryParam) . ']);' . PHP_EOL; } if ($customCampaignKeywordParam) { $options .= ' _paq.push(["setCampaignKeywordKey", ' . json_encode($customCampaignKeywordParam) . ']);' . PHP_EOL; } if ($doNotTrack) { $options .= ' _paq.push(["setDoNotTrack", true]);' . PHP_EOL; } if ($disableCookies) { $options .= ' _paq.push(["disableCookies"]);' . PHP_EOL; } $codeImpl = array( 'idSite' => $idSite, // TODO why sanitizeInputValue() and not json_encode? 'piwikUrl' => Common::sanitizeInputValue($piwikUrl), 'options' => $options, 'optionsBeforeTrackerUrl' => $optionsBeforeTrackerUrl, 'protocol' => '//' ); $parameters = compact('mergeSubdomains', 'groupPageTitlesByDomain', 'mergeAliasUrls', 'visitorCustomVariables', 'pageCustomVariables', 'customCampaignNameQueryParam', 'customCampaignKeywordParam', 'doNotTrack'); /** * Triggered when generating JavaScript tracking code server side. Plugins can use * this event to customise the JavaScript tracking code that is displayed to the * user. * * @param array &$codeImpl An array containing snippets of code that the event handler * can modify. Will contain the following elements: * * - **idSite**: The ID of the site being tracked. * - **piwikUrl**: The tracker URL to use. * - **options**: A string of JavaScript code that customises * the JavaScript tracker. * - **optionsBeforeTrackerUrl**: A string of Javascript code that customises * the JavaScript tracker inside of anonymous function before * adding setTrackerUrl into paq. * - **protocol**: Piwik url protocol. * * The **httpsPiwikUrl** element can be set if the HTTPS * domain is different from the normal domain. * @param array $parameters The parameters supplied to `TrackerCodeGenerator::generate()`. */ Piwik::postEvent('Piwik.getJavascriptCode', array(&$codeImpl, $parameters)); $setTrackerUrl = 'var u="' . $codeImpl['protocol'] . '{$piwikUrl}/";'; if (!empty($codeImpl['httpsPiwikUrl'])) { $setTrackerUrl = 'var u=((document.location.protocol === "https:") ? "https://{$httpsPiwikUrl}/" : "http://{$piwikUrl}/");'; $codeImpl['httpsPiwikUrl'] = rtrim($codeImpl['httpsPiwikUrl'], "/"); } $codeImpl = array('setTrackerUrl' => htmlentities($setTrackerUrl)) + $codeImpl; foreach ($codeImpl as $keyToReplace => $replaceWith) { $jsCode = str_replace('{$' . $keyToReplace . '}', $replaceWith, $jsCode); } return $jsCode; } private function getJavascriptTagOptions($idSite, $mergeSubdomains, $mergeAliasUrls) { try { $websiteUrls = APISitesManager::getInstance()->getSiteUrlsFromId($idSite); } catch (\Exception $e) { return ''; } // We need to parse_url to isolate hosts $websiteHosts = array(); foreach ($websiteUrls as $site_url) { $referrerParsed = parse_url($site_url); $websiteHosts[] = $referrerParsed['host']; } $options = ''; if ($mergeSubdomains && !empty($websiteHosts)) { $options .= ' _paq.push(["setCookieDomain", "*.' . $websiteHosts[0] . '"]);' . PHP_EOL; } if ($mergeAliasUrls && !empty($websiteHosts)) { $urls = '["*.' . implode('","*.', $websiteHosts) . '"]'; $options .= ' _paq.push(["setDomains", ' . $urls . ']);' . PHP_EOL; } return $options; } }