-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfgoogleanalytics.php
128 lines (111 loc) · 2.93 KB
/
bfgoogleanalytics.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
<?php
/**
* @package Plugin for adding Google Analytics to site.
* @version 0.0.1
* @author https://www.brainforge.co.uk
* @copyright Copyright (C) 2011-2022 Jonathan Brain. All rights reserved.
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
// no direct access
defined('_JEXEC') or die;
class plgSystemBFGoogleAnalytics extends CMSPlugin
{
protected $application;
protected $trackingcodes;
protected $measurementID0;
/*
*/
function __construct(&$subject, $config)
{
parent::__construct($subject, $config);
$this->application = Factory::getApplication();
$this->trackingcodes = (array)$this->params->get('trackingcodes', array());
if (!empty($this->trackingcodes))
{
$this->measurementID0 = trim(reset($this->trackingcodes)->measurementid);
if (!preg_match('/^UA-[0-9]{6,9}-[0-9]+$/', $this->measurementID0))
{
$this->measurementID0 = null;
}
}
}
/*
*/
function onBeforeRender()
{
if (empty($this->measurementID0) || $this->measurementID0 == 'UA-00000000-0')
{
if($this->application->isClient('administrator'))
{
$this->application->getLanguage()->load('plg_system_bfgoogleanalytics.sys', __DIR__);
$this->application->enqueueMessage(Text::_('PLG_BFGOOGLEANALYTICS_TRACKINGCODE_ERROR'), 'warning');
}
$this->trackingcodes = null;
}
}
/*
*/
function onAfterRender()
{
if (empty($this->measurementID0)) {
return;
}
if ($this->application->isClient('administrator'))
{
if ($this->params->get('showInAdmin') != '1')
{
return;
}
}
else if (!$this->application->isClient('site'))
{
return;
}
if (!empty($_SERVER['SERVER_ADDR']) &&
$this->params->get('showInDevelopment') != '1')
{
if ($_SERVER['SERVER_ADDR'] == @$_SERVER['REMOTE_ADDR'])
{
return;
}
if (substr($_SERVER["SERVER_ADDR"], 0, 2) == 'fd' ||
substr($_SERVER["SERVER_ADDR"], 0, 2) == 'fe')
{
return;
}
$baseIP = explode('.', $_SERVER["SERVER_ADDR"]);
switch ($baseIP[0])
{
case '127': return;
case '192':
if ($baseIP[1] == '168') return;
break;
}
}
$gaCode = '
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=' . $this->measurementID0 . '"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(\'js\', new Date());
';
foreach($this->trackingcodes as $trackingcode)
{
$gaCode .= " gtag('config', '" . trim($trackingcode->measurementid) . "');\n";
}
$gaCode .= "</script>\n";
$buffer = $this->application->getBody();
$pos = stripos($buffer, '<head');
if ($pos === false) return;
$pos = stripos($buffer, '>', $pos);
if ($pos === false) return;
$pos += 1;
$buffer = substr($buffer, 0, $pos) . $gaCode . substr($buffer, $pos);
$this->application->setBody($buffer);
}
}
?>