ユーザーが Firebase Performance Monitoring の使用をオプトインまたはオプトアウトできるようにするには、Performance Monitoring の有効または無効を切り替えられるようにアプリを構成します。この機能はアプリの開発やテストの際にも役立つ場合があります。
また、次のオプションについても考慮してください。
- アプリをビルドするときに Performance Monitoring SDK を無効にし、ランタイム時に必要に応じて再度有効にする。 
- Performance Monitoring SDK を有効にした状態でアプリをビルドするが、ランタイム時に Firebase Remote Config を使用して無効にするオプションがある。 
- 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 を使用する詳細については、Apple プラットフォームで Firebase Remote Config を使用するをご覧ください。
- 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 パラメータを作成し、値を