pytracking2


Namepytracking2 JSON
Version 0.4.6 PyPI version JSON
download
home_pagehttps://github.com/xelixdev/pytracking
SummaryEmail open and click tracking
upload_time2023-09-19 16:54:44
maintainer
docs_urlNone
authorMikuláš Poul
requires_python>=3.8
licenseNew BSD
keywords email open click tracking
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            About this fork
===============

This repo is a fork of `powergo/pytracking` since it's unmaintained, with code used from `QueraTeam/pytracking` which
made changes to make the code compatible with Python 3.5-3.10 and Django 1.11-4.0.
This fork made further changes and gets released on PyPI under the name `pytracking2`.

Tests are run against Python 3.7-3.10 and Django 3.2, 4.0, 4.1.

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
   :target: https://github.com/psf/black

pytracking - Email Open and Click Tracking Library
==================================================

:Authors:
  Resulto Developpement Web Inc., QueraTeam, Mikuláš Poul
:Version: 0.4.5

This library provides a set of functions that provide open and click tracking
when sending emails. This is particularly useful if you rely on an Email
Service Provider (ESP) such as Amazon SES or PostmarkApp that does not provide
open and click tracking.

The library only provides building blocks and does not handle the actual
sending of email or the serving of tracking pixel and links, but it comes
pretty close to this.

.. image:: https://img.shields.io/pypi/v/pytracking2.svg
    :target: https://pypi.python.org/pypi/pytracking2

.. image:: https://img.shields.io/pypi/l/pytracking2.svg

.. image:: https://img.shields.io/pypi/pyversions/pytracking2.svg


.. contents:: Summary
    :backlinks: entry
    :local:


Overview
--------

There are two main steps when tracking email opens and link clicks:

1. Adding tracking information to emails
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To track email opens, the generally accepted strategy is to add a small 1x1
transparent pixel at the end of an email. When a user opens an email, the email
client (e.g., gmail, outlook, thunderbird) will load the pixel by making a GET
request. The web server serving the request will then record the open and
notify the sender of the email.

To track link clicks, the generally accepted strategy is to rewrite links in an
email to change the destination to a proxy. Once a user clicks on the link, the
proxy redirects the user to the real link and notifies the sender of the email.

pytracking provides a stateless strategy to open and click tracking: all the
information you want to track are encoded in the pixel (open) and proxy (click)
URLs. For example, if you want to track the customer id and the transaction id
associated with a particular email, pytracking will encode this information in
the URL. When the user opens the email or clicks on a link, the customer id and
transaction id will be decoded and can then be sent to a webhook.

See the `Get Open Tracking Link`_ section for a quick example.


2. Handling email opens and link clicks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Once a user opens an email or clicks on a link, the email client will send a
request to the encoded URL. Your web server will receive such request and pass
it to pytracking, which will decode the tracking information. You can then use
the tracking information directly (e.g., update your tracking database) or you
can send the information to a webhook.

In the case of link tracking, the decoded information will contain the original
URL that you must redirect the email client to.

See the `Get Open Tracking Data from URL`_ section for a quick example.



Optional Major Features provided by pytracking
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1. Encryption: pytracking uses base 64 to encode your tracking information,
   which can be decoded by anyone. You can optionaly encrypt your tracking
   information, which can only be decoded if you have the key. See the
   `Encrypting Data`_ section for more information.

2. HTML modification: pytracking can modify an HTML email to replace all links
   and add a tracking pixel. See the `Modifying HTML emails to add tracking links`_ section.

3. Django: if you use Django to serve open and click tracking URLs, you can
   extend pytracking Django views, which already provides the redirect and
   pixel serving. See the `Using pytracking with Django`_ section.

4. Webhooks: pytracking offers a shortcut function to make a POST request to a
   webhook. See the `Notifying Webhooks`_ section.


Requirements
------------

pytracking works with Python 3.4+. It doesn't require any external library, but
there are many optional features that have dependencies.


Installation
------------

