Skip to main content

Django module to proxy requests to Plausible Analytics.

Project description

Django Plausible Proxy

Django module to proxy requests and send server-side events to Plausible Analytics.

Proxying

Proxying allows a project owner concerned about missing data seeing a more complete picture. See Adblockers and using a proxy for analytics for the detailed outline of the problem and solution.

When installed and configured in settings.py and urls.py, the app proxies the HTTP requests as such:

https://<yourdomain.com>/js/script.js -> https://plausible.io/js/script.js
https://<yourdomain.com>/api/event    -> https://plausible.io/api/event

Server-side events

Track on the server side events that can't be tracker otherwise, such as API requests.

from plausible_proxy import send_custom_event
...
send_custom_event(request, name="Register", props={"plan": "Premium"})

Installation

Install the package from PyPI.

pip install django-plausible-proxy

Configure Django setting in the settings.py.

# Register the app to enable {% plausble %} templatetag.
INSTALLED_APPS = [
    # ...
    "plausible_proxy"
    # ...
]

# Optionally, define a default value for Plausible domain to provide a default value
# for the Plausible domain and the `send_custom_event()` function.
PLAUSIBLE_DOMAIN = "yourdomain.com"

Update urls.py.

from django.urls import include, path

urlpatterns = [
    # ...
    path("", include("plausible-proxy.urls")),
    # ...
]

Update your base HTML template to include the plausible templatetag.

{% load plausible %}
<html>
  <head>
      ...
      {% plausible script='script.js' %}
  </head>

API reference

{% plausible %}

A templatetag to include the Plausible analytics script to the page.

Arguments:

  • domain (default to settings.PLAUSIBLE_DOMAIN): defines the data-domain parameter, the is the domain for the Plausible analytics.
  • script (default to script.js): defines the Plausible script to use. See Script extensions for enhanced measurement for the list of alternative script names and what they can track for you.

Usage example:

{% load plausible %}
<html>
  <head>
      ...
      {% plausible domain='example.com' script='script.outbound-links.js' %}
  </head>

plausible_proxy.services.send_custom_event()

end a custom event to Plausible and return successful status.

See Plausible events API for more information

Arguments:

  • request (HttpRequest): Original Django HTTP request. Will be used to create X-Forwarded-For and User-Agent headers.
  • name (string): Name of the event. Can specify pageview which is a special type of event in Plausible. All other names will be treated as custom events.
  • domain (optional string): Domain name of the site in Plausible. The value from settings.PLAUSIBLE_DOMAIN is used by default.
  • url (optional string): URL of the page where the event was triggered. If not provided, the function extracts the URL from the request. If the URL contains UTM parameters, they will be extracted and stored. If URL is not set, will be extracted from the request.
  • referrer (optional string): Referrer for this event.
  • screen_width (optional integer): Width of the screen.
  • props (optional dict): Custom properties for the event. See: Using custom props.

Returns: True if request was accepted successfully.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-plausible-proxy-0.1.2.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

django_plausible_proxy-0.1.2-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file django-plausible-proxy-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django-plausible-proxy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 43a12108ba3744b422828c6e803f8507df3ac7d09d9c7b8d157f234899fe556b
MD5 d4c3a88f38b0fa9de4b6a5177e3acfa5
BLAKE2b-256 214bfcc2b42c23a1f04684faff3b1d1aa8a7236d30475e52d984e68610b594e8

See more details on using hashes here.

Provenance

File details

Details for the file django_plausible_proxy-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_plausible_proxy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2465d054ef81cd3d20ae8e43a9f976894d0ab51a7e506f5e26044f8ea9a4f7c8
MD5 b47c1d774616179ad586b8da4618a61c
BLAKE2b-256 1b4824fb519c2e0cd1068af9794b00d48cb984c4bb9937771dc4a6eb1eda5235

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page