trackEvent method

  1. @override
Future<void> trackEvent(
  1. String name, [
  2. Map<String, dynamic>? properties
])
override

Tracks a custom event

  • name: Event name (e.g., "purchase", "signup")
  • properties: Optional event properties (must be JSON-serializable)
  • Throws: LinkFortyError if tracking fails

Implementation

@override
Future<void> trackEvent(
  String name, [
  Map<String, dynamic>? properties,
]) async {
  // Validate event name
  if (name.trim().isEmpty) {
    throw InvalidEventDataError('Event name cannot be empty');
  }

  // Get install ID
  final installId = _storageManager.getInstallId();
  if (installId == null) {
    throw const NotInitializedError();
  }

  // Stamp the event with the active last-click attribution context so the
  // backend can credit the deep link that drove it (organic events carry only
  // the session id).
  final stamp = _attributionContext.getStamp();
  final event = EventRequest(
    installId: installId,
    eventName: name,
    eventData: properties ?? {},
    attributedLinkId: stamp.attributedLinkId,
    attributedClickId: stamp.attributedClickId,
    linkOpenedAt: stamp.linkOpenedAt,
    sessionId: stamp.sessionId,
  );

  // Try to send immediately
  try {
    await _sendEvent(event);
    LinkFortyLogger.log('Event tracked: $name');

    // If send succeeds, try to flush queue
    await flushQueue();
  } catch (e) {
    // If send fails, queue the event and persist
    _eventQueue.enqueue(event);
    await _persistQueue();
    LinkFortyLogger.log('Event queued due to error: $e');
    rethrow;
  }
}