השבתת מעקב אחר ביצועים ב-Firebase


כדי לאפשר למשתמשים להביע הסכמה או לבטל את הסכמתם לשימוש ב-Firebase Performance Monitoring, כדאי לבצע את הפעולות הבאות רוצה להגדיר את האפליקציה כך שתוכל להפעיל ולהשבית את Performance Monitoring. שלך היכולת הזו יכולה להיות שימושית גם במהלך הפיתוח והבדיקה של האפליקציה.

ריכזנו כאן כמה אפשרויות שכדאי לבדוק:

  • אפשר להשבית את ה-SDK של Performance Monitoring כשיוצרים את האפליקציה, עם אפשרות להפעיל אותו מחדש בזמן הריצה.

  • אפשר לבנות את האפליקציה כש-SDK של Performance Monitoring מופעל, אבל יש לך אפשרות להשבית אותו בזמן הריצה באמצעות Firebase Remote Config.

  • אפשר להשבית לגמרי את ה-SDK של Performance Monitoring, בלי אפשרות להפעיל אותו בזמן ריצה.

השבתת Performance Monitoring במהלך תהליך ה-build של האפליקציה

מצב אחד שבו כדאי להשבית את Performance Monitoring במהלך תהליך ה-build של האפליקציה הוא כדי להימנע מהדיווח על נתוני ביצועים מגרסה של האפליקציה לפני השקתה, במהלך הפיתוח והבדיקה של האפליקציה.

כדי להשבית או להשבית את Performance Monitoring, אפשר להוסיף אחד משני מקשים קובץ רשימת נכסים (Info.plist) עבור אפליקציית Apple שלך:

  • כדי להשבית את Performance Monitoring אבל לאפשר לאפליקציה להפעיל אותו בזמן הריצה, צריך להגדיר firebase_performance_collection_enabled עד false באפליקציה שלך קובץ Info.plist.

  • כדי להשבית לגמרי את Performance Monitoring, בלי אפשרות להפעיל אותו בזמן הריצה, להגדיר את הערך true של firebase_performance_collection_deactivated באפליקציה שלך קובץ Info.plist.

השבתת האפליקציה בזמן הריצה באמצעות Remote Config

באמצעות Firebase Remote Config אפשר לבצע שינויים בהתנהגות ובמראה של האפליקציה שלך, ולכן הוא מספק דרך אידיאלית לאפשר לך להשבית את Performance Monitoring במופעים של האפליקציה שנפרסו.

כדי להשבית את איסוף הנתונים של Performance Monitoring בפעם הבאה שאפליקציית Apple תופעל, צריך להשתמש בקוד לדוגמה שמוצג למטה. מידע נוסף על השימוש Remote Config באפליקציה של Apple: צריך להשתמש ב-Firebase Remote Config בפלטפורמות של Apple.

  1. יש לוודא שנעשה שימוש ב-Remote Config ב-Podfile:

    pod 'Firebase/RemoteConfig'
    
  2. מוסיפים את הפרטים הבאים לחלק העליון של קובץ AppDelegate של האפליקציה:

    Swift

    הערה: מוצר Firebase הזה לא זמין ביעדי macOS, Mac Catalyst ו-watchOS.
    import FirebaseRemoteConfig
    

    Objective-C

    הערה: מוצר Firebase הזה לא זמין ביעדי macOS, Mac Catalyst ו-watchOS.
    @import FirebaseRemoteConfig;
    
  3. בקובץ AppDelegate, צריך להוסיף את הקוד הבא אל launchOptions הצהרות במופע application:didFinishLaunchingWithOptions: method:

    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];
    
  4. ב-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);
      }
    }];
    
  5. כדי להשבית את Performance Monitoring במסוף Firebase, צריך ליצור perf_disable בפרויקט של האפליקציה, ולאחר מכן מגדירים את הערך שלו ל-true.

    אם מגדירים את הערך של perf_disable ל-false, הערך של Performance Monitoring יישאר מופעל.

השבתה של איסוף אוטומטי או מותאם אישית של נתונים בנפרד

אפשר לבצע מספר שינויים בקוד שמוצג למעלה ובמסוף Firebase כדי לאפשר לך להשבית את כל המעקב האוטומטי (מה שייצור) בנפרד מעקב מותאם אישית.

  1. צריך להוסיף את הקוד הבא להצהרות launchOptions ב שיטת המכונה application:didFinishLaunchingWithOptions: (במקום מה שמוצג למעלה לגבי אותה שיטה של מופע):

    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];
    
  2. במסוף Firebase, מבצעים את הפעולות הבאות:

    • כדי להשבית את כל המעקבים האוטומטיים (יש כלום), צריך ליצור perf_disable_auto בפרויקט של האפליקציה, ואז מגדירים את הפרמטר לערך true.
    • כדי להשבית את כל המעקבים המותאמים אישית, יוצרים perf_disable_ manual בפרויקט של האפליקציה, ולאחר מכן מגדירים את הערך שלו ל-true.