Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: Add Discussion Track and Screen Analytics #81

Merged
merged 7 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
30 changes: 0 additions & 30 deletions app/src/main/java/org/openedx/app/AnalyticsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,31 +166,6 @@ class AnalyticsManager(
put(Key.COURSE_NAME.keyName, courseName)
})
}

override fun discussionAllPostsClickedEvent(courseId: String, courseName: String) {
logEvent(Event.DISCUSSION_ALL_POSTS_CLICKED, buildMap {
put(Key.COURSE_ID.keyName, courseId)
put(Key.COURSE_NAME.keyName, courseName)
})
}

override fun discussionFollowingClickedEvent(courseId: String, courseName: String) {
logEvent(Event.DISCUSSION_FOLLOWING_CLICKED, buildMap {
put(Key.COURSE_ID.keyName, courseId)
put(Key.COURSE_NAME.keyName, courseName)
})
}

override fun discussionTopicClickedEvent(
courseId: String, courseName: String, topicId: String, topicName: String,
) {
logEvent(Event.DISCUSSION_TOPIC_CLICKED, buildMap {
put(Key.COURSE_ID.keyName, courseId)
put(Key.COURSE_NAME.keyName, courseName)
put(Key.TOPIC_ID.keyName, topicId)
put(Key.TOPIC_NAME.keyName, topicName)
})
}
}

