grantEventsOf function

List<ChanceAnalyticsEvent> grantEventsOf(
  1. Map<String, dynamic> answer, {
  2. required bool granted,
  3. required String? chanceId,
  4. required String userId,
  5. required String triggerId,
  6. required String? testInvocationId,
})

The chance events a grantChance answer's facts call for:

  • Digia Gamification Assigned iff the answer carries holdout;
  • Digia Chance Granted (source trigger) iff granted and isNew;
  • Digia Chance Denied iff denied with a denyReason and isNew.

None for a test invocation, or when the answer names no campaign. Never throws: a missing or mistyped fact is no event.

Implementation

List<ChanceAnalyticsEvent> grantEventsOf(
  Map<String, dynamic> answer, {
  required bool granted,
  required String? chanceId,
  required String userId,
  required String triggerId,
  required String? testInvocationId,
}) {
  try {
    if (testInvocationId != null) return const [];
    final campaignId = _textOf(answer['campaignId']);
    if (campaignId == null) return const [];
    final isNew = answer['isNew'] == true;
    final out = <ChanceAnalyticsEvent>[];
    final holdout = answer['holdout'];
    if (holdout is Map) {
      final group = _textOf(holdout['group']);
      final period = holdout['period'];
      if (group != null && period is num) {
        out.add(_event(
          _assignedName,
          'assigned:$campaignId:$userId:$triggerId',
          campaignId,
          {'group': group, 'period': period},
        ));
      }
    }
    if (granted && isNew && chanceId != null) {
      out.add(_event(
        _grantedName,
        'granted:$chanceId',
        campaignId,
        _withoutNulls({
          'chance_id': chanceId,
          'source': 'trigger',
          'expires_at': _textOf(answer['expiresAt']),
        }),
      ));
    }
    final denyReason = _textOf(answer['denyReason']);
    if (!granted && isNew && denyReason != null) {
      out.add(_event(
        _deniedName,
        'denied:$campaignId:$userId:$triggerId',
        campaignId,
        _withoutNulls({'reason': denyReason, 'cap': _textOf(answer['cap'])}),
      ));
    }
    return out;
  } catch (_) {
    return const [];
  }
}