Skip to main content

电子邮件打开和点击跟踪

项目描述

作者

强力去

版本
0.2.2

该库提供了一组函数,可在发送电子邮件时提供打开和点击跟踪。如果您依赖不提供打开和点击跟踪的电子邮件服务提供商 (ESP),这将特别有用。

该库仅提供构建块,不处理电子邮件的实际发送或跟踪像素和链接的服务,但它非常接近这一点。

https://github.com/powergo/pytracking/actions/workflows/test.yml/badge.svg https://img.shields.io/pypi/v/pytracking.svg https://img.shields.io/pypi/l/pytracking.svg https://img.shields.io/pypi/pyversions/pytracking.svg <nav class="contents local" id="summary" role="doc-toc">

概括

</nav>

概述

跟踪电子邮件打开和链接点击有两个主要步骤:

1. 在邮件中添加追踪信息

要跟踪电子邮件的打开情况,普遍接受的策略是在电子邮件末尾添加一个小的 1x1 透明像素。当用户打开电子邮件时,电子邮件客户端(例如,gmail、outlook、thunderbird)将通过发出 GET 请求来加载像素。然后,服务请求的 Web 服务器将记录打开并通知电子邮件的发件人。

要跟踪链接点击,普遍接受的策略是重写电子邮件中的链接以将目的地更改为代理。一旦用户点击链接,代理将用户重定向到真实链接并通知发件人电子邮件。

pytracking 提供了一种无状态策略来打开和点击跟踪:您要跟踪的所有信息都编码在像素(打开)和代理(点击)URL 中。例如,如果您想跟踪与特定电子邮件关联的客户 ID 和交易 ID,pytracking 将在 URL 中编码此信息。当用户打开电子邮件或单击链接时,客户 ID 和交易 ID 将被解码,然后可以发送到 Webhook。

有关快速示例,请参阅获取打开的跟踪链接部分。

pytracking 提供的可选主要功能

  1. 加密:pytracking 使用 base 64 对您的跟踪信息进行编码,任何人都可以对其进行解码。您可以选择加密您的跟踪信息,只有在您拥有密钥的情况下才能对其进行解码。有关详细信息,请参阅 加密数据部分。

  2. HTML 修改:pytracking 可以修改 HTML 电子邮件以替换所有链接并添加跟踪像素。请参阅修改 HTML 电子邮件以添加跟踪链接部分。

  3. Django:如果你使用 Django 来提供打开和点击跟踪 URL,你可以扩展 pytracking Django 视图,它已经提供了重定向和像素服务。请参阅将pytracking 与 Django 一起使用部分。

  4. Webhook:pytracking 提供了一个快捷功能来向 webhook 发出 POST 请求。请参阅通知 Webhook部分。

要求

pytracking 适用于 Python 3.6+。它不需要任何外部库,但有许多具有依赖关系的可选功能。

安装

您可以使用 pip 安装 pytracking:

pip install pytracking

您可以安装带有附加功能的特定功能:

pip install pytracking[django,crypto]

您还可以安装所有功能:

pip install pytracking[all]

基本库使用

您可以使用 pytracking 生成两种跟踪链接:指向透明跟踪像素的链接和重定向到另一个链接的链接。

编码

您可以对这两种链接中的元数据进行编码。例如,您可以将客户 ID 与点击跟踪链接相关联,这样当客户点击该链接时,您就可以确切地知道是哪个客户点击了该链接。

pylinktracking 实现了一种无状态的跟踪策略:所有必要的信息都可以编码在跟踪链接中。您可以选择将通用设置(例如,与所有链接关联的默认元数据、webhook URL)保留在单独的配置中。

该信息使用 url-safe base64 编码,因此任何拦截您的链接的人,包括您的客户,都可能解码该信息。您可以选择加密跟踪信息(见下文)。

大多数函数将pytracking.Configuration实例作为参数,该 实例告诉如何生成链接。您还可以将配置参数作为**kwargs参数传递,或者可以混合使用两者:kwargs 将覆盖配置参数。

解码

一旦您从跟踪链接获得请求,您可以使用 pytracking 对链接进行解码并获取pytracking.TrackingResult实例,其中包含要重定向到的链接(如果是点击跟踪链接)、相关元数据、要通知的 webhook URL 等。

基本库示例

从 URL 获取打开的跟踪数据

import pytracking

full_url = "https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda="
tracking_result = pytracking.get_open_tracking_result(
    full_url, base_open_tracking_url="https://trackingdomain.com/path/")

# Metadata is in tracking_result.metadata
# Webhook URL is in tracking_result.webhook_url

从 URL 获取点击跟踪数据

import pytracking

full_url = "https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda="
tracking_result = pytracking.get_click_tracking_result(
    full_url, base_click_tracking_url="https://trackingdomain.com/path/")

# Metadata is in tracking_result.metadata
# Webhook URL is in tracking_result.webhook_url
# Tracked URL to redirect to is in tracking_result.tracked_url

获取 1x1 透明 PNG 像素

import pytracking

(pixel_byte_string, mime_type) = pytracking.get_open_tracking_pixel()

加密数据

您可以加密您的编码数据,以防止第三方访问您链接中编码的跟踪数据。

要使用加密功能,您必须使用 pytracking[crypto]安装 pytracking ,它使用密码学 Python 库

加密您的数据会稍微增加生成的 URL 的长度。

import pytracking
from cryptography.fernet import Fernet

key = Fernet.generate_key()

