-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_analytics_4.php
41 lines (35 loc) · 1.38 KB
/
google_analytics_4.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
<?php
defined('_JEXEC') or die;
class plgSystemGoogle_Analytics_4 extends JPlugin
{
public function onBeforeCompileHead()
{
try {
$doc = JFactory::getDocument();
if ($doc->getType() !== 'html') {
return;
}
$measurementId = $this->params->get('measurement_id', '');
// Validate the format of the Measurement ID
if (!preg_match('/^G-[a-zA-Z0-9]+$/', $measurementId)) {
throw new Exception('Invalid Measurement ID format.');
}
$script = <<<EOL
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id={$measurementId}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{$measurementId}');
</script>
EOL;
$doc->addCustomTag($script);
} catch (Exception $e) {
// Log the error message
JLog::add($e->getMessage(), JLog::ERROR, 'plg_system_google_analytics_4');
// Add a warning message to Joomla's system message queue
JFactory::getApplication()->enqueueMessage('Google Analytics 4 Plugin: ' . $e->getMessage(), 'warning');
}
}
}