@@ -6,60 +6,55 @@ import kotlinx.coroutines.withContext
66import org.wikipedia.WikipediaApp
77import org.wikipedia.auth.AccountUtil
88import org.wikipedia.dataclient.ServiceFactory
9- import org.wikipedia.dataclient.okhttp.HttpStatusException
109import org.wikipedia.page.PageTitle
10+ import org.wikipedia.settings.Prefs
1111
12- object MachineGeneratedArticleDescriptionsAnalyticsHelper {
12+ class MachineGeneratedArticleDescriptionsAnalyticsHelper {
1313
14- private const val MACHINE_GEN_DESC_SUGGESTIONS = " machineSuggestions"
1514 private var apiFailed = false
16- val machineGeneratedDescriptionsABTest = MachineGeneratedArticleDescriptionABCTest ()
1715 var apiOrderList = emptyList<String >()
1816 var displayOrderList = emptyList<String >()
19- var chosenSuggestion = " "
20- var isUserExperienced = false
21- var isUserInExperiment = false
17+ private var chosenSuggestion = " "
2218 private var startTime = 0L
2319
2420 fun articleDescriptionEditingStart (context : Context ) {
25- log(context, " ArticleDescriptionEditing.start" )
26- startTime = System .currentTimeMillis()
21+ log(context, composeGroupString() + " .start" )
2722 }
2823
2924 fun resetTimer () {
3025 startTime = System .currentTimeMillis()
3126 }
3227
3328 fun articleDescriptionEditingEnd (context : Context ) {
34- log(context, " ArticleDescriptionEditing .end.timeSpentMs.${System .currentTimeMillis() - startTime} " )
29+ log(context, composeGroupString() + " .end.timeSpentMs.${System .currentTimeMillis() - startTime} " )
3530 }
3631
3732 fun logAttempt (context : Context , finalDescription : String , wasChosen : Boolean , wasModified : Boolean , title : PageTitle ) {
38- log(
39- context, composeLogString(title) + " .attempt:$finalDescription .suggestionChosen:${if (! wasChosen) - 1 else displayOrderList.indexOf(chosenSuggestion) + 1 } " +
40- " .api.${apiOrderList.indexOf(chosenSuggestion) + 1 } .modified:$wasModified "
41- )
33+ log(context, composeLogString(title) + " .attempt:$finalDescription " +
34+ " .suggestion1:${encode(apiOrderList.first())} " + (if (apiOrderList.size > 1 ) " .suggestion2.${encode(apiOrderList.last())} " else " " ) +
35+ getOrderString(wasChosen, chosenSuggestion) + " .modified:$wasModified " )
4236 }
4337
4438 fun logSuccess (context : Context , finalDescription : String , wasChosen : Boolean , wasModified : Boolean , title : PageTitle , revId : Long ) {
45- log(context, composeLogString(title) + " .success:$finalDescription .suggestionChosen:${if (! wasChosen) - 1 else displayOrderList.indexOf(
46- chosenSuggestion) + 1 } .api.${apiOrderList.indexOf(chosenSuggestion) + 1 } .modified:$wasModified .revId:$revId " )
39+ log(context, composeLogString(title) + " .success:$finalDescription " +
40+ " .suggestion1:${encode(apiOrderList.first())} " + (if (apiOrderList.size > 1 ) " .suggestion2.${encode(apiOrderList.last())} " else " " ) +
41+ getOrderString(wasChosen, chosenSuggestion) + " .modified:$wasModified .revId:$revId " )
4742 }
4843
4944 fun logSuggestionsReceived (context : Context , isBlp : Boolean , title : PageTitle ) {
5045 apiFailed = false
51- log(context, composeLogString(title) + " .blp:$isBlp .count:${apiOrderList.size} .api1 :${apiOrderList.first()} " +
52- if (apiOrderList.size > 1 ) " .api2 .${apiOrderList.last()} " else " " )
46+ log(context, composeLogString(title) + " .blp:$isBlp .count:${apiOrderList.size} .suggestion1 :${encode( apiOrderList.first() )} " +
47+ if (apiOrderList.size > 1 ) " .suggestion2 .${encode( apiOrderList.last() )} " else " " )
5348 }
5449
5550 fun logSuggestionsShown (context : Context , title : PageTitle ) {
56- log(context, composeLogString(title) + " .count:${displayOrderList.size} .display1:${displayOrderList.first()} " +
57- if (displayOrderList.size > 1 ) " .display2.${displayOrderList.last()} " else " " )
51+ log(context, composeLogString(title) + " .count:${displayOrderList.size} .display1:${encode( displayOrderList.first())} " +
52+ if (displayOrderList.size > 1 ) " .display2.${encode( displayOrderList.last() )} " else " " )
5853 }
5954
6055 fun logSuggestionChosen (context : Context , suggestion : String , title : PageTitle ) {
6156 chosenSuggestion = suggestion
62- log(context, composeLogString(title) + " .selected:$suggestion . ${getOrderString()} " )
57+ log(context, composeLogString(title) + " .selected:${encode( suggestion)} ${getOrderString(true , suggestion )} " )
6358 }
6459
6560 fun logSuggestionsDismissed (context : Context , title : PageTitle ) {
@@ -68,25 +63,23 @@ object MachineGeneratedArticleDescriptionsAnalyticsHelper {
6863
6964 fun logSuggestionReported (context : Context , suggestion : String , reportReasonsList : List <String >, title : PageTitle ) {
7065 val reportReasons = reportReasonsList.joinToString(" |" )
71- log(context, composeLogString(title) + " .reportDialog.$suggestion . ${getOrderString()} .reasons:$reportReasons .reported" )
66+ log(context, composeLogString(title) + " .reportDialog.${encode( suggestion)} ${getOrderString(true , suggestion )} .reasons:$reportReasons .reported" )
7267 }
7368
7469 fun logReportDialogDismissed (context : Context ) {
7570 log(context, composeGroupString() + " .reportDialog.dismissed" )
7671 }
7772
7873 fun logOnboardingShown (context : Context ) {
79- log(context, " $MACHINE_GEN_DESC_SUGGESTIONS .onboardingShown" )
74+ log(context, composeGroupString() + " .onboardingShown" )
8075 }
8176
8277 fun logGroupAssigned (context : Context , testGroup : Int ) {
8378 log(context, " $MACHINE_GEN_DESC_SUGGESTIONS .groupAssigned:$testGroup " )
8479 }
8580
8681 fun logApiFailed (context : Context , throwable : Throwable , title : PageTitle ) {
87- if (throwable is HttpStatusException ) {
88- log(context, " Api failed with response code ${throwable.code} for : ${composeLogString(title)} " )
89- }
82+ log(context, composeLogString(title) + " .apiError.${throwable.message} " )
9083 apiFailed = true
9184 }
9285
@@ -97,22 +90,35 @@ object MachineGeneratedArticleDescriptionsAnalyticsHelper {
9790 EventPlatformClient .submit(BreadCrumbLogEvent (BreadCrumbViewUtil .getReadableScreenName(context), logString))
9891 }
9992
100- private fun getOrderString (): String {
101- return " api.${apiOrderList.indexOf(chosenSuggestion) + 1 } .display.${displayOrderList.indexOf(chosenSuggestion) + 1 } "
93+ private fun getOrderString (wasChosen : Boolean , suggestion : String ): String {
94+ return " .chosenApiIndex.${if (! wasChosen) - 1 else apiOrderList.indexOf(suggestion) + 1 } " +
95+ " .chosenDisplayIndex:${if (! wasChosen) - 1 else displayOrderList.indexOf(suggestion) + 1 } "
10296 }
10397
10498 private fun composeLogString (title : PageTitle ): String {
105- return " ${composeGroupString()} .lang:${title.wikiSite.languageCode} .title:${title.prefixedText} "
99+ return " ${composeGroupString()} .lang:${title.wikiSite.languageCode} .title:${encode( title.prefixedText) } "
106100 }
107101
108102 private fun composeGroupString (): String {
109- return " $MACHINE_GEN_DESC_SUGGESTIONS .group:${machineGeneratedDescriptionsABTest.aBTestGroup } .experienced:$isUserExperienced "
103+ return " $MACHINE_GEN_DESC_SUGGESTIONS .group:${abcTest.group } .experienced:${ Prefs .suggestedEditsMachineGeneratedDescriptionsIsExperienced} "
110104 }
111105
112- suspend fun setUserExperienced () =
113- withContext(Dispatchers .Default ) {
114- val totalContributions = ServiceFactory .get(WikipediaApp .instance.wikiSite)
115- .globalUserInfo(AccountUtil .userName!! ).query?.globalUserInfo?.editCount ? : 0
116- isUserExperienced = totalContributions > 50
106+ companion object {
107+ private const val MACHINE_GEN_DESC_SUGGESTIONS = " machineSuggestions"
108+ val abcTest = MachineGeneratedArticleDescriptionABCTest ()
109+ var isUserInExperiment = false
110+
111+ // HACK: We're using periods and colons as delimiting characters in these events, so let's
112+ // urlencode just those characters, if they appear in our strings.
113+ private fun encode (str : String ): String {
114+ return str.replace(" ." , " %2E" ).replace(" :" , " %3A" )
117115 }
116+
117+ suspend fun setUserExperienced () =
118+ withContext(Dispatchers .Default ) {
119+ val totalContributions = ServiceFactory .get(WikipediaApp .instance.wikiSite)
120+ .globalUserInfo(AccountUtil .userName!! ).query?.globalUserInfo?.editCount ? : 0
121+ Prefs .suggestedEditsMachineGeneratedDescriptionsIsExperienced = totalContributions > 50
122+ }
123+ }
118124}
0 commit comments