Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
.tox/
dist/
cache/
docs/_build/
venv/
32 changes: 32 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the OS, Python version and other tools you might need
build:
os: ubuntu-22.04
tools:
python: "3.12"
# You can also specify other tool versions:
# nodejs: "19"
# rust: "1.64"
# golang: "1.19"

# Build documentation in the "docs/" directory with Sphinx
sphinx:
configuration: docs/conf.py

# Optionally build your docs in additional formats such as PDF and ePub
# formats:
# - pdf
# - epub

# Optional but recommended, declare the Python requirements required
# to build your documentation
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
python:
install:
- requirements: docs/requirements.txt
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog - pytracking
======================

0.2.4 - September 19th 2024
--------------------------

- Added pixel_position configuration parameter to specify where to insert the tracking pixel in the HTML
- Do not replace href in <a> tags that are already tracking URLs

0.2.3 - November 24th 2022
--------------------------

Expand Down
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
5 changes: 5 additions & 0 deletions docs/authors.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=======
Credits
=======

.. include:: ../AUTHORS.txt
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. include:: ../CHANGELOG.rst
44 changes: 44 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'pytracking'
copyright = '2024, Power Go'
author = 'Power Go'

import re
with open('../setup.py', 'r') as f:
setup_content = f.read()
version_match = re.search(r"version\s*=\s*['\"]([^'\"]+)['\"]", setup_content)
if version_match:
version = release = version_match.group(1)
else:
version = release = 'unknown'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
"myst_parser",
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
"sphinx.ext.viewcode",
]

autoclass_content = "init"


templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']



# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'sphinx_rtd_theme'
html_static_path = ['_static']
3 changes: 3 additions & 0 deletions docs/ex/django.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
========
Django
========
54 changes: 54 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
.. pytracking documentation master file, created by
sphinx-quickstart on Mon Sep 9 15:34:05 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

Welcome to pytracking's documentation!
======================================

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) which 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://github.com/powergo/pytracking/actions/workflows/test.yml/badge.svg
:target: https://github.com/powergo/pytracking/actions/workflows/test.yml

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

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

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


Further reading
~~~~~~~~~~~~~~~

.. toctree::
:maxdepth: 1
:caption: Package documentation:

usage/installation
usage/api
usage/quickstart
usage/configuration
usage/advanced
changelog
authors

.. toctree::
:maxdepth: 1
:caption: Examples documentation:

ex/django

Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
6 changes: 6 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sphinx
myst-parser
sphinx_rtd_theme>=0.5.0
# install pytracking from pip for autodoc
django
-e .[all]
7 changes: 7 additions & 0 deletions docs/usage/advanced.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Advanced Usage

## Custom Webhook Handlers

You can provide your own webhook handler to process the webhook data.

To do this, you can subclass the `BaseWebhookHandler` class and implement the `handle_webhook` method.
29 changes: 29 additions & 0 deletions docs/usage/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# API

## pytracking

```{eval-rst}
.. automodule:: pytracking
:members:
```

## pytracking.html

```{eval-rst}
.. automodule:: pytracking.html
:members:
```

## pytracking.webhook

```{eval-rst}
.. automodule:: pytracking.webhook
:members:
```

## pytracking.django

```{eval-rst}
.. automodule:: pytracking.django
:members:
```
112 changes: 112 additions & 0 deletions docs/usage/configuration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
=============
Configuration
=============

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"},
"append_slash": True
}


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"),
]

.. ## Flask

.. ## FastAPI
Loading