measurement-api 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ from .main import MeasurementAPI
2
+
3
+ __all__ = ["MeasurementAPI"]
@@ -0,0 +1,143 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ import logging
4
+ from types import TracebackType
5
+ from typing import Self
6
+
7
+ import httpx
8
+
9
+ logger = logging.getLogger(__name__)
10
+
11
+
12
+ class MeasurementAPI:
13
+ """A client for interacting with the Google Analytics 4 Measurement Protocol."""
14
+
15
+ def __init__(
16
+ self,
17
+ m10t_id: str,
18
+ secret_key: str,
19
+ *,
20
+ debug: bool = False,
21
+ client: httpx.AsyncClient | None = None,
22
+ ) -> None:
23
+ """Initialize the client for sending events to Google Analytics.
24
+
25
+ Args:
26
+ m10t_id: Measurement ID (GA4 data stream measurement ID).
27
+ secret_key: API Secret Key.
28
+ debug: Debug mode (sends validation requests to the GA4 validation server).
29
+ client: External AsyncClient for connection sharing and pooling.
30
+ """
31
+ self.id = m10t_id
32
+ self.secret_key = secret_key
33
+ self.debug = debug
34
+ self.client = client
35
+ self._owns_client = client is None
36
+
37
+ async def __aenter__(self) -> Self:
38
+ if self.client is None:
39
+ self.client = httpx.AsyncClient()
40
+ self._owns_client = True
41
+ return self
42
+
43
+ async def __aexit__(
44
+ self,
45
+ exc_type: type[BaseException] | None,
46
+ exc_val: BaseException | None,
47
+ exc_tb: TracebackType | None,
48
+ ) -> None:
49
+ await self.close()
50
+
51
+ async def close(self) -> None:
52
+ """Close the httpx client if it was created internally within the class."""
53
+ if self._owns_client and self.client is not None:
54
+ await self.client.aclose()
55
+ self.client = None
56
+
57
+ async def log_event(self, client_id: str, event_name: str, **kwargs) -> bool:
58
+ """Log an event in Google Analytics.
59
+
60
+ Args:
61
+ client_id: A unique identifier for the client.
62
+ event_name: The name of the event.
63
+ **kwargs: Event parameters (supports primitives, lists, and dicts).
64
+
65
+ Returns:
66
+ bool: True if the event was successfully sent/validated, False otherwise.
67
+ """
68
+ suffix = "debug/" if self.debug else ""
69
+ base_url = f"https://www.google-analytics.com/{suffix}mp/collect"
70
+ query_params = {"measurement_id": self.id, "api_secret": self.secret_key}
71
+
72
+ valid_params = {
73
+ k: v
74
+ for k, v in kwargs.items()
75
+ if isinstance(v, (str, int, float, bool, list, dict, tuple))
76
+ }
77
+
78
+ payload = {
79
+ "client_id": client_id,
80
+ "events": [
81
+ {
82
+ "name": event_name,
83
+ "params": valid_params,
84
+ }
85
+ ],
86
+ }
87
+
88
+ client = self.client
89
+ own_client = False
90
+ if client is None:
91
+ client = httpx.AsyncClient()
92
+ own_client = True
93
+
94
+ try:
95
+ logger.debug(
96
+ f"Sending GA event '{event_name}' to {base_url} with payload {payload}"
97
+ )
98
+ response = await client.post(
99
+ base_url, params=query_params, json=payload, timeout=5.0
100
+ )
101
+
102
+ if self.debug:
103
+ if response.status_code != 200:
104
+ logger.warning(
105
+ f"GA debug unexpected status: {response.status_code}"
106
+ )
107
+ return False
108
+
109
+ debug_result = response.json()
110
+ validation_messages = debug_result.get("validationMessages", [])
111
+
112
+ if validation_messages:
113
+ logger.warning(f"GA validation issues: {validation_messages}")
114
+ return False
115
+ else:
116
+ logger.debug(f"GA event '{event_name}' validated successfully")
117
+ return True
118
+ else:
119
+ if response.status_code == 204:
120
+ logger.info(f"GA event '{event_name}' sent successfully")
121
+ return True
122
+ else:
123
+ logger.warning(
124
+ f"GA unexpected status {response.status_code} "
125
+ f"for event '{event_name}'"
126
+ )
127
+ return False
128
+
129
+ except httpx.TimeoutException:
130
+ logger.error("Analytics request timeout")
131
+ return False
132
+
133
+ except httpx.RequestError as e:
134
+ logger.error(f"Analytics request failed: {e}")
135
+ return False
136
+
137
+ except Exception as e:
138
+ logger.exception(f"Unexpected analytics error: {e}")
139
+ return False
140
+
141
+ finally:
142
+ if own_client:
143
+ await client.aclose()
@@ -0,0 +1,165 @@
1
+ Metadata-Version: 2.4
2
+ Name: measurement-api
3
+ Version: 0.1.0
4
+ Summary: A lightweight asynchronous Python client for the Google Analytics 4 Measurement Protocol
5
+ Author-email: Andrii Bogdanovych <a@bogdanovych.org>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://www.bogdanovych.org
8
+ Project-URL: Repository, https://github.com/BogdanovychA/measurement-api
9
+ Project-URL: Bug Tracker, https://github.com/BogdanovychA/measurement-api/issues
10
+ Project-URL: AI Agent Skill, https://skills.sh/bogdanovycha/measurement-api/measurement-api
11
+ Keywords: google-analytics,ga4,measurement-protocol,async,asyncio,httpx,analytics
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Classifier: Framework :: AsyncIO
20
+ Requires-Python: >=3.12
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ License-File: AUTHORS.md
24
+ Requires-Dist: httpx>=0.24.0
25
+ Dynamic: license-file
26
+
27
+ # measurement-api
28
+
29
+ A lightweight asynchronous Python client for the Google Analytics 4 (GA4) Measurement Protocol.
30
+
31
+ [![PyPI version](https://img.shields.io/pypi/v/measurement-api.svg)](https://pypi.org/project/measurement-api/)
32
+ [![Python versions](https://img.shields.io/pypi/pyversions/measurement-api.svg)](https://pypi.org/project/measurement-api/)
33
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
34
+
35
+ ## Features
36
+
37
+ - ⚡ **Asynchronous**: Built on top of `httpx` for efficient, non-blocking requests.
38
+ - 🔄 **Connection Pooling**: Supports async context manager syntax and custom `httpx.AsyncClient` injection to share/reuse TCP connections.
39
+ - 🐛 **Validation Mode**: Integrates with GA4's validation server `/debug/mp/collect` to verify your payloads without logging dummy events in production.
40
+ - 📦 **Complex Event Parameters**: Supports nested structures like lists (e.g. for E-commerce tracking `items`), dictionaries, and primitives.
41
+
42
+ ## Installation
43
+
44
+ Install using `pip`:
45
+
46
+ ```bash
47
+ pip install measurement-api
48
+ ```
49
+
50
+ Or using `uv`:
51
+
52
+ ```bash
53
+ uv add measurement-api
54
+ ```
55
+
56
+ ## Quickstart
57
+
58
+ ### Basic Async Usage
59
+
60
+ ```python
61
+ import asyncio
62
+ from measurement_api import MeasurementAPI
63
+
64
+ async def main():
65
+ # Initialize the client
66
+ api = MeasurementAPI(
67
+ m10t_id="G-XXXXXXXXXX",
68
+ secret_key="your_api_secret_key"
69
+ )
70
+
71
+ # Log an event
72
+ success = await api.log_event(
73
+ client_id="user_12345",
74
+ event_name="button_click",
75
+ button_id="submit_form",
76
+ page_url="https://example.com"
77
+ )
78
+
79
+ if success:
80
+ print("Event sent successfully!")
81
+ else:
82
+ print("Failed to send event.")
83
+
84
+ asyncio.run(main())
85
+ ```
86
+
87
+ ## Advanced Usage
88
+
89
+ ### Context Manager (Connection Reuse)
90
+
91
+ When sending multiple events, use the async context manager to reuse the HTTP client's connection pool. This avoids the overhead of establishing a new TCP/TLS connection for every single event.
92
+
93
+ ```python
94
+ import asyncio
95
+ from measurement_api import MeasurementAPI
96
+
97
+ async def main():
98
+ async with MeasurementAPI("G-XXXXXXXXXX", "your_secret") as api:
99
+ # Both events share the same connection pool
100
+ await api.log_event("user_1", "view_item", item_name="T-Shirt")
101
+ await api.log_event("user_1", "add_to_cart", item_name="T-Shirt")
102
+
103
+ asyncio.run(main())
104
+ ```
105
+
106
+ ### Passing a Custom `httpx.AsyncClient`
107
+
108
+ If your application already manages a global HTTP client (e.g. in FastAPI or Sanic), you can inject it directly:
109
+
110
+ ```python
111
+ import httpx
112
+ from measurement_api import MeasurementAPI
113
+
114
+ async def send_analytics():
115
+ # Inject your own shared client
116
+ async with httpx.AsyncClient() as shared_client:
117
+ api = MeasurementAPI(
118
+ "G-XXXXXXXXXX",
119
+ "your_secret",
120
+ client=shared_client
121
+ )
122
+
123
+ # This will not close the injected client on exit or completion
124
+ await api.log_event("user_1", "purchase", value=99.99)
125
+ ```
126
+
127
+ ### Debug & Validation Mode
128
+
129
+ To validate your event structure against Google's GA4 validation server, initialize the client with `debug=True`.
130
+ *Note: Events sent to the validation server do not show up in GA4 reports.*
131
+
132
+ ```python
133
+ from measurement_api import MeasurementAPI
134
+
135
+ api = MeasurementAPI("G-XXXXXXXXXX", "your_secret", debug=True)
136
+
137
+ # This request goes to https://www.google-analytics.com/debug/mp/collect
138
+ # Returns True if validation passes, False if there are validation warnings/errors
139
+ success = await api.log_event("user_1", "purchase", items=[{"item_id": "SKU_123"}])
140
+ ```
141
+
142
+ ## Development & Testing
143
+
144
+ ### Running Tests
145
+
146
+ We use `pytest` and `respx` to test the client offline without sending actual requests to Google Analytics.
147
+
148
+ 1. Install dependencies:
149
+ ```bash
150
+ uv sync --group dev
151
+ ```
152
+
153
+ 2. Run the test suite:
154
+ ```bash
155
+ uv run pytest
156
+ ```
157
+
158
+ 3. Run linting checks:
159
+ ```bash
160
+ uv run ruff check
161
+ ```
162
+
163
+ ## License
164
+
165
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,8 @@
1
+ measurement_api/__init__.py,sha256=w3Sz6ofFgSuHtLcca0-sPz5irZmAxjifLH5yFvCJ7GE,63
2
+ measurement_api/main.py,sha256=MwsPvHPVLZvHxMcLVqBKQnzImCuncGUN11AUclpKEXc,4604
3
+ measurement_api-0.1.0.dist-info/licenses/AUTHORS.md,sha256=tUUQeXubDx91l_FJTpY6CGc-34Ri6Y497kM-GBbFhUs,182
4
+ measurement_api-0.1.0.dist-info/licenses/LICENSE,sha256=uZ9Mr4r9lJayvgB_hif-TnHqCN8hVudeMB2oXwOOUms,1062
5
+ measurement_api-0.1.0.dist-info/METADATA,sha256=SP2Tnunq7VL_f0zQRwF7_HHnZmkjAyQ7liLQY32bPjs,5171
6
+ measurement_api-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
7
+ measurement_api-0.1.0.dist-info/top_level.txt,sha256=RdyN-gxcV0IDBmQ-ai_jv7EkIb6VYo5U19S7oA6pvnI,16
8
+ measurement_api-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,6 @@
1
+ # Authors
2
+
3
+ ## Lead Developer
4
+ * **Andrii Bogdanovych**
5
+ * [bogdanovych.org](https://www.bogdanovych.org/)
6
+ * ORCID: [0009-0006-9045-5675](https://orcid.org/0009-0006-9045-5675)
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2026 Andrii Bogdanovych
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ measurement_api