Skip to content

Commit

Permalink
adds utm_base_urls
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Mar 25, 2024
1 parent f13f6e3 commit 3adf3cb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 0.0.14dev
* [Feature] Keep existing date if the current file has been rendered already
* [Feature] Add utm codes to front matter url (`marketing.url`)
* [Feature] Adding `jupyblog.version_jupyblog` to rendered markdown files
* [Feature] Only add UTM tags to URLs included in `utm_base_urls` (`jupyblog.yml`)

## 0.0.13 (2023-03-14)

Expand Down
1 change: 1 addition & 0 deletions src/jupyblog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def _render(local, cfg="jupyblog.yaml", incsource=False, log=None):
front_matter_template=cfg.load_front_matter_template(name=post_name),
utm_medium=cfg.utm_medium,
utm_source=cfg.utm_source,
utm_base_urls=cfg.utm_base_urls,
)

# TODO: test that expands based on img_dir
Expand Down
3 changes: 3 additions & 0 deletions src/jupyblog/md.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def __init__(
utm_source=None,
utm_medium=None,
path_to_out=None,
utm_base_urls=None,
):
self.path = path_to_mds
self.path_to_out = path_to_out
Expand All @@ -234,6 +235,7 @@ def __init__(
self._front_matter_template = front_matter_template
self._utm_source = utm_source
self._utm_medium = utm_medium
self._utm_base_urls = utm_base_urls
self.env = Environment(
loader=FileSystemLoader(path_to_mds), undefined=DebugUndefined
)
Expand Down Expand Up @@ -372,6 +374,7 @@ def render(self, name, *, include_source_in_footer, metadata=None):
source=self._utm_source,
medium=self._utm_medium,
campaign=canonical_name,
base_urls=self._utm_base_urls,
)

# FIXME: remove canonical name, add it as a parameter
Expand Down
4 changes: 4 additions & 0 deletions src/jupyblog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class Config(BaseModel):
utm_medium : str
The utm_source tag to add to all URLs
utm_base_urls : list
List of strings with URLs where UTM tags should be added. Matches substrings.
"""

root: str
Expand All @@ -71,6 +74,7 @@ class Config(BaseModel):
footer: str = None
utm_source: str = None
utm_medium: str = None
utm_base_urls: list = None

def path_to_posts_abs(self):
return Path(self.root, self.path_to_posts)
Expand Down
9 changes: 7 additions & 2 deletions src/jupyblog/utm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ def add_utm_to_url(url, source, medium, campaign=None):
return parsed.geturl()


def add_utm_to_all_urls(text, source, medium, campaign):
def add_utm_to_all_urls(text, source, medium, campaign, base_urls=None):
"""Adds utms to urls found in text, ignores image resources"""
out = text

urls = [urlparse(url) for url in find_urls(text)]
urls = find_urls(text)

if base_urls:
urls = [url for url in urls if any(base_url in url for base_url in base_urls)]

urls = [urlparse(url) for url in urls]

# ignore static resources
urls = [url for url in urls if not is_image(url.path)]
Expand Down

0 comments on commit 3adf3cb

Please sign in to comment.