ユーザーが Firebase Performance Monitoring の使用をオプトインまたはオプトアウトできるようにするには、Performance Monitoring の有効または無効を切り替えられるようにアプリを構成します。この機能はアプリの開発やテストの際にも役立つ場合があります。
また、次のオプションについても考慮してください。
アプリをビルドするときに Performance Monitoring SDK を無効にし、ランタイム時に必要に応じて再度有効にする。
Performance Monitoring SDK を有効にした状態でアプリをビルドできるが、ランタイム時に Firebase Remote Config を使用して SDK を無効にすることもできる。
Performance Monitoring SDK を完全に無効にし、ランタイム時に再度有効にできないようにする。
アプリのビルドプロセス時に Performance Monitoring を無効にする
アプリのビルドプロセス中に Performance Monitoring を無効にしたほうがよい状況の一例としては、アプリの開発またはテスト中に、リリース前バージョンのアプリからパフォーマンス データが報告されないようにする場合が挙げられます。
Performance Monitoring を無効にする、または完全に無効にするには、Apple アプリのプロパティ リストファイル(Info.plist
)に次の 2 つのキーのいずれか一方を追加します。
Performance Monitoring を無効にし、ランタイム時にアプリで有効にできるようにするには、アプリの
Info.plist
ファイルでfirebase_performance_collection_enabled
をfalse
に設定します。Performance Monitoring を完全に無効にし、ランタイム時に有効にできないようにするには、アプリの
Info.plist
ファイルでfirebase_performance_collection_deactivated
をtrue
に設定します。
Remote Config を使用してランタイム時にアプリを無効にする
Firebase Remote Config を使用すると、アプリの動作や外観を変更できます。これは、デプロイされたアプリのインスタンスで Performance Monitoring を無効にする場合に最適な方法です。
次回 Apple アプリが起動したときに Performance Monitoring のデータ収集を無効にするには、下記のサンプルコードを使用します。Apple アプリで Remote Config を使用する方法については、Firebase Remote Config を Apple プラットフォームで使用するをご覧ください。
Podfile
に次のコードを記述し、Remote Config が使用されるようにします。pod 'Firebase/RemoteConfig'
アプリの
AppDelegate
ファイルの先頭に次のコードを追加します。Swift
注: この Firebase プロダクトは、macOS、Mac Catalyst、watchOS の各ターゲットでは使用できません。import FirebaseRemoteConfig
Objective-C
注: この Firebase プロダクトは、macOS、Mac Catalyst、watchOS の各ターゲットでは使用できません。@import FirebaseRemoteConfig;
AppDelegate
ファイルで、application:didFinishLaunchingWithOptions:
インスタンス メソッドのlaunchOptions
ステートメントに次のコードを追加します。Swift
注: このプロダクトは、macOS、Mac Catalyst、watchOS の各ターゲットでは使用できません。remoteConfig = RemoteConfig.remoteConfig() // You can change the "false" below to "true" to permit more fetches when validating // your app, but you should change it back to "false" or remove this statement before // distributing your app in production. let remoteConfigSettings = RemoteConfigSettings(developerModeEnabled: false) remoteConfig.configSettings = remoteConfigSettings! // Load in-app defaults from a plist file that sets perf_disable to false until // you update values in the Firebase console. remoteConfig.setDefaultsFromPlistFileName("RemoteConfigDefaults") // Important! This needs to be applied before FirebaseApp.configure() if !remoteConfig["perf_disable"].boolValue { // The following line disables all automatic (out-of-the-box) monitoring Performance.sharedInstance().isInstrumentationEnabled = false // The following line disables all custom monitoring Performance.sharedInstance().isDataCollectionEnabled = false } else { Performance.sharedInstance().isInstrumentationEnabled = true Performance.sharedInstance().isDataCollectionEnabled = true } // Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
注: この Firebase プロダクトは、macOS、Mac Catalyst、watchOS の各ターゲットでは使用できません。self.remoteConfig = [FIRRemoteConfig remoteConfig]; // You can change the NO below to YES to permit more fetches when validating // your app, but you should change it back to NO or remove this statement before // distributing your app in production. FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:NO]; self.remoteConfig.configSettings = remoteConfigSettings; // Load in-app defaults from a plist file that sets perf_disable to false until // you update values in the Firebase console. [self.remoteConfig setDefaultsFromPlistFileName:@"RemoteConfigDefaults"]; // Important! This needs to be applied before [FIRApp configure] if (!self.remoteConfig[@"perf_disable"].numberValue.boolValue) { // The following line disables all automatic (out-of-the-box) monitoring [FIRPerformance sharedInstance].instrumentationEnabled = NO; // The following line disables all custom monitoring [FIRPerformance sharedInstance].dataCollectionEnabled = NO; } else { [FIRPerformance sharedInstance].instrumentationEnabled = YES; [FIRPerformance sharedInstance].dataCollectionEnabled = YES; } // Use Firebase library to configure APIs [FIRApp configure];
ViewController.m
、またはアプリで使用する別の実装ファイルに次のコードを追加し、Remote Config の値をフェッチして有効にします。Swift
注: この Firebase プロダクトは、macOS、Mac Catalyst、watchOS の各ターゲットでは使用できません。//RemoteConfig fetch and activation in your app, shortly after startup remoteConfig.fetch(withExpirationDuration: TimeInterval(30.0)) { (status, error) -> Void in if status == .success { print("Config fetched!") self.remoteConfig.activateFetched() } else { print("Config not fetched") print("Error \(error!.localizedDescription)") } }
Objective-C
注: この Firebase プロダクトは、macOS、Mac Catalyst、watchOS の各ターゲットでは使用できません。//RemoteConfig fetch and activation in your app, shortly after startup [self.remoteConfig fetchWithExpirationDuration:30.0 completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) { if (status == FIRRemoteConfigFetchStatusSuccess) { NSLog(@"Config fetched!"); [self.remoteConfig activateFetched]; } else { NSLog(@"Config not fetched"); NSLog(@"Error %@", error.localizedDescription); } }];
Firebase コンソールで Performance Monitoring を無効にするには、アプリのプロジェクトに perf_disable パラメータを作成し、値を
true
に設定します。perf_disable の値を
false
に設定すると、Performance Monitoring は引き続き有効になります。
自動またはカスタムのデータ収集を個別に無効にする
上記のコードを一部変更すると、Firebase コンソールで、すべての自動(設定不要)モニタリングの無効化とカスタム モニタリングの無効化を個別に行えます。
application:didFinishLaunchingWithOptions:
インスタンス メソッドのlaunchOptions
ステートメントに次のコードを追加します(上記の例でも同じインスタンス メソッドにコードを追加していますが、記述内容が異なります)。Swift
注: この Firebase プロダクトは、macOS、Mac Catalyst、watchOS の各ターゲットでは使用できません。remoteConfig = FIRRemoteConfig.remoteConfig() let remoteConfigSettings = FIRRemoteConfigSettings(developerModeEnabled: true) remoteConfig.configSettings = remoteConfigSettings! // Important! This needs to be applied before FirebaseApp.configure() if remoteConfig["perf_disable_auto"].boolValue { // The following line disables all automatic (out-of-the-box) monitoring Performance.sharedInstance().isInstrumentationEnabled = false } else { Performance.sharedInstance().isInstrumentationEnabled = true } if remoteConfig["perf_disable_manual"].boolValue { // The following line disables all custom monitoring Performance.sharedInstance().isDataCollectionEnabled = false } else { Performance.sharedInstance().isDataCollectionEnabled = true } // Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
注: この Firebase プロダクトは、macOS、Mac Catalyst、watchOS の各ターゲットでは使用できません。self.remoteConfig = [FIRRemoteConfig remoteConfig]; FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES]; self.remoteConfig.configSettings = remoteConfigSettings; // Important! This needs to be applied before [FirebaseApp configure] if (self.remoteConfig[@"perf_disable_auto"].numberValue.boolValue) { // The following line disables all automatic (out-of-the-box) monitoring [FIRPerformance sharedInstance].instrumentationEnabled = NO; } else { [FIRPerformance sharedInstance].instrumentationEnabled = YES; } if (self.remoteConfig[@"perf_disable_manual"].numberValue.boolValue) { // The following line disables all custom monitoring [FIRPerformance sharedInstance].dataCollectionEnabled = NO; } else { [FIRPerformance sharedInstance].dataCollectionEnabled = YES; } // Use Firebase library to configure APIs [FirebaseApp configure];
Firebase コンソールで次の操作を行います。
- すべての自動(設定不要)モニタリングを無効にするには、アプリのプロジェクトに perf_disable_auto パラメータを作成し、値を
true
に設定します。 - すべてのカスタム モニタリングを無効にするには、アプリのプロジェクトに perf_disable_manual パラメータを作成し、値を
true
に設定します。
- すべての自動(設定不要)モニタリングを無効にするには、アプリのプロジェクトに perf_disable_auto パラメータを作成し、値を