跳转到主要内容

电子邮件打开和点击跟踪

项目描述

关于此分支

此仓库是 powergo/pytracking 的分支,因为它已不再维护。它使用了 QueraTeam/pytracking 中的代码,该代码对代码进行了修改,使其与 Python 3.5-3.10 和 Django 1.11-4.0 兼容。此分支进一步进行了更改,并在 PyPI 上以 pytracking2 的名称发布。

测试在 Python 3.7-3.10 和 Django 3.2, 4.0, 4.1 上运行。

https://img.shields.io/badge/code%20style-black-000000.svg

pytracking - 电子邮件打开和点击跟踪库

作者:

Resulto Developpement Web Inc.,QueraTeam,Mikuláš Poul

版本:

0.4.5

此库提供了一组函数,用于在发送电子邮件时提供打开和点击跟踪。如果您依赖于不提供打开和点击跟踪的电子邮件服务提供商(ESP)如 Amazon SES 或 PostmarkApp,这尤其有用。

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

https://img.shields.io/pypi/v/pytracking2.svg https://img.shields.io/pypi/l/pytracking2.svg https://img.shields.io/pypi/pyversions/pytracking2.svg

概述

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

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视图,它已提供了重定向和像素服务。请参阅与Django一起使用pytracking部分。

  4. Webhooks:pytracking提供了一个快捷函数来向webhook发送POST请求。请参阅通知Webhooks部分。

要求

pytracking与Python 3.4+兼容。它不需要任何外部库,但有许多具有依赖关系的可选功能。

安装

您可以使用pip安装pytracking

pip install pytracking2

您可以使用extra安装特定功能

pip install pytracking2[django,crypto]

您还可以安装所有功能

pip install pytracking2[all]

基本库使用

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

编码

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

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

信息使用URL安全的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,它使用了cryptography 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将为跟踪URL返回一个302重定向响应。

如果跟踪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"}
}

扩展默认视图

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()

URL配置

将此添加到您的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"),
]

通知Webhooks

您可以将跟踪结果作为POST请求发送到webhook。webhook功能将跟踪结果作为JSON字符串打包在POST体中。它还将内容编码设置为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.4进行测试。

待办事项

  1. 添加各种检查以确保输入数据合理,并且不会超出任何已知限制(例如,URL长度)。

  2. 添加更多示例。

  3. 允许多个webhook以及每种跟踪方法下的webhook。

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

  5. 添加选项以将webhook超时编码到跟踪URL中。

  6. 记录使用pytracking.html的注意事项(例如:长邮件常被邮件客户端截断,因此跟踪像素无法加载)。

  7. 添加一些形式的API文档(至少包括配置和TrackingResult),可能作为单独的文档。

许可

本软件根据《新BSD许可证》授权。有关完整的许可证文本,请参阅存储库中的《LICENSE》文件。

项目详情


下载文件

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

源分发

pytracking2-0.4.6.tar.gz (20.3 kB 查看哈希值)

上传时间

构建分发

pytracking2-0.4.6-py3-none-any.whl (13.7 kB 查看哈希值)

上传时间 Python 3

由以下组织支持

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