You can install pytracking using pip:

::

    pip install pytracking2

You can install specific features with extras:

::

    pip install pytracking2[django,crypto]

You can also install all features:

::

    pip install pytracking2[all]



Basic Library Usage
-------------------

You can generate two kinds of tracking links with pytracking: a link to a
transparent tracking pixel and a link that redirects to another link.

Encoding
~~~~~~~~

You can encode metadata in both kinds of links. For example, you can associate
a customer id with a click tracking link so when the customer clicks on the
link, you'll know exactly which customer clicked on it.

pylinktracking implements a stateless tracking strategy: all necessary
information can be encoded in the tracking links. You can optionally keep
common settings (e.g., default metadata to associate with all links, webhook
URL) in a separate configuration.

The information is encoded using url-safe base64 so anyone intercepting your
links, including your customers, could potentially decode the information. You
can optionally encrypt the tracking information (see below).

Most functions take as a parameter a ``pytracking.Configuration``
instance that tells how to generate the links. You can also pass the
configuration parameters as ``**kwargs`` argument or can mix both: the kwargs
will override the configuration parameters.

Decoding
~~~~~~~~

Once you get a request from a tracking link, you can use pytracking to decode
the link and get a ``pytracking.TrackingResult`` instance, which contains
information such as the link to redirect to (if it's a click tracking link),
the associated metadata, the webhook URL to notify, etc.

Basic Library Examples
----------------------

Get Open Tracking Link
~~~~~~~~~~~~~~~~~~~~~~

::

    import pytracking

    open_tracking_url = pytracking.get_open_tracking_url(
        {"customer_id": 1}, base_open_tracking_url="https://trackingdomain.com/path/",
        webhook_url="http://requestb.in/123", include_webhook_url=True)

    # This will produce a URL such as:
    # https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda=


Get Open Tracking Link with Configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

    import pytracking

    configuration = pytracking.Configuration(
        base_open_tracking_url="https://trackingdomain.com/path/",
        webhook_url="http://requestb.in/123",
        include_webhook_url=False)

    open_tracking_url = pytracking.get_open_tracking_url(
        {"customer_id": 1}, configuration=configuration)

    # This will produce a URL such as:
    # https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda=


Get Click Tracking Link
~~~~~~~~~~~~~~~~~~~~~~~

::

    import pytracking

    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)

    # This will produce a URL such as:
    # https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda=


Get Open Tracking Data from 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


Get Click Tracking Data from 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


Get a 1x1 transparent PNG pixel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

    import pytracking

    (pixel_byte_string, mime_type) = pytracking.get_open_tracking_pixel()



Encrypting Data
---------------

You can encrypt your encoded data to prevent third parties from accessing the
tracking data encoded in your link.

To use the encryption feature, you must install pytracking with
``pytracking[crypto]``, which uses the `cryptography Python library
<https://cryptography.io/en/latest/>`_.

Encrypting your data slightly increases the length of the generated 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)


Using pytracking with Django
----------------------------

pytracking comes with View classes that you can extend and that handle open and
click tracking link request.

For example, the ``pytracking.django.OpenTrackingView`` will return a 1x1
transparent PNG pixel for GET requests. The
``pytracking.django.ClickTrackingView`` will return a 302 redirect response to
the tracked URL.

Both views will return a 404 response if the tracking URL is invalid. Both
views will capture the user agent and the user ip of the request. This
information will be available in TrackingResult.request_data.

You can extend both views to determine what to do with the tracking result
(e.g., call a webhook or submit a task to a celery queue). Finally, you can
encode your configuration parameters in your Django settings or you can compute
them in your view.

To use the django feature, you must install pytracking with
``pytracking[django]``.

Configuration parameters in Django settings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can provide default configuration parameters in your Django settings by
adding this key in your settings file:

::

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


Extending default views
~~~~~~~~~~~~~~~~~~~~~~~

::

    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 configuration
~~~~~~~~~~~~~~~~~~

