-
Notifications
You must be signed in to change notification settings - Fork 31
/
GoogleAnalyticsPlugin.php
160 lines (144 loc) · 4.78 KB
/
GoogleAnalyticsPlugin.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
<?php
/**
* @file plugins/generic/googleAnalytics/GoogleAnalyticsPlugin.php
*
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class GoogleAnalyticsPlugin
*
* @ingroup plugins_generic_googleAnalytics
*
* @brief Google Analytics plugin class
*/
namespace APP\plugins\generic\googleAnalytics;
use APP\core\Application;
use APP\template\TemplateManager;
use PKP\core\JSONMessage;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use PKP\core\PKPPageRouter;
class GoogleAnalyticsPlugin extends GenericPlugin
{
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
if (Application::isUnderMaintenance()) {
return true;
}
if ($success && $this->getEnabled($mainContextId)) {
// Insert Google Analytics page tag to footer
Hook::add('TemplateManager::display', $this->registerScript(...));
}
return $success;
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName()
{
return __('plugins.generic.googleAnalytics.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription()
{
return __('plugins.generic.googleAnalytics.description');
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $verb)
{
$router = $request->getRouter();
return array_merge(
$this->getEnabled() ? [
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, ['verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic']),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
] : [],
parent::getActions($request, $verb)
);
}
/**
* @copydoc Plugin::manage()
*/
public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
case 'settings':
$context = $request->getContext();
$templateMgr = TemplateManager::getManager($request);
$templateMgr->registerPlugin('function', 'plugin_url', $this->smartyPluginUrl(...));
$form = new GoogleAnalyticsSettingsForm($this, $context->getId());
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
/**
* Register the Google Analytics script tag
*
* @param string $hookName
* @param array $params
*/
public function registerScript($hookName, $params)
{
$request = Application::get()->getRequest();
$context = $request->getContext();
if (!$context) {
return false;
}
$router = $request->getRouter();
if (!$router instanceof PKPPageRouter) {
return false;
}
$googleAnalyticsSiteId = $this->getSetting($context->getId(), 'googleAnalyticsSiteId');
if (empty($googleAnalyticsSiteId)) {
return false;
}
$googleAnalyticsCode = "
(function (w, d, s, l, i) { w[l] = w[l] || []; var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true;
j.src = 'https://www.googletagmanager.com/gtag/js?id=' + i + dl; f.parentNode.insertBefore(j, f);
function gtag(){dataLayer.push(arguments)}; gtag('js', new Date()); gtag('config', i); })
(window, document, 'script', 'dataLayer', '{$googleAnalyticsSiteId}');
";
$templateMgr = TemplateManager::getManager($request);
$templateMgr->addJavaScript(
'googleanalytics',
$googleAnalyticsCode,
[
'priority' => TemplateManager::STYLE_SEQUENCE_LAST,
'inline' => true,
]
);
return false;
}
}
if (!PKP_STRICT_MODE) {
class_alias('\APP\plugins\generic\googleAnalytics\GoogleAnalyticsPlugin', '\GoogleAnalyticsPlugin');
}