Skip to content

Commit

Permalink
Add tracking to all URL in an email, regardless of the domain
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Sep 2, 2024
1 parent 0b9df5a commit 30ee4de
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 41 deletions.
5 changes: 0 additions & 5 deletions emark/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@ def update_url_params(self, url, **params):
if not self.uuid:
return redirect_url
site_url = self.get_site_url()
# external links should not be tracked
top_level_domain = utils.extract_domain(site_url)

if not redirect_url_parts.netloc.endswith(top_level_domain):
return redirect_url
tracking_url = reverse("emark:email-click", kwargs={"pk": self.uuid})
tracking_url = parse.urljoin(site_url, tracking_url)
tracking_url_parts = parse.urlparse(tracking_url)
Expand Down
26 changes: 0 additions & 26 deletions emark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

__all__ = ["HTML2TextParser"]

from urllib import parse

import tldextract


@dataclasses.dataclass
class Node:
Expand Down Expand Up @@ -126,25 +122,3 @@ def __str__(self) -> str:
# sanitize all wide vertical or horizontal spaces
text = self.DOUBLE_NEWLINE.sub("\n\n", text.strip())
return self.DOUBLE_SPACE.sub(" ", text)


def extract_domain(url: str) -> str:
"""Extracts the registered domain from a given URL.
If the domain is "localhost", it includes the port number in the returned string.
Args:
url (str): The URL from which to extract the domain.
Returns:
str: The registered domain or "localhost" with port if applicable.
"""
extractor = tldextract.TLDExtract(suffix_list_urls=())
extracted = extractor(url)
if extracted.domain == "localhost":
registered_domain = "localhost"
else:
registered_domain = extracted.registered_domain
if port := parse.urlparse(url).port:
return f"{registered_domain}:{port}"
return registered_domain
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ classifiers = [
"Framework :: Django :: 5.0",
]
requires-python = ">=3.10"
dependencies = ["django", "markdown", "premailer", "tldextract"]
dependencies = ["django", "markdown", "premailer"]

[project.optional-dependencies]
test = [
Expand Down
9 changes: 0 additions & 9 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,3 @@ def test_parse_html_email(self):
"--------------------------------------------------\n"
"some footer"
)


def test_extract_domain():
assert utils.extract_domain("https://example.com") == "example.com"
assert utils.extract_domain("https://www.example.com") == "example.com"
assert utils.extract_domain("https://www.example.co.uk") == "example.co.uk"
assert utils.extract_domain("https://www.example.com:1337") == "example.com:1337"
assert utils.extract_domain("https://localhost") == "localhost"
assert utils.extract_domain("https://localhost:8000") == "localhost:8000"

0 comments on commit 30ee4de

Please sign in to comment.