-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalytics.php
More file actions
91 lines (77 loc) · 2.17 KB
/
analytics.php
File metadata and controls
91 lines (77 loc) · 2.17 KB
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
<?php
namespace Plugins\Analytics;
use \Typemill\Plugin;
class Analytics extends Plugin
{
protected $settings;
public static function getSubscribedEvents()
{
return array(
'onTwigLoaded' => 'onTwigLoaded',
'onCspLoaded' => 'onCspLoaded',
);
}
public function onTwigLoaded()
{
if($this->adminroute)
{
return;
}
# get Twig Instance and add the cookieconsent template-folder to the path
$twig = $this->getTwig();
$loader = $twig->getLoader();
$loader->addPath(__DIR__ . '/templates');
$analyticSettings = $this->getPluginSettings();
if(isset($analyticSettings['tool']))
{
# fetch the template, render it with twig and add javascript with settings
if($analyticSettings['tool'] == 'matomo')
{
$this->addInlineJS($twig->fetch('/matomoanalytics.twig', $analyticSettings));
}
elseif($analyticSettings['tool'] == 'google')
{
$this->addJS('https://www.googletagmanager.com/gtag/js?id=' . $analyticSettings['google_id']);
$this->addInlineJS($twig->fetch('/googleanalytics.twig', $analyticSettings));
}
elseif($analyticSettings['tool'] == 'fathom')
{
if(empty($analyticSettings['fathom_url']))
{
$analyticSettings['fathom_url'] = 'https://cdn.usefathom.com/script.js';
}
$this->addInlineJS($twig->fetch('/fathomanalytics.twig', $analyticSettings));
}
}
}
public function onCspLoaded($csp)
{
$data = $csp->getData();
$domain = '';
$analyticSettings = $this->getPluginSettings();
if(!$this->adminroute && isset($analyticSettings['tool']))
{
if($analyticSettings['tool'] == 'matomo')
{
if(!empty($analyticSettings['matomo_url']))
{
$domain = trim($analyticSettings['matomo_url']);
}
}
elseif($analyticSettings['tool'] == 'google')
{
$domain = 'https://www.googletagmanager.com/';
}
elseif($analyticSettings['tool'] == 'fathom')
{
$domain = 'https://cdn.usefathom.com/';
if(!empty($analyticSettings['fathom_url']))
{
$domain = trim($analyticSettings['fathom_url']);
}
}
}
$data[] = $domain;
$csp->setData($data);
}
}