Disattiva Firebase Performance Monitoring


Per consentire agli utenti di attivare o disattivare l'utilizzo di Firebase Performance Monitoring, ti consigliamo di configurare l'app in modo da poter attivare e disattivare Performance Monitoring. Questa funzionalità potrebbe essere utile anche durante lo sviluppo e il test delle app.

Ecco alcune opzioni da prendere in considerazione:

  • Puoi disattivare l'SDK Performance Monitoring durante la creazione dell'app, con l'opzione di riattivarlo in fase di runtime.

  • Puoi creare la tua app con l'SDK Performance Monitoring abilitato, ma hai la possibilità di disattivarlo in fase di runtime utilizzando Firebase Remote Config.

  • Puoi disattivare completamente l'SDK Performance Monitoring, senza possibilità di attivarlo durante il runtime.

Disattiva Performance Monitoring durante il processo di creazione dell'app

Una situazione in cui la disattivazione di Performance Monitoring durante il processo di creazione dell'app potrebbe essere utile è evitare di generare report sui dati sul rendimento di una versione pre-release dell'app durante lo sviluppo e i test dell'app.

Per disattivare o disabilitare Performance Monitoring, puoi aggiungere una delle due chiavi al file dell'elenco delle proprietà (Info.plist) per la tua app Apple:

  • Per disattivare Performance Monitoring, ma consentire alla tua app di attivarlo in fase di runtime, imposta firebase_performance_collection_enabled su false nel file Info.plist dell'app.

  • Per disattivare completamente Performance Monitoring, senza la possibilità di attivarlo in fase di runtime, imposta firebase_performance_collection_deactivated su true nel file Info.plist della tua app.

Disattiva l'app in fase di runtime utilizzando Remote Config

Firebase Remote Config ti consente di apportare modifiche al comportamento e all'aspetto della tua app, quindi è un modo ideale per disattivare Performance Monitoring nelle istanze di cui è stato eseguito il deployment della tua app.

Per disattivare la raccolta dei dati di Performance Monitoring al successivo avvio dell'app Apple, utilizza il codice di esempio riportato di seguito. Per ulteriori informazioni sull'utilizzo di Remote Config in un'app Apple, consulta Utilizza Firebase Remote Config su piattaforme Apple.

  1. Assicurati che Remote Config sia utilizzato nel tuo Podfile:

    pod 'Firebase/RemoteConfig'
    
  2. Aggiungi quanto segue all'inizio del file AppDelegate dell'app:

    Swift

    Nota:questo prodotto Firebase non è disponibile su macOS, Mac Catalyst e watchOS.
    import FirebaseRemoteConfig
    

    Objective-C

    Nota:questo prodotto Firebase non è disponibile su macOS, Mac Catalyst e watchOS.
    @import FirebaseRemoteConfig;
    
  3. Nel file AppDelegate, aggiungi il seguente codice alle istruzioni launchOptions nel metodo dell'istanza application:didFinishLaunchingWithOptions::

    Swift

    Nota: questo prodotto non è disponibile su macOS, Mac Catalyst e 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

    Nota:questo prodotto Firebase non è disponibile su macOS, Mac Catalyst e 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];
    
  4. In ViewController.m o in un altro file di implementazione utilizzato dalla tua app, aggiungi il seguente codice per recuperare e attivare i valori Remote Config:

    Swift

    Nota:questo prodotto Firebase non è disponibile su macOS, Mac Catalyst e 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

    Nota:questo prodotto Firebase non è disponibile su macOS, Mac Catalyst e 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);
      }
    }];
    
  5. Per disattivare Performance Monitoring nella console Firebase, crea un parametro perf_disable nel progetto della tua app, quindi imposta il relativo valore su true.

    Se imposti il valore di perf_disable su false, Performance Monitoring rimane attivo.

Disattiva separatamente la raccolta dei dati automatica o personalizzata

Puoi apportare alcune modifiche al codice mostrato sopra e nella console Firebase per disattivare tutto il monitoraggio automatico (pronto all'uso) separatamente dal monitoraggio personalizzato.

  1. Aggiungi il seguente codice alle istruzioni launchOptions nel metodo di istanza application:didFinishLaunchingWithOptions: (anziché a quanto mostrato sopra per lo stesso metodo di istanza):

    Swift

    Nota:questo prodotto Firebase non è disponibile su macOS, Mac Catalyst e 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

    Nota:questo prodotto Firebase non è disponibile su macOS, Mac Catalyst e 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];
    
  2. Completa i seguenti passaggi nella console Firebase:

    • Per disattivare tutto il monitoraggio automatico (pronto all'uso), crea un parametro perf_disable_auto nel progetto della tua app, quindi imposta il relativo valore su true.
    • Per disattivare tutto il monitoraggio personalizzato, crea un parametro perf_disable_manual nel progetto della tua app, quindi imposta il relativo valore su true.