Analytics
zenml.analytics
The 'analytics' module of ZenML.
Attributes
source_context: ContextVar[SourceContextTypes] = ContextVar('Source-Context', default=SourceContextTypes.PYTHON)
module-attribute
Classes
AnalyticsEvent
Bases: str
, Enum
Enum of events to track in segment.
SourceContextTypes
Functions
alias(user_id: UUID, previous_id: UUID) -> bool
Alias user IDs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id
|
UUID
|
The user ID. |
required |
previous_id
|
UUID
|
Previous ID for the alias. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if event is sent successfully, False is not. |
Source code in src/zenml/analytics/__init__.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
group(group_id: UUID, group_metadata: Optional[Dict[str, Any]] = None) -> bool
Attach metadata to a segment group.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group_id
|
UUID
|
ID of the group. |
required |
group_metadata
|
Optional[Dict[str, Any]]
|
Metadata to attach to the group. |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if event is sent successfully, False if not. |
Source code in src/zenml/analytics/__init__.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
identify(metadata: Optional[Dict[str, Any]] = None) -> bool
Attach metadata to user directly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metadata
|
Optional[Dict[str, Any]]
|
Dict of metadata to attach to the user. |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if event is sent successfully, False is not. |
Source code in src/zenml/analytics/__init__.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
track(event: AnalyticsEvent, metadata: Optional[Dict[str, Any]] = None) -> bool
Track segment event if user opted-in.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
AnalyticsEvent
|
Name of event to track in segment. |
required |
metadata
|
Optional[Dict[str, Any]]
|
Dict of metadata to track. |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if event is sent successfully, False if not. |
Source code in src/zenml/analytics/__init__.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
Modules
client
The analytics client of ZenML.
Classes
Client(send: bool = True, timeout: int = 15)
Bases: object
The client class for ZenML analytics.
Initialization of the client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
send
|
bool
|
Flag to determine whether to send the message. |
True
|
timeout
|
int
|
Timeout in seconds. |
15
|
Source code in src/zenml/analytics/client.py
32 33 34 35 36 37 38 39 40 |
|
alias(user_id: UUID, previous_id: UUID) -> Tuple[bool, str]
Method to alias user IDs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id
|
UUID
|
The user ID. |
required |
previous_id
|
UUID
|
Previous ID for the alias. |
required |
Returns:
Type | Description |
---|---|
Tuple[bool, str]
|
Tuple (success flag, the original message). |
Source code in src/zenml/analytics/client.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
group(user_id: UUID, group_id: UUID, traits: Optional[Dict[Any, Any]]) -> Tuple[bool, str]
Method to group users.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id
|
UUID
|
The user ID. |
required |
group_id
|
UUID
|
The group ID. |
required |
traits
|
Optional[Dict[Any, Any]]
|
Traits to assign to the group. |
required |
Returns:
Type | Description |
---|---|
Tuple[bool, str]
|
Tuple (success flag, the original message). |
Source code in src/zenml/analytics/client.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
identify(user_id: UUID, traits: Optional[Dict[Any, Any]]) -> Tuple[bool, str]
Method to identify a user with given traits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id
|
UUID
|
The user ID. |
required |
traits
|
Optional[Dict[Any, Any]]
|
The traits for the identification process. |
required |
Returns:
Type | Description |
---|---|
Tuple[bool, str]
|
Tuple (success flag, the original message). |
Source code in src/zenml/analytics/client.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
track(user_id: UUID, event: AnalyticsEvent, properties: Optional[Dict[Any, Any]]) -> Tuple[bool, str]
Method to track events.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id
|
UUID
|
The user ID. |
required |
event
|
AnalyticsEvent
|
The type of the event. |
required |
properties
|
Optional[Dict[Any, Any]]
|
Dict of additional properties for the event. |
required |
Returns:
Type | Description |
---|---|
Tuple[bool, str]
|
Tuple (success flag, the original message). |
Source code in src/zenml/analytics/client.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
Functions
context
The analytics module of ZenML.
This module is based on the 'analytics-python' package created by Segment. The base functionalities are adapted to work with the ZenML analytics server.
Classes
AnalyticsContext()
Client class for ZenML Analytics v2.
Initialization.
Use this as a context manager to ensure that analytics are initialized properly, only tracked when configured to do so and that any errors are handled gracefully.
Source code in src/zenml/analytics/context.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
in_server: bool
property
Flag to check whether the code is running in a ZenML server.
Returns:
Type | Description |
---|---|
bool
|
True if running in a server, False otherwise. |
alias(user_id: UUID, previous_id: UUID) -> bool
Alias user IDs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id
|
UUID
|
The user ID. |
required |
previous_id
|
UUID
|
Previous ID for the alias. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if alias information was sent, False otherwise. |
Source code in src/zenml/analytics/context.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
group(group_id: UUID, traits: Optional[Dict[str, Any]] = None) -> bool
Group the user.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group_id
|
UUID
|
Group ID. |
required |
traits
|
Optional[Dict[str, Any]]
|
Traits of the group. |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if tracking information was sent, False otherwise. |
Source code in src/zenml/analytics/context.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
identify(traits: Optional[Dict[str, Any]] = None) -> bool
Identify the user through segment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
traits
|
Optional[Dict[str, Any]]
|
Traits of the user. |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if tracking information was sent, False otherwise. |
Source code in src/zenml/analytics/context.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
track(event: AnalyticsEvent, properties: Optional[Dict[str, Any]] = None) -> bool
Track an event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
AnalyticsEvent
|
Event to track. |
required |
properties
|
Optional[Dict[str, Any]]
|
Event properties. |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if tracking information was sent, False otherwise. |
Source code in src/zenml/analytics/context.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
Functions
enums
Collection of analytics events for ZenML.
Classes
AnalyticsEvent
Bases: str
, Enum
Enum of events to track in segment.
models
Helper models for ZenML analytics.
Classes
AnalyticsTrackedModelMixin
Bases: BaseModel
Mixin for models that are tracked through analytics events.
Classes that have information tracked in analytics events can inherit
from this mixin and implement the abstract methods. The @track_method
decorator will detect function arguments and return values that inherit
from this class and will include the ANALYTICS_FIELDS
attributes as
tracking metadata.
get_analytics_metadata() -> Dict[str, Any]
Get the analytics metadata for the model.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict of analytics metadata. |
Source code in src/zenml/analytics/models.py
33 34 35 36 37 38 39 40 41 42 |
|
request
The 'analytics' module of ZenML.
This module is based on the 'analytics-python' package created by Segment. The base functionalities are adapted to work with the ZenML analytics server.
Classes
Functions
post(batch: List[str], timeout: int = 15) -> requests.Response
Post a batch of messages to the ZenML analytics server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
List[str]
|
The messages to send. |
required |
timeout
|
int
|
Timeout in seconds. |
15
|
Returns:
Type | Description |
---|---|
Response
|
The response. |
Raises:
Type | Description |
---|---|
AnalyticsAPIError
|
If the post request has failed. |
Source code in src/zenml/analytics/request.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
utils
Utility functions and classes for ZenML analytics.
Classes
AnalyticsAPIError(status: int, message: str)
Bases: Exception
Custom exception class for API-related errors.
Initialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
status
|
int
|
The status code of the response. |
required |
message
|
str
|
The text of the response. |
required |
Source code in src/zenml/analytics/utils.py
61 62 63 64 65 66 67 68 69 |
|
AnalyticsEncoder
Bases: JSONEncoder
Helper encoder class for JSON serialization.
default(obj: Any) -> Any
The default method to handle UUID and 'AnalyticsEvent' objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
The object to encode. |
required |
Returns:
Type | Description |
---|---|
Any
|
The encoded object. |
Source code in src/zenml/analytics/utils.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
analytics_disabler()
Context manager which disables ZenML analytics.
Initialization of the context manager.
Source code in src/zenml/analytics/utils.py
103 104 105 |
|
track_handler(event: AnalyticsEvent, metadata: Optional[Dict[str, Any]] = None)
Bases: object
Context handler to enable tracking the success status of an event.
Initialization of the context manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
AnalyticsEvent
|
The type of the analytics event |
required |
metadata
|
Optional[Dict[str, Any]]
|
The metadata of the event. |
None
|
Source code in src/zenml/analytics/utils.py
207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
Functions
email_opt_int(opted_in: bool, email: Optional[str], source: str) -> None
Track the event of the users response to the email prompt, identify them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
opted_in
|
bool
|
Did the user decide to opt-in |
required |
email
|
Optional[str]
|
The email the user optionally provided |
required |
source
|
str
|
Location when the user replied ["zenml go", "zenml server"] |
required |
Source code in src/zenml/analytics/utils.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
is_analytics_disabled_internally() -> bool
Whether analytics are disabled by an internal helper function.
Returns:
Type | Description |
---|---|
bool
|
Whether analytics are disabled by an internal helper function. |
Source code in src/zenml/analytics/utils.py
131 132 133 134 135 136 137 |
|
track_decorator(event: AnalyticsEvent) -> Callable[[F], F]
Decorator to track event.
If the decorated function takes in a AnalyticsTrackedModelMixin
object as
an argument or returns one, it will be called to track the event. The return
value takes precedence over the argument when determining which object is
called to track the event.
If the decorated function is a method of a class that inherits from
AnalyticsTrackerMixin
, the parent object will be used to intermediate
tracking analytics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
AnalyticsEvent
|
Event string to stamp with. |
required |
Returns:
Type | Description |
---|---|
Callable[[F], F]
|
A decorator that applies the analytics tracking to a function. |
Source code in src/zenml/analytics/utils.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|