Add this to your urls.py file:

::

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


Notifying Webhooks
------------------

You can send a POST request to a webhook with the tracking result. The webhook
feature just packages the tracking result as a json string in the POST body. It
also sets the content encoding to ``application/json``.

To use the webhook feature, you must install pytracking with
``pytracking[webhook]``.


::

    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)



Modifying HTML emails to add tracking links
-------------------------------------------

If you have an HTML email, pytracking can update all links with tracking links
and it can also add a transparent tracking pixel at the end of your email.

To use the HTML feature, you must install pytracking with ``pytracking[html]``,
which uses the `lxml library <http://lxml.de/>`_.

::

    import pytracking
    from pytracking.html import adapt_html

    html_email_text = "..."
    new_html_email_text = adapt_html(
        html_email_text, extra_metadata={"customer_id": 1},
        click_tracking=True, open_tracking=True)


Testing pytracking
------------------

pytracking uses `tox <https://tox.readthedocs.io/en/latest/>`_ and `py.test
<http://docs.pytest.org/en/latest/>`_. If you have tox installed, just run
``tox`` and all possible configurations of pytracking will be tested on Python
3.4.


TODO
----

1. Add various checks to ensure that the input data is sane and does not bust
   any known limits (e.g., URL length).

2. Add more examples.

3. Allow mulitple webhooks and webhooks per tracking method.

4. Transform Django views into view mixins.

5. Add option to encode the webhook timeout in the tracking URL.

6. Document caveats of using pytracking.html (example: long emails are often
   cut off by the email clients and the tracking pixel is thus not loaded).

7. Add some form of API documentation (at least Configuration and
   TrackingResult), maybe as a separate document.

License
-------

