Add plausible analytics if env variable exists

This commit is contained in:
Gabriel Augendre 2020-11-10 15:44:05 +01:00
parent 698a2db6a7
commit 36c8a83018
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
5 changed files with 28 additions and 0 deletions

View file

@ -37,3 +37,7 @@ def git_version(request):
version = "latest" version = "latest"
url = settings.BLOG["repo"]["log"] url = settings.BLOG["repo"]["log"]
return {"git_version": version, "git_version_url": url} return {"git_version": version, "git_version_url": url}
def plausible(request):
return {"plausible_domain": settings.PLAUSIBLE_DOMAIN}

View file

@ -0,0 +1,3 @@
{% if plausible_domain is not None %}
<script async defer data-domain="{{ plausible_domain }}" src="https://plausible.augendre.info/js/plausible.js"></script>
{% endif %}

View file

@ -11,6 +11,7 @@
<script src="{% static 'toggle-dark-mode.js' %}" async></script> <script src="{% static 'toggle-dark-mode.js' %}" async></script>
<link rel="alternate" type="application/rss+xml" title="Gab's Notes » Feed" href="{% url 'complete-feed' %}"> <link rel="alternate" type="application/rss+xml" title="Gab's Notes » Feed" href="{% url 'complete-feed' %}">
{% include "articles/favicon.html" %} {% include "articles/favicon.html" %}
{% include "articles/analytics.html" %}
</head> </head>
<body> <body>
<nav> <nav>

View file

@ -42,3 +42,20 @@ def test_access_article_by_slug(client: Client, published_article: Article):
content = res.content.decode("utf-8") content = res.content.decode("utf-8")
assert published_article.title in content assert published_article.title in content
assert published_article.get_formatted_content() in content assert published_article.get_formatted_content() in content
@pytest.mark.django_db
def test_has_plausible_if_set(client: Client, settings):
settings.PLAUSIBLE_DOMAIN = "gabnotes.org"
res = client.get(reverse("articles-list"))
content = res.content.decode("utf-8")
assert "https://plausible.augendre.info/js/plausible.js" in content
assert 'data-domain="gabnotes.org"' in content
@pytest.mark.django_db
def test_doesnt_have_plausible_if_unset(client: Client, settings):
settings.PLAUSIBLE_DOMAIN = None
res = client.get(reverse("articles-list"))
content = res.content.decode("utf-8")
assert "https://plausible.augendre.info/js/plausible.js" not in content

View file

@ -100,6 +100,7 @@ TEMPLATES = [
"articles.context_processors.drafts_count", "articles.context_processors.drafts_count",
"articles.context_processors.date_format", "articles.context_processors.date_format",
"articles.context_processors.git_version", "articles.context_processors.git_version",
"articles.context_processors.plausible",
], ],
}, },
}, },
@ -184,3 +185,5 @@ BLOG = {
} }
SHORTPIXEL_API_KEY = os.getenv("SHORTPIXEL_API_KEY") SHORTPIXEL_API_KEY = os.getenv("SHORTPIXEL_API_KEY")
PLAUSIBLE_DOMAIN = os.getenv("PLAUSIBLE_DOMAIN")