Skip to content

Commit

Permalink
feat(analytics): send analytic event when 500 error during /token
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-tran committed May 21, 2024
1 parent 9f48635 commit 3d90770
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions benefits/enrollment/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ def __init__(self, request, status, error=None, payment_group=None):
self.update_event_properties(payment_group=payment_group)


class FailedAccessTokenRequestEvent(core.Event):
"""Analytics event representing a failure to acquire an access token for card tokenization."""

def __init__(self, request, status_code=None):
super().__init__(request, "failed access token request")
if status_code is not None:
self.update_event_properties(status_code=status_code)


def returned_error(request, error):
"""Send the "returned enrollment" analytics event with an error status and message."""
core.send_event(ReturnedEnrollmentEvent(request, status="error", error=error))
Expand All @@ -29,3 +38,8 @@ def returned_retry(request):
def returned_success(request, payment_group):
"""Send the "returned enrollment" analytics event with a success status."""
core.send_event(ReturnedEnrollmentEvent(request, status="success", payment_group=payment_group))


def failed_access_token_request(request, status_code=None):
"""Send the "failed access token request" analytics event with the response status code."""
core.send_event(FailedAccessTokenRequestEvent(request, status_code=status_code))
1 change: 1 addition & 0 deletions benefits/enrollment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def token(request):
response = client.request_card_tokenization_access()
except Exception as e:
if isinstance(e, HTTPError) and e.response.status_code >= 500:
analytics.failed_access_token_request(request, e.response.status_code)
sentry_sdk.capture_exception(e)
data = {"redirect": reverse(ROUTE_SYSTEM_ERROR)}
return JsonResponse(data)
Expand Down
10 changes: 10 additions & 0 deletions tests/pytest/enrollment/test_analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest

from benefits.enrollment.analytics import FailedAccessTokenRequestEvent


@pytest.mark.django_db
def test_FailedAccessTokenRequestEvent_sets_status_code(app_request):
event = FailedAccessTokenRequestEvent(app_request, 500)

assert event.event_properties["status_code"] == 500
4 changes: 3 additions & 1 deletion tests/pytest/enrollment/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_token_valid(mocker, client):

@pytest.mark.django_db
@pytest.mark.usefixtures("mocked_session_agency", "mocked_session_eligibility")
def test_token_http_error_500(mocker, client):
def test_token_http_error_500(mocker, client, mocked_analytics_module):
mocker.patch("benefits.core.session.enrollment_token_valid", return_value=False)

mock_client_cls = mocker.patch("benefits.enrollment.views.Client")
Expand All @@ -126,6 +126,8 @@ def test_token_http_error_500(mocker, client):
assert "token" not in data
assert "redirect" in data
assert data["redirect"] == reverse(ROUTE_SYSTEM_ERROR)
mocked_analytics_module.failed_access_token_request.assert_called_once()
assert 500 in mocked_analytics_module.failed_access_token_request.call_args.args


@pytest.mark.django_db
Expand Down

0 comments on commit 3d90770

Please sign in to comment.