This software is licensed under the `New BSD License`. See the `LICENSE` file
in the repository for the full license text.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/xelixdev/pytracking",
    "name": "pytracking2",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "email open click tracking",
    "author": "Mikul\u00e1\u0161 Poul",
    "author_email": "mikulas.poul@xelix.com",
    "download_url": "https://files.pythonhosted.org/packages/b1/b3/cb0bf0226f88d139fbded91de3a2e23d6dcc73aa1827de35c809d845ebd2/pytracking2-0.4.6.tar.gz",
    "platform": null,
    "description": "About this fork\n===============\n\nThis repo is a fork of `powergo/pytracking` since it's unmaintained, with code used from `QueraTeam/pytracking` which\nmade changes to make the code compatible with Python 3.5-3.10 and Django 1.11-4.0.\nThis fork made further changes and gets released on PyPI under the name `pytracking2`.\n\nTests are run against Python 3.7-3.10 and Django 3.2, 4.0, 4.1.\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n   :target: https://github.com/psf/black\n\npytracking - Email Open and Click Tracking Library\n==================================================\n\n:Authors:\n  Resulto Developpement Web Inc., QueraTeam, Mikul\u00e1\u0161 Poul\n:Version: 0.4.5\n\nThis library provides a set of functions that provide open and click tracking\nwhen sending emails. This is particularly useful if you rely on an Email\nService Provider (ESP) such as Amazon SES or PostmarkApp that does not provide\nopen and click tracking.\n\nThe library only provides building blocks and does not handle the actual\nsending of email or the serving of tracking pixel and links, but it comes\npretty close to this.\n\n.. image:: https://img.shields.io/pypi/v/pytracking2.svg\n    :target: https://pypi.python.org/pypi/pytracking2\n\n.. image:: https://img.shields.io/pypi/l/pytracking2.svg\n\n.. image:: https://img.shields.io/pypi/pyversions/pytracking2.svg\n\n\n.. contents:: Summary\n    :backlinks: entry\n    :local:\n\n\nOverview\n--------\n\nThere are two main steps when tracking email opens and link clicks:\n\n1. Adding tracking information to emails\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nTo track email opens, the generally accepted strategy is to add a small 1x1\ntransparent pixel at the end of an email. When a user opens an email, the email\nclient (e.g., gmail, outlook, thunderbird) will load the pixel by making a GET\nrequest. The web server serving the request will then record the open and\nnotify the sender of the email.\n\nTo track link clicks, the generally accepted strategy is to rewrite links in an\nemail to change the destination to a proxy. Once a user clicks on the link, the\nproxy redirects the user to the real link and notifies the sender of the email.\n\npytracking provides a stateless strategy to open and click tracking: all the\ninformation you want to track are encoded in the pixel (open) and proxy (click)\nURLs. For example, if you want to track the customer id and the transaction id\nassociated with a particular email, pytracking will encode this information in\nthe URL. When the user opens the email or clicks on a link, the customer id and\ntransaction id will be decoded and can then be sent to a webhook.\n\nSee the `Get Open Tracking Link`_ section for a quick example.\n\n\n2. Handling email opens and link clicks\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nOnce a user opens an email or clicks on a link, the email client will send a\nrequest to the encoded URL. Your web server will receive such request and pass\nit to pytracking, which will decode the tracking information. You can then use\nthe tracking information directly (e.g., update your tracking database) or you\ncan send the information to a webhook.\n\nIn the case of link tracking, the decoded information will contain the original\nURL that you must redirect the email client to.\n\nSee the `Get Open Tracking Data from URL`_ section for a quick example.\n\n\n\nOptional Major Features provided by pytracking\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n1. Encryption: pytracking uses base 64 to encode your tracking information,\n   which can be decoded by anyone. You can optionaly encrypt your tracking\n   information, which can only be decoded if you have the key. See the\n   `Encrypting Data`_ section for more information.\n\n2. HTML modification: pytracking can modify an HTML email to replace all links\n   and add a tracking pixel. See the `Modifying HTML emails to add tracking links`_ section.\n\n3. Django: if you use Django to serve open and click tracking URLs, you can\n   extend pytracking Django views, which already provides the redirect and\n   pixel serving. See the `Using pytracking with Django`_ section.\n\n4. Webhooks: pytracking offers a shortcut function to make a POST request to a\n   webhook. See the `Notifying Webhooks`_ section.\n\n\nRequirements\n------------\n\npytracking works with Python 3.4+. It doesn't require any external library, but\nthere are many optional features that have dependencies.\n\n\nInstallation\n------------\n\nYou can install pytracking using pip:\n\n::\n\n    pip install pytracking2\n\nYou can install specific features with extras:\n\n::\n\n    pip install pytracking2[django,crypto]\n\nYou can also install all features:\n\n::\n\n    pip install pytracking2[all]\n\n\n\nBasic Library Usage\n-------------------\n\nYou can generate two kinds of tracking links with pytracking: a link to a\ntransparent tracking pixel and a link that redirects to another link.\n\nEncoding\n~~~~~~~~\n\nYou can encode metadata in both kinds of links. For example, you can associate\na customer id with a click tracking link so when the customer clicks on the\nlink, you'll know exactly which customer clicked on it.\n\npylinktracking implements a stateless tracking strategy: all necessary\ninformation can be encoded in the tracking links. You can optionally keep\ncommon settings (e.g., default metadata to associate with all links, webhook\nURL) in a separate configuration.\n\nThe information is encoded using url-safe base64 so anyone intercepting your\nlinks, including your customers, could potentially decode the information. You\ncan optionally encrypt the tracking information (see below).\n\nMost functions take as a parameter a ``pytracking.Configuration``\ninstance that tells how to generate the links. You can also pass the\nconfiguration parameters as ``**kwargs`` argument or can mix both: the kwargs\nwill override the configuration parameters.\n\nDecoding\n~~~~~~~~\n\nOnce you get a request from a tracking link, you can use pytracking to decode\nthe link and get a ``pytracking.TrackingResult`` instance, which contains\ninformation such as the link to redirect to (if it's a click tracking link),\nthe associated metadata, the webhook URL to notify, etc.\n\nBasic Library Examples\n----------------------\n\nGet Open Tracking Link\n~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n    import pytracking\n\n    open_tracking_url = pytracking.get_open_tracking_url(\n        {\"customer_id\": 1}, base_open_tracking_url=\"https://trackingdomain.com/path/\",\n        webhook_url=\"http://requestb.in/123\", include_webhook_url=True)\n\n    # This will produce a URL such as:\n    # https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda=\n\n\nGet Open Tracking Link with Configuration\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n    import pytracking\n\n    configuration = pytracking.Configuration(\n        base_open_tracking_url=\"https://trackingdomain.com/path/\",\n        webhook_url=\"http://requestb.in/123\",\n        include_webhook_url=False)\n\n    open_tracking_url = pytracking.get_open_tracking_url(\n        {\"customer_id\": 1}, configuration=configuration)\n\n    # This will produce a URL such as:\n    # https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda=\n\n\nGet Click Tracking Link\n~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n    import pytracking\n\n    click_tracking_url = pytracking.get_click_tracking_url(\n        \"http://www.example.com/?query=value\", {\"customer_id\": 1},\n        base_click_tracking_url=\"https://trackingdomain.com/path/\",\n        webhook_url=\"http://requestb.in/123\", include_webhook_url=True)\n\n    # This will produce a URL such as:\n    # https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda=\n\n\nGet Open Tracking Data from URL\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n    import pytracking\n\n    full_url = \"https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda=\"\n    tracking_result = pytracking.get_open_tracking_result(\n        full_url, base_open_tracking_url=\"https://trackingdomain.com/path/\")\n\n    # Metadata is in tracking_result.metadata\n    # Webhook URL is in tracking_result.webhook_url\n\n\nGet Click Tracking Data from URL\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n    import pytracking\n\n    full_url = \"https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda=\"\n    tracking_result = pytracking.get_click_tracking_result(\n        full_url, base_click_tracking_url=\"https://trackingdomain.com/path/\")\n\n    # Metadata is in tracking_result.metadata\n    # Webhook URL is in tracking_result.webhook_url\n    # Tracked URL to redirect to is in tracking_result.tracked_url\n\n\nGet a 1x1 transparent PNG pixel\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n    import pytracking\n\n    (pixel_byte_string, mime_type) = pytracking.get_open_tracking_pixel()\n\n\n\nEncrypting Data\n---------------\n\nYou can encrypt your encoded data to prevent third parties from accessing the\ntracking data encoded in your link.\n\nTo use the encryption feature, you must install pytracking with\n``pytracking[crypto]``, which uses the `cryptography Python library\n<https://cryptography.io/en/latest/>`_.\n\nEncrypting your data slightly increases the length of the generated URL.\n\n::\n\n    import pytracking\n    from cryptography.fernet import Fernet\n\n    key = Fernet.generate_key()\n\n    # Encode\n    click_tracking_url = pytracking.get_click_tracking_url(\n        \"http://www.example.com/?query=value\", {\"customer_id\": 1},\n        base_click_tracking_url=\"https://trackingdomain.com/path/\",\n        webhook_url=\"http://requestb.in/123\", include_webhook_url=True,\n        encryption_bytestring_key=key)\n\n    # Decode\n    tracking_result = pytracking.get_open_tracking_result(\n        full_url, base_click_tracking_url=\"https://trackingdomain.com/path/\",\n        encryption_bytestring_key=key)\n\n\nUsing pytracking with Django\n----------------------------\n\npytracking comes with View classes that you can extend and that handle open and\nclick tracking link request.\n\nFor example, the ``pytracking.django.OpenTrackingView`` will return a 1x1\ntransparent PNG pixel for GET requests. The\n``pytracking.django.ClickTrackingView`` will return a 302 redirect response to\nthe tracked URL.\n\nBoth views will return a 404 response if the tracking URL is invalid. Both\nviews will capture the user agent and the user ip of the request. This\ninformation will be available in TrackingResult.request_data.\n\nYou can extend both views to determine what to do with the tracking result\n(e.g., call a webhook or submit a task to a celery queue). Finally, you can\nencode your configuration parameters in your Django settings or you can compute\nthem in your view.\n\nTo use the django feature, you must install pytracking with\n``pytracking[django]``.\n\nConfiguration parameters in Django settings\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nYou can provide default configuration parameters in your Django settings by\nadding this key in your settings file:\n\n::\n\n    PYTRACKING_CONFIGURATION = {\n        \"webhook_url\": \"http://requestb.in/123\",\n        \"base_open_tracking_url\": \"http://tracking.domain.com/open/\",\n        \"base_click_tracking_url\": \"http://tracking.domain.com/click/\",\n        \"default_metadata\": {\"analytics_key\": \"123456\"}\n    }\n\n\nExtending default views\n~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n    from pytracking import Configuration\n    from pytracking.django import OpenTrackingView, ClickTrackingView\n\n    class MyOpenTrackingView(OpenTrackingView):\n\n        def notify_tracking_event(self, tracking_result):\n            # Override this method to do something with the tracking result.\n            # tracking_result.request_data[\"user_agent\"] and\n            # tracking_result.request_data[\"user_ip\"] contains the user agent\n            # and ip of the client.\n            send_tracking_result_to_queue(tracking_result)\n\n        def notify_decoding_error(self, exception, request):\n            # Called when the tracking link cannot be decoded\n            # Override this to, for example, log the exception\n            logger.log(exception)\n\n        def get_configuration(self):\n            # By defaut, fetchs the configuration parameters from the Django\n            # settings. You can return your own Configuration object here if\n            # you do not want to use Django settings.\n            return Configuration()\n\n\n    class MyClickTrackingView(ClickTrackingView):\n\n        def notify_tracking_event(self, tracking_result):\n            # Override this method to do something with the tracking result.\n            # tracking_result.request_data[\"user_agent\"] and\n            # tracking_result.request_data[\"user_ip\"] contains the user agent\n            # and ip of the client.\n            send_tracking_result_to_queue(tracking_result)\n\n        def notify_decoding_error(self, exception, request):\n            # Called when the tracking link cannot be decoded\n            # Override this to, for example, log the exception\n            logger.log(exception)\n\n        def get_configuration(self):\n            # By defaut, fetchs the configuration parameters from the Django\n            # settings. You can return your own Configuration object here if\n            # you do not want to use Django settings.\n            return Configuration()\n\nURLs configuration\n~~~~~~~~~~~~~~~~~~\n\nAdd this to your urls.py file:\n\n::\n\n    urlpatterns = [\n        url(\n            \"^open/(?P<path>[\\w=-]+)/$\", MyOpenTrackingView.as_view(),\n            name=\"open_tracking\"),\n        url(\n            \"^click/(?P<path>[\\w=-]+)/$\", MyClickTrackingView.as_view(),\n            name=\"click_tracking\"),\n    ]\n\n\nNotifying Webhooks\n------------------\n\nYou can send a POST request to a webhook with the tracking result. The webhook\nfeature just packages the tracking result as a json string in the POST body. It\nalso sets the content encoding to ``application/json``.\n\nTo use the webhook feature, you must install pytracking with\n``pytracking[webhook]``.\n\n\n::\n\n    import pytracking\n    from pytracking.webhook import send_webhook\n\n    # Assumes that the webhook url is encoded in the url.\n    full_url = \"https://trackingdomain.com/path/e30203jhd9239754jh21387293jhf989sda=\"\n    tracking_result = pytracking.get_open_tracking_result(\n        full_url, base_click_tracking_url=\"https://trackingdomain.com/path/\")\n\n    # Will send a POST request with the following json str body:\n    #  {\n    #    \"is_open_tracking\": False,\n    #    \"is_click_tracking\": True,\n    #    \"metadata\": {...},\n    #    \"request_data\": None,\n    #    \"tracked_url\": \"http://...\",\n    #    \"timestamp\": 1389177318\n    #  }\n    send_webhook(tracking_result)\n\n\n\nModifying HTML emails to add tracking links\n-------------------------------------------\n\nIf you have an HTML email, pytracking can update all links with tracking links\nand it can also add a transparent tracking pixel at the end of your email.\n\nTo use the HTML feature, you must install pytracking with ``pytracking[html]``,\nwhich uses the `lxml library <http://lxml.de/>`_.\n\n::\n\n    import pytracking\n    from pytracking.html import adapt_html\n\n    html_email_text = \"...\"\n    new_html_email_text = adapt_html(\n        html_email_text, extra_metadata={\"customer_id\": 1},\n        click_tracking=True, open_tracking=True)\n\n\nTesting pytracking\n------------------\n\npytracking uses `tox <https://tox.readthedocs.io/en/latest/>`_ and `py.test\n<http://docs.pytest.org/en/latest/>`_. If you have tox installed, just run\n``tox`` and all possible configurations of pytracking will be tested on Python\n3.4.\n\n\nTODO\n----\n\n1. Add various checks to ensure that the input data is sane and does not bust\n   any known limits (e.g., URL length).\n\n2. Add more examples.\n\n3. Allow mulitple webhooks and webhooks per tracking method.\n\n4. Transform Django views into view mixins.\n\n5. Add option to encode the webhook timeout in the tracking URL.\n\n6. Document caveats of using pytracking.html (example: long emails are often\n   cut off by the email clients and the tracking pixel is thus not loaded).\n\n7. Add some form of API documentation (at least Configuration and\n   TrackingResult), maybe as a separate document.\n\nLicense\n-------\n\nThis software is licensed under the `New BSD License`. See the `LICENSE` file\nin the repository for the full license text.\n\n\n",
    "bugtrack_url": null,
    "license": "New BSD",
    "summary": "Email open and click tracking",
    "version": "0.4.6",
    "project_urls": {
        "Homepage": "https://github.com/xelixdev/pytracking"
    },
    "split_keywords": [
        "email",
        "open",
        "click",
        "tracking"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ee97fa5f11aeb87c52b920e9cc159a571f37d06401568ebe98e26e977d9e2eb8",
                "md5": "52a95dbea91012e89e90b4d093da4bd7",
                "sha256": "42821c25de2ef077b5e326620cfe811a7ac55daebe5f5c164a43cb2bbf71934c"
            },
            "downloads": -1,
            "filename": "pytracking2-0.4.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "52a95dbea91012e89e90b4d093da4bd7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 13701,
            "upload_time": "2023-09-19T16:54:43",
            "upload_time_iso_8601": "2023-09-19T16:54:43.182336Z",
            "url": "https://files.pythonhosted.org/packages/ee/97/fa5f11aeb87c52b920e9cc159a571f37d06401568ebe98e26e977d9e2eb8/pytracking2-0.4.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b1b3cb0bf0226f88d139fbded91de3a2e23d6dcc73aa1827de35c809d845ebd2",
                "md5": "e025dcb807fa9bd46095d5c65b26fb51",
                "sha256": "8e39db6c9b7f457228dfb30373ba2f222cbae7518af662bf52e323369ce0d373"
            },
            "downloads": -1,
            "filename": "pytracking2-0.4.6.tar.gz",
            "has_sig": false,
            "md5_digest": "e025dcb807fa9bd46095d5c65b26fb51",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 20343,
            "upload_time": "2023-09-19T16:54:44",
            "upload_time_iso_8601": "2023-09-19T16:54:44.709358Z",
            "url": "https://files.pythonhosted.org/packages/b1/b3/cb0bf0226f88d139fbded91de3a2e23d6dcc73aa1827de35c809d845ebd2/pytracking2-0.4.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-19 16:54:44",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "xelixdev",
    "github_project": "pytracking",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pytracking2"
}
        
Elapsed time: 0.11992s