Skip to content

Commit

Permalink
#414 analytics logging for user a added
Browse files Browse the repository at this point in the history
  • Loading branch information
danmash committed Oct 17, 2022
1 parent dc12eb8 commit 72938fa
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 29 deletions.
12 changes: 12 additions & 0 deletions app/conversations/enums.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import typing
from enum import IntEnum

from app.user_b.analytics_logging import eventType


class ConversationState(IntEnum):
"""
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions app/conversations/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
72 changes: 43 additions & 29 deletions app/user_b/analytics_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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()
Expand Down

0 comments on commit 72938fa

Please sign in to comment.