-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrackingCodeCustomizer.php
106 lines (84 loc) · 4.13 KB
/
TrackingCodeCustomizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Piwik\Plugins\TrackingCodeCustomizer;
/**
* HOOK DOCUMENTATION
* Present in >=v2.9.0 /piwik/core/Tracker/TrackerCodeGenerator.php
* https://github.com/piwik/piwik/blob/master/core/Tracker/TrackerCodeGenerator.php
* 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()`.
*/
class TrackingCodeCustomizer extends \Piwik\Plugin
{
private static $hooks = array(
'Tracker.getJavascriptCode' => 'applyTrackingCodeCustomizations'
);
public function registerEvents()
{
return self::$hooks;
}
/*
* @param array &$sysparams
* @key int idSite
* @key string piwikUrl
* @key string options
* @key string optionsBeforeTrackerUrl
* @key string protocol
* @param array $parameters
* @key bool mergeSubdomains
* @key bool groupPageTitlesByDomain
* @key bool mergeAliasUrls
* @key bool visitorCustomVariables
* @key bool pageCustomVariables
* @key bool customCampaignNameQueryParam
* @key bool customCampaignKeywordParam
* @key bool doNotTrack
**/
public function applyTrackingCodeCustomizations(&$sysparams,$parameters){
$originalSysparams = $sysparams;
$storedSettings = $this->getSettings();
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);
foreach($sysparams as $key => $value){
$sysparams[$key] = $this->replaceTokens($value,$originalSysparams,$sysparams);
}
}
private function getSettings()
{
$outParams = array();
$params = array("idSite","piwikUrl","options","optionsBeforeTrackerUrl","httpsPiwikUrl","protocol");
$settings = new SystemSettings();
//print_r($settings);
//end();
foreach($params as $param){
$value = $settings->{$param}->getValue();
if(!empty($value))
$outParams[$param] = $value;
}
return $outParams;
}
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;
}
}