Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added matrix for user engagement over last 8 days. #25

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions app/database/services/analytics/analysis_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
get_patients_most_commonly_visited_screens,
get_patients_retention_rate_in_specific_time_interval,
get_patients_retention_rate_on_specific_days,
get_user_engagement_over_last_8_days,
get_weekly_active_patients,
get_most_fired_events
)
Expand Down Expand Up @@ -105,8 +106,8 @@ async def calculate(
saved_analytics = await save_analytics(analysis_code, metrics)
print(f"Saved analytics -> {analysis_code}")

await generate_reports(analysis_code, metrics)
print(f"Generated reports -> {analysis_code}")
# await generate_reports(analysis_code, metrics)
# print(f"Generated reports -> {analysis_code}")

return metrics

Expand Down Expand Up @@ -199,7 +200,8 @@ async def calculate_generic_engagement_metrics(filters: AnalyticsFilters | None
get_patients_most_commonly_used_features(filters),
get_patients_most_commonly_visited_screens(filters),
get_most_fired_events(filters),
get_most_fired_events_by_event_category(filters)
get_most_fired_events_by_event_category(filters),
get_user_engagement_over_last_8_days(filters)
)

daily_active_users = results[0]
Expand All @@ -212,8 +214,9 @@ async def calculate_generic_engagement_metrics(filters: AnalyticsFilters | None
stickiness_ratio = results[7]
most_common_features = results[8]
most_commonly_visited_screens = results[9]
most_fired_events = results[10]
most_fired_events_by_event_category = results[11]
most_fired_events = results[10]
most_fired_events_by_event_category = results[11]
user_engagement_over_last_8_days = results[12]

generic_engagement_metrics = GenericEngagementMetrics(
TenantId = filters.TenantId,
Expand All @@ -231,7 +234,8 @@ async def calculate_generic_engagement_metrics(filters: AnalyticsFilters | None
MostCommonlyVisitedFeatures = most_common_features,
MostCommonlyVisitedScreens = most_commonly_visited_screens,
MostFiredEvents = most_fired_events,
MostFiredEventsByEventCategory = most_fired_events_by_event_category
MostFiredEventsByEventCategory = most_fired_events_by_event_category,
UserEngagementOverLast8Days = user_engagement_over_last_8_days
)

return generic_engagement_metrics
Expand Down
45 changes: 45 additions & 0 deletions app/database/services/analytics/generic_engagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,3 +917,48 @@ async def get_most_fired_events_by_event_category(filters: AnalyticsFilters) ->
print(e)
return []

async def get_user_engagement_over_last_8_days(filters) -> list:
try:

tenant_id = filters.TenantId
start_date = filters.StartDate
end_date = filters.EndDate
role_id = filters.RoleId

connector = get_analytics_db_connector()

query = f"""
SELECT
EventCategory as event_category,
EventName as event_name,
COUNT(*) AS event_count
FROM
events e
JOIN
users user ON e.UserId = user.id
WHERE
e.Timestamp >= NOW() - INTERVAL 8 DAY
AND
e.Timestamp BETWEEN '{start_date}' AND '{end_date}'
__CHECKS__
GROUP BY
EventCategory,
EventName
ORDER BY
EventCategory,
event_count DESC
"""

checks_str = add_common_checks(tenant_id, role_id)
if len(checks_str) > 0:
checks_str = "AND " + checks_str
query = query.replace("__CHECKS__", checks_str)

result = connector.execute_read_query(query)

return result

except Exception as e:
print(e)
return []

2 changes: 2 additions & 0 deletions app/domain_types/schemas/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class GenericEngagementMetrics(BaseModel):
MostCommonlyVisitedScreens : list|dict|None = Field(description="Most common screens visited by users")
MostFiredEvents : list|dict|None = Field(description="Most frequently fired events")
MostFiredEventsByEventCategory : list|dict|None = Field(description="Most frequently fired events by event category")
UserEngagementOverLast8Days : list|dict|None = Field(description="User engagement over last 8 days")


class FeatureEngagementMetrics(BaseModel):
Feature : str = Field(description="Name of the feature")
Expand Down
Loading