機能フラグとA/Bテストのためのチャンネルの使用方法
Capgoのチャンネルシステムは、ユーザーを分割し機能へのアクセスを制御するための柔軟な方法を提供します。Capgoには、プラン管理やA/Bテストの機能は組み込まれていませんが、チャンネル割り当てを自分で管理することで、これらの機能を実現できます。
チャンネルの理解
チャンネルは、Capgoで次のことが可能です。
- 特定のユーザーグループに異なる機能を提供することができます。
- A/B テストを実行するには、ユーザーを異なるチャネルに割り当てる
- 新機能を段階的にリリースする
- ベータテストプログラムを作成する
チャネル割り当て方法
1. バックエンド割り当て (推奨)
__CAPGO_KEEP_0__ と __CAPGO_KEEP_1__ を呼び出すために、バックエンドは __CAPGO_KEEP_0__ を呼び出す必要があります。
- この方法は、次の手順で実行されます。
- デバイス ID を取得する
- Your backend calls the Capgo API to assign the device
バックエンドは、デバイスを特定のチャネルに割り当てるために __CAPGO_KEEP_0__ を呼び出します。
import { CapacitorUpdater } from '@capgo/capacitor-updater'
// Get device ID
const getDeviceId = async () => {
const { deviceId } = await CapacitorUpdater.getDeviceId()
return deviceId
}
// Send device ID to your backend
const assignToChannel = async (channel: string) => {
const deviceId = await getDeviceId()
// Your backend will call Capgo API to assign the device
await yourBackend.assignDeviceToChannel(deviceId, channel)
}
実装方法
バックエンドの実装
- CapgoのAPIキーをCapgoダッシュボードから取得
- Capgo APIを呼び出して、デバイスをチャンネルに割り当てる
API キーを取得するには
- Capgo ダッシュボードにログイン
- 設定 > API キーに移動
- 「新しいキーを生成」ボタンをクリック
- 選択
allモードを使用してデバイスとチャンネルを管理 - 生成されたキーを安全にバックエンド環境変数に保存
- キーは32桁の16進数文字列
- code側のクライアント側では絶対に公開してはいけない秘密のキー
Node.jsの例
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
}
}
バックエンドも以下を実行する必要があります。
- ユーザーの権限を検証する
- すべてのチャネル割り当てをログに記録する
- リクエスト制限を実行する
- 失敗した割り当てに対してリトライロジックを実装する
2. 自動割り当て (セキュリティが低い)
このメソッドは、デバイスが直接チャネルに割り当てられることを許可します。テストに便利ですが、生産環境ではセキュリティが低いです。
import { CapacitorUpdater } from '@capgo/capacitor-updater'
// Assign device to channel
const assignToChannel = async (channel: string) => {
await CapacitorUpdater.setChannel(channel)
}
// Get current channel
const getCurrentChannel = async () => {
const { channel } = await CapacitorUpdater.getChannel()
return channel
}
ユーザーがチャネルに自動割り当てできるようにする前に、Capgo ダッシュボードでこの機能を有効にする必要があります。
- Capgo ダッシュボードのチャネルセクションに移動する
- __CAPGO_KEEP_0__ ダッシュボードのチャネル名をクリックする
- チャネル設定で「デバイスが自動的に関連付けられることを許可する」にチェックを入れる
- 変更を保存する
この設定がfalseの場合、チャネルにこの設定を使用するたびに失敗します。 setChannel チャネルを使用して機能アクセスを制御する
機能フラグの実装
機能アクセスを制御するためにチャネルを使用する
const isFeatureEnabled = async (feature: string) => {
// Example: Check if user is in beta channel
const channel = await getCurrentChannel()
return channel === 'beta'
}
A/Bテストの実装
ユーザーを異なるチャネルに割り当ててA/Bテストを実行する
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
}
ベストプラクティス
- バックエンド割り当てを使用するプロダクションでは、常にバックエンド割り当てメソッドを使用する
- 一貫した割り当てユーザーIDや他の安定した識別子を使用して、一貫したチャネル割り当てを実行する
- 監視: __CAPGO_KEEP_0__の機能使用とパフォーマンスメトリクスを各チャネルでトラッキング
- Gradual Rollouts: 小規模なユーザーセグメントから始めて徐々に拡大
- Clear Documentation: チャネル戦略と目的をドキュメント化
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.
Capgoのチャネル管理に関する詳細は、 channels documentation.