measurement-api 0.1.0__tar.gz

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,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,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,139 @@
1
+ # measurement-api
2
+
3
+ A lightweight asynchronous Python client for the Google Analytics 4 (GA4) Measurement Protocol.
4
+
5
+ [![PyPI version](https://img.shields.io/pypi/v/measurement-api.svg)](https://pypi.org/project/measurement-api/)
6
+ [![Python versions](https://img.shields.io/pypi/pyversions/measurement-api.svg)](https://pypi.org/project/measurement-api/)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
+
9
+ ## Features
10
+
11
+ - ⚡ **Asynchronous**: Built on top of `httpx` for efficient, non-blocking requests.
12
+ - 🔄 **Connection Pooling**: Supports async context manager syntax and custom `httpx.AsyncClient` injection to share/reuse TCP connections.
13
+ - 🐛 **Validation Mode**: Integrates with GA4's validation server `/debug/mp/collect` to verify your payloads without logging dummy events in production.
14
+ - 📦 **Complex Event Parameters**: Supports nested structures like lists (e.g. for E-commerce tracking `items`), dictionaries, and primitives.
15
+
16
+ ## Installation
17
+
18
+ Install using `pip`:
19
+
20
+ ```bash
21
+ pip install measurement-api
22
+ ```
23
+
24
+ Or using `uv`:
25
+
26
+ ```bash
27
+ uv add measurement-api
28
+ ```
29
+
30
+ ## Quickstart
31
+
32
+ ### Basic Async Usage
33
+
34
+ ```python
35
+ import asyncio
36
+ from measurement_api import MeasurementAPI
37
+
38
+ async def main():
39
+ # Initialize the client
40
+ api = MeasurementAPI(
41
+ m10t_id="G-XXXXXXXXXX",
42
+ secret_key="your_api_secret_key"
43
+ )
44
+
45
+ # Log an event
46
+ success = await api.log_event(
47
+ client_id="user_12345",
48
+ event_name="button_click",
49
+ button_id="submit_form",
50
+ page_url="https://example.com"
51
+ )
52
+
53
+ if success:
54
+ print("Event sent successfully!")
55
+ else:
56
+ print("Failed to send event.")
57
+
58
+ asyncio.run(main())
59
+ ```
60
+
61
+ ## Advanced Usage
62
+
63
+ ### Context Manager (Connection Reuse)
64
+
65
+ 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.
66
+
67
+ ```python
68
+ import asyncio
69
+ from measurement_api import MeasurementAPI
70
+
71
+ async def main():
72
+ async with MeasurementAPI("G-XXXXXXXXXX", "your_secret") as api:
73
+ # Both events share the same connection pool
74
+ await api.log_event("user_1", "view_item", item_name="T-Shirt")
75
+ await api.log_event("user_1", "add_to_cart", item_name="T-Shirt")
76
+
77
+ asyncio.run(main())
78
+ ```
79
+
80
+ ### Passing a Custom `httpx.AsyncClient`
81
+
82
+ If your application already manages a global HTTP client (e.g. in FastAPI or Sanic), you can inject it directly:
83
+
84
+ ```python
85
+ import httpx
86
+ from measurement_api import MeasurementAPI
87
+
88
+ async def send_analytics():
89
+ # Inject your own shared client
90
+ async with httpx.AsyncClient() as shared_client:
91
+ api = MeasurementAPI(
92
+ "G-XXXXXXXXXX",
93
+ "your_secret",
94
+ client=shared_client
95
+ )
96
+
97
+ # This will not close the injected client on exit or completion
98
+ await api.log_event("user_1", "purchase", value=99.99)
99
+ ```
100
+
101
+ ### Debug & Validation Mode
102
+
103
+ To validate your event structure against Google's GA4 validation server, initialize the client with `debug=True`.
104
+ *Note: Events sent to the validation server do not show up in GA4 reports.*
105
+
106
+ ```python
107
+ from measurement_api import MeasurementAPI
108
+
109
+ api = MeasurementAPI("G-XXXXXXXXXX", "your_secret", debug=True)
110
+
111
+ # This request goes to https://www.google-analytics.com/debug/mp/collect
112
+ # Returns True if validation passes, False if there are validation warnings/errors
113
+ success = await api.log_event("user_1", "purchase", items=[{"item_id": "SKU_123"}])
114
+ ```
115
+
116
+ ## Development & Testing
117
+
118
+ ### Running Tests
119
+
120
+ We use `pytest` and `respx` to test the client offline without sending actual requests to Google Analytics.
121
+
122
+ 1. Install dependencies:
123
+ ```bash
124
+ uv sync --group dev
125
+ ```
126
+
127
+ 2. Run the test suite:
128
+ ```bash
129
+ uv run pytest
130
+ ```
131
+
132
+ 3. Run linting checks:
133
+ ```bash
134
+ uv run ruff check
135
+ ```
136
+
137
+ ## License
138
+
139
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,61 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "measurement-api"
7
+ version = "0.1.0"
8
+ authors = [
9
+ { name = "Andrii Bogdanovych", email = "a@bogdanovych.org" }
10
+ ]
11
+ description = "A lightweight asynchronous Python client for the Google Analytics 4 Measurement Protocol"
12
+ readme = "README.md"
13
+ license = "MIT"
14
+ keywords = [
15
+ "google-analytics",
16
+ "ga4",
17
+ "measurement-protocol",
18
+ "async",
19
+ "asyncio",
20
+ "httpx",
21
+ "analytics",
22
+ ]
23
+ classifiers = [
24
+ "Development Status :: 4 - Beta",
25
+ "Intended Audience :: Developers",
26
+ "Programming Language :: Python :: 3",
27
+ "Programming Language :: Python :: 3.12",
28
+ "Programming Language :: Python :: 3.13",
29
+ "Operating System :: OS Independent",
30
+ "Topic :: Software Development :: Libraries :: Python Modules",
31
+ "Framework :: AsyncIO",
32
+ ]
33
+ requires-python = ">=3.12"
34
+ dependencies = [
35
+ "httpx>=0.24.0",
36
+ ]
37
+ [dependency-groups]
38
+ dev = [
39
+ "pre_commit",
40
+ "twine",
41
+ "pytest",
42
+ "pytest-asyncio",
43
+ "respx",
44
+ "ruff",
45
+ ]
46
+
47
+ [project.urls]
48
+ "Homepage" = "https://www.bogdanovych.org"
49
+ "Repository" = "https://github.com/BogdanovychA/measurement-api"
50
+ "Bug Tracker" = "https://github.com/BogdanovychA/measurement-api/issues"
51
+ "AI Agent Skill" = "https://skills.sh/bogdanovycha/measurement-api/measurement-api"
52
+
53
+ [tool.setuptools.packages.find]
54
+ where = ["src"]
55
+
56
+ [tool.ruff]
57
+ line-length = 88
58
+ target-version = "py312"
59
+
60
+ [tool.ruff.format]
61
+ quote-style = "preserve"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -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,12 @@
1
+ AUTHORS.md
2
+ LICENSE
3
+ README.md
4
+ pyproject.toml
5
+ src/measurement_api/__init__.py
6
+ src/measurement_api/main.py
7
+ src/measurement_api.egg-info/PKG-INFO
8
+ src/measurement_api.egg-info/SOURCES.txt
9
+ src/measurement_api.egg-info/dependency_links.txt
10
+ src/measurement_api.egg-info/requires.txt
11
+ src/measurement_api.egg-info/top_level.txt
12
+ tests/test_measurement_api.py
@@ -0,0 +1 @@
1
+ httpx>=0.24.0
@@ -0,0 +1 @@
1
+ measurement_api
@@ -0,0 +1,201 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ import httpx
4
+ import pytest
5
+ import respx
6
+
7
+ from measurement_api import MeasurementAPI
8
+
9
+ pytestmark = pytest.mark.asyncio
10
+
11
+
12
+ @respx.mock
13
+ async def test_log_event_success() -> None:
14
+ api = MeasurementAPI("G-12345", "secret_abc")
15
+
16
+ route = respx.post("https://www.google-analytics.com/mp/collect").mock(
17
+ return_value=httpx.Response(204)
18
+ )
19
+
20
+ result = await api.log_event("client_1", "test_event", param1="value1", num=42)
21
+
22
+ assert result is True
23
+ assert route.called
24
+ request = route.calls.last.request
25
+ assert "measurement_id=G-12345" in request.url.query.decode()
26
+ assert "api_secret=secret_abc" in request.url.query.decode()
27
+
28
+ # Verify JSON payload
29
+ import json
30
+
31
+ payload = json.loads(request.content)
32
+ assert payload["client_id"] == "client_1"
33
+ assert payload["events"][0]["name"] == "test_event"
34
+ assert payload["events"][0]["params"] == {"param1": "value1", "num": 42}
35
+
36
+
37
+ @respx.mock
38
+ async def test_log_event_debug_validation_success() -> None:
39
+ api = MeasurementAPI("G-12345", "secret_abc", debug=True)
40
+
41
+ route = respx.post("https://www.google-analytics.com/debug/mp/collect").mock(
42
+ return_value=httpx.Response(200, json={"validationMessages": []})
43
+ )
44
+
45
+ result = await api.log_event("client_1", "test_event")
46
+
47
+ assert result is True
48
+ assert route.called
49
+
50
+
51
+ @respx.mock
52
+ async def test_log_event_debug_validation_failure() -> None:
53
+ api = MeasurementAPI("G-12345", "secret_abc", debug=True)
54
+
55
+ route = respx.post("https://www.google-analytics.com/debug/mp/collect").mock(
56
+ return_value=httpx.Response(
57
+ 200,
58
+ json={
59
+ "validationMessages": [
60
+ {
61
+ "description": "Measurement ID is invalid",
62
+ "validationCode": "VALUE_INVALID",
63
+ }
64
+ ]
65
+ },
66
+ )
67
+ )
68
+
69
+ result = await api.log_event("client_1", "test_event")
70
+
71
+ assert result is False
72
+ assert route.called
73
+
74
+
75
+ @respx.mock
76
+ async def test_log_event_unexpected_status() -> None:
77
+ api = MeasurementAPI("G-12345", "secret_abc")
78
+
79
+ respx.post("https://www.google-analytics.com/mp/collect").mock(
80
+ return_value=httpx.Response(500)
81
+ )
82
+
83
+ result = await api.log_event("client_1", "test_event")
84
+ assert result is False
85
+
86
+
87
+ @respx.mock
88
+ async def test_log_event_debug_unexpected_status() -> None:
89
+ api = MeasurementAPI("G-12345", "secret_abc", debug=True)
90
+
91
+ respx.post("https://www.google-analytics.com/debug/mp/collect").mock(
92
+ return_value=httpx.Response(400)
93
+ )
94
+
95
+ result = await api.log_event("client_1", "test_event")
96
+ assert result is False
97
+
98
+
99
+ @respx.mock
100
+ async def test_log_event_parameter_filtering() -> None:
101
+ api = MeasurementAPI("G-12345", "secret_abc")
102
+
103
+ route = respx.post("https://www.google-analytics.com/mp/collect").mock(
104
+ return_value=httpx.Response(204)
105
+ )
106
+
107
+ # We send valid parameters (primitives, list, dict, tuple) and an invalid parameter (set)
108
+ result = await api.log_event(
109
+ "client_1",
110
+ "purchase",
111
+ str_val="ok",
112
+ int_val=10,
113
+ float_val=10.5,
114
+ bool_val=True,
115
+ list_val=[{"item_id": "1"}],
116
+ dict_val={"nested": "yes"},
117
+ tuple_val=(1, 2),
118
+ invalid_val={1, 2, 3}, # Set is not valid and should be filtered out
119
+ )
120
+
121
+ assert result is True
122
+ import json
123
+
124
+ payload = json.loads(route.calls.last.request.content)
125
+ params = payload["events"][0]["params"]
126
+
127
+ assert params["str_val"] == "ok"
128
+ assert params["int_val"] == 10
129
+ assert params["float_val"] == 10.5
130
+ assert params["bool_val"] is True
131
+ assert params["list_val"] == [{"item_id": "1"}]
132
+ assert params["dict_val"] == {"nested": "yes"}
133
+ assert params["tuple_val"] == [1, 2] # JSON converts tuple to list
134
+ assert "invalid_val" not in params
135
+
136
+
137
+ @respx.mock
138
+ async def test_shared_client_reuse() -> None:
139
+ async with httpx.AsyncClient() as shared_client:
140
+ api = MeasurementAPI("G-12345", "secret_abc", client=shared_client)
141
+
142
+ route = respx.post("https://www.google-analytics.com/mp/collect").mock(
143
+ return_value=httpx.Response(204)
144
+ )
145
+
146
+ result1 = await api.log_event("client_1", "event_1")
147
+ result2 = await api.log_event("client_1", "event_2")
148
+
149
+ assert result1 is True
150
+ assert result2 is True
151
+ assert route.call_count == 2
152
+
153
+ # Verify client is not closed by the API
154
+ assert not shared_client.is_closed
155
+
156
+ # The block exit closes the client
157
+ assert shared_client.is_closed
158
+
159
+
160
+ @respx.mock
161
+ async def test_context_manager_lifecycle() -> None:
162
+ api_instance = None
163
+ async with MeasurementAPI("G-12345", "secret_abc") as api:
164
+ api_instance = api
165
+ assert api.client is not None
166
+ assert not api.client.is_closed
167
+
168
+ respx.post("https://www.google-analytics.com/mp/collect").mock(
169
+ return_value=httpx.Response(204)
170
+ )
171
+ result = await api.log_event("client_1", "event_1")
172
+ assert result is True
173
+
174
+ # After exiting the context manager, client should be closed and set to None
175
+ assert api_instance.client is None
176
+
177
+
178
+ @respx.mock
179
+ async def test_exceptions_handling() -> None:
180
+ api = MeasurementAPI("G-12345", "secret_abc")
181
+
182
+ # 1. TimeoutException
183
+ respx.post("https://www.google-analytics.com/mp/collect").mock(
184
+ side_effect=httpx.TimeoutException("Timeout")
185
+ )
186
+ result = await api.log_event("client_1", "event_1")
187
+ assert result is False
188
+
189
+ # 2. RequestError
190
+ respx.post("https://www.google-analytics.com/mp/collect").mock(
191
+ side_effect=httpx.RequestError("Request failed")
192
+ )
193
+ result = await api.log_event("client_1", "event_1")
194
+ assert result is False
195
+
196
+ # 3. Generic unexpected exception
197
+ respx.post("https://www.google-analytics.com/mp/collect").mock(
198
+ side_effect=ValueError("Unexpected error")
199
+ )
200
+ result = await api.log_event("client_1", "event_1")
201
+ assert result is False