TriggersProvider enables real-time reactive programming patterns by allowing you to observe cohort membership changes and trigger actions when users enter or exit specific cohorts. This is powerful for personalizing user experiences, A/B testing, and dynamic content delivery.
Overview
The Triggers Provider allows you to:- React to cohort changes in real-time - Execute code when users enter or exit cohorts
- Observe activations - Monitor when users qualify for specific ad platform activations
- Build personalized experiences - Show/hide content based on cohort membership
- Implement A/B tests - Dynamically segment users and track their behavior
Creating a TriggersProvider
Create aTriggersProvider instance from your Permutive SDK instance:
💡 Lifecycle Management
You can create any number of TriggersProvider instances. They’re lightweight and don’t maintain heavy state.
Use Cases
1. Observing Cohort Membership
Monitor when a user is in a specific cohort:2. Observing Multiple Cohorts
React to multiple cohort memberships:Kotlin
3. Observing Activations for Ad Platforms
Monitor cohorts activated for specific ad platforms:Kotlin
Available Activation Types
| Activation Type | Description |
|---|---|
"dfp" | Google Ad Manager (formerly DoubleClick for Publishers) activations |
"appnexus_adserver" | Xandr/AppNexus ad server activations |
"freewheel" | Freewheel activations |
"dfp_contextual" | Google Ad Manager contextual cohorts |
"appnexus_adserver_contextual" | Xandr/AppNexus contextual cohorts |
4. A/B Testing Based on Cohorts
Implement feature flags or A/B tests using cohort membership:Kotlin
TriggerAction Lifecycle
TheTriggerAction interface represents an active trigger subscription. It’s crucial to manage its lifecycle properly to prevent memory leaks.
Key Points
- Create triggers when you need to start observing
- Close triggers when you’re done (typically in
onDestroy()) - Store references if you need to close them later
- Don’t recreate unnecessarily - triggers survive configuration changes if managed properly
Best Practices
✅ Good: Proper Lifecycle Management
✅ Good: Using ViewModel for Configuration Changes
❌ Bad: Not Closing Triggers
Thread Safety
TheTriggersProvider and TriggerAction are thread-safe. Callbacks are delivered on a background thread, so if you need to update UI, use appropriate threading mechanisms:
Advanced Patterns
Combining Multiple Cohorts
Trigger actions only when user is in multiple cohorts:Deprecated APIs
Legacy Segment-Based Triggers
The following methods are deprecated as they only support integer-based segment IDs:📘 Why the Change? The SDK now supports Classification Model cohorts which use string IDs. The new APIs support both traditional integer-based cohorts and newer string-based cohorts seamlessly.
Troubleshooting
Callbacks Not Firing
Callbacks Not Firing
Problem: Your trigger callback is never invoked.Possible Causes:
- SDK hasn’t finished initializing
- Cohort ID is incorrect
- User hasn’t generated enough events to be segmented
- Enable debug logging:
permutive.setDeveloperMode(true) - Check logs for cohort updates
- Verify cohort ID matches your dashboard configuration
- Ensure events are being tracked (see Verification Guide)
Memory Leaks
Memory Leaks
Problem: App memory usage grows over time.Cause:
TriggerAction instances not being closed.Solution: Always call close() on triggers in appropriate lifecycle methods (usually onDestroy() or onCleared()).Callbacks on Wrong Thread
Callbacks on Wrong Thread
Problem: Crash when trying to update UI from callback.Cause: Callbacks execute on background threads.Solution: Use
Handler, coroutines, or runOnUiThread() to update UI (see Thread Safety section above).Best Practices
- Do
- Don't
- Close all triggers when done to prevent memory leaks
- Handle threading appropriately when updating UI
- Use descriptive cohort IDs that match your dashboard
- Store trigger references if you need to close them later
- Test with debug logging enabled
Example: Complete Implementation
Here’s a complete example showing proper implementation:Related Documentation
Cohorts and Activations
Understanding how cohorts work
Event Tracking
How events feed into segmentation
Identity Management
Tracking users across sessions
Contextual Data
Content-based segmentation
API Reference
For complete API documentation, see the Javadocs.Key Interfaces
TriggersProvider- Factory for creating trigger subscriptionsTriggerAction- Represents an active trigger subscriptionMethod<T>- Callback interface for Java compatibility