Skip to content

Commit

Permalink
Support IP address in new_event and new_page_view payloads Fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeckennedy committed Feb 13, 2024
1 parent 0e3e9d9 commit 7cb23c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion umami/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies = [
"httpx",
"pydantic",
]
version = "0.1.12"
version = "0.1.13"


[project.urls]
Expand Down
28 changes: 23 additions & 5 deletions umami/umami/impl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from umami import models, urls

__version__ = '0.1.12'
__version__ = '0.1.13'

from umami.errors import ValidationError, OperationNotAllowedError

Expand Down Expand Up @@ -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
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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.
"""
Expand All @@ -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'
Expand All @@ -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
Expand All @@ -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.
"""
Expand All @@ -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'
Expand Down

0 comments on commit 7cb23c9

Please sign in to comment.