enum class Event(val eventName: String) {
Expand All @@ -206,18 +181,13 @@ enum class Event(val eventName: String) {
FINISH_VERTICAL_CLICKED("Finish_Vertical_Clicked"),
FINISH_VERTICAL_NEXT_CLICKED("Finish_Vertical_Next_section_Clicked"),
FINISH_VERTICAL_BACK_CLICKED("Finish_Vertical_Back_to_outline_Clicked"),
DISCUSSION_ALL_POSTS_CLICKED("Discussion_All_Posts_Clicked"),
DISCUSSION_FOLLOWING_CLICKED("Discussion_Following_Clicked"),
DISCUSSION_TOPIC_CLICKED("Discussion_Topic_Clicked"),
}

private enum class Key(val keyName: String) {
COURSE_ID("course_id"),
COURSE_NAME("course_name"),
BLOCK_ID("block_id"),
BLOCK_NAME("block_name"),
TOPIC_ID("topic_id"),
TOPIC_NAME("topic_name"),
FORCE("force"),
LABEL("label"),
COURSE_COUNT("courses_count"),
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/openedx/app/di/ScreenModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ val screenModule = module {
get(),
get(),
get(),
get(),
)
}
viewModel { (courseId: String, thread: Thread) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,63 @@ open class BaseDiscussionViewModel(
private val analytics: DiscussionAnalytics,
) : BaseViewModel() {

fun logTopicScreenEvent(
HamzaIsrar12 marked this conversation as resolved.
Show resolved Hide resolved
topicId: String,
) {
if (topicId.isEmpty()) return
logScreenEvent(
event = DiscussionAnalyticsEvent.DISCUSSION_TOPIC_VIEWED,
params = buildMap {
put(DiscussionAnalyticsKey.TOPIC_ID.key, topicId)
}
)
}

fun logPostScreenEvent(
topicId: String,
threadId: String,
) {
logScreenEvent(
event = DiscussionAnalyticsEvent.DISCUSSION_POST_VIEWED,
params = buildMap {
put(DiscussionAnalyticsKey.TOPIC_ID.key, topicId)
put(DiscussionAnalyticsKey.THREAD_ID.key, threadId)
}
)
}

fun logResponseScreenEvent(
threadId: String,
responseId: String,
) {
logScreenEvent(
event = DiscussionAnalyticsEvent.DISCUSSION_RESPONSE_VIEWED,
params = buildMap {
put(DiscussionAnalyticsKey.THREAD_ID.key, threadId)
put(DiscussionAnalyticsKey.RESPONSE_ID.key, responseId)
}
)
}

fun logAllPostsClickedEvent() {
logEvent(DiscussionAnalyticsEvent.DISCUSSION_ALL_POSTS_CLICKED)
}

fun logFollowingPostsClickedEvent() {
logEvent(DiscussionAnalyticsEvent.DISCUSSION_FOLLOWING_POSTS_CLICKED)
}

fun logTopicClickedEvent(
topicId: String,
) {
logEvent(
event = DiscussionAnalyticsEvent.DISCUSSION_TOPIC_CLICKED,
params = buildMap {
put(DiscussionAnalyticsKey.TOPIC_ID.key, topicId)
}
)
}

fun logPostCreatedEvent(
topicId: String,
postType: String,
Expand All @@ -18,10 +75,10 @@ open class BaseDiscussionViewModel(
logEvent(
event = DiscussionAnalyticsEvent.DISCUSSION_POST_CREATED,
params = buildMap {
put(DiscussionAnalyticsParam.TOPIC_ID.key, topicId)
put(DiscussionAnalyticsParam.POST_TYPE.key, postType)
put(DiscussionAnalyticsParam.FOLLOW_POST.key, followPost.toString())
put(DiscussionAnalyticsParam.AUTHOR.key, author)
put(DiscussionAnalyticsKey.TOPIC_ID.key, topicId)
put(DiscussionAnalyticsKey.POST_TYPE.key, postType)
put(DiscussionAnalyticsKey.FOLLOW_POST.key, followPost.toString())
put(DiscussionAnalyticsKey.AUTHOR.key, author)
}
)
}
Expand All @@ -34,9 +91,9 @@ open class BaseDiscussionViewModel(
event = DiscussionAnalyticsEvent.DISCUSSION_RESPONSE_ADDED,
params = buildMap {
responseId.takeIfNotEmpty()?.let {
put(DiscussionAnalyticsParam.RESPONSE_ID.key, responseId)
put(DiscussionAnalyticsKey.RESPONSE_ID.key, responseId)
}
put(DiscussionAnalyticsParam.AUTHOR.key, author)
put(DiscussionAnalyticsKey.AUTHOR.key, author)
}
)
}
Expand All @@ -50,12 +107,12 @@ open class BaseDiscussionViewModel(
event = DiscussionAnalyticsEvent.DISCUSSION_COMMENT_ADDED,
params = buildMap {
responseId.takeIfNotEmpty()?.let {
put(DiscussionAnalyticsParam.RESPONSE_ID.key, responseId)
put(DiscussionAnalyticsKey.RESPONSE_ID.key, responseId)
}
commentId.takeIfNotEmpty()?.let {
put(DiscussionAnalyticsParam.COMMENT_ID.key, commentId)
put(DiscussionAnalyticsKey.COMMENT_ID.key, commentId)
}
put(DiscussionAnalyticsParam.AUTHOR.key, author)
put(DiscussionAnalyticsKey.AUTHOR.key, author)
}
)
}
Expand All @@ -67,8 +124,8 @@ open class BaseDiscussionViewModel(
logEvent(
event = DiscussionAnalyticsEvent.DISCUSSION_POST_FOLLOW_TOGGLE,
params = buildMap {
put(DiscussionAnalyticsParam.FOLLOW.key, followPost.toString())
put(DiscussionAnalyticsParam.AUTHOR.key, author)
put(DiscussionAnalyticsKey.FOLLOW.key, followPost.toString())
put(DiscussionAnalyticsKey.AUTHOR.key, author)
}
)
}
Expand All @@ -84,14 +141,14 @@ open class BaseDiscussionViewModel(
event = DiscussionAnalyticsEvent.DISCUSSION_LIKE_TOGGLE,
params = buildMap {
responseId.takeIfNotEmpty()?.let {
put(DiscussionAnalyticsParam.RESPONSE_ID.key, responseId)
put(DiscussionAnalyticsKey.RESPONSE_ID.key, responseId)
}
commentId.takeIfNotEmpty()?.let {
put(DiscussionAnalyticsParam.COMMENT_ID.key, commentId)
put(DiscussionAnalyticsKey.COMMENT_ID.key, commentId)
}
put(DiscussionAnalyticsParam.DISCUSSION_TYPE.key, discussionType)
put(DiscussionAnalyticsParam.LIKE.key, likePost.toString())
put(DiscussionAnalyticsParam.AUTHOR.key, author)
put(DiscussionAnalyticsKey.DISCUSSION_TYPE.key, discussionType)
put(DiscussionAnalyticsKey.LIKE.key, likePost.toString())
put(DiscussionAnalyticsKey.AUTHOR.key, author)
}
)
}
Expand All @@ -107,27 +164,43 @@ open class BaseDiscussionViewModel(
event = DiscussionAnalyticsEvent.DISCUSSION_REPORT_TOGGLE,
params = buildMap {
responseId.takeIfNotEmpty()?.let {
put(DiscussionAnalyticsParam.RESPONSE_ID.key, responseId)
put(DiscussionAnalyticsKey.RESPONSE_ID.key, responseId)
}
commentId.takeIfNotEmpty()?.let {
put(DiscussionAnalyticsParam.COMMENT_ID.key, commentId)
put(DiscussionAnalyticsKey.COMMENT_ID.key, commentId)
}
put(DiscussionAnalyticsParam.DISCUSSION_TYPE.key, discussionType)
put(DiscussionAnalyticsParam.REPORT.key, reportPost.toString())
put(DiscussionAnalyticsParam.AUTHOR.key, author)
put(DiscussionAnalyticsKey.DISCUSSION_TYPE.key, discussionType)
put(DiscussionAnalyticsKey.REPORT.key, reportPost.toString())
put(DiscussionAnalyticsKey.AUTHOR.key, author)
}
)
}

