configure method

  1. @override
Future<void> configure({
  1. AmplifyConfig? config,
  2. required AmplifyAuthProviderRepository authProviderRepo,
})
inherited

Configures the plugin using the registered config.

Implementation

@override
Future<void> configure({
  AmplifyConfig? config,
  required AmplifyAuthProviderRepository authProviderRepo,
}) async {
  // Parse config values from amplifyconfiguration.json
  if (config == null ||
      config.analytics == null ||
      config.analytics?.awsPlugin == null) {
    throw ConfigurationError('No Pinpoint plugin config available.');
  }

  final pinpointConfig = config.analytics!.awsPlugin!;
  final pinpointAppId = pinpointConfig.pinpointAnalytics.appId;
  final region = pinpointConfig.pinpointAnalytics.region;

  // Prepare PinpointClient
  final authProvider = authProviderRepo
      .getAuthProvider(APIAuthorizationType.iam.authProviderToken);

  if (authProvider == null) {
    throw ConfigurationError(
      'No credential provider found for Analytics.',
      recoverySuggestion:
          "If you haven't already, please add amplify_auth_cognito plugin to your App.",
    );
  }

  final eventStoragePath = await _pathProvider?.getApplicationSupportPath();
  final eventStore = DartQueuedItemStore(eventStoragePath);

  final endpointStorage = _secureStorageFactory(
    AmplifySecureStorageScope.awsPinpointAnalyticsPlugin,
  );

  final analyticsClient = dependencies.get<AnalyticsClient>() ??
      AnalyticsClient(
        endpointStorage: endpointStorage,
        deviceContextInfoProvider: _deviceContextInfoProvider,
        legacyNativeDataProvider: _legacyNativeDataProvider,
      );

  await analyticsClient.init(
    pinpointAppId: pinpointAppId,
    region: region,
    authProvider: authProvider,
    eventStore: eventStore,
  );

  _endpointClient = analyticsClient.endpointClient;
  _eventClient = analyticsClient.eventClient;

  try {
    await _endpointClient.updateEndpoint();
  } on Exception catch (e) {
    _logger.warn('Could not update endpoint: $e');
  }

  _sessionManager = SessionManager(
    fixedEndpointId: _endpointClient.fixedEndpointId,
    appLifecycleProvider: _appLifecycleProvider,
    onSessionStart: (session) async {
      _logger.debug('Session started');
      if (!_analyticsEnabled) return;
      await _eventClient.recordEvent(
        eventType: zSessionStartEventType,
        session: session,
      );
      await _eventClient.flushEvents();
    },
    onSessionEnd: (session) async {
      _logger.debug('Session ended');
      if (!_analyticsEnabled) return;
      await _eventClient.recordEvent(
        eventType: zSessionStopEventType,
        session: session,
      );
      await _eventClient.flushEvents();
    },
  );

  final autoFlushEventsInterval = _options.autoFlushEventsInterval;

  if (autoFlushEventsInterval.isNegative) {
    throw ConfigurationError(
      'The autoFlushEventsInterval field in your Amplify configuration must be a positive integer or 0 to disable auto-flushing.',
    );
  }

  /// Setting autoFlushEventsInterval to 0 disables autoflush.
  if (autoFlushEventsInterval.inSeconds > 0) {
    autoEventSubmitter = StoppableTimer(
      duration: autoFlushEventsInterval,
      callback: flushEvents,
      onError: (e) => _logger.warn('Exception in events auto flush', e),
    );
  } else {
    autoEventSubmitter = null;
  }

  _isConfigured = true;
  await enable();
  _sessionManager.startSession();
}