From 7cb23c997823bb6f8f36d0cca67d09dd12efaccc Mon Sep 17 00:00:00 2001 From: Michael Kennedy Date: Tue, 13 Feb 2024 09:03:15 -0800 Subject: [PATCH] Support IP address in new_event and new_page_view payloads Fixes #2 --- umami/pyproject.toml | 2 +- umami/umami/impl/__init__.py | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/umami/pyproject.toml b/umami/pyproject.toml index d8b1aff..46501c0 100644 --- a/umami/pyproject.toml +++ b/umami/pyproject.toml @@ -27,7 +27,7 @@ dependencies = [ "httpx", "pydantic", ] -version = "0.1.12" +version = "0.1.13" [project.urls] diff --git a/umami/umami/impl/__init__.py b/umami/umami/impl/__init__.py index 52ad4a6..a7912e6 100644 --- a/umami/umami/impl/__init__.py +++ b/umami/umami/impl/__init__.py @@ -5,7 +5,7 @@ from umami import models, urls -__version__ = '0.1.12' +__version__ = '0.1.13' from umami.errors import ValidationError, OperationNotAllowedError @@ -168,7 +168,7 @@ def websites() -> list[models.Website]: async def new_event_async(event_name: str, hostname: Optional[str] = None, url: str = '/', website_id: Optional[str] = None, title: Optional[str] = None, custom_data=None, referrer: str = '', language: str = 'en-US', - screen: str = "1920x1080") -> str: + screen: str = "1920x1080", ip_address: Optional[str] = None) -> str: """ Creates a new custom event in Umami for the given website_id and hostname (both use the default if you have set them with the other functions such as set_hostname()). These events will both @@ -185,6 +185,7 @@ async def new_event_async(event_name: str, hostname: Optional[str] = None, url: referrer: The referrer of the client if there is any (what location lead them to this event) language: The language of the event / client. screen: The screen resolution of the client. + ip_address: OPTIONAL: The true IP address of the user, used when handling requests in APIs, etc. on the server. Returns: The text returned from the Umami API. """ @@ -214,6 +215,9 @@ async def new_event_async(event_name: str, hostname: Optional[str] = None, url: "data": custom_data } + if ip_address and ip_address.strip(): + payload['ip'] = ip_address + event_data = { 'payload': payload, 'type': 'event' @@ -229,7 +233,7 @@ async def new_event_async(event_name: str, hostname: Optional[str] = None, url: def new_event(event_name: str, hostname: Optional[str] = None, url: str = '/event-api-endpoint', website_id: Optional[str] = None, title: Optional[str] = None, custom_data=None, referrer: str = '', language: str = 'en-US', - screen: str = "1920x1080") -> str: + screen: str = "1920x1080", ip_address: Optional[str] = None) -> str: """ Creates a new custom event in Umami for the given website_id and hostname (both use the default if you have set them with the other functions such as set_hostname()). These events will both @@ -246,6 +250,7 @@ def new_event(event_name: str, hostname: Optional[str] = None, url: str = '/even referrer: The referrer of the client if there is any (what location lead them to this event) language: The language of the event / client. screen: The screen resolution of the client. + ip_address: OPTIONAL: The true IP address of the user, used when handling requests in APIs, etc. on the server. Returns: The text returned from the Umami API. """ @@ -275,6 +280,9 @@ def new_event(event_name: str, hostname: Optional[str] = None, url: str = '/even "data": custom_data } + if ip_address and ip_address.strip(): + payload['ip'] = ip_address + event_data = { 'payload': payload, 'type': 'event' @@ -288,7 +296,8 @@ def new_event(event_name: str, hostname: Optional[str] = None, url: str = '/even async def new_page_view_async(page_title: str, url: str, hostname: Optional[str] = None, website_id: Optional[str] = None, referrer: str = '', - language: str = 'en-US', screen: str = "1920x1080", ua: str = event_user_agent) -> str: + language: str = 'en-US', screen: str = "1920x1080", ua: str = event_user_agent, + ip_address: Optional[str] = None) -> str: """ Creates a new page view event in Umami for the given website_id and hostname (both use the default if you have set them with the other functions such as set_hostname()). This is equivalent to what @@ -303,6 +312,7 @@ async def new_page_view_async(page_title: str, url: str, hostname: Optional[str] language: OPTIONAL: The language of the event / client. screen: OPTIONAL: The screen resolution of the client. ua: OPTIONAL: The UserAgent resolution of the client. Note umami blocks non browsers by default. + ip_address: OPTIONAL: The true IP address of the user, used when handling requests in APIs, etc. on the server. Returns: The text returned from the Umami API. """ @@ -328,6 +338,9 @@ async def new_page_view_async(page_title: str, url: str, hostname: Optional[str] "website": website_id, } + if ip_address and ip_address.strip(): + payload['ip'] = ip_address + event_data = { 'payload': payload, 'type': 'event' @@ -342,7 +355,8 @@ async def new_page_view_async(page_title: str, url: str, hostname: Optional[str] def new_page_view(page_title: str, url: str, hostname: Optional[str] = None, website_id: Optional[str] = None, referrer: str = '', - language: str = 'en-US', screen: str = "1920x1080", ua: str = event_user_agent) -> str: + language: str = 'en-US', screen: str = "1920x1080", ua: str = event_user_agent, + ip_address: Optional[str] = None) -> str: """ Creates a new page view event in Umami for the given website_id and hostname (both use the default if you have set them with the other functions such as set_hostname()). This is equivalent to what @@ -357,6 +371,7 @@ def new_page_view(page_title: str, url: str, hostname: Optional[str] = None, language: OPTIONAL: The language of the event / client. screen: OPTIONAL: The screen resolution of the client. ua: OPTIONAL: The UserAgent resolution of the client. Note umami blocks non browsers by default. + ip_address: OPTIONAL: The true IP address of the user, used when handling requests in APIs, etc. on the server. Returns: The text returned from the Umami API. """ @@ -382,6 +397,9 @@ def new_page_view(page_title: str, url: str, hostname: Optional[str] = None, "website": website_id, } + if ip_address and ip_address.strip(): + payload['ip'] = ip_address + event_data = { 'payload': payload, 'type': 'event'