track method

  1. @override
Future<RawEvent?> track(
  1. TrackEvent event
)
override

Tracks an event by sending it to AppsFlyer

Takes an event object containing properties and maps them to AppsFlyer's format. Extracts revenue, currency, quantity, price and order ID into AppsFlyer's standard parameters. Removes the original properties after extraction to avoid duplication. Finally logs the event to AppsFlyer with the transformed properties.

Implementation

@override

/// Tracks an event by sending it to AppsFlyer
///
/// Takes an event object containing properties and maps them to AppsFlyer's format.
/// Extracts revenue, currency, quantity, price and order ID into AppsFlyer's standard parameters.
/// Removes the original properties after extraction to avoid duplication.
/// Finally logs the event to AppsFlyer with the transformed properties.
track(event) async {
  final properties = event.properties ?? {};
  final eventName = eventMapping[event.event] ?? event.event;

  // Extract all relevant properties
  final Map<String, dynamic> eventProperties = Map.from(properties);
  final extractedProperties = {
    'af_revenue': extractRevenue('revenue', properties) ??
        extractRevenue('value', properties),
    'af_currency': extractCurrency('currency', properties, 'USD'),
    'af_quantity': extractQuantity('quantity', properties),
    'af_price': extractPrice('price', properties),
    'af_order_id': properties['transaction_id']?.toString() ??
        properties['order_id']?.toString(),
  };

  // Remove original properties that we've extracted
  final keysToRemove = [
    'revenue',
    'currency',
    'value',
    'quantity',
    'price',
    'transaction_id',
    'order_id'
  ];
  eventProperties.removeWhere((key, _) => keysToRemove.contains(key));

  // Add extracted properties if they have values
  extractedProperties.forEach((key, value) {
    if (value != null) {
      eventProperties[key] = value;
    }
  });

  await appsFlyer.logEvent(eventName, eventProperties);
  return event;
}