Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion API.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
52 changes: 52 additions & 0 deletions Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Settings extends \Piwik\Plugin\Settings
public $optionsBeforeTrackerUrl;
public $httpsPiwikUrl;
public $protocol;
public $piwikJs;
public $piwikPhp;
public $paqVariable;
public $removePiwikBranding;

protected function init()
{
Expand Down Expand Up @@ -77,6 +81,54 @@ protected function init()
$this->httpsPiwikUrl->inlineHelp = 'secure-tracker.example.com/piwik use hostname+basepath only (omit protocol and trailing slash)';

$this->addSetting($this->httpsPiwikUrl);


$this->piwikJs = new SystemSetting('piwikJs', $this->t('piwikJsSettingTitle'));
$this->piwikJs->type = static::TYPE_STRING;
$this->piwikJs->uiControlType = static::CONTROL_TEXT;
$this->piwikJs->uiControlAttributes = $default_textbox_size;
$this->piwikJs->description = $this->t('piwikJsSettingDescription');
$this->piwikJs->readableByCurrentUser = true;
$this->piwikJs->defaultValue = "";
$this->piwikJs->inlineHelp = 'alternative name for piwik.js in Javascript tracking code';

$this->addSetting($this->piwikJs);


$this->piwikPhp = new SystemSetting('piwikPhp', $this->t('piwikPhpSettingTitle'));
$this->piwikPhp->type = static::TYPE_STRING;
$this->piwikPhp->uiControlType = static::CONTROL_TEXT;
$this->piwikPhp->uiControlAttributes = $default_textbox_size;
$this->piwikPhp->description = $this->t('piwikPhpSettingDescription');
$this->piwikPhp->readableByCurrentUser = true;
$this->piwikPhp->defaultValue = "";
$this->piwikPhp->inlineHelp = 'alternative name for piwik.php in Javascript tracking code';

$this->addSetting($this->piwikPhp);


$this->paqVariable = new SystemSetting('paqVariable', $this->t('paqVariableSettingTitle'));
$this->paqVariable->type = static::TYPE_STRING;
$this->paqVariable->uiControlType = static::CONTROL_TEXT;
$this->paqVariable->uiControlAttributes = $default_textbox_size;
$this->paqVariable->description = $this->t('paqVariableSettingDescription');
$this->paqVariable->readableByCurrentUser = true;
$this->paqVariable->defaultValue = "";
$this->paqVariable->inlineHelp = 'alternative name for _paq';

$this->addSetting($this->paqVariable);


$this->removePiwikBranding = new SystemSetting('removePiwikBranding', $this->t('removePiwikBrandingSettingTitle'));
$this->removePiwikBranding->type = static::TYPE_BOOL;
$this->removePiwikBranding->uiControlType = static::CONTROL_CHECKBOX;
$this->removePiwikBranding->description = $this->t('removePiwikBrandingSettingDescription');
$this->removePiwikBranding->readableByCurrentUser = true;
$this->removePiwikBranding->defaultValue = "";
$this->removePiwikBranding->inlineHelp = 'Remove Piwik branding in comments';

$this->addSetting($this->removePiwikBranding);


$this->options = new SystemSetting('options', $this->t('optionsSettingTitle'));
$this->options->type = static::TYPE_STRING;
Expand Down
119 changes: 106 additions & 13 deletions TrackingCodeCustomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
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',
'CoreUpdater.update.end' => 'afterUpdate'
);

//2.15 includes a new function for registering hooks. 2.15 wi
Expand Down Expand Up @@ -74,29 +77,119 @@ 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();
$trackingCodeFileOriginal = $trackingCodeFile . ".original";

if(!file_exists($trackingCodeFileOriginal)) {
copy($trackingCodeFile, $trackingCodeFileOriginal);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes webserver has write permissions to file. Need to check permissions or handle failure and provide instruction to user. User would need to either SSH in to do the process manually or set write perm for web server process.

}

$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);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes webserver has write permissions to file. Need to check permissions or handle failure and provide instruction to user. User would need to either SSH in to do the process manually or set write perm for web server process.

}

/**
* @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('/<!--(.|\s)*?-->/', '', $result);
$result = str_replace(" Piwik ", " Tracking ", $result);
}
}
}
10 changes: 9 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
"optionsSettingTitle":"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."
"optionsBeforeTrackerUrlSettingDescription":"A string of Javascript code that customizes the JavaScript tracker inside of anonymous function before adding setTrackerUrl into paq.",
"piwikJsSettingTitle": "piwik.js Filename",
"piwikJsSettingDescription": "Let's you customize the piwik.js Filename, don't forget to create a rewrite accordingly.",
"piwikPhpSettingTitle": "piwik.php Filename",
"piwikPhpSettingDescription": "Let's you customize the piwik.php Filename, don't forget to create a rewrite accordingly.",
"paqVariableSettingTitle": "Name for _paq in Javascript tracking code",
"paqVariableSettingDescription": "Let's you customize the _paq JS Variable in your tracking code",
"removePiwikBrandingSettingTitle": "Remove PIWIK branking in tracking code",
"removePiwikBrandingSettingDescription": "Removed the name PIWIK out of the comments in the code"
}
}