# Encode
click_tracking_url = pytracking.get_click_tracking_url(
    "http://www.example.com/?query=value", {"customer_id": 1},
    base_click_tracking_url="https://trackingdomain.com/path/",
    webhook_url="http://requestb.in/123", include_webhook_url=True,
    encryption_bytestring_key=key)

# Decode
tracking_result = pytracking.get_open_tracking_result(
    full_url, base_click_tracking_url="https://trackingdomain.com/path/",
    encryption_bytestring_key=key)

在 Django 中使用 pytracking

pytracking 带有 View 类,您可以扩展这些类并处理打开和点击跟踪链接请求。

例如,pytracking.django.OpenTrackingView将为 GET 请求返回一个 1x1 透明 PNG 像素。pytracking.django.ClickTrackingView 返回一个 302 重定向响应到被跟踪的 URL。

如果跟踪 URL 无效,两个视图都将返回 404 响应。两个视图都将捕获请求的用户代理和用户 ip。此信息将在 TrackingResult.request_data 中提供。

您可以扩展这两个视图以确定如何处理跟踪结果(例如,调用 webhook 或将任务提交到 celery 队列)。最后,您可以在 Django 设置中对配置参数进行编码,也可以在视图中计算它们。

要使用 django 功能,您必须使用 pytracking[django]安装 pytracking 。

Django 设置中的配置参数

您可以通过在设置文件中添加此键来在 Django 设置中提供默认配置参数:

PYTRACKING_CONFIGURATION = {
    "webhook_url": "http://requestb.in/123",
    "base_open_tracking_url": "http://tracking.domain.com/open/",
    "base_click_tracking_url": "http://tracking.domain.com/click/",
    "default_metadata": {"analytics_key": "123456"},
    "append_slash": True
}

扩展默认视图

from pytracking import Configuration
from pytracking.django import OpenTrackingView, ClickTrackingView

class MyOpenTrackingView(OpenTrackingView):

    def notify_tracking_event(self, tracking_result):
        # Override this method to do something with the tracking result.
        # tracking_result.request_data["user_agent"] and
        # tracking_result.request_data["user_ip"] contains the user agent
        # and ip of the client.
        send_tracking_result_to_queue(tracking_result)

    def notify_decoding_error(self, exception, request):
        # Called when the tracking link cannot be decoded
        # Override this to, for example, log the exception
        logger.log(exception)

    def get_configuration(self):
        # By defaut, fetchs the configuration parameters from the Django
        # settings. You can return your own Configuration object here if
        # you do not want to use Django settings.
        return Configuration()


class MyClickTrackingView(ClickTrackingView):

    def notify_tracking_event(self, tracking_result):
        # Override this method to do something with the tracking result.
        # tracking_result.request_data["user_agent"] and
        # tracking_result.request_data["user_ip"] contains the user agent
        # and ip of the client.
        send_tracking_result_to_queue(tracking_result)

    def notify_decoding_error(self, exception, request):
        # Called when the tracking link cannot be decoded
        # Override this to, for example, log the exception
        logger.log(exception)

    def get_configuration(self):
        # By defaut, fetchs the configuration parameters from the Django
        # settings. You can return your own Configuration object here if
        # you do not want to use Django settings.
        return Configuration()

网址配置

将此添加到您的 urls.py 文件中:

urlpatterns = [
    url(
        "^open/(?P<path>[\w=-]+)/$", MyOpenTrackingView.as_view(),
        name="open_tracking"),
    url(
        "^click/(?P<path>[\w=-]+)/$", MyClickTrackingView.as_view(),
        name="click_tracking"),
]

通知 Webhook

您可以使用跟踪结果向 webhook 发送 POST 请求。webhook 功能只是将跟踪结果打包为 POST 正文中的 json 字符串。它还将内容编码设置为application/json

要使用 webhook 功能,您必须使用 pytracking[webhook]安装 pytracking 。

import pytracking
from pytracking.webhook import send_webhook

# Assumes that the webhook url is encoded in the url.
full_url = "https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda="
tracking_result = pytracking.get_open_tracking_result(
    full_url, base_click_tracking_url="https://trackingdomain.com/path/")

# Will send a POST request with the following json str body:
#  {
#    "is_open_tracking": False,
#    "is_click_tracking": True,
#    "metadata": {...},
#    "request_data": None,
#    "tracked_url": "http://...",
#    "timestamp": 1389177318
#  }
send_webhook(tracking_result)

测试 pytracking

pytracking 使用toxpy.test。如果您安装了 tox,只需运行 tox,所有可能的 pytracking 配置都将在 Python 3.6-3.9 上进行测试。

去做

  1. 添加各种检查以确保输入数据是健全的并且不会破坏任何已知的限制(例如,URL 长度)。

  2. 添加更多示例。

  3. 允许每个跟踪方法使用多个 webhook 和 webhook。

  4. 将 Django 视图转换为视图混合。

  5. 添加选项以在跟踪 URL 中对 webhook 超时进行编码。

  6. 使用 pytracking.html 的文档注意事项(例如:长电子邮件通常会被电子邮件客户端截断,因此不会加载跟踪像素)。

  7. 添加某种形式的 API 文档(至少是 Configuration 和 TrackingResult),可能作为单独的文档。

执照

该软件根据新 BSD 许可证获得许可。有关完整的许可证文本,请参阅存储库中的LICENSE文件。

项目详情


下载文件

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

源分布

pytracking-0.2.2.tar.gz (15.2 kB 查看哈希

已上传 source

内置分布

pytracking-0.2.2-py2.py3-none-any.whl (14.0 kB 查看哈希

已上传 py2 py3