|
| 1 | +# Destination Filters Guide |
| 2 | + |
| 3 | +Destination Filters brings Segment's powerful server-side filtering capability directly to your mobile app. This feature allows you to filter analytics events on-device using the same JavaScript filtering logic as your server-side implementation, ensuring consistent filtering behavior across your entire analytics pipeline. |
| 4 | + |
| 5 | +## Core Benefits |
| 6 | + |
| 7 | +- **Consistent Filtering**: Uses the same JavaScript logic as Segment's server-side filters |
| 8 | +- **Device-Mode Support**: Extends filtering capabilities to device-mode destinations |
| 9 | +- **Network Optimization**: Filters events before transmission, reducing bandwidth usage |
| 10 | +- **Data Point Savings**: Prevents unwanted events from being sent, reducing consumed data points |
| 11 | +- **Zero Code Changes**: Configured entirely through the Segment web app |
| 12 | + |
| 13 | +## How It Works |
| 14 | + |
| 15 | +Destination Filters downloads your workspace's filtering rules and applies them locally on the device. When an analytics event is generated: |
| 16 | + |
| 17 | +1. **Event Generated**: Your app creates a track, screen, identify, or group event |
| 18 | +2. **Filters Applied**: The event is checked against your downloaded filtering rules |
| 19 | +3. **Destination Routing**: Only events that pass the filters are sent to their respective destinations |
| 20 | +4. **Consistent Logic**: The same filtering logic runs on-device and server-side |
| 21 | + |
| 22 | +This ensures that device-mode destinations (like Firebase, Amplitude, etc.) receive exactly the same filtered events as your server-side destinations. |
| 23 | + |
| 24 | +## Setup |
| 25 | + |
| 26 | +### Prerequisites |
| 27 | + |
| 28 | +1. **Segment Workspace**: Destination Filters enabled on your workspace |
| 29 | +2. **Web App Configuration**: Filters configured at https://app.segment.com |
| 30 | +3. **Analytics-Swift**: Version 1.8.0 or higher |
| 31 | + |
| 32 | +> **Note**: Destination Filters may need to be enabled on your workspace by your Segment representative. |
| 33 | +
|
| 34 | +### Basic Setup |
| 35 | + |
| 36 | +```swift |
| 37 | +import AnalyticsLive |
| 38 | + |
| 39 | +let filters = DestinationFilters() |
| 40 | +Analytics.main.add(plugin: filters) |
| 41 | +``` |
| 42 | + |
| 43 | +That's it! The plugin automatically: |
| 44 | +- Downloads your workspace's filtering rules |
| 45 | +- Applies them to all events |
| 46 | +- Updates rules when your workspace configuration changes |
| 47 | + |
| 48 | +### Complete Setup Example |
| 49 | + |
| 50 | +```swift |
| 51 | +import AnalyticsLive |
| 52 | + |
| 53 | +// Set up your analytics instance as usual |
| 54 | +let analytics = Analytics(configuration: Configuration( |
| 55 | + writeKey: "<YOUR WRITE KEY>" |
| 56 | +)) |
| 57 | + |
| 58 | +// Add destination filters |
| 59 | +let filters = DestinationFilters() |
| 60 | +analytics.add(plugin: filters) |
| 61 | + |
| 62 | +// Add other plugins as needed |
| 63 | +let livePlugins = LivePlugins() |
| 64 | +analytics.add(plugin: livePlugins) |
| 65 | + |
| 66 | +// Your app is now filtering events on-device! |
| 67 | +``` |
| 68 | + |
| 69 | +## Configuration |
| 70 | + |
| 71 | +Destination Filters are configured entirely through the Segment web app - no code changes required. |
| 72 | + |
| 73 | +### Web App Configuration |
| 74 | + |
| 75 | +1. **Log into Segment**: Visit https://app.segment.com |
| 76 | +2. **Navigate to Destinations**: Select your workspace and go to Destinations |
| 77 | +3. **Configure Filters**: Set up filtering rules for your destinations |
| 78 | +4. **Test Filters**: Use Segment's testing tools to verify filter behavior |
| 79 | +5. **Deploy**: Changes are automatically downloaded to your mobile apps |
| 80 | + |
| 81 | +### Filter Types |
| 82 | + |
| 83 | +Destination Filters supports all Segment filter types: |
| 84 | + |
| 85 | +#### Event Filters |
| 86 | +Filter events based on event name, properties, or context: |
| 87 | +```javascript |
| 88 | +// Example: Only allow purchase events over $50 |
| 89 | +event.event === "Purchase" && event.properties.value > 50 |
| 90 | +``` |
| 91 | + |
| 92 | +#### Property Filters |
| 93 | +Transform or remove specific properties: |
| 94 | +```javascript |
| 95 | +// Example: Remove PII from events |
| 96 | +delete event.properties.email |
| 97 | +delete event.context.traits.phone |
| 98 | +``` |
| 99 | + |
| 100 | +#### Destination-Specific Filters |
| 101 | +Apply different filtering logic to different destinations: |
| 102 | +```javascript |
| 103 | +// Example: Send full data to analytics, limited data to advertising |
| 104 | +if (destination.name === "Google Analytics") { |
| 105 | + return event |
| 106 | +} else if (destination.name === "Facebook Pixel") { |
| 107 | + delete event.properties.customer_id |
| 108 | + return event |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Benefits and Use Cases |
| 113 | + |
| 114 | +### Network Optimization |
| 115 | + |
| 116 | +By filtering events on-device, you reduce: |
| 117 | +- **Bandwidth Usage**: Fewer HTTP requests to destination APIs |
| 118 | +- **Battery Consumption**: Less network activity |
| 119 | +- **Data Costs**: Important for users on limited data plans |
| 120 | + |
| 121 | +### Data Point Management |
| 122 | + |
| 123 | +Prevent unwanted events from consuming your destination quotas: |
| 124 | +- **Debug Events**: Filter out test/debug events in production |
| 125 | +- **Spam Prevention**: Block malformed or excessive events |
| 126 | +- **Compliance**: Remove events containing PII for specific destinations |
| 127 | + |
| 128 | +### Consistent User Experience |
| 129 | + |
| 130 | +Ensure device-mode and server-side destinations receive identical data: |
| 131 | +- **A/B Testing**: Consistent test group assignment across all destinations |
| 132 | +- **Feature Flags**: Same feature visibility logic everywhere |
| 133 | +- **User Segmentation**: Identical user categorization |
| 134 | + |
| 135 | +## Advanced Scenarios |
| 136 | + |
| 137 | +### Development vs Production |
| 138 | + |
| 139 | +Use different filtering strategies for development and production: |
| 140 | + |
| 141 | +```javascript |
| 142 | +// Example filter that blocks test events in production |
| 143 | +if (event.properties.environment === "test" && context.app.version.includes("prod")) { |
| 144 | + return null // Block test events in production builds |
| 145 | +} |
| 146 | +return event |
| 147 | +``` |
| 148 | + |
| 149 | +### Gradual Rollouts |
| 150 | + |
| 151 | +Implement percentage-based filtering for gradual feature rollouts: |
| 152 | + |
| 153 | +```javascript |
| 154 | +// Example: Send new event type to only 10% of users |
| 155 | +if (event.event === "New Feature Used") { |
| 156 | + const userId = event.userId || event.anonymousId |
| 157 | + const hash = simpleHash(userId) |
| 158 | + if (hash % 100 < 10) { |
| 159 | + return event // Send to 10% of users |
| 160 | + } |
| 161 | + return null // Block for 90% of users |
| 162 | +} |
| 163 | +return event |
| 164 | +``` |
| 165 | + |
| 166 | +### Compliance and Privacy |
| 167 | + |
| 168 | +Automatically remove sensitive data for specific regions or destinations: |
| 169 | + |
| 170 | +```javascript |
| 171 | +// Example: Remove PII for GDPR compliance |
| 172 | +if (event.context.location.country === "Germany" && destination.name === "Marketing Tool") { |
| 173 | + delete event.properties.email |
| 174 | + delete event.properties.phone |
| 175 | + delete event.context.traits.firstName |
| 176 | + delete event.context.traits.lastName |
| 177 | +} |
| 178 | +return event |
| 179 | +``` |
| 180 | + |
| 181 | +## Troubleshooting |
| 182 | + |
| 183 | +### Filters Not Applied |
| 184 | + |
| 185 | +If your filters aren't working: |
| 186 | + |
| 187 | +1. **Check Workspace Settings**: Ensure Destination Filters is enabled |
| 188 | +2. **Verify Filter Logic**: Test filters in the Segment web app |
| 189 | +3. **App Restart**: Restart your app to download latest filter rules |
| 190 | +4. **Network Connectivity**: Ensure the device can reach Segment's CDN |
| 191 | + |
| 192 | +### Testing Filters |
| 193 | + |
| 194 | +To verify filters are working: |
| 195 | + |
| 196 | +1. **Segment Debugger**: Use the live debugger to see filtered events |
| 197 | +2. **Destination Analytics**: Check if filtered events appear in destination dashboards |
| 198 | +3. **Console Logging**: Add temporary logging to your filter JavaScript |
| 199 | +4. **A/B Testing**: Create simple test filters to verify behavior |
| 200 | + |
| 201 | +### Performance Considerations |
| 202 | + |
| 203 | +- **Filter Complexity**: Keep filter logic simple for best performance |
| 204 | +- **Event Volume**: High-volume apps should test filter performance thoroughly |
| 205 | +- **Memory Usage**: Complex filters may increase memory usage slightly |
| 206 | + |
| 207 | +## Migration from Server-Side Only |
| 208 | + |
| 209 | +If you're currently using only server-side filters: |
| 210 | + |
| 211 | +1. **Enable Feature**: Contact your Segment representative to enable Destination Filters |
| 212 | +2. **Copy Logic**: Your existing server-side filter logic will work on-device |
| 213 | +3. **Test Thoroughly**: Verify consistent behavior between server and device filtering |
| 214 | +4. **Monitor Performance**: Watch for any performance impact in your mobile app |
| 215 | +5. **Gradual Rollout**: Consider enabling for a subset of users initially |
| 216 | + |
| 217 | +Your existing filter configurations will automatically apply to device-mode destinations once Destination Filters is enabled. |
0 commit comments