Skip to content

Commit 7cb23c9

Browse files
committed
Support IP address in new_event and new_page_view payloads Fixes #2
1 parent 0e3e9d9 commit 7cb23c9

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

umami/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dependencies = [
2727
"httpx",
2828
"pydantic",
2929
]
30-
version = "0.1.12"
30+
version = "0.1.13"
3131

3232

3333
[project.urls]

umami/umami/impl/__init__.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from umami import models, urls
77

8-
__version__ = '0.1.12'
8+
__version__ = '0.1.13'
99

1010
from umami.errors import ValidationError, OperationNotAllowedError
1111

@@ -168,7 +168,7 @@ def websites() -> list[models.Website]:
168168
async def new_event_async(event_name: str, hostname: Optional[str] = None, url: str = '/',
169169
website_id: Optional[str] = None, title: Optional[str] = None,
170170
custom_data=None, referrer: str = '', language: str = 'en-US',
171-
screen: str = "1920x1080") -> str:
171+
screen: str = "1920x1080", ip_address: Optional[str] = None) -> str:
172172
"""
173173
Creates a new custom event in Umami for the given website_id and hostname (both use the default
174174
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:
185185
referrer: The referrer of the client if there is any (what location lead them to this event)
186186
language: The language of the event / client.
187187
screen: The screen resolution of the client.
188+
ip_address: OPTIONAL: The true IP address of the user, used when handling requests in APIs, etc. on the server.
188189
189190
Returns: The text returned from the Umami API.
190191
"""
@@ -214,6 +215,9 @@ async def new_event_async(event_name: str, hostname: Optional[str] = None, url:
214215
"data": custom_data
215216
}
216217

218+
if ip_address and ip_address.strip():
219+
payload['ip'] = ip_address
220+
217221
event_data = {
218222
'payload': payload,
219223
'type': 'event'
@@ -229,7 +233,7 @@ async def new_event_async(event_name: str, hostname: Optional[str] = None, url:
229233
def new_event(event_name: str, hostname: Optional[str] = None, url: str = '/event-api-endpoint',
230234
website_id: Optional[str] = None, title: Optional[str] = None,
231235
custom_data=None, referrer: str = '', language: str = 'en-US',
232-
screen: str = "1920x1080") -> str:
236+
screen: str = "1920x1080", ip_address: Optional[str] = None) -> str:
233237
"""
234238
Creates a new custom event in Umami for the given website_id and hostname (both use the default
235239
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
246250
referrer: The referrer of the client if there is any (what location lead them to this event)
247251
language: The language of the event / client.
248252
screen: The screen resolution of the client.
253+
ip_address: OPTIONAL: The true IP address of the user, used when handling requests in APIs, etc. on the server.
249254
250255
Returns: The text returned from the Umami API.
251256
"""
@@ -275,6 +280,9 @@ def new_event(event_name: str, hostname: Optional[str] = None, url: str = '/even
275280
"data": custom_data
276281
}
277282

283+
if ip_address and ip_address.strip():
284+
payload['ip'] = ip_address
285+
278286
event_data = {
279287
'payload': payload,
280288
'type': 'event'
@@ -288,7 +296,8 @@ def new_event(event_name: str, hostname: Optional[str] = None, url: str = '/even
288296

289297
async def new_page_view_async(page_title: str, url: str, hostname: Optional[str] = None,
290298
website_id: Optional[str] = None, referrer: str = '',
291-
language: str = 'en-US', screen: str = "1920x1080", ua: str = event_user_agent) -> str:
299+
language: str = 'en-US', screen: str = "1920x1080", ua: str = event_user_agent,
300+
ip_address: Optional[str] = None) -> str:
292301
"""
293302
Creates a new page view event in Umami for the given website_id and hostname (both use the default
294303
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]
303312
language: OPTIONAL: The language of the event / client.
304313
screen: OPTIONAL: The screen resolution of the client.
305314
ua: OPTIONAL: The UserAgent resolution of the client. Note umami blocks non browsers by default.
315+
ip_address: OPTIONAL: The true IP address of the user, used when handling requests in APIs, etc. on the server.
306316
307317
Returns: The text returned from the Umami API.
308318
"""
@@ -328,6 +338,9 @@ async def new_page_view_async(page_title: str, url: str, hostname: Optional[str]
328338
"website": website_id,
329339
}
330340

341+
if ip_address and ip_address.strip():
342+
payload['ip'] = ip_address
343+
331344
event_data = {
332345
'payload': payload,
333346
'type': 'event'
@@ -342,7 +355,8 @@ async def new_page_view_async(page_title: str, url: str, hostname: Optional[str]
342355

343356
def new_page_view(page_title: str, url: str, hostname: Optional[str] = None,
344357
website_id: Optional[str] = None, referrer: str = '',
345-
language: str = 'en-US', screen: str = "1920x1080", ua: str = event_user_agent) -> str:
358+
language: str = 'en-US', screen: str = "1920x1080", ua: str = event_user_agent,
359+
ip_address: Optional[str] = None) -> str:
346360
"""
347361
Creates a new page view event in Umami for the given website_id and hostname (both use the default
348362
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,
357371
language: OPTIONAL: The language of the event / client.
358372
screen: OPTIONAL: The screen resolution of the client.
359373
ua: OPTIONAL: The UserAgent resolution of the client. Note umami blocks non browsers by default.
374+
ip_address: OPTIONAL: The true IP address of the user, used when handling requests in APIs, etc. on the server.
360375
361376
Returns: The text returned from the Umami API.
362377
"""
@@ -382,6 +397,9 @@ def new_page_view(page_title: str, url: str, hostname: Optional[str] = None,
382397
"website": website_id,
383398
}
384399

400+
if ip_address and ip_address.strip():
401+
payload['ip'] = ip_address
402+
385403
event_data = {
386404
'payload': payload,
387405
'type': 'event'

0 commit comments

Comments
 (0)