private fun logScreenEvent(
event: DiscussionAnalyticsEvent,
params: Map<String, Any?> = emptyMap(),
) {
analytics.logScreenEvent(
screenName = event.eventName,
params = buildMap {
put(DiscussionAnalyticsKey.NAME.key, event.biValue)
put(DiscussionAnalyticsKey.COURSE_ID.key, courseId)
putAll(params)
}
)
}

private fun logEvent(event: DiscussionAnalyticsEvent, params: Map<String, Any?>) {
private fun logEvent(
event: DiscussionAnalyticsEvent,
params: Map<String, Any?> = emptyMap(),
) {
analytics.logEvent(
event = event.eventName,
params = buildMap {
put(DiscussionAnalyticsParam.NAME.key, event.biValue)
put(DiscussionAnalyticsParam.COURSE_ID.key, courseId)
put(DiscussionAnalyticsKey.NAME.key, event.biValue)
put(DiscussionAnalyticsKey.COURSE_ID.key, courseId)
threadId.takeIfNotEmpty()?.let {
put(DiscussionAnalyticsParam.THREAD_ID.key, threadId)
put(DiscussionAnalyticsKey.THREAD_ID.key, threadId)
}
putAll(params)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
package org.openedx.discussion.presentation

interface DiscussionAnalytics {
fun discussionAllPostsClickedEvent(courseId: String, courseName: String)
fun discussionFollowingClickedEvent(courseId: String, courseName: String)
fun discussionTopicClickedEvent(
courseId: String,
courseName: String,
topicId: String,
topicName: String
)

fun logEvent(event: String, params: Map<String, Any?>)
fun logScreenEvent(screenName: String, params: Map<String, Any?>)
}

enum class DiscussionAnalyticsEvent(val eventName: String, val biValue: String) {
DISCUSSION_ALL_POSTS_CLICKED(
"Discussion:All Posts Clicked",
"edx.bi.app.discussion.all_posts_clicked"
),
DISCUSSION_FOLLOWING_POSTS_CLICKED(
"Discussion:Following Posts Clicked",
"edx.bi.app.discussion.following_posts_clicked"
),
DISCUSSION_TOPIC_CLICKED(
"Discussion:Topic Clicked",
"edx.bi.app.discussion.topic_clicked"
),
DISCUSSION_TOPIC_VIEWED(
"Discussion:Topic Viewed",
"edx.bi.app.discussion.topic.viewed"
),
DISCUSSION_POST_VIEWED(
"Discussion:Post Viewed",
"edx.bi.app.discussion.post.viewed"
),
DISCUSSION_RESPONSE_VIEWED(
"Discussion:Response Viewed",
"edx.bi.app.discussion.response.viewed"
),
DISCUSSION_POST_CREATED(
"Discussion:Post Created",
"edx.bi.app.discussion.post_created"
Expand All @@ -40,7 +56,7 @@ enum class DiscussionAnalyticsEvent(val eventName: String, val biValue: String)
)
}

enum class DiscussionAnalyticsParam(val key: String) {
enum class DiscussionAnalyticsKey(val key: String) {
NAME("name"),
COURSE_ID("course_id"),
TOPIC_ID("topic_id"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ class DiscussionCommentsViewModel(
init {
this.thread = thread
getThreadComments()
logPostScreenEvent(
HamzaIsrar12 marked this conversation as resolved.
Show resolved Hide resolved
topicId = thread.topicId,
threadId = thread.id,
)
}

private fun sendThreadUpdated() {
Expand Down Expand Up @@ -338,4 +342,4 @@ class DiscussionCommentsViewModel(
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class DiscussionResponsesViewModel(

init {
loadCommentResponses()
logResponseScreenEvent(
HamzaIsrar12 marked this conversation as resolved.
Show resolved Hide resolved
threadId = threadId,
responseId = comment.id,
)
}

private fun loadCommentResponses() {
Expand Down Expand Up @@ -214,4 +218,4 @@ class DiscussionResponsesViewModel(
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.openedx.core.BaseViewModel
import org.openedx.core.R
import org.openedx.core.SingleEventLiveData
import org.openedx.core.UIMessage
import org.openedx.core.extension.isInternetError
import org.openedx.core.system.ResourceManager
import org.openedx.discussion.domain.interactor.DiscussionInteractor
import org.openedx.discussion.presentation.BaseDiscussionViewModel
import org.openedx.discussion.presentation.DiscussionAnalytics
import org.openedx.discussion.presentation.topics.DiscussionTopicsViewModel
import org.openedx.discussion.system.notifier.DiscussionNotifier
import org.openedx.discussion.system.notifier.DiscussionThreadAdded
Expand All @@ -24,7 +25,8 @@ class DiscussionThreadsViewModel(
private val interactor: DiscussionInteractor,
private val resourceManager: ResourceManager,
private val notifier: DiscussionNotifier,
) : BaseViewModel() {
analytics: DiscussionAnalytics,
) : BaseDiscussionViewModel(courseId, "", analytics) {

private val _uiState = MutableLiveData<DiscussionThreadsUIState>()
val uiState: LiveData<DiscussionThreadsUIState>
Expand Down Expand Up @@ -73,6 +75,7 @@ class DiscussionThreadsViewModel(

init {
getThreadByType(SortType.LAST_ACTIVITY_AT.queryParam)
logTopicScreenEvent(topicId)
}

fun getThreadByType(orderBy: String) {
Expand Down Expand Up @@ -245,4 +248,4 @@ class DiscussionThreadsViewModel(
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ fun DiscussionTopicsScreen(
discussionTopicsViewModel.discussionClickedEvent(
action,
data,
title
)
discussionTopicsViewModel.discussionRouter.navigateToDiscussionThread(
fragmentManager,
Expand Down Expand Up @@ -322,4 +321,4 @@ private val mockTopic = Topic(
name = "All Topics",
threadListUrl = "",
children = emptyList()
)
)
Loading
Loading