promotedai / ios-metrics-sdk

iOS client library for Promoted.ai metrics tracking.
MIT License
7 stars 1 forks source link

Implement Remote Config using Firebase #141

Closed yunapotamus closed 3 years ago

yunapotamus commented 3 years ago

Summary

  1. Add the ability to load configs remotely using Firebase.
  2. Cache these remote configs locally so they can be applied on next startup.
  3. Add PendingLogMessages to handle console logging before ClientConfig is loaded.
  4. Add tests for Module dependencies.
  5. Start formatting source files closer to Swift community defaults.

Configuration

Configuring Remote Config requires the following steps:

  1. Download a plist file from Firebase Console for the app used for Remote Config. Add this file to your app resources.
  2. Add a dependency onto the PromotedFirebaseRemoteConfig module in your build system.
  3. Add initialization code as marked below.

Example:

PROClientConfig *config = [[PROClientConfig alloc] init];
config.metricsLoggingURL = @"https://...";
config.metricsLoggingAPIKey = @"...";
PROModuleConfig *moduleConfig = [PROModuleConfig defaultConfig];
moduleConfig.initialConfig = config;

// Remote Config initialization below.
// Requires a plist file downloaded from Firebase Console.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
  NSString *configPath = [NSBundle.mainBundle pathForResource:@"GoogleService-Info-Promoted" ofType:@"plist"];
  FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:configPath];
  [FIRApp configureWithName:@"Promoted" options:options];
});
FIRApp *app = [FIRApp appNamed:@"Promoted"];
[moduleConfig useFirebaseRemoteConfigWithApp:app];

NSError *error = nil;
PROMetricsLoggerService *service = [[PROMetricsLoggerService alloc] initWithModuleConfig:moduleConfig error:&error];
if (error != nil) {
  NSLog(@"Error initializing Promoted logging: %@", [error description]);
  return nil;
}
[service startLoggingServicesAndReturnError:&error];
if (error != nil) {
  NSLog(@"Error initializing Promoted logging: %@", [error description]);
  return nil;
}

ClientConfigService and Firebase

Remote configs apply on the next startup after they are fetched. This means that the first startup will always use initialConfig.

ClientConfig

Dependencies

Dependency management with library

Dependency management is more complicated because there's a nontrivial part of the library that now runs before ClientConfig is loaded.

Formatting