Coverage for benefits / eligibility / analytics.py: 83%

27 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-13 19:36 +0000

1""" 

2The eligibility application: analytics implementation. 

3""" 

4 

5from benefits.core import analytics as core, models 

6 

7 

8class EligibilityEvent(core.Event): 

9 """Base analytics event for eligibility verification.""" 

10 

11 def __init__( 

12 self, request, event_type, flow: models.EnrollmentFlow, enrollment_method=models.EnrollmentMethods.SELF_SERVICE 

13 ): 

14 super().__init__(request, event_type, enrollment_method) 

15 # overwrite core.Event enrollment flow 

16 self.update_enrollment_flows(flow) 

17 

18 

19class SelectedFlowEvent(EligibilityEvent): 

20 """Analytics event representing the user selecting an enrollment flow.""" 

21 

22 def __init__(self, request, flow: models.EnrollmentFlow, enrollment_method=models.EnrollmentMethods.SELF_SERVICE): 

23 super().__init__(request, "selected enrollment flow", flow, enrollment_method) 

24 

25 

26class StartedEligibilityEvent(EligibilityEvent): 

27 """Analytics event representing the beginning of an eligibility verification check.""" 

28 

29 def __init__(self, request, flow: models.EnrollmentFlow, enrollment_method=models.EnrollmentMethods.SELF_SERVICE): 

30 super().__init__(request, "started eligibility", flow, enrollment_method) 

31 

32 

33class ReturnedEligibilityEvent(EligibilityEvent): 

34 """Analytics event representing the end of an eligibility verification check.""" 

35 

36 def __init__( 

37 self, request, flow: models.EnrollmentFlow, status, error=None, enrollment_method=models.EnrollmentMethods.SELF_SERVICE 

38 ): 

39 super().__init__(request, "returned eligibility", flow, enrollment_method) 

40 status = str(status).lower() 

41 if status in ("error", "fail", "success"): 41 ↛ exitline 41 didn't return from function '__init__' because the condition on line 41 was always true

42 self.update_event_properties(status=status, error=error) 

43 

44 

45def selected_flow(request, flow: models.EnrollmentFlow, enrollment_method: str = models.EnrollmentMethods.SELF_SERVICE): 

46 """Send the "selected enrollment flow" analytics event.""" 

47 core.send_event(SelectedFlowEvent(request, flow, enrollment_method=enrollment_method)) 

48 

49 

50def started_eligibility(request, flow: models.EnrollmentFlow, enrollment_method: str = models.EnrollmentMethods.SELF_SERVICE): 

51 """Send the "started eligibility" analytics event.""" 

52 core.send_event(StartedEligibilityEvent(request, flow, enrollment_method=enrollment_method)) 

53 

54 

55def returned_error(request, flow: models.EnrollmentFlow, error): 

56 """Send the "returned eligibility" analytics event with an error status.""" 

57 core.send_event(ReturnedEligibilityEvent(request, flow, status="error", error=error)) 

58 

59 

60def returned_fail(request, flow: models.EnrollmentFlow): 

61 """Send the "returned eligibility" analytics event with a fail status.""" 

62 core.send_event(ReturnedEligibilityEvent(request, flow, status="fail")) 

63 

64 

65def returned_success(request, flow: models.EnrollmentFlow, enrollment_method: str = models.EnrollmentMethods.SELF_SERVICE): 

66 """Send the "returned eligibility" analytics event with a success status.""" 

67 core.send_event(ReturnedEligibilityEvent(request, flow, enrollment_method=enrollment_method, status="success"))