如要讓使用者選擇加入或選擇不採用 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) 中新增下列其中一個金鑰:
如要停用 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 FirebaseRemoteConfigObjective-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 參數,然後將其值設為