diff --git a/API.php b/API.php index 057f6cf..3206171 100644 --- a/API.php +++ b/API.php @@ -11,7 +11,7 @@ public function getSettings() { $outParams = array(); - $params = array("idSite","piwikUrl","options","optionsBeforeTrackerUrl","httpsPiwikUrl","protocol"); + $params = array("idSite","piwikUrl","options","optionsBeforeTrackerUrl","httpsPiwikUrl","protocol","piwikJs", "piwikPhp","paqVariable","removePiwikBranding"); $settings = new Settings(self::$plugin_name); diff --git a/Settings.php b/Settings.php index fece049..c4e6419 100644 --- a/Settings.php +++ b/Settings.php @@ -14,7 +14,12 @@ class Settings extends \Piwik\Plugin\Settings public $optionsBeforeTrackerUrl; public $httpsPiwikUrl; public $protocol; - + public $piwikJs; + public $piwikPhp; + public $paqVariable; + public $removePiwikBranding; + public $infoTextWritePermissions; + protected function init() { $this->setIntroduction($this->t('PluginDescription')); @@ -23,13 +28,12 @@ protected function init() $this->idSite = new SystemSetting('idSite', $this->t('idSiteSettingTitle')); + $this->idSite->inlineHelp = $this->t('idSiteSettingHelp'); $this->idSite->type = static::TYPE_STRING; $this->idSite->uiControlType = static::CONTROL_TEXT; $this->idSite->uiControlAttributes = array("size" => "6", "maxlenth" => "8"); - $this->idSite->description = $this->t('idSiteSettingDescription'); $this->idSite->readableByCurrentUser = true; $this->idSite->defaultValue = ""; - $this->idSite->inlineHelp = 'Probably not useful in most scenarios. The idSite option is included for completeness.'; $this->idSite->validate = function ($value, $setting) { if ($value != "" && preg_match("/^[0-9]+$/",$value) !== 1) { @@ -40,13 +44,12 @@ protected function init() $this->addSetting($this->idSite); $this->protocol = new SystemSetting('protocol', $this->t('protocolSettingTitle')); + $this->protocol->inlineHelp = $this->t('protocolSettingHelp'); $this->protocol->type = static::TYPE_STRING; $this->protocol->uiControlType = static::CONTROL_TEXT; $this->protocol->uiControlAttributes = array("size" => "10", "maxlenth" => "8"); - $this->protocol->description = $this->t('protocolSettingDescription'); $this->protocol->readableByCurrentUser = true; $this->protocol->defaultValue = ""; - $this->protocol->inlineHelp = 'http(s)://'; $this->protocol->validate = function ($value, $setting) { if ($value != "" && !($value == "http://" || $value == "https://")) { @@ -57,26 +60,25 @@ protected function init() $this->addSetting($this->protocol); $this->piwikUrl = new SystemSetting('piwikUrl', $this->t('piwikUrlSettingTitle')); + $this->piwikUrl->inlineHelp = $this->t('piwikUrlSettingHelp'); $this->piwikUrl->type = static::TYPE_STRING; $this->piwikUrl->uiControlType = static::CONTROL_TEXT; $this->piwikUrl->uiControlAttributes = $default_textbox_size; - $this->piwikUrl->description = $this->t('piwikUrlSettingDescription'); $this->piwikUrl->readableByCurrentUser = true; $this->piwikUrl->defaultValue = ""; - $this->piwikUrl->inlineHelp = 'tracker.example.com/piwik use hostname+basepath only (omit protocol and trailing slash)'; $this->addSetting($this->piwikUrl); $this->httpsPiwikUrl = new SystemSetting('httpsPiwikUrl', $this->t('httpsPiwikUrlSettingTitle')); + $this->httpsPiwikUrl->inlineHelp = $this->t('httpsPiwikUrlSettingHelp'); $this->httpsPiwikUrl->type = static::TYPE_STRING; $this->httpsPiwikUrl->uiControlType = static::CONTROL_TEXT; $this->httpsPiwikUrl->uiControlAttributes = $default_textbox_size; - $this->httpsPiwikUrl->description = $this->t('httpsPiwikUrlSettingDescription'); $this->httpsPiwikUrl->readableByCurrentUser = true; $this->httpsPiwikUrl->defaultValue = ""; - $this->httpsPiwikUrl->inlineHelp = 'secure-tracker.example.com/piwik use hostname+basepath only (omit protocol and trailing slash)'; - + $this->addSetting($this->httpsPiwikUrl); + $this->options = new SystemSetting('options', $this->t('optionsSettingTitle')); $this->options->type = static::TYPE_STRING; @@ -97,6 +99,53 @@ protected function init() $this->optionsBeforeTrackerUrl->inlineHelp = '{$original_paramname} and {$paramname} tokens are available for referencing values.'; $this->addSetting($this->optionsBeforeTrackerUrl); + + + + $this->piwikJs = new SystemSetting('piwikJs', $this->t('piwikJsSettingTitle')); + if(!is_writeable(PIWIK_INCLUDE_PATH . "/piwik.js")) { + $this->piwikJs->introduction = $this->t("writePermissionInfoText"); + } + $this->piwikJs->inlineHelp = $this->t("piwikJsSettingHelp"); + $this->piwikJs->type = static::TYPE_STRING; + $this->piwikJs->uiControlType = static::CONTROL_TEXT; + $this->piwikJs->uiControlAttributes = $default_textbox_size; + $this->piwikJs->readableByCurrentUser = true; + $this->piwikJs->defaultValue = ""; + + $this->addSetting($this->piwikJs); + + + $this->piwikPhp = new SystemSetting('piwikPhp', $this->t('piwikPhpSettingTitle')); + $this->piwikPhp->inlineHelp = $this->t('piwikPhpSettingHelp'); + $this->piwikPhp->type = static::TYPE_STRING; + $this->piwikPhp->uiControlType = static::CONTROL_TEXT; + $this->piwikPhp->uiControlAttributes = $default_textbox_size; + $this->piwikPhp->readableByCurrentUser = true; + $this->piwikPhp->defaultValue = ""; + + $this->addSetting($this->piwikPhp); + + + $this->paqVariable = new SystemSetting('paqVariable', $this->t('paqVariableSettingTitle')); + $this->paqVariable->inlineHelp = $this->t('paqVariableSettingHelp'); + $this->paqVariable->type = static::TYPE_STRING; + $this->paqVariable->uiControlType = static::CONTROL_TEXT; + $this->paqVariable->uiControlAttributes = $default_textbox_size; + $this->paqVariable->readableByCurrentUser = true; + $this->paqVariable->defaultValue = ""; + + $this->addSetting($this->paqVariable); + + + $this->removePiwikBranding = new SystemSetting('removePiwikBranding', $this->t('removePiwikBrandingSettingTitle')); + $this->removePiwikBranding->inlineHelp = $this->t('removePiwikBrandingSettingHelp'); + $this->removePiwikBranding->type = static::TYPE_BOOL; + $this->removePiwikBranding->uiControlType = static::CONTROL_CHECKBOX; + $this->removePiwikBranding->readableByCurrentUser = true; + $this->removePiwikBranding->defaultValue = ""; + + $this->addSetting($this->removePiwikBranding); } private function t($key) diff --git a/TrackingCodeCustomizer.php b/TrackingCodeCustomizer.php index bb859f5..0ce510c 100644 --- a/TrackingCodeCustomizer.php +++ b/TrackingCodeCustomizer.php @@ -37,7 +37,11 @@ class TrackingCodeCustomizer extends \Piwik\Plugin { private static $hooks = array( - 'Piwik.getJavascriptCode' => 'applyTrackingCodeCustomizations' + 'Piwik.getJavascriptCode' => 'applyTrackingCodeCustomizations', + 'Controller.CoreAdminHome.setPluginSettings.end' => 'rewriteJavascriptFiles', + 'API.SitesManager.getJavascriptTag.end' => 'rewriteJavascriptTag', + 'Controller.SitesManager.siteWithoutData.end' => 'rewriteJavascriptTag', + 'CoreUpdater.update.end' => 'afterUpdate' ); //2.15 includes a new function for registering hooks. 2.15 wi @@ -74,29 +78,121 @@ public function getListHooksRegistered() public function applyTrackingCodeCustomizations(&$sysparams,$parameters){ $originalSysparams = $sysparams; + $storedSettings = $this->getSettings(); - $settings = API::getInstance(); - - $storedSettings = $settings->getSettings(); - - if(array_key_exists("options", $storedSettings)) - $storedSettings["options"] .= $sysparams["options"]; + if(array_key_exists("options", $storedSettings)) { + $storedSettings["options"] .= $sysparams["options"]; + } - if(array_key_exists("optionsBeforeTrackerUrl", $storedSettings)) - $storedSettings["optionsBeforeTrackerUrl"] .=$sysparams["optionsBeforeTrackerUrl"]; - - $sysparams = array_merge($sysparams,$storedSettings); + if(array_key_exists("optionsBeforeTrackerUrl", $storedSettings)) { + $storedSettings["optionsBeforeTrackerUrl"] .= $sysparams["optionsBeforeTrackerUrl"]; + } + + $sysparams = array_merge($sysparams, $storedSettings); foreach($sysparams as $key => $value){ - $sysparams[$key] = $this->replaceTokens($value,$originalSysparams,$sysparams); } } - + + /** + * @param $subject + * @param $originalSysparams + * @param $sysparams + * @return mixed + */ private function replaceTokens($subject,$originalSysparams,$sysparams){ $output = str_replace(array_map(function($item){return '{$original_'.$item.'}';},array_keys($originalSysparams)),array_values($originalSysparams),$subject); $output = str_replace(array_map(function($item){return '{$'.$item.'}';},array_keys($sysparams)),array_values($sysparams),$output); return $output; } + + /** + * @return array + */ + protected function getSettings() { + $settings = API::getInstance(); + $storedSettings = $settings->getSettings(); + return $storedSettings; + } + + /** + * @return string + */ + protected function getTrackingCodeFile() { + return $trackingCodeFile = PIWIK_INCLUDE_PATH . "/piwik.js"; + } + + /** + * + */ + public function afterUpdate() { + + // cleanup old original file + $oldOriginalCodeFile = $this->getTrackingCodeFile() . ".original"; + if(file_exists($oldOriginalCodeFile)) { + unlink($oldOriginalCodeFile); + } + + $this->rewriteJavascriptFiles(); + } + + /** + * + */ + public function rewriteJavascriptFiles() { + $trackingCodeFile = $this->getTrackingCodeFile(); + + if(is_writeable($trackingCodeFile)) { + $trackingCodeFileOriginal = $trackingCodeFile . ".original"; + + if (!file_exists($trackingCodeFileOriginal)) { + copy($trackingCodeFile, $trackingCodeFileOriginal); + } + + $trackingCode = file_get_contents($trackingCodeFileOriginal); + + $settings = $this->getSettings(); + + if (array_key_exists("paqVariable", $settings)) { + $trackingCode = str_replace("_paq", $settings["paqVariable"], $trackingCode); + } + + if (array_key_exists("piwikJs", $settings)) { + $trackingCode = str_replace("piwik.js", $settings["piwikJs"], $trackingCode); + } + + if (array_key_exists("piwikPhp", $settings)) { + $trackingCode = str_replace("piwik.php", $settings["piwikPhp"], $trackingCode); + } + + file_put_contents($trackingCodeFile, $trackingCode); + } + } + + /** + * @param $result + * @param $parameters + */ + public function rewriteJavascriptTag(&$result, $parameters) { + + $settings = $this->getSettings(); + + if(array_key_exists("paqVariable", $settings)) { + $result = str_replace("_paq", $settings["paqVariable"], $result); + } + + if(array_key_exists("piwikJs", $settings)) { + $result = str_replace("piwik.js", $settings["piwikJs"], $result); + } + + if(array_key_exists("piwikPhp", $settings)) { + $result = str_replace("piwik.php", $settings["piwikPhp"], $result); + } + + if(array_key_exists("removePiwikBranding", $settings)) { + $result = preg_replace('/<\!\-\- .* \-\->/', '', $result); + } + } } diff --git a/lang/en.json b/lang/en.json index 274d144..1a425b6 100644 --- a/lang/en.json +++ b/lang/en.json @@ -1,17 +1,37 @@ { "TrackingCodeCustomizer": { "PluginDescription":"The provided values will replace the defaults in the generated tracking code (leave blank to use defaults). Viewing https://github.com/piwik/piwik/blob/master/core/Tracker/TrackerCodeGenerator.php is recommended to deconstruct how the tracking code is built.", - "idSiteSettingTitle":"idSite", - "idSiteSettingDescription":"The siteId that will be included in the tracking code.", - "protocolSettingTitle":"protocol", - "protocolSettingDescription":"Piwik url protocol.", - "piwikUrlSettingTitle":"piwikUrl", - "piwikUrlSettingDescription":"The tracker URL to use.", - "httpsPiwikUrlSettingTitle":"httpsPiwikUrl", - "httpsPiwikUrlSettingDescription":"Set if the HTTPS domain is different from the normal domain.", - "optionsSettingTitle":"options", + + "idSiteSettingTitle":"The siteId that will be included in the tracking code.", + "idSiteSettingHelp": "Probably not useful in most scenarios. The idSite option is included for completeness.", + + "protocolSettingTitle":"Piwik default URL protocol", + "protocolSettingHelp":"eg. http(s):// - allows to set the default protocol instead of the protocol relative URL", + + "piwikUrlSettingTitle":"The tracker URL to use.", + "piwikUrlSettingHelp":"eg. 'tracker.example.com/piwik' use hostname + basepath only (omit protocol and trailing slash)", + + "httpsPiwikUrlSettingTitle":"HTTPS tracker URL (if different from normal domain)", + "httpsPiwikUrlSettingHelp":"Set if the HTTPS domain is different from the normal domain. Example: 'tracker.example.com/piwik' use hostname + basepath only (omit protocol and trailing slash)", + + "piwikJsSettingTitle": "piwik.js Filename", + "piwikJsSettingHelp": "Let's you customize the piwik.js Filename. This is useful to avoid detections by AdBlockers. Don't forget to create a rewrite rule accordingly in your web-server configuration: RewriteRule \"^/p/p.(js|php)$\" \"/piwik/piwik.$1\" [PT,L]", + + "piwikPhpSettingTitle": "piwik.php Filename", + "piwikPhpSettingHelp": "Let's you customize the piwik.php Filename. This is useful to avoid detections by AdBlockers. Don't forget to create a rewrite rule accordingly in your web-server configuration: RewriteRule \"^/p/p.(js|php)$\" \"/piwik/piwik.$1\" [PT,L] Please note that changing the trackers filename needs to be addressed when using the log analyzer (use --replay-tracking-expected-tracker-file=yourfilename.php)", + + "paqVariableSettingTitle": "Name for _paq in Javascript tracking code", + "paqVariableSettingHelp": "Let's you customize the _paq JS Variable in your tracking code. This can be useful to avoid issues with multiple Piwik tracking codes on the same page.", + + "removePiwikBrandingSettingTitle": "Remove Piwik branding comments in tracking code", + "removePiwikBrandingSettingHelp": "Remove the comments containing 'Piwik' out of the standard tracking code", + + "optionsSettingTitle":"Code options", "optionsSettingDescription":"A string of JavaScript code that customizes the JavaScript tracker (after _paq = _paq || [] and before trackPageView).", - "optionsBeforeTrackerUrlSettingTitle":"optionsBeforeTrackerUrl", - "optionsBeforeTrackerUrlSettingDescription":"A string of Javascript code that customizes the JavaScript tracker inside of anonymous function before adding setTrackerUrl into paq." + + "optionsBeforeTrackerUrlSettingTitle":"Code options before tracker URL", + "optionsBeforeTrackerUrlSettingDescription":"A string of Javascript code that customizes the JavaScript tracker inside of anonymous function before adding setTrackerUrl into paq.", + + "writePermissionInfoText": "WARNING: %PIWIK_INCLUDE_PATH%/piwik.js is not writeable by the webserver. If you'd like to use the following settings, please change the permissions of the file or apply the changes manually." } } \ No newline at end of file