Skip to main content
The 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.

Creating Provider

Use Cases

Lifecycle

Advanced

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 a TriggersProvider 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


4. A/B Testing Based on Cohorts

Implement feature flags or A/B tests using cohort membership:

Kotlin


TriggerAction Lifecycle

The TriggerAction interface represents an active trigger subscription. It’s crucial to manage its lifecycle properly to prevent memory leaks.

Key Points

  1. Create triggers when you need to start observing
  2. Close triggers when you’re done (typically in onDestroy())
  3. Store references if you need to close them later
  4. 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

The TriggersProvider 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:
Migration Guide: Old (Deprecated):
New:
📘 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

Problem: Your trigger callback is never invoked.Possible Causes:
  1. SDK hasn’t finished initializing
  2. Cohort ID is incorrect
  3. User hasn’t generated enough events to be segmented
Solutions:
  • 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)
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()).
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

  • 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:

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 subscriptions
  • TriggerAction - Represents an active trigger subscription
  • Method<T> - Callback interface for Java compatibility