forked from maloja/pico-matomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PicoMatomo.php
77 lines (73 loc) · 2.69 KB
/
PicoMatomo.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
<?php
/**
* Matomo web analytics plugin for Pico CMS
* Edited January 2020 by maloja
*
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://github.com/maloja/pico-matomo
* @author maloja
*
* Configuration (config/config.yml)
* matomo:
* id: [matomo id]
* server: [matomo server]
*
* Operation:
* This Plugin will let you enjoy the power of Matomo web analytics for all
* your [Pico CMS](http://picocms.org) pages.The matomo Opt-Out Code can be
* added into pages with the keyword (% matomo %)
*/
class PicoMatomo extends AbstractPicoPlugin
{
const API_VERSION = 2;
protected $enabled = true;
protected $dependsOn = array();
/**
* Private variables
*/
private $id;
private $server;
private $lang;
private $opt_out_code;
private $pluginPath;
/**
* Read the configuration files and prepair opt_out string
*/
public function onConfigLoaded(array &$config)
{
$this->pluginPath = $config[ 'plugins_url' ]."/PicoMatomo/";
$this->id = $config['matomo']['id'];
$this->server = $config['matomo']['server'];
$this->lang = $config['matomo']['lang'];
if ( ($this->id) && ($this->server) ) {
$this->server = rtrim($this->server, '/') . '/';
if (!preg_match("~^(?:f|ht)tps?://~i", $this->server)) {
$this->server = "https://" . $this->server;
}
$this->opt_out_code = '<iframe title="Matomo OptOut" id="iFrame1" style="border: 0px solid #888; height: 300px; width: 100%;" ';
$this->opt_out_code .= 'src="' . $this->server . 'index.php?';
$this->opt_out_code .= 'module=CoreAdminHome&action=optOut&language=' . $this->lang;
$this->opt_out_code .= '&backgroundColor=&fontColor=488cdb&fontSize=18px&fontFamily=sans"></iframe>';
}
}
/**
* Insert tracking code
* Triggered after Pico has rendered the page
*/
public function onPageRendered(&$output) {
if ( ($this->id) && ($this->server) ) {
$tracking = file_get_contents($this->pluginPath . "tracking-code.inc");
$tracking = preg_replace( '/\\[\\[server\\]\\]/', $this->server, $tracking);
$tracking = preg_replace( '/\\[\\[id\\]\\]/', $this->id, $tracking);
$output = preg_replace( '/\<\/head\>[\s|\r|\n]*?\<body\>/', "\n</head>\n<body>\n{$tracking}", $output, 1);
}
}
/**
* Replace (% matomo %) opt-out tags
* Triggered after Pico has prepared the raw file contents for parsing
*/
public function onContentPrepared(&$content)
{
$content = preg_replace('/\\(%\s*matomo\s*%\\)/', $this->opt_out_code, $content );
}
}