-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkupWayFathomAnalytics.module.php
222 lines (171 loc) · 4.87 KB
/
MarkupWayFathomAnalytics.module.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace ProcessWire;
require_once(__DIR__ . '/WayFathom.php');
/**
* MarkupWayFathomAnalytics
*
* Markup module for WayFathomAnalytics to generate the tracking code.
*
* @author Craig A Rodway
*
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer
* https://processwire.com
*
*/
class MarkupWayFathomAnalytics extends WireData implements Module
{
public static function getModuleInfo()
{
return [
'title' => "MarkupWayFathomAnalytics",
'version' => "0.0.2",
'summary' => 'Markup module for WayFathomAnalytics to generate the tracking code.',
'author' => "Craig A Rodway",
'href' => 'https://github.com/craigrodway/WayFathomAnalytics',
'autoload' => true,
'singular' => true,
'permanent' => false,
'icon' => 'bar-chart',
'requires' => [
'PHP>=7.0.0',
'ProcessWire>=3.0.0',
'WayFathomAnalytics',
],
];
}
public function ready()
{
$wayFathom = $this->modules->get('WayFathomAnalytics');
$siteId = $wayFathom->siteId;
$trackingAddScript = $wayFathom->trackingAddScript;
// Register Page::render() hook which will add the <script> tag
if (strlen($siteId) && $trackingAddScript == '1' && $this->page->template->name != 'admin') {
$this->addHookAfter('Page::render', $this, 'addEmbed');
}
}
/**
* Hook to automatically add the embed code at the end of the <body> element.
*
* @param HookEevent $event The Page::render() event.
*
*/
public function addEmbed(HookEvent $event)
{
$html = $event->return;
$script = $this->render();
$event->return = str_replace('</head>', "{$script}\n</head>", $html);
}
/**
* Get the script tag that loads Fathom.
*
* @param array $options Override module settings with custom values.
* @return string Script tag to load Fathom Analytics.
*
*/
public function render(array $options = [])
{
$attributes = $this->getScriptAttributes($options);
$tag = sprintf('<script %s></script>', $attributes);
if (isset($options['hideComments']) && $options['hideComments']) {
return $tag;
}
$out = "\n<!-- Fathom - beautiful, simple website analytics -->\n";
$out .= "{$tag}\n";
$out .= "<!-- / Fathom -->\n";
return $out;
}
/**
* Get the URL to the Fathom tracking script.
*
* @param array $options Override module settings with custom values.
*
*/
public function getScriptUrl($options = [])
{
$config = $this->getConfig($options);
$customDomain = $config->customDomain;
$customDomain = $this->sanitizer->text($customDomain);
$customDomain = $this->sanitizer->entities($customDomain);
if (strlen($customDomain)) {
$host = str_replace(['http://', 'https://', '/'], '', $customDomain);
} else {
$host = 'cdn.usefathom.com';
}
$url = sprintf("https://%s/script.js", $host);
return $url;
}
/**
* Get the attributes for the script tag.
*
* @param array $options Override module settings with custom values.
*
*/
public function getScriptAttributes(array $options = [])
{
$config = $this->getConfig($options);
$attrs = [
'src' => $this->getScriptUrl($options),
];
if (strlen($config->siteId)) {
$attrs['site'] = $config->siteId;
}
if ($config->trackingHonorDnt == 1) {
$attrs['honor-dnt'] = 'true';
}
if ($config->trackingAutomatic == 0) {
$attrs['auto'] = 'false';
}
if ($config->trackingCanonical == 0) {
$attrs['canonical'] = 'false';
}
if (strlen($config->trackingExcludeDomains)) {
$attrs['excluded-domains'] = $config->trackingExcludeDomains;
}
if (strlen($config->trackingIncludeDomains)) {
$attrs['included-domains'] = $config->trackingIncludeDomains;
}
if (strlen($config->trackingSpaMode)) {
$attrs['spa'] = $config->trackingSpaMode;
}
$attrs['defer'] = '';
$attrList = [];
foreach ($attrs as $attr => $value) {
switch ($attr) {
case 'included-domains':
case 'excluded-domains':
$value = str_replace(',', "\n", $value);
$items = explode("\n", $value);
$items = array_filter($items, 'trim');
$value = implode(',', $items);
$value = $this->sanitizer->textarea($value);
$value = $this->sanitizer->entities($value);
break;
default:
$value = $this->sanitizer->text($value);
$value = $this->sanitizer->entities($value);
}
if (strlen($value)) {
$attrList[] = "{$attr}=\"{$value}\"";
} else {
$attrList[] = "{$attr}";
}
}
$attributes = implode(' ', $attrList);
return $attributes;
}
/**
* Get a WireArray of configuration values, with optional overrides supplied via $options.
*
* @param array $options Override the defaults with custom values.
* @return WireArray All configuration values.
*
*/
private function getConfig(array $options = [])
{
$defaults = WayFathomAnalytics::getDefaults();
$moduleConfig = $this->modules->getConfig('WayFathomAnalytics');
$data = new WireArray();
$data->setArray(array_merge($defaults, $moduleConfig, $options));
return $data;
}
}