しめ鯖日記

swift, iPhoneアプリ開発, ruby on rails等のTipsや入門記事書いてます

FirebaseRemoteConfigでA/Bテストを試してみる

Firebaseを使う準備

まずは下サイトで新しいプロジェクトを作成します。

Firebase Console

f:id:llcc:20161215213452p:plain

プロジェクトを作成すると以下のような画面になるのでiOS アプリに Firebase を追加を選びます。

f:id:llcc:20161215213525p:plain

情報を入力してアプリを追加します。

f:id:llcc:20161215213609p:plain

次画面に移動するとGoogleService-Info.plistというファイルのダウンロードが開始します。
このファイルは後で使います。

f:id:llcc:20161215213643p:plain

次はXcodeでプロジェクトを作成します。
SingleViewApplicationを選択します。

f:id:llcc:20161215213858p:plain

次にFirebaseをインストールします。
Podsファイルを作成して以下のように修正して下さい。

platform :ios, ’10.0’

target 'MyApp' do
  use_frameworks!

  pod 'Firebase/Core'
  pod 'Firebase/RemoteConfig'
end

先程ダウンロードしたGoogleService-Info.plistをプロジェクトに追加します。

f:id:llcc:20161215214019p:plain

最後にAppDelegate.swiftへFirebase初期化処理を追加します。
これでFirebaseを使えるようになりました。

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        FIRApp.configure()
        
        return true
    }
}

FirebaseRemoteConfigを使ってみる

続けてFirebaseRemoteConfigを使ってみます。
Firebaseの管理画面に戻り、左メニューのRemote Configを選択します。

f:id:llcc:20161215214415p:plain

そのページで最初のパラメータを追加ボタンを押してキーと値を追加します。

f:id:llcc:20161215214524p:plain

最後に変更を公開ボタンを押して追加したパラメータを有効化します。

f:id:llcc:20161215214834p:plain

設定したパラメータの取得方法は以下の通りです。

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    let remoteConfig = FIRRemoteConfig.remoteConfig()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        FIRApp.configure()
        remoteConfig.fetch { [weak self] status, error in
            print(status.rawValue)
            print(error)
            self?.remoteConfig.activateFetched() // これでパラメータを取得できる
            print(self?.remoteConfig["test_key"].stringValue) // → "test_value"
        }
        
        return true
    }
}

FirebaseRemoteConfigでA/Bテストをする

最後にFirebaseRemoteConfigを使ったA/Bテストの方法を見ていきます。
まず管理画面上の条件を追加ボタンから条件を追加します。

f:id:llcc:20161215223617p:plain

条件はUser in random percentileにして、好きな割合を入力します。

f:id:llcc:20161215223714p:plain

保存すると、2種類の値を設定できるようになります。
これらは先程設定した割合に応じて出しわけられます。

f:id:llcc:20161215223823p:plain

余談ですが、条件は推定地域などを選ぶ事ができます。
国に応じて出し分けをしたい場合などに有効そうです。

f:id:llcc:20161215223914p:plain

最後にFirebaseAnalyticsの設定を行います。
サイドメニューのAnalyticsタブのユーザープロパティーを選択して下さい。

f:id:llcc:20161215224125p:plain

新しいユーザーのプロパティーボタンからプロパティーをセットします。

f:id:llcc:20161215224240p:plain

次はXcode上でユーザーのプロパティーをセットします。
FIRAnalytics.setUserPropertyStringを使ってプロパティーのセットを行って下さい。

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    let remoteConfig = FIRRemoteConfig.remoteConfig()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        FIRApp.configure()
        remoteConfig.fetch { [weak self] status, error in
            self?.remoteConfig.activateFetched()
            FIRAnalytics.setUserPropertyString(self?.remoteConfig["test_key"].stringValue, forName: "my_property")
        }
        
        return true
    }
}

これで実装は完了です。
Analytics上でプロパティー毎の数字を見る事ができるので、これを使って値の出し分けの成果を見ます。

f:id:llcc:20161215224559p:plain

その他

FirebaseRemoteConfigはとてもおもしろかったのですが、コホート分析でプロパティーによるフィルターが使えないのが少し痛かったです。
アクティブ率をKPIにする事が多いので、ここでA/Bテストを多用したかったです。

f:id:llcc:20161215224837p:plain

この辺りはGoogleAnalyticsのカスタムディメンションを使ったほうがうまくいくかもしれません。
GoogleAnalyticsだと下のように見ることができます。

f:id:llcc:20161221103050p:plain

参考URL

Firebase Remote Config  |  Firebase