Skip to main content

Django module to provide easy Plausible integration, with Wagtail support

Project description

django-plausible-iplweb

CI PyPI Python Django PyPI - Status PyPI - License

Django module to provide easy Plausible integration, with Wagtail support.

Installation

pip install django-plausible-iplweb

Then simply add plausible to INSTALLED_APPS.

Usage

django-plausible-iplweb provides a plausible template tag that emits Plausible's current per-site tracking script. Each Plausible site now has its own script URL (e.g. https://plausible.io/js/pa-XXXXXXXX.js) — there is no data-domain attribute any more. Copy the script URL from your Plausible dashboard (Site Settings → General → Site Installation) into PLAUSIBLE_SCRIPT_URL, then place the tag in your <head>:

{% load plausible %}

{% plausible %}

With PLAUSIBLE_SCRIPT_URL = "https://plausible.io/js/pa-XXXXXXXX.js", this renders:

<script defer src="https://plausible.io/js/pa-XXXXXXXX.js"></script>
<script>
  window.plausible = window.plausible || function () { (window.plausible.q = window.plausible.q || []).push(arguments) };
  plausible.init({"autoCapturePageviews": false});
  (function () {
    var path = window.location.pathname;
    var masks = [];
    for (var i = 0; i < masks.length; i++) {
      path = path.replace(new RegExp(masks[i].pattern, masks[i].flags || "g"), masks[i].replacement);
    }
    var url = window.location.origin + path + window.location.search;
    plausible("pageview", { url: url });
  })();
</script>

Automatic pageview capture is disabled (autoCapturePageviews: false) so the package can send a single, optionally-masked pageview itself (see URL masking).

If PLAUSIBLE_SCRIPT_URL is empty (the default), the tag renders nothing — a convenient way to switch analytics off in development.

Configuration

All configuration lives in settings.py:

Setting Default Purpose
PLAUSIBLE_SCRIPT_URL "" Full per-site script URL from your Plausible dashboard. Empty → tag renders nothing.
PLAUSIBLE_INIT_OPTIONS {"autoCapturePageviews": False} Dict passed (as JSON) to plausible.init(...). Add any init option here, e.g. {"autoCapturePageviews": False, "hashBasedRouting": True}.
PLAUSIBLE_URL_MASKS [] Ordered list of client-side path-masking rules (see below).
PLAUSIBLE_KEEP_QUERY_STRING True Append location.search to the reported URL (keeps utm_*/ref for acquisition reports). Set False to drop the query string entirely.

You can also pass the script URL at call time, e.g. to use a different site on one page:

{% plausible script_url="https://plausible.io/js/pa-OTHER.js" %}

URL masking

Plausible records the page URL. If your paths contain identifiers (invitation codes, user IDs, UUIDs, …) those would leak into your analytics. PLAUSIBLE_URL_MASKS lets you redact them client-side before the pageview is sent. Each rule is applied to location.pathname in order via JavaScript's String.replace(new RegExp(pattern, flags), replacement):

PLAUSIBLE_URL_MASKS = [
    # Mask invitation/passphrase codes:  /i/<passphrase>  ->  /i/__code__
    {"pattern": r"^/i/[^/]+", "replacement": "/i/__code__"},
    # Mask UUIDs anywhere in the path:  /<uuid>  ->  /__id__
    {"pattern": r"/[0-9a-f-]{36}", "replacement": "/__id__", "flags": "gi"},
    # Mask numeric ids:  /123  ->  /__id__
    {"pattern": r"/\d+", "replacement": "/__id__"},
]

pattern and flags are JavaScript regular-expression syntax (the rules run in the browser). flags defaults to "g". The patterns above are examples — adapt them to your own routes; the default ([]) applies no masking.

Caveat: like Plausible's own redaction guidance, this masking runs once when the page loads. It may not behave as expected in single-page applications or with hash-based routing, where location changes without a full page load.

Overriding the rendered markup

The snippet is rendered from the plausible/plausible.html template. To fully customise it, shadow that path in your own project's templates directory.

Usage with Wagtail

django-plausible-iplweb also provides an optional Wagtail integration that lets editors set the script URL per site through the Wagtail admin. Add plausible.contrib.wagtail to INSTALLED_APPS and run migrate.

Configuration is done through the "Plausible Analytics" setting:

  • script_url: the full per-site Plausible script URL. Blank (the default) → nothing is rendered for that site.

Masking (PLAUSIBLE_URL_MASKS), init options, and query-string handling are still configured globally via the Django settings above.

Load plausible_wagtail rather than plausible; the tag itself is still plausible:

{% load plausible_wagtail %}

{% plausible %}

Migrating from 0.5.x

Version 0.6.0 is a clean break to match Plausible's new tracker. Update your settings:

Old (0.5.x) New (0.6.0)
PLAUSIBLE_DOMAIN + PLAUSIBLE_SCRIPT_NAME PLAUSIBLE_SCRIPT_URL (the full script URL from your dashboard)
data-domain (request host / site_domain) (gone — the site is identified by the script URL)
{% plausible site_domain=… plausible_domain=… script_name=… %} {% plausible %} (optionally script_url=…)
Wagtail site_domain / plausible_domain / script_name fields Wagtail script_url field (migration 0002 applies the change)

License

This project is licensed under the BSD 3-Clause License. See the LICENSE file for details.

This is a fork of django-plausible by Jake Howard.

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_iplweb-0.6.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_plausible_iplweb-0.6.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file django_plausible_iplweb-0.6.0.tar.gz.

File metadata

  • Download URL: django_plausible_iplweb-0.6.0.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_plausible_iplweb-0.6.0.tar.gz
Algorithm Hash digest
SHA256 0368b33f415b2aa5fabc12eea70fe37120c8826efad5a90bd72bc763c36f57c6
MD5 4ecabd81fd9b8354bf79984d3a93d195
BLAKE2b-256 0510c9049c2e069fa6aca5c52531617a0368b4d1246a930d8b3e7b5eaababf63

See more details on using hashes here.

File details

Details for the file django_plausible_iplweb-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: django_plausible_iplweb-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_plausible_iplweb-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f65a5ba86e4e9c33208c488044485ae3da174f196fc52ea7659a0b859f860e8
MD5 f0aca15edc75f8377016f263dc720319
BLAKE2b-256 45febd90c23d268fe56ef8b29f1e7a2b52725a94a4718bf22349fdb727c94c26

See more details on using hashes here.

Supported by

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