Skip to content

Commit

Permalink
add matomo integration
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk authored and m4ra committed Jul 2, 2024
1 parent 2c4945b commit d36699e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
20 changes: 20 additions & 0 deletions apps/core/static/js/matomo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function initMatomo () {
const matomo = document.getElementById('matomo')
const _paq = window._paq = window._paq || []
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
if (matomo.dataset.cookieDisabed) {
_paq.push(['disableCookies'])
}
_paq.push(['trackPageView'])
_paq.push(['enableLinkTracking'])
insertMatomo(_paq, matomo.dataset.url, matomo.dataset.id)
}

function insertMatomo (_paq, url, id) {
_paq.push(['setTrackerUrl', url + 'matomo.php'])
_paq.push(['setSiteId', id])
const d = document; const g = d.createElement('script'); const s = d.getElementsByTagName('script')[0]
g.type = 'text/javascript'; g.async = true; g.src = url + 'matomo.js'; s.parentNode.insertBefore(g, s)
}

initMatomo()
2 changes: 2 additions & 0 deletions apps/core/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

{# External stylesheets #}
<link rel="stylesheet" href="{% static 'all.css' %}">
{% matomo_enabled as matomo %}
{% if matomo %} {% matomo_tracking_code %} {% endif %}

{% block extra_css %}

Expand Down
4 changes: 4 additions & 0 deletions apps/core/templates/matomo/tracking_code.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load static %}
<!-- Matomo -->
<script id="matomo" data-cookieDisabled={{ cookie_disabled }} data-url="{{ url }}" data-id="{{ id }}" async src="{% static 'matomo.js' %}"></script>
<!-- End Matomo Code -->
28 changes: 28 additions & 0 deletions apps/core/templatetags/core_tags.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.http import Http404
from django.urls import resolve

Expand Down Expand Up @@ -54,3 +56,29 @@ def file_type(media_file):
return "audio/wav"
else:
return "type invalid"


@register.simple_tag()
def matomo_enabled():
if hasattr(settings, "MATOMO_ENABLED"):
return settings.MATOMO_ENABLED
return False


@register.inclusion_tag("matomo/tracking_code.html")
def matomo_tracking_code():
if not hasattr(settings, "MATOMO_SITE_ID"):
raise ImproperlyConfigured("MATOMO_SITE_ID does not exist.")

if not hasattr(settings, "MATOMO_URL"):
raise ImproperlyConfigured("MATOMO_URL does not exist.")

cookie_disabled = True
if hasattr(settings, "MATOMO_COOKIE_DISABLED"):
cookie_disabled = settings.MATOMO_COOKIE_DISABLED

return {
"id": settings.MATOMO_SITE_ID,
"url": settings.MATOMO_URL,
"cookie_disabled": cookie_disabled,
}
3 changes: 3 additions & 0 deletions changelog/8236.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added

- added matomo integration
3 changes: 3 additions & 0 deletions webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module.exports = {
all: [
'./apps/core/static/scss/all.scss',
'./apps/core/static/js/app.js'
],
matomo: [
'./apps/core/static/js/matomo.js'
]
},

Expand Down

0 comments on commit d36699e

Please sign in to comment.