{"id":22820726,"url":"https://github.com/indeedeng/django-ptrack","last_synced_at":"2025-05-15T09:15:57.227Z","repository":{"id":21499560,"uuid":"91736491","full_name":"indeedeng/django-ptrack","owner":"indeedeng","description":"Tracking pixel library for Django","archived":false,"fork":false,"pushed_at":"2023-03-18T21:06:29.000Z","size":61,"stargazers_count":40,"open_issues_count":7,"forks_count":17,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-05-07T23:40:06.883Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/indeedeng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-18T20:50:23.000Z","updated_at":"2024-08-22T03:19:40.000Z","dependencies_parsed_at":"2024-12-12T15:33:56.295Z","dependency_job_id":"be642660-22d3-489c-b243-82b15a951fa9","html_url":"https://github.com/indeedeng/django-ptrack","commit_stats":{"total_commits":60,"total_committers":12,"mean_commits":5.0,"dds":0.4833333333333333,"last_synced_commit":"362120ac77adad8bfff878ee9e9968c74fa9efc6"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indeedeng%2Fdjango-ptrack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indeedeng%2Fdjango-ptrack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indeedeng%2Fdjango-ptrack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/indeedeng%2Fdjango-ptrack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/indeedeng","download_url":"https://codeload.github.com/indeedeng/django-ptrack/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254170826,"owners_count":22026327,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-12T15:27:05.434Z","updated_at":"2025-05-15T09:15:57.171Z","avatar_url":"https://github.com/indeedeng.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Django Ptrack\n\n![OSS Lifecycle](https://img.shields.io/osslifecycle/indeedeng/django-ptrack.svg)\n\nPtrack is a tracking pixel library for Django.\n\nYou can use Ptrack to detect email open rates or to create your own pixel tracking API.\nWhen used by Indeed, Ptrack generates pixels with average request to response lifecycles of \u003c80 ms.\n\nEach tracking pixel is a unique encoded image generated per arg/kwargs set.\nUnlike other tracking pixel libraries, Ptrack is stateless and does not require a database.\nInstead, ptrack allows developers to pass in metadata which is encrypted and stored in the image url.\n\n## Getting Started\nInstall the library using Pip:\n```\npip install django-ptrack\n```\n\n### Configuration\n1. Add Ptrack to your installed apps in settings:\n```\nINSTALLED_APPS = (\n    ...,\n    'ptrack',\n    ...\n)\n```\n\n2. Define a secret that is 32-character bytes or fewer.\nThe secret is used to create an encrypted tracking pixel url.\n```\nPTRACK_SECRET = \"\"\n```\n\n3.  Define a Ptrack app URL in your settings. This is the domain that the tracking pixel will be based on.\n```\nPTRACK_APP_URL = \"\" # Example: PTRACK_APP_URL = \"https://www.example.com\"\n```\n\n*NOTE:* The PTRACK_APP_URL gives you a lot of flexibility.\nFor example, if you are trying to track emails from a web app hosted on an internal network, you can deploy a public facing mirror web app that records the pixels.\nAs long as the internal and external apps share the same secret and are registered on the same URL path prefix, tracking should work.\n\n\n\n## Using Ptrack\nLoad and define Ptrack in templates:\n```\n{% load ptrack %}\n{% ptrack 'arg' key1='arg1' key2='arg2' ... %}\n```\n\nWhen the ptrack template tag is expanded, it generates a tracking pixel of form:\n```\n\u003cimg src=\"{{ENCRYPTED_URL}}\" width=1 height=1\u003e\n```\n\n*NOTE:*\n* Keep in mind that valid arg and kwarg values must be json serializable ints or strings.\nIf non-valid inputs are provided, the template tag will throw an exception.\n* When testing a tracking pixel in an email locally or with a domain that is not publicly accessible, the tracking pixel in the email will appear as an empty box rather than as an invisible pixel.\nThe reason the image is rendered as an error image is because most email servers, such as Gmail, will proxy img tags.\n* If the server has downtime, the pixel appears as an empty box.\nFor this reason, it is best to include the tracking pixel at the bottom of an email or page.\n* Realize that the encoded metadata tied to the tracking pixel is stored in the URL.\nAs a best practice, the number of characters you store should be less than half the maximum character limit of your supported browser.\n\n\n### Define tracking functionality\nPtrack automatically searches your project for a file called `pixels.py`, in which you register your pixel tracking callbacks.\n\nCreate this file in your project.\nDefine the tracking functionality by overriding base class, defining the record() method, and registering the new class:\n```\nimport ptrack\nclass CustomTrackingPixel(ptrack.TrackingPixel):\n    def record(self, request, *args, **kwargs):\n        log.info(request.META['HTTP_USER_AGENT'])\n        for arg in args:\n            log.info(arg)\n        for key, value in kwargs:\n            if key == \"testemail1\":\n                log.info(\"Recorded test email\")\n            else:\n                log.info(key + \":\" + value)\n\nptrack.tracker.register(CustomTrackingPixel)\n```\n\nWhenever your tracking pixel is loaded, the record() callback method is executed.\n\n### Define multiple callbacks\nYou can register multiple definitions of `ptrack.TrackingPixel` to chain callbacks, although there is no guarantee of the order they will execute.\n\n*NOTE:* The tracking response will not complete until all the record() methods have finished executing, so you shouldn't run any long running blocking processes.\n\n### Register ptrack.urls\nIn `url.py`, register 'ptrack.urls' on your desired url prefix pattern:\n```\nurl('^ptrack/', include('ptrack.urls')),\n```\n\n### Tracking dictionary values in template tag\nThe general use of `ptrack` as a template tag is:\n```\n{% ptrack key1=value1 key2=value2 %}\n```\nHowever, doing above requires explicity writing out all key value pairs. If the template context contains a field having dictionary type data (example: `dict_field = {\"df_key1\": \"df_value\", \"df_key2\": 2}`), or field that references a model object with a property method that returns a dictionary type data (example: `model_field = model`, such that `model.dict_property` returns `{\"model_key1\": \"model_key\", \"model_key2\": 3}`), then it is possible to collect these values within `ptrack` by passing them as positional arguments to the `ptrack` template tag, like:\n```\n{% ptrack dict_field model_field.dict_property key1=value1 key2=value2 %}\n```\nAdditionally, the `CustomTrackingPixel.record` method must be modified to read the positional arguments and include them with the keyworded arguments, like:\n```\nimport ptrack\nclass CustomTrackingPixel(ptrack.TrackingPixel):\n    def record(self, request, *args, **kwargs):\n        args_updated_dict = { **kwargs }\n        for arg in args:\n            if instance(arg, dict):\n                args_updated_dict.update(arg)\n            else:\n                log.info(arg)\n        # Below, `args_updated_dict` includes keywords from both `kwargs` and dictionary type `arg` argument\n        for key, value in args_updated_dict:  \n            log.info(key + \":\" + value)\n\nptrack.tracker.register(CustomTrackingPixel)\n```\n\n### Capturing time to open\nIt is possible to accommodate use case where it is needed to identify the time between when tracking pixel was created to when it gets activated. For example, the time difference between when an email containing the tracking pixel is created to when the user opens it. To do so, a numeric values `pixel_create_time` can be added in the `ptrack` template tag, and `CustomTrackingPixel.record` method can be modified to read the `pixel_create_time` and evaluate the time difference since then. This is assuming that the email was sent as soon as it was rendered.\n\n## Validation requirements\nPtrack ignores anything it cannot decrypt or deserialize.\nCallbacks are not run if someone attempts to guess a URL endpoint.\n\n\n## Testing\nTo build tests, navigate to the ptrack directory on your local machine and run `tox`. Install tox if neccesary with `pip install tox`.\n\n## Overriding the encoder\nWhile ptrack should work out of the box, you have the ability to create your own encoder.\n\nSuppose you created a class MyEncoder, with _static_ `encrypt` and `decrypt` methods.\nIn your application's `pixels.py`, you then register the encoder:\n```\nimport ptrack\nptrack.ptrack_encoder = MyEncoder\n```\n\n## Installation Errors\nIf you run into installation errors, such as:\n```\ndistutils.errors.DistutilsError: Setup script exited with error: command 'x86_64-linux-gnu-gcc' failed with exit status 1\n```\n\nYou may need to install system dependencies for PyNacl:\n```\nsudo apt-get install python-dev\nsudo apt-get install libffi-dev\n```\n\n## Code of Conduct\nThis project is governed by the [Contributor Covenant v 1.4.1](CODE_OF_CONDUCT.md)\n\n## License\nThis project uses the [Apache 2.0](LICENSE.txt) license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Findeedeng%2Fdjango-ptrack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Findeedeng%2Fdjango-ptrack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Findeedeng%2Fdjango-ptrack/lists"}