incCounter method
Increment a counter metric
Implementation
Future<void> incCounter(
String key,
String fbl,
String operation,
String view,
String state, {
int? date,
int increment = 1,
}) async {
final hub = _hub;
if (!_isInitialized || hub == null) {
ObslyLogger.warn('MetricsController not initialized');
return;
}
// Check if metrics is enabled
final config = ConfigController.instance.config;
if (!(config?.enableMetrics ?? true)) {
ObslyLogger.debug('Metrics disabled, ignoring incCounter call');
return;
}
try {
final currentView =
NavigationIntegrationV2.isNavigationAvailable ? NavigationIntegrationV2.getCurrentViewName() : view;
// Update internal state
final metricKey = _createMetricKey(key, fbl, operation, currentView);
final currentState = _metricStates[metricKey] ?? MetricState(key: key, type: MetricType.counter, value: 0);
currentState.value = (currentState.value as num) + increment;
currentState.lastUpdated = DateTime.now();
_metricStates[metricKey] = currentState;
// Create dimension with user-provided values (empty values will use session fallbacks)
final dimensions = Dimension(
fbl: fbl.isNotEmpty ? fbl : '', // Pass empty if not provided, Hub will use session fallback
operation: operation.isNotEmpty ? operation : '', // Pass empty if not provided, Hub will use session fallback
view: currentView.isNotEmpty ? currentView : '', // Pass empty if not provided, Hub will use session fallback
state: state,
app: '', // Will be filled by Hub with app info
platform: '', // Will be filled by Hub with app info
version: '', // Will be filled by Hub with app info
);
final event = MetricEventBase(
key: key,
value: increment,
dimensions: dimensions,
metricType: MetricType.counter.value,
);
final reservation = hub.reserveEventMetadata();
hub.captureEvent(event, reservation);
ObslyLogger.debug('📊 Counter incremented: $key (+$increment) = ${currentState.value}');
} catch (e) {
ObslyLogger.error('Error incrementing counter: $e');
}
}