-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatomoWire.module
111 lines (91 loc) · 4.14 KB
/
MatomoWire.module
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
<?php namespace ProcessWire;
/**
* MatomoWire
* This module adds embedding methods for Matomo (aka Piwik)
*
* @author blaueQuelle
*
* ProcessWire 3.x
* Copyright (C) 2011 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class MatomoWire extends WireData implements Module, ConfigurableModule
{
public static function getModuleInfo()
{
return [
'title' => "MatomoWire",
'summary' => "This module adds embedding methods for Matomo (aka Piwik)",
'author' => "blaueQuelle",
'href' => "https://github.com/blaueQuelle/matomowire",
'version' => 5,
'autoload' => true,
'singular' => true,
'requires' => ["PHP>=7.2", "ProcessWire>=3.0.110"],
'installs' => ["TextformatterMatomoWire"],
'icon' => 'flask'
];
}
public function ready()
{
if (
$this->wire('page')->template == 'admin' || // exclude admin pages
$this->wire('page')->template == 'form-builder' || // exclude from form-builder iframe
empty($this->matomo_url) || // do nothing, when there is no matomo URL
(empty($this->container_id) && empty($this->site_id)) // do nothing, if both the container and site id is empty
) {
return;
}
$this->addHookAfter('Page::render', $this, 'render');
}
public function ___render(HookEvent $event)
{
$replaceTag = ($this->render_position == "head") ? "</head>" : "</body>";
$scriptContent = $this->getScriptContent();
if (empty($scriptContent)) {
return;
}
$event->return = str_replace($replaceTag, $this->getScriptOpeningTag() . $scriptContent . "</script>" . $replaceTag, $event->return);
}
public function ___getScriptOpeningTag(): string
{
$async = ($this->render_position == 'head') ? 'async' : '';
if ($this->integrate_privacywire == true && $this->wire('modules')->isInstalled('PrivacyWire') && $this->wire('modules')->get('PrivacyWire')) {
return '<script type="text/plain" data-type="text/javascript" data-category="' . $this->privacywire_cookie_category . '" ' . $async . '>';
}
return '<script type="text/javascript" ' . $async . '>';
}
public function ___getScriptContent(): string
{
return ($this->embedding_method == "tag") ? $this->getTagManagerScriptContent() : $this->getSiteScriptContent();
}
public function ___getTagManagerScriptContent(): string
{
if (empty($this->container_id)) {
return '';
}
$matomo_url = rtrim($this->wire('sanitizer')->url($this->matomo_url), '/') . '/';
return 'var _mtm=window._mtm=window._mtm||[];_mtm.push({"mtm.startTime":(new Date().getTime()),"event":"mtm.Start"});var d=document,g=d.createElement("script"),s=d.getElementsByTagName("script")[0];g.type="text/javascript";g.async=true;g.src="' . $matomo_url . 'js/container_' . $this->container_id . '.js";s.parentNode.insertBefore(g,s);';
}
public function ___getSiteScriptContent(): string
{
if (empty($this->site_id)) {
return '';
}
$set_do_not_track = ($this->set_do_not_track == true) ? '_paq.push(["setDoNotTrack", true]);' : '';
$disable_cookies = ($this->disable_cookies == true) ? '_paq.push(["disableCookies"]);' : '';
$matomo_url = rtrim($this->wire('sanitizer')->url($this->matomo_url), '/') . '/';
$site_id = $this->wire('sanitizer')->int($this->site_id);
return 'var _paq=window._paq=window._paq||[];' . $set_do_not_track . $disable_cookies . '_paq.push(["trackPageView"]);_paq.push(["enableLinkTracking"]);(function(){var u="' . $matomo_url . '";_paq.push(["setTrackerUrl", u+"matomo.php"]);_paq.push(["setSiteId", "' . $site_id . '"]);var d=document,g=d.createElement("script"),s=d.getElementsByTagName("script")[0];g.type="text/javascript";g.async=true;g.src=u+"matomo.js";s.parentNode.insertBefore(g,s);})();';
}
public function ___install()
{
}
public function ___uninstall()
{
}
}