forked from jbrule/piwik-SiteUrlTrackingID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSiteUrlTrackingID.php
159 lines (134 loc) · 3.34 KB
/
SiteUrlTrackingID.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SiteUrlTrackingID;
use Piwik\Common;
use Piwik\Plugins\SitesManager\Model as SiteModel;
use Piwik\Plugins\SitesManager\SiteUrls;
use Piwik\Tracker\Cache as TrackerCache;
class SiteUrlTrackingID extends \Piwik\Plugin
{
const MAPPING_CACHE_KEY = 'SiteUrlTrackingID.Mapping';
/**
* Register event observers
*
* @return array
*/
public function registerEvents()
{
return [
'Piwik.getJavascriptCode' => 'getSiteURLjs',
'SitesManager.getImageTrackingCode' => 'getSiteURLimg',
'Tracker.Request.getIdSite' => 'getSiteID',
'Tracker.setTrackerCacheGeneral' => 'setTrackerCacheGeneral',
];
}
/**
* Run upon activation of plugin. Clears Tracker Cache so that this plugins setTrackerCacheGeneral method will be called and added to cache
*
* @return void
*/
public function activate()
{
TrackerCache::clearCacheGeneral();
}
/**
* Run upon deactivation of plugin. Clears this plugins data from the Tracker Cache
*
* @return void
*/
public function deactivate()
{
TrackerCache::clearCacheGeneral();
}
/**
* Cache all possible website urls (including aliases) for later lookup
*
* @param array &$cacheContent
* @return void
*/
public function setTrackerCacheGeneral(&$cacheContent)
{
$siteUrls = new SiteUrls();
$urls = $siteUrls->getAllCachedSiteUrls();
$map = [];
foreach($urls as $id=>$aliases)
{
foreach($aliases as $alias)
{
$siteKey = preg_replace('/^.+?\:\/\//', '', strtolower($alias));
$siteKey = sha1($siteKey);
$map[$siteKey] = $id;
}
}
$cacheContent[self::MAPPING_CACHE_KEY] = $map;
}
/**
* Get main site URL from the site ID
*
* @param int $idSite
* @return string|int
*/
public function getSiteURL($idSite)
{
$siteModel = new SiteModel();
$site = $siteModel->getSiteFromId($idSite);
$siteURL = $site["main_url"];
return (!empty($siteURL) ? preg_replace('/^.+?\:\/\//i', '', $siteURL) : $idSite);
}
/**
* Get site ID from the site URL
*
* @param int &$idSite
* @param array $params
* @return void
*/
public function getSiteID(&$idSite, $params)
{
if (!(is_int($idSite) && $idSite > 0))
{
$siteURLfull = preg_replace(['/^.+?\:\/\//','/\/\s*$/'], '', strtolower($params['idsite']));
$siteKey = sha1($siteURLfull);
$trackerCache = TrackerCache::getCacheGeneral();
$idMapping = $trackerCache[self::MAPPING_CACHE_KEY];
if(!empty($idMapping[$siteKey]))
{
$idSite = $idMapping[$siteKey];
}
}
}
/**
* Get site URL for the JavaScript Tracking Code
*
* @param array &$codeImpl
* @param array $parameters
* @return void
*/
public function getSiteURLjs(&$codeImpl, $parameters)
{
$settings = new SystemSettings();
if($settings->useUrlInTrackingCode->getValue())
{
$codeImpl['idSite'] = $this->getSiteURL($codeImpl['idSite']);
}
}
/**
* Get site URL for the Image Tracking Link
*
* @param array &$piwikUrl
* @param array &$urlParams
* @return void
*/
public function getSiteURLimg(&$piwikUrl, &$urlParams)
{
$settings = new SystemSettings();
if($settings->useUrlInTrackingCode->getValue())
{
$urlParams['idsite'] = $this->getSiteURL($urlParams['idsite']);
}
}
}