How to Use Channels for Feature Flags and A/B Testing
CapGo’s channel system provides a flexible way to segment users and control feature access. While CapGo doesn’t have built-in plan management or A/B testing, you can implement these features by managing channel assignments yourself.
Understanding Channels
Channels in CapGo allow you to:
- Target specific user groups with different features
- Run A/B tests by assigning users to different channels
- Gradually roll out new features
- Create beta testing programs
Channel Assignment Methods
1. Backend Assignment (Recommended)
This is the more secure method. It involves:
- Getting the device ID from the updater
- Sending it to your backend
- Your backend calls the CapGo API to assign the device
Here’s how to implement it:
import { CapacitorUpdater } from '@capgo/capacitor-updater'
// Get device IDconst getDeviceId = async () => { const { deviceId } = await CapacitorUpdater.getDeviceId() return deviceId}
// Send device ID to your backendconst assignToChannel = async (channel: string) => { const deviceId = await getDeviceId() // Your backend will call CapGo API to assign the device await yourBackend.assignDeviceToChannel(deviceId, channel)}
Backend Implementation
Your backend needs to:
- Get an API key from CapGo dashboard
- Call the CapGo API to assign the device to a channel
To get your API key:
- Log in to your CapGo dashboard
- Go to Settings > API Keys
- Click “Generate New Key”
- Select
all
mode to manage devices and channels - Copy the generated key and store it securely in your backend environment variables
- The key will be a 32-character hexadecimal string
- It’s a secret key that should never be exposed in client-side code
Here’s a Node.js example:
import axios from 'axios'
const CAPGO_API_KEY = 'your_api_key'const CAPGO_API_URL = 'https://api.capgo.app'
async function assignDeviceToChannel(deviceId: string, channel: string) { try { const response = await axios.post( `${CAPGO_API_URL}/device`, { app_id: 'YOUR_APP_ID', device_id: deviceId, channel: channel }, { headers: { 'authorization': CAPGO_API_KEY, 'Content-Type': 'application/json' } } ) return response.data } catch (error) { console.error('Failed to assign device to channel:', error) throw error }}
The backend should also:
- Validate the user’s permissions
- Log all channel assignments
- Handle rate limiting
- Implement retry logic for failed assignments
2. Self-Assignment (Less Secure)
This method allows devices to directly assign themselves to a channel. It’s useful for testing but less secure for production:
import { CapacitorUpdater } from '@capgo/capacitor-updater'
// Assign device to channelconst assignToChannel = async (channel: string) => { await CapacitorUpdater.setChannel(channel)}
// Get current channelconst getCurrentChannel = async () => { const { channel } = await CapacitorUpdater.getChannel() return channel}
Before users can self-assign to a channel, you need to enable this feature in the CapGo dashboard:
- Go to the Channels section in your CapGo dashboard
- Click on the channel name you want to manage
- In the channel settings, enable “Allow devices to self associate”
- Save the changes
If this setting is false, any attempt to call setChannel
with this channel will fail.
Implementing Feature Flags
Use channels to control feature access:
const isFeatureEnabled = async (feature: string) => { // Example: Check if user is in beta channel const channel = await getCurrentChannel() return channel === 'beta'}
A/B Testing Implementation
Run A/B tests by assigning users to different channels:
const assignToABTest = async (userId: string) => { // Use consistent hashing to assign users const hash = await hashUserId(userId) const variant = hash % 2 === 0 ? 'variant-a' : 'variant-b'
await assignToChannel(variant) return variant}
Best Practices
- Use Backend Assignment: For production, always use the backend assignment method
- Consistent Assignment: Use user IDs or other stable identifiers for consistent channel assignment
- Monitoring: Track feature usage and performance metrics for each channel
- Gradual Rollouts: Start with small user segments and gradually expand
- Clear Documentation: Document your channel strategy and purposes
Conclusion
By leveraging CapGo’s channel system, you can create more personalized app experiences and run A/B tests. For production use, always prefer the backend assignment method for better security and control.
For more details on channel management, check out our channels documentation.