diff --git a/app/conversations/enums.py b/app/conversations/enums.py index 027552d5..c170a7be 100644 --- a/app/conversations/enums.py +++ b/app/conversations/enums.py @@ -1,5 +1,8 @@ +import typing from enum import IntEnum +from app.user_b.analytics_logging import eventType + class ConversationState(IntEnum): """ @@ -14,6 +17,15 @@ class ConversationState(IntEnum): TalkedButtonClicked = 4 RatingDone = 5 + def get_analytics_event_type(self) -> typing.Optional[eventType]: + conversation_state_to_analytics_event_type_mapping = { + self.AlignButtonClicked: eventType.UA_ALIGN_CLICK, + self.TopicsButtonClicked: eventType.UA_TOPICS_CLICK, + self.TalkedButtonClicked: eventType.UA_TALKED_CLICK, + self.RatingDone: eventType.UA_RATING_DONE, + } + return conversation_state_to_analytics_event_type_mapping.get(self) + class ConversationUserARating(IntEnum): AWFUL = 1 diff --git a/app/conversations/routes.py b/app/conversations/routes.py index c648e739..ff806a7b 100644 --- a/app/conversations/routes.py +++ b/app/conversations/routes.py @@ -31,6 +31,7 @@ ) from app.models import Users, Conversations from app.sendgrid.utils import send_user_b_shared_email +from app.user_b.analytics_logging import log_user_b_event, eventType @bp.route("/conversation", methods=["POST"]) @@ -234,8 +235,19 @@ def edit_conversation(conversation_uuid): json_data[uuid_field_name] = conversation_uuid try: + prev_conversation_state = conversation.state conversation = schema.load(json_data, instance=conversation, partial=True) db.session.commit() + + if prev_conversation_state != conversation.state: + state_enum = ConversationState(conversation.state) + log_user_b_event( + conversation_uuid, + session_uuid, + state_enum.get_analytics_event_type(), + True, + ) + return schema.jsonify(conversation) except SQLAlchemyError: return DatabaseError(message="Couldn't edit conversation") diff --git a/app/models.py b/app/models.py index 266c094f..c9a03f06 100644 --- a/app/models.py +++ b/app/models.py @@ -186,6 +186,7 @@ class AnalyticsData(db.Model): page_url = db.Column(db.String(255)) +# TODO: rename class UserBAnalyticsData(db.Model): __tablename__ = "user_b_analytics_data" event_log_uuid = db.Column(UNIQUEIDENTIFIER, primary_key=True) diff --git a/app/user_b/analytics_logging.py b/app/user_b/analytics_logging.py index fdda4637..fc32e87d 100644 --- a/app/user_b/analytics_logging.py +++ b/app/user_b/analytics_logging.py @@ -9,6 +9,7 @@ class eventType(Enum): """ + TODO: rename Event types to be used for analytics logging. LINK - the unique link for user b has been used @@ -18,19 +19,52 @@ class eventType(Enum): QUIZ - user b has completed the quiz LMEFFECT - user b has clicked on a shared impact card to learn more LMSOLUTION - user b has clicked on a shared solution card to learn more + + UA_ALIGN_CLICK - user a clicked align button + UA_TOPICS_CLICK - user a clicked topics button + UA_TALKED_CLICK - user a clicked talked button + UA_RATING_DONE = rating done by user a """ - LINK = 1 - SOLUTION = 2 - EFFECT = 3 - CONSENT = 4 - QUIZ = 5 - LMEFFECT = 6 - LMSOLUTION = 7 + LINK = "link clicked" + SOLUTION = "solution choice" + EFFECT = "effect choice" + CONSENT = "consent updated" + QUIZ = "quiz completed" + LMEFFECT = "learn more - impact" + LMSOLUTION = "learn more - solution" + + UA_ALIGN_CLICK = "align button clicked" + UA_TOPICS_CLICK = "topics button clicked" + UA_TALKED_CLICK = "talked button clicked" + UA_RATING_DONE = "rating done" + + def get_event_value_type(self): + event_to_event_type_mapping = { + self.LINK: EventValueTypeEnum.BOOLEAN, + self.CONSENT: EventValueTypeEnum.BOOLEAN, + self.EFFECT: EventValueTypeEnum.UUID, + self.SOLUTION: EventValueTypeEnum.UUID, + self.QUIZ: EventValueTypeEnum.UUID, + self.LMEFFECT: EventValueTypeEnum.IRI, + self.LMSOLUTION: EventValueTypeEnum.IRI, + self.UA_ALIGN_CLICK: EventValueTypeEnum.BOOLEAN, + self.UA_TOPICS_CLICK: EventValueTypeEnum.BOOLEAN, + self.UA_TALKED_CLICK: EventValueTypeEnum.BOOLEAN, + self.UA_RATING_DONE: EventValueTypeEnum.BOOLEAN, + } + return event_to_event_type_mapping[self] + + +class EventValueTypeEnum(Enum): + BOOLEAN = "boolean" + UUID = "UUID" + IRI = "IRI" def log_user_b_event(conversation_uuid, session_uuid, event_type, event_value): """ + TODO: rename Log an event in the user b analytics data table. """ try: @@ -40,28 +74,8 @@ def log_user_b_event(conversation_uuid, session_uuid, event_type, event_value): event_to_add.event_value = event_value event_to_add.event_timestamp = datetime.now(timezone.utc) event_to_add.session_uuid = session_uuid - - if event_type.name == "LINK": - event_to_add.event_type = "link clicked" - event_to_add.event_value_type = "boolean" - elif event_type.name == "CONSENT": - event_to_add.event_type = "consent updated" - event_to_add.event_value_type = "boolean" - elif event_type.name == "EFFECT": - event_to_add.event_type = "effect choice" - event_to_add.event_value_type = "UUID" - elif event_type.name == "SOLUTION": - event_to_add.event_type = "solution choice" - event_to_add.event_value_type = "UUID" - elif event_type.name == "QUIZ": - event_to_add.event_type = "quiz completed" - event_to_add.event_value_type = "UUID" - elif event_type.name == "LMEFFECT": - event_to_add.event_type = "learn more - impact" - event_to_add.event_value_type = "IRI" - elif event_type.name == "LMSOLUTION": - event_to_add.event_type = "learn more - solution" - event_to_add.event_value_type = "IRI" + event_to_add.event_type = event_type.value + event_to_add.event_value_type = event_type.get_event_value_type().value db.session.add(event_to_add) db.session.commit()