Skip to content

Commit

Permalink
Integrate Matomo Tag Manager (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
istride authored Nov 15, 2023
1 parent 3607388 commit ca7170c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 2 deletions.
27 changes: 27 additions & 0 deletions home/migrations/0052_sitesettings_mtm_container_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.2.20 on 2023-11-09 17:56

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("home", "0051_auto_20230330_1105"),
]

operations = [
migrations.AddField(
model_name="sitesettings",
name="mtm_container_id",
field=models.CharField(
blank=True,
help_text=(
"Currently this feature only works on devices using JavaScript"
" (e.g. basic featurephones are not supported), and does not work"
" with MNO zero-rating or Meta Free Basics."
),
max_length=255,
null=True,
verbose_name="Matomo Tag Manager container ID",
),
),
]
19 changes: 19 additions & 0 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,16 @@ class SiteSettings(BaseSetting):
blank=True,
on_delete=models.SET_NULL)
opt_in_to_google_web_light = models.BooleanField(default=False)
mtm_container_id = models.CharField(
verbose_name=_("Matomo Tag Manager container ID"),
max_length=255,
null=True,
blank=True,
help_text=_(
"Currently this feature only works on devices using JavaScript (e.g. basic"
" feature phones are not supported), and does not work with MNO zero-rating"
" or Meta Free Basics.")
)

panels = [
ImageChooserPanel('logo'),
Expand Down Expand Up @@ -754,6 +764,15 @@ class SiteSettings(BaseSetting):
],
heading="Opt in to Google web light",
),
MultiFieldPanel(
[
FieldPanel(
"mtm_container_id",
heading="Tag Manager container ID",
),
],
heading="Matomo",
),
]

@classmethod
Expand Down
1 change: 1 addition & 0 deletions iogt/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
{# Override this in templates to add extra stylesheets #}
{% endblock %}
{% matomo_tracking_tags %}
{% matomo_tag_manager settings.home.SiteSettings.mtm_container_id %}
</head>

{% get_current_language_bidi as LANGUAGE_BIDI %}
Expand Down
10 changes: 10 additions & 0 deletions matomo/templates/matomo_tag_manager.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% if mtm_src %}
<!-- Matomo Tag Manager -->
<script>
var _mtm = window._mtm = window._mtm || [];
_mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src='{{ mtm_src }}'; s.parentNode.insertBefore(g,s);
</script>
<!-- End Matomo Tag Manager -->
{% endif %}
13 changes: 13 additions & 0 deletions matomo/templatetags/matomo_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
register = template.Library()


@register.inclusion_tag("matomo_tag_manager.html", takes_context=True)
def matomo_tag_manager(context, container_id=None):
try:
server_url = settings.MATOMO_SERVER_URL
except AttributeError:
server_url = None

if server_url and container_id:
context.update({"mtm_src": f"{server_url}js/container_{container_id}.js"})

return context


@register.inclusion_tag("matomo_tracking_tags.html", takes_context=True)
def matomo_tracking_tags(context):
enabled = is_enabled()
Expand Down
39 changes: 37 additions & 2 deletions matomo/tests/test_matomo_tags.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from django.test import TestCase, override_settings

from matomo.templatetags.matomo_tags import matomo_tracking_tags
from matomo.templatetags.matomo_tags import matomo_tag_manager, matomo_tracking_tags


@override_settings(
MATOMO_TRACKING=True,
MATOMO_SERVER_URL="https://example.com/",
MATOMO_SITE_ID=456,
)
class MatomoTagsTests(TestCase):
class MatomoTrackingTagsTests(TestCase):
def test_only_enabled_when_required_settings_are_set(self):
self.assertTrue(matomo_tracking_tags(create_context()).get("tracking_enabled"))

Expand Down Expand Up @@ -44,6 +44,41 @@ def test_visitor_id_is_created_for_clients_without_javascript(self):
)


class MatomoTagManagerTests(TestCase):
def test_enable_only_when_container_id_and_server_url(self):
with override_settings(MATOMO_SERVER_URL=None):
self.assertIsNone(
matomo_tag_manager(
create_context(),
container_id="ID",
).get("mtm_src")
)

with override_settings(MATOMO_SERVER_URL="https://example.com/"):
self.assertIsNone(
matomo_tag_manager(
create_context(),
container_id=None,
).get("mtm_src")
)
self.assertIsNotNone(
matomo_tag_manager(
create_context(),
container_id="ID",
).get("mtm_src")
)

def test_generate_correct_tag_manager_url(self):
with override_settings(MATOMO_SERVER_URL="https://example.com/"):
self.assertEqual(
matomo_tag_manager(
create_context(),
container_id="ID",
).get("mtm_src"),
"https://example.com/js/container_ID.js",
)


class MockRequest:
def __init__(self):
self.session = dict()
Expand Down

0 comments on commit ca7170c

Please sign in to comment.