altissimo-mixpanel 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,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to the Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by the Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding any notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2024 Altissimo HQ
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
@@ -0,0 +1,248 @@
1
+ Metadata-Version: 2.4
2
+ Name: altissimo-mixpanel
3
+ Version: 0.1.0
4
+ Summary: Reusable Mixpanel analytics client for Altissimo projects
5
+ License-Expression: Apache-2.0
6
+ License-File: LICENSE
7
+ Keywords: mixpanel,analytics,altissimo
8
+ Author: Altissimo HQ
9
+ Requires-Python: >=3.11
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Typing :: Typed
17
+ Provides-Extra: mixpanel
18
+ Requires-Dist: mixpanel (>=4.0) ; extra == "mixpanel"
19
+ Project-URL: Changelog, https://github.com/altissimo-hq/mixpanel-python/blob/main/CHANGELOG.md
20
+ Project-URL: Homepage, https://github.com/altissimo-hq/mixpanel-python
21
+ Project-URL: Issues, https://github.com/altissimo-hq/mixpanel-python/issues
22
+ Project-URL: Repository, https://github.com/altissimo-hq/mixpanel-python
23
+ Description-Content-Type: text/markdown
24
+
25
+ # altissimo-mixpanel
26
+
27
+ Reusable, typed Mixpanel analytics client for Altissimo projects.
28
+
29
+ ## Features
30
+
31
+ - **Typed API** — full type annotations for all operations
32
+ - **Lazy SDK import** — `mixpanel` is imported only when needed, zero import-time side effects
33
+ - **Disabled mode** — no-op all calls when analytics is turned off or isn't configured
34
+ - **Error handling** — swallow-and-log by default, configurable via `on_error`
35
+ - **Protocol for DI** — `MixpanelClientProtocol` for dependency injection
36
+ - **Test helpers** — `FakeMixpanelClient` records all calls in-memory
37
+ - **Optional dependency** — works without `mixpanel` installed (fails gracefully)
38
+
39
+ ## Installation
40
+
41
+ Published on PyPI as [`altissimo-mixpanel`](https://pypi.org/project/altissimo-mixpanel/);
42
+ the import name is `altissimo.mixpanel`.
43
+
44
+ ```bash
45
+ # With the Mixpanel SDK (recommended)
46
+ pip install "altissimo-mixpanel[mixpanel]"
47
+
48
+ # Without the SDK (for type checking / protocol-only usage)
49
+ pip install altissimo-mixpanel
50
+ ```
51
+
52
+ ## Quick Start
53
+
54
+ ### Using `from_env()`
55
+
56
+ ```python
57
+ from altissimo.mixpanel import MixpanelClient
58
+
59
+ # Reads MIXPANEL_TOKEN from environment
60
+ # Missing token → disabled mode, with a warning (see Disabled Mode)
61
+ client = MixpanelClient.from_env()
62
+
63
+ # Track an event
64
+ client.track("user-123", "Page Viewed", properties={"page": "/home"})
65
+
66
+ # Set user profile properties
67
+ client.people_set("user-123", {"$name": "Jane Doe", "$email": "jane@example.com"})
68
+ ```
69
+
70
+ ### Direct Construction
71
+
72
+ ```python
73
+ client = MixpanelClient(
74
+ token="your-project-token",
75
+ enabled=True,
76
+ on_error="log", # "log" | "raise" | "ignore"
77
+ )
78
+ ```
79
+
80
+ ### Disabled Mode
81
+
82
+ ```python
83
+ # Explicitly disabled
84
+ client = MixpanelClient(token="...", enabled=False)
85
+ client.track("user-123", "Page Viewed") # silently does nothing
86
+
87
+ # Via environment variable
88
+ # MIXPANEL_ENABLED=false → disabled
89
+ client = MixpanelClient.from_env()
90
+
91
+ # Missing token → automatically disabled, and a warning names the variable
92
+ # (no MIXPANEL_TOKEN set)
93
+ client = MixpanelClient.from_env()
94
+ assert not client.enabled
95
+ ```
96
+
97
+ When the token is missing, `from_env()` behaves according to how tracking was
98
+ turned on:
99
+
100
+ | `enabled` | Missing or empty token |
101
+ | --- | --- |
102
+ | `True` (passed explicitly) | Raises `ValueError` naming the variable |
103
+ | `None` (default; `MIXPANEL_ENABLED` unset or truthy) | Disabled, one warning naming the variable |
104
+ | `False`, or `MIXPANEL_ENABLED` falsey | Disabled, no warning |
105
+
106
+ Pass `enabled=True` where a deployment must send events, so a missing secret
107
+ fails at startup instead of looking like Mixpanel going quiet. An app that only
108
+ sends events when deployed can pass that condition, e.g.
109
+ `MixpanelClient.from_env(enabled=IS_DEPLOYED)`: quiet locally, an error on a
110
+ deployment missing its token.
111
+
112
+ ### Error Handling
113
+
114
+ ```python
115
+ # Default: catch and log errors (analytics never crashes your app)
116
+ client = MixpanelClient(token="...", on_error="log")
117
+
118
+ # Raise errors (useful for debugging)
119
+ client = MixpanelClient(token="...", on_error="raise")
120
+
121
+ # Silently ignore errors
122
+ client = MixpanelClient(token="...", on_error="ignore")
123
+ ```
124
+
125
+ ### Custom Consumer
126
+
127
+ ```python
128
+ from mixpanel import BufferedConsumer
129
+
130
+ # Use a buffered consumer for high-throughput
131
+ client = MixpanelClient(token="...", consumer=BufferedConsumer())
132
+ ```
133
+
134
+ A `BufferedConsumer` only sends when its buffer fills, so flush it before the
135
+ process exits (a Cloud Run instance shutting down, a job or CLI command
136
+ finishing), or the buffered events are lost:
137
+
138
+ ```python
139
+ client.flush()
140
+
141
+ # Or flush automatically when the block ends, even if it raises:
142
+ with MixpanelClient(token="...", consumer=BufferedConsumer()) as client:
143
+ client.track("user-123", "Job Finished")
144
+ ```
145
+
146
+ With the default consumer, which sends each message immediately, `flush()`
147
+ does nothing. A failing flush follows `on_error` like every other call.
148
+
149
+ Any object with `send(endpoint, json_message)` works as a consumer; the
150
+ `MixpanelConsumer` protocol describes it for type checking.
151
+
152
+ ### Threads
153
+
154
+ One `MixpanelClient` can be shared across threads, such as a module-level
155
+ client in a web app. Each thread gets its own Mixpanel SDK instance and, with
156
+ the default consumer, its own HTTP session: the SDK's `Consumer` and
157
+ `BufferedConsumer` aren't safe to share between threads.
158
+
159
+ A `consumer=` is a single object every thread shares, so in a threaded app pass
160
+ a `consumer_factory` instead; it's called once per thread:
161
+
162
+ ```python
163
+ from mixpanel import BufferedConsumer, Consumer
164
+
165
+ # A shorter request timeout, one consumer per thread
166
+ client = MixpanelClient.from_env(consumer_factory=lambda: Consumer(request_timeout=5))
167
+
168
+ # Buffered, one buffer per thread
169
+ client = MixpanelClient(token="...", consumer_factory=BufferedConsumer)
170
+ ```
171
+
172
+ `flush()` flushes every thread's consumer. Call it when no other thread is
173
+ tracking, such as at shutdown.
174
+
175
+ ### Testing with `FakeMixpanelClient`
176
+
177
+ ```python
178
+ from altissimo.mixpanel import FakeMixpanelClient
179
+
180
+ def test_order_tracking():
181
+ fake = FakeMixpanelClient()
182
+ service = OrderService(mixpanel=fake)
183
+
184
+ service.create_order(user_id="user-123", total=99.99)
185
+
186
+ assert len(fake.tracked) == 1
187
+ assert fake.tracked[0][1] == "Order Created"
188
+ assert fake.tracked[0][2]["total"] == 99.99
189
+ ```
190
+
191
+ ### Protocol-Based Dependency Injection
192
+
193
+ ```python
194
+ from altissimo.mixpanel import MixpanelClientProtocol
195
+
196
+ class OrderService:
197
+ def __init__(self, mixpanel: MixpanelClientProtocol) -> None:
198
+ self._mp = mixpanel
199
+
200
+ def create_order(self, user_id: str, total: float) -> None:
201
+ # ... business logic ...
202
+ self._mp.track(user_id, "Order Created", properties={"total": total})
203
+ ```
204
+
205
+ ## API Reference
206
+
207
+ ### `MixpanelClient`
208
+
209
+ | Method | Description |
210
+ |---|---|
211
+ | `track(distinct_id, event_name, *, properties?, meta?)` | Track an event |
212
+ | `people_set(distinct_id, properties, *, meta?)` | Set user profile properties |
213
+ | `people_set_once(distinct_id, properties, *, meta?)` | Set properties only if not set |
214
+ | `people_increment(distinct_id, properties, *, meta?)` | Increment numeric properties |
215
+ | `people_union(distinct_id, properties, *, meta?)` | Append to list properties |
216
+ | `people_unset(distinct_id, property_names, *, meta?)` | Remove properties |
217
+ | `alias(alias_id, original_id, *, meta?)` | Create an identity alias |
218
+
219
+ ### Environment Variables
220
+
221
+ | Variable | Default | Description |
222
+ |---|---|---|
223
+ | `MIXPANEL_TOKEN` | — | Mixpanel project token |
224
+ | `MIXPANEL_ENABLED` | `true` | Set to `false`/`0`/`no` to disable |
225
+
226
+ ## Development
227
+
228
+ ```bash
229
+ # Install dependencies
230
+ poetry sync
231
+
232
+ # Run tests
233
+ poetry run pytest
234
+
235
+ # Run tests with coverage
236
+ poetry run pytest --cov=altissimo --cov-report=term-missing
237
+
238
+ # Lint
239
+ poetry run ruff check .
240
+
241
+ # Format
242
+ poetry run ruff format .
243
+ ```
244
+
245
+ ## License
246
+
247
+ [Apache-2.0](LICENSE)
248
+
@@ -0,0 +1,223 @@
1
+ # altissimo-mixpanel
2
+
3
+ Reusable, typed Mixpanel analytics client for Altissimo projects.
4
+
5
+ ## Features
6
+
7
+ - **Typed API** — full type annotations for all operations
8
+ - **Lazy SDK import** — `mixpanel` is imported only when needed, zero import-time side effects
9
+ - **Disabled mode** — no-op all calls when analytics is turned off or isn't configured
10
+ - **Error handling** — swallow-and-log by default, configurable via `on_error`
11
+ - **Protocol for DI** — `MixpanelClientProtocol` for dependency injection
12
+ - **Test helpers** — `FakeMixpanelClient` records all calls in-memory
13
+ - **Optional dependency** — works without `mixpanel` installed (fails gracefully)
14
+
15
+ ## Installation
16
+
17
+ Published on PyPI as [`altissimo-mixpanel`](https://pypi.org/project/altissimo-mixpanel/);
18
+ the import name is `altissimo.mixpanel`.
19
+
20
+ ```bash
21
+ # With the Mixpanel SDK (recommended)
22
+ pip install "altissimo-mixpanel[mixpanel]"
23
+
24
+ # Without the SDK (for type checking / protocol-only usage)
25
+ pip install altissimo-mixpanel
26
+ ```
27
+
28
+ ## Quick Start
29
+
30
+ ### Using `from_env()`
31
+
32
+ ```python
33
+ from altissimo.mixpanel import MixpanelClient
34
+
35
+ # Reads MIXPANEL_TOKEN from environment
36
+ # Missing token → disabled mode, with a warning (see Disabled Mode)
37
+ client = MixpanelClient.from_env()
38
+
39
+ # Track an event
40
+ client.track("user-123", "Page Viewed", properties={"page": "/home"})
41
+
42
+ # Set user profile properties
43
+ client.people_set("user-123", {"$name": "Jane Doe", "$email": "jane@example.com"})
44
+ ```
45
+
46
+ ### Direct Construction
47
+
48
+ ```python
49
+ client = MixpanelClient(
50
+ token="your-project-token",
51
+ enabled=True,
52
+ on_error="log", # "log" | "raise" | "ignore"
53
+ )
54
+ ```
55
+
56
+ ### Disabled Mode
57
+
58
+ ```python
59
+ # Explicitly disabled
60
+ client = MixpanelClient(token="...", enabled=False)
61
+ client.track("user-123", "Page Viewed") # silently does nothing
62
+
63
+ # Via environment variable
64
+ # MIXPANEL_ENABLED=false → disabled
65
+ client = MixpanelClient.from_env()
66
+
67
+ # Missing token → automatically disabled, and a warning names the variable
68
+ # (no MIXPANEL_TOKEN set)
69
+ client = MixpanelClient.from_env()
70
+ assert not client.enabled
71
+ ```
72
+
73
+ When the token is missing, `from_env()` behaves according to how tracking was
74
+ turned on:
75
+
76
+ | `enabled` | Missing or empty token |
77
+ | --- | --- |
78
+ | `True` (passed explicitly) | Raises `ValueError` naming the variable |
79
+ | `None` (default; `MIXPANEL_ENABLED` unset or truthy) | Disabled, one warning naming the variable |
80
+ | `False`, or `MIXPANEL_ENABLED` falsey | Disabled, no warning |
81
+
82
+ Pass `enabled=True` where a deployment must send events, so a missing secret
83
+ fails at startup instead of looking like Mixpanel going quiet. An app that only
84
+ sends events when deployed can pass that condition, e.g.
85
+ `MixpanelClient.from_env(enabled=IS_DEPLOYED)`: quiet locally, an error on a
86
+ deployment missing its token.
87
+
88
+ ### Error Handling
89
+
90
+ ```python
91
+ # Default: catch and log errors (analytics never crashes your app)
92
+ client = MixpanelClient(token="...", on_error="log")
93
+
94
+ # Raise errors (useful for debugging)
95
+ client = MixpanelClient(token="...", on_error="raise")
96
+
97
+ # Silently ignore errors
98
+ client = MixpanelClient(token="...", on_error="ignore")
99
+ ```
100
+
101
+ ### Custom Consumer
102
+
103
+ ```python
104
+ from mixpanel import BufferedConsumer
105
+
106
+ # Use a buffered consumer for high-throughput
107
+ client = MixpanelClient(token="...", consumer=BufferedConsumer())
108
+ ```
109
+
110
+ A `BufferedConsumer` only sends when its buffer fills, so flush it before the
111
+ process exits (a Cloud Run instance shutting down, a job or CLI command
112
+ finishing), or the buffered events are lost:
113
+
114
+ ```python
115
+ client.flush()
116
+
117
+ # Or flush automatically when the block ends, even if it raises:
118
+ with MixpanelClient(token="...", consumer=BufferedConsumer()) as client:
119
+ client.track("user-123", "Job Finished")
120
+ ```
121
+
122
+ With the default consumer, which sends each message immediately, `flush()`
123
+ does nothing. A failing flush follows `on_error` like every other call.
124
+
125
+ Any object with `send(endpoint, json_message)` works as a consumer; the
126
+ `MixpanelConsumer` protocol describes it for type checking.
127
+
128
+ ### Threads
129
+
130
+ One `MixpanelClient` can be shared across threads, such as a module-level
131
+ client in a web app. Each thread gets its own Mixpanel SDK instance and, with
132
+ the default consumer, its own HTTP session: the SDK's `Consumer` and
133
+ `BufferedConsumer` aren't safe to share between threads.
134
+
135
+ A `consumer=` is a single object every thread shares, so in a threaded app pass
136
+ a `consumer_factory` instead; it's called once per thread:
137
+
138
+ ```python
139
+ from mixpanel import BufferedConsumer, Consumer
140
+
141
+ # A shorter request timeout, one consumer per thread
142
+ client = MixpanelClient.from_env(consumer_factory=lambda: Consumer(request_timeout=5))
143
+
144
+ # Buffered, one buffer per thread
145
+ client = MixpanelClient(token="...", consumer_factory=BufferedConsumer)
146
+ ```
147
+
148
+ `flush()` flushes every thread's consumer. Call it when no other thread is
149
+ tracking, such as at shutdown.
150
+
151
+ ### Testing with `FakeMixpanelClient`
152
+
153
+ ```python
154
+ from altissimo.mixpanel import FakeMixpanelClient
155
+
156
+ def test_order_tracking():
157
+ fake = FakeMixpanelClient()
158
+ service = OrderService(mixpanel=fake)
159
+
160
+ service.create_order(user_id="user-123", total=99.99)
161
+
162
+ assert len(fake.tracked) == 1
163
+ assert fake.tracked[0][1] == "Order Created"
164
+ assert fake.tracked[0][2]["total"] == 99.99
165
+ ```
166
+
167
+ ### Protocol-Based Dependency Injection
168
+
169
+ ```python
170
+ from altissimo.mixpanel import MixpanelClientProtocol
171
+
172
+ class OrderService:
173
+ def __init__(self, mixpanel: MixpanelClientProtocol) -> None:
174
+ self._mp = mixpanel
175
+
176
+ def create_order(self, user_id: str, total: float) -> None:
177
+ # ... business logic ...
178
+ self._mp.track(user_id, "Order Created", properties={"total": total})
179
+ ```
180
+
181
+ ## API Reference
182
+
183
+ ### `MixpanelClient`
184
+
185
+ | Method | Description |
186
+ |---|---|
187
+ | `track(distinct_id, event_name, *, properties?, meta?)` | Track an event |
188
+ | `people_set(distinct_id, properties, *, meta?)` | Set user profile properties |
189
+ | `people_set_once(distinct_id, properties, *, meta?)` | Set properties only if not set |
190
+ | `people_increment(distinct_id, properties, *, meta?)` | Increment numeric properties |
191
+ | `people_union(distinct_id, properties, *, meta?)` | Append to list properties |
192
+ | `people_unset(distinct_id, property_names, *, meta?)` | Remove properties |
193
+ | `alias(alias_id, original_id, *, meta?)` | Create an identity alias |
194
+
195
+ ### Environment Variables
196
+
197
+ | Variable | Default | Description |
198
+ |---|---|---|
199
+ | `MIXPANEL_TOKEN` | — | Mixpanel project token |
200
+ | `MIXPANEL_ENABLED` | `true` | Set to `false`/`0`/`no` to disable |
201
+
202
+ ## Development
203
+
204
+ ```bash
205
+ # Install dependencies
206
+ poetry sync
207
+
208
+ # Run tests
209
+ poetry run pytest
210
+
211
+ # Run tests with coverage
212
+ poetry run pytest --cov=altissimo --cov-report=term-missing
213
+
214
+ # Lint
215
+ poetry run ruff check .
216
+
217
+ # Format
218
+ poetry run ruff format .
219
+ ```
220
+
221
+ ## License
222
+
223
+ [Apache-2.0](LICENSE)