diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-10-15 06:52:40 +0400 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-10-16 01:07:24 +0400 |
commit | d9562a6b654d0a7d615671be0e9450b10e0806b5 (patch) | |
tree | 1d5bf04aa935c4727ab78e8137a3127771b3e8de /core/Piwik.php | |
parent | 03bad4b2e7954d52d9f4376a965f212bd8774e66 (diff) |
Extracted `Piwik::getJavascriptCode()` into a separate, non static class: `Piwik\Tracker\TrackerCodeGenerator`
`Piwik::getJavascriptCode()` is now deprecated in favor of the new class.
Diffstat (limited to 'core/Piwik.php')
-rw-r--r-- | core/Piwik.php | 129 |
1 files changed, 5 insertions, 124 deletions
diff --git a/core/Piwik.php b/core/Piwik.php index 13124728f9..77cf4da82b 100644 --- a/core/Piwik.php +++ b/core/Piwik.php @@ -13,7 +13,6 @@ use Piwik\Db\Adapter; use Piwik\Db\Schema; use Piwik\Db; use Piwik\Plugin; -use Piwik\Plugins\SitesManager\API as APISitesManager; use Piwik\Plugins\UsersManager\API as APIUsersManager; use Piwik\Session; use Piwik\Tracker; @@ -125,112 +124,18 @@ class Piwik * @param int $idSite * @param string $piwikUrl http://path/to/piwik/directory/ * @return string + * + * @deprecated Use {@link TrackerCodeGenerator} instead + * @see \Piwik\Tracker\TrackerCodeGenerator */ public static function getJavascriptCode($idSite, $piwikUrl, $mergeSubdomains = false, $groupPageTitlesByDomain = false, $mergeAliasUrls = false, $visitorCustomVariables = false, $pageCustomVariables = false, $customCampaignNameQueryParam = false, $customCampaignKeywordParam = false, $doNotTrack = false, $disableCookies = false) { - // changes made to this code should be mirrored in plugins/CoreAdminHome/javascripts/jsTrackingGenerator.js var generateJsCode - $jsCode = file_get_contents(PIWIK_INCLUDE_PATH . "/plugins/Morpheus/templates/javascriptCode.tpl"); - $jsCode = htmlentities($jsCode); - if (substr($piwikUrl, 0, 4) !== 'http') { - $piwikUrl = 'http://' . $piwikUrl; - } - preg_match('~^(http|https)://(.*)$~D', $piwikUrl, $matches); - $piwikUrl = rtrim(@$matches[2], "/"); - - // Build optional parameters to be added to text - $options = ''; - $optionsBeforeTrackerUrl = ''; - if ($groupPageTitlesByDomain) { - $options .= ' _paq.push(["setDocumentTitle", document.domain + "/" + document.title]);' . PHP_EOL; - } - if ($mergeSubdomains || $mergeAliasUrls) { - $options .= self::getJavascriptTagOptions($idSite, $mergeSubdomains, $mergeAliasUrls); - } - $maxCustomVars = Plugins\CustomVariables\CustomVariables::getMaxCustomVariables(); - - if ($visitorCustomVariables) { - $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 .= ' _paq.push(["setCustomVariable", '.$index++.', "'.$visitorCustomVariable[0].'", "'.$visitorCustomVariable[1].'", "visit"]);' . PHP_EOL; - } - } - if ($pageCustomVariables) { - $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 .= ' _paq.push(["setCustomVariable", '.$index++.', "'.$pageCustomVariable[0].'", "'.$pageCustomVariable[1].'", "page"]);' . PHP_EOL; - } - } - if ($customCampaignNameQueryParam) { - $options .= ' _paq.push(["setCampaignNameKey", "'.$customCampaignNameQueryParam.'"]);' . PHP_EOL; - } - if ($customCampaignKeywordParam) { - $options .= ' _paq.push(["setCampaignKeywordKey", "'.$customCampaignKeywordParam.'"]);' . PHP_EOL; - } - if ($doNotTrack) { - $options .= ' _paq.push(["setDoNotTrack", true]);' . PHP_EOL; - } - if ($disableCookies) { - $options .= ' _paq.push(["disableCookies"]);' . PHP_EOL; - } + $generator = new Tracker\TrackerCodeGenerator(); - $codeImpl = array( - 'idSite' => $idSite, - '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 the `Piwik::getJavascriptCode()`. - */ - self::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; + return $generator->generate($idSite, $piwikUrl, $mergeSubdomains, $groupPageTitlesByDomain, $mergeAliasUrls, $visitorCustomVariables, $pageCustomVariables, $customCampaignNameQueryParam, $customCampaignKeywordParam, $doNotTrack, $disableCookies); } /** @@ -847,30 +752,6 @@ class Piwik return vsprintf($translationId, $args); } - protected static 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; - } - /** * Executes a callback with superuser privileges, making sure those privileges are rescinded * before this method exits. Privileges will be rescinded even if an exception is thrown. |