Skip to content

Commit e60908c

Browse files
add: prevent admin users from tracking their own views :-)
1 parent 3ffc052 commit e60908c

File tree

6 files changed

+55
-25
lines changed

6 files changed

+55
-25
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
# Renick.Matomo
22

3-
Just a simple OctoberCMS plugin for embedding matomo analytics. These features are provided by the initial release of
3+
Just a simple OctoberCMS plugin for embedding matomo analytics. These features are provided by the initial release of
44
this plugin:
55

66
- [x] store matomo api key as well as page id
77
- [x] middleware for matomo server-side tracking
88
- [x] component for matomo javascript integration
99
- [x] component for matomo tag manager javascript integration
10+
- [x] components hide the matomo script when an admin user is logged in
1011
- [x] integration of analytic reports within the OctoberCMS backend
1112

1213
To install this plugin, you can just run the following command:
1314
```bash
1415
php artisan plugin:install Renick.Matomo [email protected]:renickbuettner/matomo-plugin.git
1516
```
1617

17-
Feel free to ask, raise ideas, mind bugs or contribute on the Github repository. Please create a new issue or pull
18+
Feel free to ask, raise ideas, mind bugs or contribute on the Github repository. Please create a new issue or pull
1819
request.
1920

2021
---
2122

22-
Future ideas:
23+
Future ideas:
2324

24-
- [ ] measure impact of accessing plugins config from database on site performance - may add support for in-memory cache?
25+
- [ ] measure impact of accessing plugins config from database on site performance - may add support for in-memory cache?
2526
- [ ] add more report widgets (api is prepared...)
27+
- [ ] what use-cases do we may need?

components/MTagJs.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ public function containerId(): string
3131
{
3232
return $this->settings->container_id ?? "";
3333
}
34-
}
34+
35+
public function isLoggedInAdmin(): bool {
36+
return $this->settings->isLoggedInAdmin();
37+
}
38+
}

components/MatomoJs.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ public function useCookies(): bool
3636
{
3737
return $this->settings->use_cookies ?? false;
3838
}
39-
}
39+
40+
public function isLoggedInAdmin(): bool {
41+
return $this->settings->isLoggedInAdmin();
42+
}
43+
}

components/matomojs/default.htm

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
{% if __SELF__.remoteUrl and __SELF__.siteId %}
2-
<script>
3-
var _paq = window._paq = window._paq || [];
4-
{% if not __SELF__.useCookies() %}
5-
_paq.push(['disableCookies']);
6-
{% endif %}
7-
_paq.push(['trackPageView']);
8-
_paq.push(['enableLinkTracking']);
9-
(function() {
10-
var u="{{ __SELF__.remoteUrl }}";
11-
_paq.push(['setTrackerUrl', u+'matomo.php']);
12-
_paq.push(['setSiteId', '{{ __SELF__.siteId }}']);
13-
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
14-
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
15-
})();
16-
</script>
17-
<noscript><p><img src="{{ __SELF__.remoteUrl }}matomo.php?idsite={{ __SELF__.siteId }}&amp;rec=1" style="border:0;" alt="" /></p></noscript>
1+
{% if __SELF__.isLoggedInAdmin() %}
2+
<script>const _paq = 'disabled for admin user';</script>
3+
{% else %}
4+
<script>
5+
var _paq = window._paq = window._paq || [];
6+
{% if not __SELF__.useCookies() %}
7+
_paq.push(['disableCookies']);
8+
{% endif %}
9+
_paq.push(['trackPageView']);
10+
_paq.push(['enableLinkTracking']);
11+
(function() {
12+
var u="{{ __SELF__.remoteUrl }}";
13+
_paq.push(['setTrackerUrl', u+'matomo.php']);
14+
_paq.push(['setSiteId', '{{ __SELF__.siteId }}']);
15+
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
16+
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
17+
})();
18+
</script>
19+
<noscript><p><img src="{{ __SELF__.remoteUrl }}matomo.php?idsite={{ __SELF__.siteId }}&amp;rec=1" style="border:0;" alt="" /></p></noscript>
1820
{% endif %}

components/mtagjs/default.htm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
{% if __SELF__.isLoggedInAdmin() %}
2+
<script>const _mtm = 'disabled for admin user';</script>
3+
{% else %}
14
<script>
25
var _mtm = window._mtm = window._mtm || [];
36
_mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
47
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
58
g.async=true; g.src='{{ __SELF__.remoteUrl }}js/container_{{ __SELF__.containerId }}.js'; s.parentNode.insertBefore(g,s);
6-
</script>
9+
</script>
10+
{% endif %}

models/Settings.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,18 @@ public function getValidAttribute(): bool
9898

9999
return true;
100100
}
101-
}
101+
102+
/**
103+
* We want to detect, if an admin user is logged
104+
* @return bool
105+
*/
106+
public function isLoggedInAdmin(): bool {
107+
try {
108+
$user = \Backend\Facades\BackendAuth::getUser();
109+
return $user !== null;
110+
111+
} catch (\Exception $e) {
112+
return false;
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)