Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make matomo configurable via ui #1732

Merged
merged 5 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions africanlii/templates/peachjam/layouts/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,3 @@
href="{% sass_src 'stylesheets/africanlii.scss' %}"
type="text/css"/>
{% endblock %}
{% block head-js %}
{{ block.super }}
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDoNotTrack", true]);
_paq.push(["disableCookies"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://lawsafrica.matomo.cloud/"; {# replace with correct link #}
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '5']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src='//cdn.matomo.cloud/lawsafrica.matomo.cloud/matomo.js'; s.parentNode.insertBefore(g,s); {# replace with correct link #}
})();
</script>
<!-- End Matomo Code -->
{% endblock %}
20 changes: 0 additions & 20 deletions lawlibrary/templates/peachjam/layouts/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,3 @@
href="{% sass_src 'stylesheets/lawlibrary.scss' %}"
type="text/css"/>
{% endblock %}
{% block head-js %}
{{ block.super }}
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDoNotTrack", true]);
_paq.push(["disableCookies"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://lawsafrica.matomo.cloud/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '4']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src='//cdn.matomo.cloud/lawsafrica.matomo.cloud/matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
{% endblock %}
16 changes: 16 additions & 0 deletions peachjam/js/peachjam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class PeachJam {
this.setupTooltips();
this.setupPopovers();
this.scrollNavTabs();
this.clearGACookies();
}

setupConfig () {
Expand Down Expand Up @@ -176,6 +177,21 @@ class PeachJam {
}
});
}

clearGACookies () {
// if window.dataLayer is not set, then Google Analytics is not enabled, but there may be cookies still set; clear them
if (!window.dataLayer) {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i];
const eqPos = cookie.indexOf('=');
const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
if (name.trim().startsWith('_ga')) {
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=.${window.location.hostname}`;
}
}
}
}
}

const peachJam = new PeachJam();
Expand Down
35 changes: 35 additions & 0 deletions peachjam/migrations/0119_matomo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 3.2.20 on 2024-02-19 13:15

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("peachjam", "0118_auto_20240130_1221"),
]

operations = [
migrations.AddField(
model_name="peachjamsettings",
name="matomo_domain",
field=models.CharField(
blank=True,
help_text="Matomo domain (e.g. mysite.matomo.cloud)",
max_length=1024,
null=True,
verbose_name="matomo domain",
),
),
migrations.AddField(
model_name="peachjamsettings",
name="matomo_site_id",
field=models.CharField(
blank=True,
help_text="Matomo site ID (e.g. 2)",
max_length=10,
null=True,
verbose_name="matomo site ID",
),
),
]
14 changes: 14 additions & 0 deletions peachjam/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ class PeachJamSettings(SingletonModel):
blank=True,
help_text=_("Enter one or more email addresses separated by spaces."),
)
matomo_domain = models.CharField(
verbose_name=_("matomo domain"),
max_length=1024,
null=True,
blank=True,
help_text=_("Matomo domain (e.g. mysite.matomo.cloud)"),
)
matomo_site_id = models.CharField(
verbose_name=_("matomo site ID"),
max_length=10,
null=True,
blank=True,
help_text=_("Matomo site ID (e.g. 2)"),
)

class Meta:
verbose_name = verbose_name_plural = _("site settings")
Expand Down
16 changes: 16 additions & 0 deletions peachjam/templates/peachjam/layouts/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@
});
</script>
{% endif %}
{% if PEACHJAM_SETTINGS.matomo_domain and PEACHJAM_SETTINGS.matomo_site_id %}
<script>
var _paq = window._paq = window._paq || [];
_paq.push(["setDoNotTrack", true]);
_paq.push(["disableCookies"]);
_paq.push(["trackPageView"]);
_paq.push(["enableLinkTracking"]);
(function() {
var u="https://{{ PEACHJAM_SETTINGS.matomo_domain }}/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '{{ PEACHJAM_SETTINGS.matomo_site_id }}']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src='//cdn.matomo.cloud/{{ PEACHJAM_SETTINGS.matomo_domain }}/matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
{% endif %}
{% endif %}
<!-- must be last script to load -->
<script defer src="{% static 'js/app-prod.js' %}"></script>
Expand Down
Loading