Skip to content

Commit 528dd3f

Browse files
feat: downloads analytic
1 parent b4e6241 commit 528dd3f

File tree

11 files changed

+131
-15
lines changed

11 files changed

+131
-15
lines changed

app/src/main/java/org/openedx/app/AnalyticsManager.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.openedx.app
22

33
import org.openedx.auth.presentation.AuthAnalytics
44
import org.openedx.core.presentation.CoreAnalytics
5+
import org.openedx.core.presentation.DownloadsAnalytics
56
import org.openedx.core.presentation.dialog.appreview.AppReviewAnalytics
67
import org.openedx.course.presentation.CourseAnalytics
78
import org.openedx.dashboard.presentation.DashboardAnalytics
@@ -21,7 +22,8 @@ class AnalyticsManager :
2122
DiscoveryAnalytics,
2223
DiscussionAnalytics,
2324
ProfileAnalytics,
24-
WhatsNewAnalytics {
25+
WhatsNewAnalytics,
26+
DownloadsAnalytics {
2527

2628
private val analytics: MutableList<Analytics> = mutableListOf()
2729

app/src/main/java/org/openedx/app/di/AppModule.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.openedx.core.module.TranscriptManager
4040
import org.openedx.core.module.download.DownloadHelper
4141
import org.openedx.core.module.download.FileDownloader
4242
import org.openedx.core.presentation.CoreAnalytics
43+
import org.openedx.core.presentation.DownloadsAnalytics
4344
import org.openedx.core.presentation.dialog.appreview.AppReviewAnalytics
4445
import org.openedx.core.presentation.dialog.appreview.AppReviewManager
4546
import org.openedx.core.presentation.dialog.downloaddialog.DownloadDialogManager
@@ -207,6 +208,7 @@ val appModule = module {
207208
single<DiscussionAnalytics> { get<AnalyticsManager>() }
208209
single<ProfileAnalytics> { get<AnalyticsManager>() }
209210
single<WhatsNewAnalytics> { get<AnalyticsManager>() }
211+
single<DownloadsAnalytics> { get<AnalyticsManager>() }
210212

211213
factory { AgreementProvider(get(), get()) }
212214
factory { FacebookAuthHelper() }

app/src/main/java/org/openedx/app/di/ScreenModule.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,8 @@ val screenModule = module {
522522
workerController = get(),
523523
downloadHelper = get(),
524524
downloadDialogManager = get(),
525-
fileUtil = get()
525+
fileUtil = get(),
526+
analytics = get()
526527
)
527528
}
528529
}

core/src/main/java/org/openedx/core/module/DownloadWorker.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import org.openedx.core.module.download.AbstractDownloader.DownloadResult
2323
import org.openedx.core.module.download.CurrentProgress
2424
import org.openedx.core.module.download.DownloadHelper
2525
import org.openedx.core.module.download.FileDownloader
26+
import org.openedx.core.presentation.DownloadsAnalytics
27+
import org.openedx.core.presentation.DownloadsAnalyticsEvent
28+
import org.openedx.core.presentation.DownloadsAnalyticsKey
2629
import org.openedx.core.system.notifier.DownloadFailed
2730
import org.openedx.core.system.notifier.DownloadNotifier
2831
import org.openedx.core.system.notifier.DownloadProgressChanged
@@ -33,12 +36,14 @@ class DownloadWorker(
3336
parameters: WorkerParameters,
3437
) : CoroutineWorker(context, parameters), CoroutineScope {
3538

36-
private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
39+
private val notificationManager =
40+
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
3741
private val notificationBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
3842

3943
private val notifier by inject<DownloadNotifier>(DownloadNotifier::class.java)
4044
private val downloadDao: DownloadDao by inject(DownloadDao::class.java)
4145
private val downloadHelper: DownloadHelper by inject(DownloadHelper::class.java)
46+
private val analytics: DownloadsAnalytics by inject(DownloadsAnalytics::class.java)
4247

4348
private var downloadEnqueue = listOf<DownloadModel>()
4449
private var downloadError = mutableListOf<DownloadModel>()
@@ -134,9 +139,11 @@ class DownloadWorker(
134139
)
135140
)
136141
)
142+
logEvent(DownloadsAnalyticsEvent.DOWNLOAD_STARTED)
137143
val downloadResult = fileDownloader.download(downloadTask.url, downloadTask.path)
138144
when (downloadResult) {
139145
DownloadResult.SUCCESS -> {
146+
logEvent(DownloadsAnalyticsEvent.DOWNLOAD_COMPLETED)
140147
val updatedModel = downloadHelper.updateDownloadStatus(downloadTask)
141148
if (updatedModel == null) {
142149
downloadDao.removeDownloadModel(downloadTask.id)
@@ -149,10 +156,12 @@ class DownloadWorker(
149156
}
150157

151158
DownloadResult.CANCELED -> {
159+
logEvent(DownloadsAnalyticsEvent.DOWNLOAD_CANCELLED)
152160
downloadDao.removeDownloadModel(downloadTask.id)
153161
}
154162

155163
DownloadResult.ERROR -> {
164+
logEvent(DownloadsAnalyticsEvent.DOWNLOAD_ERROR)
156165
downloadDao.removeDownloadModel(downloadTask.id)
157166
downloadError.add(downloadTask)
158167
}
@@ -173,6 +182,15 @@ class DownloadWorker(
173182
notificationManager.createNotificationChannel(notificationChannel)
174183
}
175184

185+
fun logEvent(event: DownloadsAnalyticsEvent) {
186+
analytics.logEvent(
187+
event = event.eventName,
188+
params = buildMap {
189+
put(DownloadsAnalyticsKey.NAME.key, event.biValue)
190+
}
191+
)
192+
}
193+
176194
companion object {
177195
const val WORKER_TAG = "downloadWorker"
178196

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.openedx.core.presentation
2+
3+
interface DownloadsAnalytics {
4+
fun logEvent(event: String, params: Map<String, Any?>)
5+
fun logScreenEvent(screenName: String, params: Map<String, Any?>)
6+
}
7+
8+
enum class DownloadsAnalyticsEvent(val eventName: String, val biValue: String) {
9+
DOWNLOAD_COURSE_CLICKED(
10+
"Downloads:Download Course Clicked",
11+
"edx.bi.app.downloads.downloadCourseClicked"
12+
),
13+
CANCEL_DOWNLOAD_CLICKED(
14+
"Downloads:Cancel Download Clicked",
15+
"edx.bi.app.downloads.cancelDownloadClicked"
16+
),
17+
REMOVE_DOWNLOAD_CLICKED(
18+
"Downloads:Remove Download Clicked",
19+
"edx.bi.app.downloads.removeDownloadClicked"
20+
),
21+
DOWNLOAD_CONFIRMED(
22+
"Downloads:Download Confirmed",
23+
"edx.bi.app.downloads.downloadConfirmed"
24+
),
25+
DOWNLOAD_CANCELLED(
26+
"Downloads:Download Cancelled",
27+
"edx.bi.app.downloads.downloadCancelled"
28+
),
29+
DOWNLOAD_REMOVED(
30+
"Downloads:Download Removed",
31+
"edx.bi.app.downloads.downloadRemoved"
32+
),
33+
DOWNLOAD_ERROR(
34+
"Downloads:Download Error",
35+
"edx.bi.app.downloads.downloadError"
36+
),
37+
DOWNLOAD_COMPLETED(
38+
"Downloads:Download Completed",
39+
"edx.bi.app.downloads.downloadCompleted"
40+
),
41+
DOWNLOAD_STARTED(
42+
"Downloads:Download Started",
43+
"edx.bi.app.downloads.downloadStarted"
44+
),
45+
}
46+
47+
enum class DownloadsAnalyticsKey(val key: String) {
48+
NAME("name"),
49+
}

core/src/main/java/org/openedx/core/presentation/dialog/downloaddialog/DownloadConfirmDialogFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ class DownloadConfirmDialogFragment : DialogFragment(), DownloadDialog {
9999
onConfirmClick = {
100100
uiState.saveDownloadModels()
101101
dismiss()
102+
listener?.onConfirmClick()
102103
},
103104
onRemoveClick = {
104105
uiState.removeDownloadModels()
105106
dismiss()
106107
},
107108
onCancelClick = {
108109
dismiss()
109-
listener?.onCancel()
110+
listener?.onCancelClick()
110111
}
111112
)
112113
}

core/src/main/java/org/openedx/core/presentation/dialog/downloaddialog/DownloadDialogManager.kt

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import org.openedx.core.system.StorageManager
1515
import org.openedx.core.system.connection.NetworkConnection
1616

1717
interface DownloadDialogListener {
18-
fun onCancel()
18+
fun onCancelClick()
19+
fun onConfirmClick()
1920
}
2021

2122
interface DownloadDialog {
@@ -85,12 +86,21 @@ class DownloadDialogManager(
8586
}
8687

8788
val dialogListener = object : DownloadDialogListener {
88-
override fun onCancel() {
89+
override fun onCancelClick() {
8990
state.onDismissClick()
9091
}
92+
93+
override fun onConfirmClick() {
94+
state.onConfirmClick()
95+
}
96+
}
97+
if (dialog != null) {
98+
dialog.listener = dialogListener
99+
dialog.show(state.fragmentManager, dialog::class.java.simpleName)
100+
} else {
101+
state.onConfirmClick()
102+
state.saveDownloadModels()
91103
}
92-
dialog?.listener = dialogListener
93-
dialog?.show(state.fragmentManager, dialog::class.java.simpleName) ?: state.saveDownloadModels()
94104
}
95105
}
96106
}
@@ -104,6 +114,7 @@ class DownloadDialogManager(
104114
removeDownloadModels: (blockId: String, courseId: String) -> Unit,
105115
saveDownloadModels: (blockId: String) -> Unit,
106116
onDismissClick: () -> Unit = {},
117+
onConfirmClick: () -> Unit = {},
107118
) {
108119
createDownloadItems(
109120
subSectionsBlocks = subSectionsBlocks,
@@ -113,7 +124,8 @@ class DownloadDialogManager(
113124
onlyVideoBlocks = onlyVideoBlocks,
114125
removeDownloadModels = removeDownloadModels,
115126
saveDownloadModels = saveDownloadModels,
116-
onDismissClick = onDismissClick
127+
onDismissClick = onDismissClick,
128+
onConfirmClick = onConfirmClick
117129
)
118130
}
119131

@@ -159,10 +171,12 @@ class DownloadDialogManager(
159171

160172
courseIds.forEach { courseId ->
161173
val courseStructure = interactor.getCourseStructureFromCache(courseId)
162-
val allSubSectionBlocks = courseStructure.blockData.filter { it.type == BlockType.SEQUENTIAL }
174+
val allSubSectionBlocks =
175+
courseStructure.blockData.filter { it.type == BlockType.SEQUENTIAL }
163176

164177
allSubSectionBlocks.forEach { subSectionBlock ->
165-
val verticalBlocks = courseStructure.blockData.filter { it.id in subSectionBlock.descendants }
178+
val verticalBlocks =
179+
courseStructure.blockData.filter { it.id in subSectionBlock.descendants }
166180
val blocks = courseStructure.blockData.filter {
167181
it.id in verticalBlocks.flatMap { it.descendants } && it.id in blockIds
168182
}
@@ -207,13 +221,15 @@ class DownloadDialogManager(
207221
removeDownloadModels: (blockId: String, courseId: String) -> Unit,
208222
saveDownloadModels: (blockId: String) -> Unit,
209223
onDismissClick: () -> Unit = {},
224+
onConfirmClick: () -> Unit = {},
210225
) {
211226
coroutineScope.launch {
212227
val courseStructure = interactor.getCourseStructure(courseId, false)
213228
val downloadModelIds = interactor.getAllDownloadModels().map { it.id }
214229

215230
val downloadDialogItems = subSectionsBlocks.mapNotNull { subSectionBlock ->
216-
val verticalBlocks = courseStructure.blockData.filter { it.id in subSectionBlock.descendants }
231+
val verticalBlocks =
232+
courseStructure.blockData.filter { it.id in subSectionBlock.descendants }
217233
val blocks = verticalBlocks.flatMap { verticalBlock ->
218234
courseStructure.blockData.filter {
219235
it.id in verticalBlock.descendants &&
@@ -222,7 +238,10 @@ class DownloadDialogManager(
222238
}
223239
}
224240
val size = blocks.sumOf { it.getFileSize() }
225-
if (size > 0) DownloadDialogItem(title = subSectionBlock.displayName, size = size) else null
241+
if (size > 0) DownloadDialogItem(
242+
title = subSectionBlock.displayName,
243+
size = size
244+
) else null
226245
}
227246

228247
uiState.emit(
@@ -242,6 +261,7 @@ class DownloadDialogManager(
242261
},
243262
saveDownloadModels = { subSectionsBlocks.forEach { saveDownloadModels(it.id) } },
244263
onDismissClick = onDismissClick,
264+
onConfirmClick = onConfirmClick,
245265
)
246266
)
247267
}

core/src/main/java/org/openedx/core/presentation/dialog/downloaddialog/DownloadDialogUIState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ data class DownloadDialogUIState(
1515
val removeDownloadModels: () -> Unit,
1616
val saveDownloadModels: () -> Unit,
1717
val onDismissClick: () -> Unit = {},
18+
val onConfirmClick: () -> Unit = {},
1819
) : Parcelable

core/src/main/java/org/openedx/core/presentation/dialog/downloaddialog/DownloadErrorDialogFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class DownloadErrorDialogFragment : DialogFragment(), DownloadDialog {
8787
},
8888
onCancelClick = {
8989
dismiss()
90-
listener?.onCancel()
90+
listener?.onCancelClick()
9191
}
9292
)
9393
}

core/src/main/java/org/openedx/core/presentation/dialog/downloaddialog/DownloadStorageErrorDialogFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class DownloadStorageErrorDialogFragment : DialogFragment(), DownloadDialog {
8181
downloadDialogResource = downloadDialogResource,
8282
onCancelClick = {
8383
dismiss()
84-
listener?.onCancel()
84+
listener?.onCancelClick()
8585
}
8686
)
8787
}

0 commit comments

Comments
 (0)