跳转到主要内容

Django模块,用于将请求代理到Plausible Analytics。

项目描述

Dev workflow PyPI PyPI - Python Version PyPI - Status PyPI - License

Django Plausible Proxy

Django应用程序,用于代理请求并将服务器端事件发送到Plausible Analytics。与自托管和托管云服务兼容。

代理

代理允许项目所有者看到更完整的数据视图,即使数据可能丢失。有关问题的详细说明和解决方案,请参阅广告拦截器和使用代理进行数据分析

当在settings.py和urls.py中安装和配置后,该应用程序将按照以下方式代理HTTP请求

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

服务器端事件

跟踪其他方式无法跟踪的服务器端事件,例如API请求。

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

安装

从PyPI安装软件包。

pip install django-plausible-proxy

在settings.py中配置Django设置。

# 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"

# Optionally, define the plausible endpoint that you would like to post to.
# This is useful if you are self-hosting plausible.
PLAUSIBLE_BASE_URL = "https://#"

# Optionally, define the value for the script prefix. The default value is "js". When
# you include the script to the page with the {% plausible %} templatetag, it becomes
# available as "<script src='${PLAUSIBLE_SCRIPT_PREFIX}/script.js'></script>". E.g.,
# "<script src='js/script.js'></script>"
#
# Overriding PLAUSIBLE_SCRIPT_PREFIX is helpful to avoid clashes with another script
# of your site that may become available under the same name.

PLAUSIBLE_SCRIPT_PREFIX = "plsbl/js"

# Optionally, provide a timeout for the connection to your plausible endpoint in
# seconds. Defaults to 1 second. Adjust to lower values in case you can't trust your
# infrastructure to consistently deliver low load times and you don't care as much
# about consistent analytics.
PLAUSIBLE_REQUEST_TIMEOUT = 1

更新urls.py。

from django.urls import include, path

urlpatterns = [
    # ...
    path("", include("plausible_proxy.urls")),
    # ...
]

更新您的基HTML模板以包含plausible模板标签。

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

API参考

{% plausible %}

一个模板标签,用于将Plausible分析脚本包含到页面中。

参数

  • domain(默认为settings.PLAUSIBLE_DOMAIN):定义data-domain参数,即Plausible分析的数据域。
  • script(默认为script.js):定义要使用的Plausible脚本。有关替代脚本名称及其可跟踪内容的列表,请参阅脚本扩展以提高测量

使用示例

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

plausible_proxy.services.send_custom_event()

向Plausible发送自定义事件并返回成功状态。

有关更多信息,请参阅Plausible事件API

参数

  • request (HttpRequest): 原始Django HTTP请求。将用于创建X-Forwarded-For和User-Agent头部。
  • name (字符串): 事件名称。可以指定Plausible中的特殊事件类型pageview。所有其他名称将被视为自定义事件。
  • domain (可选字符串): Plausible中站点的域名。默认情况下使用settings.PLAUSIBLE_DOMAIN的值。
  • url (可选字符串): 触发事件的页面URL。如果没有提供,函数将从请求中提取URL。如果URL包含UTM参数,将提取并存储。如果URL未设置,将从中提取请求。
  • referrer (可选字符串): 此事件的引用者。
  • screen_width (可选整数): 屏幕宽度。
  • props (可选字典): 事件的自定义属性。参见:使用自定义属性

返回值:如果请求成功接受,则为True。

示例

def vote(request, candidate_id):
    candidate = get_object_or_404(Candidate, pk=candidate_id)
    send_custom_event(request, 'vote', props={"candidate": candidate.full_name})
    ...

贡献者

项目详情


下载文件

下载适用于您平台文件的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

django_plausible_proxy-0.5.1.tar.gz (9.9 kB 查看哈希值)

上传时间

构建分布

django_plausible_proxy-0.5.1-py3-none-any.whl (8.8 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面