Skip to content

Commit aa9f296

Browse files
committed
Initial Commit
0 parents  commit aa9f296

File tree

9 files changed

+306
-0
lines changed

9 files changed

+306
-0
lines changed

API.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Piwik\Plugins\TrackingCodeCustomizer;
4+
5+
class API extends \Piwik\Plugin\API
6+
{
7+
8+
private static $plugin_name = 'TrackingCodeCustomizer';
9+
10+
public function getSettings()
11+
{
12+
$outParams = array();
13+
14+
$params = array("idSite","piwikUrl","options","optionsBeforeTrackerUrl","httpsPiwikUrl","protocol");
15+
16+
$settings = new Settings(self::$plugin_name);
17+
18+
foreach($params as $param){
19+
20+
$value = $settings->{$param}->getValue();
21+
if(!empty($value))
22+
$outParams[$param] = $value;
23+
}
24+
25+
return $outParams;
26+
}
27+
28+
}

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Piwik TrackingCodeCustomizer Plugin
2+
Tracking Code Customizer plugin for the Piwik Web Analytics software package
3+
4+
##Description
5+
Allows Piwik admininstrators to customize the tracking code that is autogenerated for users. This is useful for directing requests to the correct servers in a multi-server setup, include additional parameters in default tracking, or to perform conditional checks before initiating a tracking call.
6+
7+
##Instructions
8+
The easiest way to install is to find the plugin in the [Piwik Marketplace](http://plugins.piwik.org/).
9+
10+
##Usage
11+
12+
Set additional default OPTIONS for tracking. The follwing is entered into the "options" field.
13+
14+
```javascript
15+
if (typeof(hash) !== 'undefined'){_paq.push(['setCustomVariable','1','U',hash,'visit']); _paq.push(['setUserId',hash]);};
16+
```
17+
Resultant tracking code
18+
```javascript
19+
20+
<!-- Piwik -->
21+
<script type="text/javascript">
22+
var _paq = _paq || [];
23+
if (typeof(hash) !== 'undefined'){_paq.push(['setCustomVariable','1','U',hash,'visit']); _paq.push(['setUserId',hash]);};
24+
_paq.push(['trackPageView']);
25+
_paq.push(['enableLinkTracking']);
26+
var u="//webanalytics-tracker.XXXX.XXX/";
27+
_paq.push(['setTrackerUrl', u+'piwik.php']);
28+
_paq.push(['setSiteId', 1]);
29+
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
30+
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
31+
})();
32+
</script>
33+
<noscript><p><img src="//webanalytics-tracker.XXXX.XXX/piwik.php?idsite=1" style="border:0;" alt="" /></p></noscript>
34+
<!-- End Piwik Code -->
35+
```
36+
37+
##Changelog
38+
0.1.0 Initial Release
39+
40+
##License
41+
GPL v3 / fair use
42+
43+
## Support
44+
Please [report any issues](https://github.com/jbrule/piwikplugin-TrackingCodeCustomizer/issues). Pull requests welcome.

Settings.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Piwik\Plugins\TrackingCodeCustomizer;
4+
5+
use Piwik\Piwik;
6+
use Piwik\Settings\SystemSetting;
7+
use Piwik\Settings\UserSetting;
8+
9+
class Settings extends \Piwik\Plugin\Settings
10+
{
11+
public $idSite;
12+
public $piwikUrl;
13+
public $options;
14+
public $optionsBeforeTrackerUrl;
15+
public $httpsPiwikUrl;
16+
public $protocol;
17+
18+
protected function init()
19+
{
20+
$this->setIntroduction($this->t('PluginDescription'));
21+
22+
$default_textbox_size = array("size"=> 65);
23+
24+
25+
$this->idSite = new SystemSetting('idSite', $this->t('idSiteSettingTitle'));
26+
$this->idSite->type = static::TYPE_STRING;
27+
$this->idSite->uiControlType = static::CONTROL_TEXT;
28+
$this->idSite->uiControlAttributes = array("size" => "6", "maxlenth" => "8");
29+
$this->idSite->description = $this->t('idSiteSettingDescription');
30+
$this->idSite->readableByCurrentUser = true;
31+
$this->idSite->defaultValue = "";
32+
$this->idSite->inlineHelp = 'Probably not useful in most scenarios. The idSite option is included for completeness.';
33+
34+
$this->idSite->validate = function ($value, $setting) {
35+
if ($value != "" && preg_match("/^[0-9]+$/",$value) !== 1) {
36+
throw new \Exception('Value is invalid. Must be positive integer');
37+
}
38+
};
39+
40+
$this->addSetting($this->idSite);
41+
42+
$this->protocol = new SystemSetting('protocol', $this->t('protocolSettingTitle'));
43+
$this->protocol->type = static::TYPE_STRING;
44+
$this->protocol->uiControlType = static::CONTROL_TEXT;
45+
$this->protocol->uiControlAttributes = array("size" => "10", "maxlenth" => "8");
46+
$this->protocol->description = $this->t('protocolSettingDescription');
47+
$this->protocol->readableByCurrentUser = true;
48+
$this->protocol->defaultValue = "";
49+
$this->protocol->inlineHelp = 'http(s)://';
50+
51+
$this->protocol->validate = function ($value, $setting) {
52+
if ($value != "" && !($value == "http://" || $value == "https://")) {
53+
throw new \Exception('Value is invalid');
54+
}
55+
};
56+
57+
$this->addSetting($this->protocol);
58+
59+
$this->piwikUrl = new SystemSetting('piwikUrl', $this->t('piwikUrlSettingTitle'));
60+
$this->piwikUrl->type = static::TYPE_STRING;
61+
$this->piwikUrl->uiControlType = static::CONTROL_TEXT;
62+
$this->piwikUrl->uiControlAttributes = $default_textbox_size;
63+
$this->piwikUrl->description = $this->t('piwikUrlSettingDescription');
64+
$this->piwikUrl->readableByCurrentUser = true;
65+
$this->piwikUrl->defaultValue = "";
66+
$this->piwikUrl->inlineHelp = 'tracker.example.com/piwik use hostname+basepath only (omit protocol and trailing slash)';
67+
68+
$this->addSetting($this->piwikUrl);
69+
70+
$this->httpsPiwikUrl = new SystemSetting('httpsPiwikUrl', $this->t('httpsPiwikUrlSettingTitle'));
71+
$this->httpsPiwikUrl->type = static::TYPE_STRING;
72+
$this->httpsPiwikUrl->uiControlType = static::CONTROL_TEXT;
73+
$this->httpsPiwikUrl->uiControlAttributes = $default_textbox_size;
74+
$this->httpsPiwikUrl->description = $this->t('httpsPiwikUrlSettingDescription');
75+
$this->httpsPiwikUrl->readableByCurrentUser = true;
76+
$this->httpsPiwikUrl->defaultValue = "";
77+
$this->httpsPiwikUrl->inlineHelp = 'secure-tracker.example.com/piwik use hostname+basepath only (omit protocol and trailing slash)';
78+
79+
$this->addSetting($this->httpsPiwikUrl);
80+
81+
$this->options = new SystemSetting('options', $this->t('optionsSettingTitle'));
82+
$this->options->type = static::TYPE_STRING;
83+
$this->options->uiControlType = static::CONTROL_TEXTAREA;
84+
$this->options->description = $this->t('optionsSettingDescription');
85+
$this->options->readableByCurrentUser = true;
86+
$this->options->defaultValue = "";
87+
$this->options->inlineHelp = '{$original_paramname} and {$paramname} tokens are available for referencing values.';
88+
89+
$this->addSetting($this->options);
90+
91+
$this->optionsBeforeTrackerUrl = new SystemSetting('optionsBeforeTrackerUrl', $this->t('optionsBeforeTrackerUrlSettingTitle'));
92+
$this->optionsBeforeTrackerUrl->type = static::TYPE_STRING;
93+
$this->optionsBeforeTrackerUrl->uiControlType = static::CONTROL_TEXTAREA;
94+
$this->optionsBeforeTrackerUrl->description = $this->t('optionsBeforeTrackerUrlSettingDescription');
95+
$this->optionsBeforeTrackerUrl->readableByCurrentUser = true;
96+
$this->optionsBeforeTrackerUrl->defaultValue = "";
97+
$this->optionsBeforeTrackerUrl->inlineHelp = '{$original_paramname} and {$paramname} tokens are available for referencing values.';
98+
99+
$this->addSetting($this->optionsBeforeTrackerUrl);
100+
}
101+
102+
private function t($key)
103+
{
104+
return Piwik::translate('TrackingCodeCustomizer_' . $key);
105+
}
106+
}

TrackingCodeCustomizer.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Piwik - free/libre analytics platform
4+
*
5+
* @link http://piwik.org
6+
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7+
*
8+
*/
9+
namespace Piwik\Plugins\TrackingCodeCustomizer;
10+
11+
/**
12+
* HOOK DOCUMENTATION
13+
* Present in >=v2.9.0 /piwik/core/Tracker/TrackerCodeGenerator.php
14+
* https://github.com/piwik/piwik/blob/master/core/Tracker/TrackerCodeGenerator.php
15+
* Triggered when generating JavaScript tracking code server side. Plugins can use
16+
* this event to customise the JavaScript tracking code that is displayed to the
17+
* user.
18+
*
19+
* @param array &$codeImpl An array containing snippets of code that the event handler
20+
* can modify. Will contain the following elements:
21+
*
22+
* - **idSite**: The ID of the site being tracked.
23+
* - **piwikUrl**: The tracker URL to use.
24+
* - **options**: A string of JavaScript code that customises
25+
* the JavaScript tracker.
26+
* - **optionsBeforeTrackerUrl**: A string of Javascript code that customises
27+
* the JavaScript tracker inside of anonymous function before
28+
* adding setTrackerUrl into paq.
29+
* - **protocol**: Piwik url protocol.
30+
*
31+
* The **httpsPiwikUrl** element can be set if the HTTPS
32+
* domain is different from the normal domain.
33+
* @param array $parameters The parameters supplied to `TrackerCodeGenerator::generate()`.
34+
35+
*/
36+
37+
class TrackingCodeCustomizer extends \Piwik\Plugin
38+
{
39+
public function getListHooksRegistered()
40+
{
41+
$hooks = array(
42+
'Piwik.getJavascriptCode' => 'applyTrackingCodeCustomizations'
43+
);
44+
return $hooks;
45+
}
46+
47+
/*
48+
* @param array &$sysparams
49+
* @key int idSite
50+
* @key string piwikUrl
51+
* @key string options
52+
* @key string optionsBeforeTrackerUrl
53+
* @key string protocol
54+
* @param array $parameters
55+
* @key bool mergeSubdomains
56+
* @key bool groupPageTitlesByDomain
57+
* @key bool mergeAliasUrls
58+
* @key bool visitorCustomVariables
59+
* @key bool pageCustomVariables
60+
* @key bool customCampaignNameQueryParam
61+
* @key bool customCampaignKeywordParam
62+
* @key bool doNotTrack
63+
**/
64+
public function applyTrackingCodeCustomizations($sysparams,$parameters){
65+
66+
$originalSysparams = $sysparams;
67+
68+
$settings = API::getInstance();
69+
70+
$storedSettings = $settings->getSettings();
71+
72+
if($storedSettings["options"])
73+
$storedSettings["options"] .= $sysparams["options"];
74+
75+
if($storedSettings["optionsBeforeTrackerUrl"])
76+
$storedSettings["optionsBeforeTrackerUrl"] .=$sysparams["optionsBeforeTrackerUrl"];
77+
78+
$sysparams = array_merge($sysparams,$storedSettings);
79+
80+
foreach($sysparams as $key => $value){
81+
82+
$sysparams[$key] = $this->replaceTokens($value,$originalSysparams,$sysparams);
83+
}
84+
85+
}
86+
87+
private function replaceTokens($subject,$originalSysparams,$sysparams){
88+
$output = str_replace(array_map(function($item){return '{$original_'.$item.'}';},array_keys($originalSysparams)),array_values($originalSysparams),$subject);
89+
$output = str_replace(array_map(function($item){return '{$'.$item.'}';},array_keys($sysparams)),array_values($sysparams),$output);
90+
return $output;
91+
}
92+
}

lang/en.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"TrackingCodeCustomizer": {
3+
"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.",
4+
"idSiteSettingTitle":"idSite",
5+
"idSiteSettingDescription":"The siteId that will be included in the tracking code.",
6+
"protocolSettingTitle":"protocol",
7+
"protocolSettingDescription":"Piwik url protocol.",
8+
"piwikUrlSettingTitle":"piwikUrl",
9+
"piwikUrlSettingDescription":"The tracker URL to use.",
10+
"httpsPiwikUrlSettingTitle":"httpsPiwikUrl",
11+
"httpsPiwikUrlSettingDescription":"Set if the HTTPS domain is different from the normal domain.",
12+
"optionsSettingTitle":"options",
13+
"optionsSettingDescription":"A string of JavaScript code that customizes the JavaScript tracker (after _paq = _paq || [] and before trackPageView).",
14+
"optionsBeforeTrackerUrlSettingTitle":"optionsBeforeTrackerUrl",
15+
"optionsBeforeTrackerUrlSettingDescription":"A string of Javascript code that customizes the JavaScript tracker inside of anonymous function before adding setTrackerUrl into paq."
16+
}
17+
}

plugin.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "TrackingCodeCustomizer",
3+
"version": "0.1.0",
4+
"license": "GPL v3",
5+
"keywords": ["tracking", "javascript tracking","customize tracking","customise tracking"],
6+
"description": "Allows Piwik admininstrators to customize the tracking code that is autogenerated for users. This is useful for directing requests to the correct servers in a multi-server setup, include additional parameters in default tracking, or to perform conditional checks before initiating a tracking call. ",
7+
"homepage": "https://github.com/jbrule/piwikplugin-TrackingCodeCustomizer",
8+
"theme": false,
9+
"require": {
10+
"piwik": ">=v2.9.0"
11+
},
12+
"authors": [
13+
{
14+
"name": "Josh Brule",
15+
"email": "",
16+
"homepage": "https://www.linkedin.com/pub/joshua-brule/15/326/9b9"
17+
}
18+
]
19+
}

screenshots/.gitkeep

Whitespace-only changes.

screenshots/Plugin_Settings.png

184 KB
Loading

screenshots/Tracking_Code.png

112 KB
Loading

0 commit comments

Comments
 (0)