Define interventions with the Signals Python SDK
To use the Signals Python SDK to define interventions, start by connecting to Signals to create a Signals object. You'll need this connection to publish interventions.
There are two ways to define an intervention using the SDK or Signals API:
- Rule-based interventions (default)
- Direct interventions
Rule-based interventions
Rule-based interventions are triggered automatically when their criteria are met. They use RuleIntervention objects, and are published to Signals using publish(), similar to other configuration objects.
An intervention is sent only the first time the criteria are met. Read an example of how this works on the Concepts page.
This is the minimum configuration needed to create an intervention definition:
from snowplow_signals import RuleIntervention, InterventionCriterion
hello_intervention = RuleIntervention(
name="say_hello",
owner="me@example.com",
criteria=InterventionCriterion(
attribute="example_group:test_attribute",
operator="is not null",
),
)
sp_signals.publish([hello_intervention])
Once applied and active, this intervention will trigger the first time Signals processes an event that first sets the example_group attribute group's test_attribute attribute to a value that is not null (e.g. the first time it gets set).
Options
The table below lists all available arguments for a RuleIntervention:
| Argument | Description | Type | Required? |
|---|---|---|---|
name | The unique name/identifier of the intervention | string | ✅ |
owner | Email address for the owner of this intervention definition | string | ✅ |
description | A human-readable description of the intervention | string | ❌ |
version | A numeric version for this definition | integer | ❌ |
target_attribute_keys | List of attribute key names to publish this intervention to. If not defined, defaults to the attribute keys associated with any attribute groups you reference in criteria. | string[] | ❌ |
criteria | Tree of Criterion expressions to evaluate against attribute key attributes | One of: InterventionCriterion, InterventionCriteriaAll, InterventionCriteriaAny, InterventionCriteriaNone | ✅ |
Evaluating attributes with criteria
The criteria tree defines the conditions that an attribute key's attributes should meet to be eligible for the intervention to trigger.
When a referenced attribute is updated, the updated and previous states are evaluated against the criteria. If the previous state did not meet the conditions but the newly updated state does, the trigger activates and the intervention gets published to the attribute keys that have a value and are defined in the target_attributes_key setting.
Criteria always refer to the latest published version of the attribute group that contained that attribute.
The simplest criteria tree takes an InterventionCriterion instance, with possible arguments:
| Argument | Description | Type |
|---|---|---|
attribute | The attribute group and attribute name to evaluate, separated by a colon | string |
operator | The operator used for evaluating the attribute | str, see table below |
value | If required by the operator, the comparison value | str, int, float, bool, list[str], list[int], list[float], list[bool] or None |
This table shows the available operators:
| Operator | Description | Value is required? |
|---|---|---|
= | Equals | ✅ |
!= | Not equals | ✅ |
< | Less than | ✅ |
> | Greater than | ✅ |
<= | Less than or equal | ✅ |
>= | Greater than or equal |