Skip to content

Commit

Permalink
Add support for plausible analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
rlskoeser committed Oct 8, 2024
1 parent 15441d3 commit 2c9aab7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Revisions to data exports in preparation for 2.0 dataset publication:
with an optional flag to include them
- Address export includes care of person id and name, location name, and member names, sort names, and uris
- Nested information in JSON data exports is now grouped by entity
- Add support for Plausible analytics, configurable with PLAUSIBLE_ANALYTICS_SCRIPT and PLAUSIBLE_ANALYTICS_404s in django settings

1.6.2
-----
Expand Down
6 changes: 6 additions & 0 deletions mep/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ def template_settings(request):
"site": site,
# needed for footer; easier to get here than in template
"about_page": site.root_page.get_children().filter(slug="about").first(),
"PLAUSIBLE_ANALYTICS_SCRIPT": getattr(
settings, "PLAUSIBLE_ANALYTICS_SCRIPT", None
),
"PLAUSIBLE_ANALYTICS_404s": getattr(
settings, "PLAUSIBLE_ANALYTICS_404s", False
),
}
return context_extras
7 changes: 7 additions & 0 deletions mep/local_settings.py.sample
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ LOGGING = {
# https://support.google.com/analytics/answer/2709828
# GTAGS_ANALYTICS_ENV = ''

# INCLUDE_ANALYTICS = True

# Configure to enable Plausible analytics in production with desired options
# PLAUSIBLE_ANALYTICS_SCRIPT = "https://plausible.io/js/script.file-downloads.hash.outbound-links.js"
# PLAUSIBLE_ANALYTICS_404s = True


# secret settings for twitter api access 100 years twitter account
TWITTER_100YEARS = {
'API': {
Expand Down
8 changes: 8 additions & 0 deletions templates/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@
src="{% static 'img/404-error-1x.png' %}"
alt="beach notecard reading 'absent on business, will return shortly'"
class="e404">
{% endblock %}


{% block js %}
{{ block.super }}
{% if PLAUSIBLE_ANALYTICS_404s %}
<script>document.addEventListener('DOMContentLoaded', function () { plausible('404', { props: { path: document.location.pathname } }); });</script>
{% endif %}
{% endblock %}
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<!-- scripts -->
{% render_bundle 'main' 'js' %}
{% block js %}{% endblock %}
{% include "snippets/plausible_analytics.html" %}
</head>
<body>
{% include 'snippets/test_banner.html' %}
Expand Down
7 changes: 7 additions & 0 deletions templates/snippets/plausible_analytics.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% if INCLUDE_ANALYTICS and PLAUSIBLE_ANALYTICS_SCRIPT %}
{# plausible analytics #}
<script defer data-domain="shakespeareandco.princeton.edu" src="{{ PLAUSIBLE_ANALYTICS_SCRIPT }} "></script>
{% if PLAUSIBLE_ANALYTICS_404s %}
<script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>
{% endif %}
{% endif %}

0 comments on commit 2c9aab7

Please sign in to comment.