Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
348 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
app/src/main/java/openfoodfacts/github/scrachx/openfood/analytics/AnalyticsEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package openfoodfacts.github.scrachx.openfood.analytics | ||
|
||
sealed class AnalyticsEvent { | ||
|
||
abstract val category: String | ||
abstract val action: String | ||
abstract val name: String? | ||
abstract val value: Float? | ||
|
||
data class ScannedBarcode(override val name: String) : AnalyticsEvent() { | ||
override val category = "scanner" | ||
override val action = "scanned" | ||
override val value: Float? = null | ||
} | ||
|
||
data class ScannedBarcodeResultExpanded(override val name: String?) : AnalyticsEvent() { | ||
override val category = "scanner" | ||
override val action = "result-expanded" | ||
override val value: Float? = null | ||
} | ||
|
||
data class AllergenAlertCreated(override val name: String) : AnalyticsEvent() { | ||
override val category = "allergen-alerts" | ||
override val action = "created" | ||
override val value: Float? = null | ||
} | ||
|
||
data class ProductCreated(override val name: String?) : AnalyticsEvent() { | ||
override val category = "products" | ||
override val action = "created" | ||
override val value: Float? = null | ||
} | ||
|
||
data class ProductEdited(override val name: String?) : AnalyticsEvent() { | ||
override val category = "products" | ||
override val action = "edited" | ||
override val value: Float? = null | ||
} | ||
|
||
data class ProductIngredientsPictureEdited(override val name: String?) : AnalyticsEvent() { | ||
override val category = "products" | ||
override val action = "edited-ingredients-picture" | ||
override val value: Float? = null | ||
} | ||
|
||
object UserLogin : AnalyticsEvent() { | ||
override val category = "user-account" | ||
override val action = "login" | ||
override val name: String? = null | ||
override val value: Float? = null | ||
} | ||
|
||
object UserLogout : AnalyticsEvent() { | ||
override val category = "user-account" | ||
override val action = "logout" | ||
override val name: String? = null | ||
override val value: Float? = null | ||
} | ||
|
||
object RobotoffLoginPrompt : AnalyticsEvent() { | ||
override val category = "user-account" | ||
override val action = "login-prompt" | ||
override val name = "robotoff" | ||
override val value: Float? = null | ||
} | ||
|
||
object RobotoffLoggedInAfterPrompt : AnalyticsEvent() { | ||
override val category = "user-account" | ||
override val action = "logged-in-after-prompt" | ||
override val name = "robotoff" | ||
override val value: Float? = null | ||
} | ||
|
||
object ShoppingListCreated : AnalyticsEvent() { | ||
override val category = "shopping-lists" | ||
override val action = "created" | ||
override val name: String? = null | ||
override val value: Float? = null | ||
} | ||
|
||
object ShoppingListExported : AnalyticsEvent() { | ||
override val category = "shopping-lists" | ||
override val action = "exported" | ||
override val name: String? = null | ||
override val value: Float? = null | ||
} | ||
|
||
data class IngredientAnalysisEnabled(override val name: String) : AnalyticsEvent() { | ||
override val category = "ingredient-analysis" | ||
override val action = "enabled" | ||
override val value: Float? = null | ||
} | ||
|
||
data class IngredientAnalysisDisabled(override val name: String) : AnalyticsEvent() { | ||
override val category = "ingredient-analysis" | ||
override val action = "disabled" | ||
override val value: Float? = null | ||
} | ||
|
||
data class AddProductToComparison(override val name: String) : AnalyticsEvent() { | ||
override val category = "products" | ||
override val action = "compare-add" | ||
override val value: Float? = null | ||
} | ||
|
||
data class CompareProducts(override val value: Float) : AnalyticsEvent() { | ||
override val category = "products" | ||
override val action = "compare-multiple" | ||
override val name: String? = null | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/openfoodfacts/github/scrachx/openfood/analytics/AnalyticsView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package openfoodfacts.github.scrachx.openfood.analytics | ||
|
||
enum class AnalyticsView(val path: String) { | ||
Scanner("scanner"), | ||
ProductEditOverview("products/edit/overview"), | ||
ProductEditIngredients("products/edit/ingredients"), | ||
ProductEditNutritionFacts("products/edit/nutrition_facts"); | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/openfoodfacts/github/scrachx/openfood/analytics/MatomoAnalytics.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package openfoodfacts.github.scrachx.openfood.analytics | ||
|
||
import androidx.fragment.app.FragmentManager | ||
import androidx.preference.PreferenceManager | ||
import openfoodfacts.github.scrachx.openfood.BuildConfig | ||
import openfoodfacts.github.scrachx.openfood.R | ||
import openfoodfacts.github.scrachx.openfood.app.OFFApplication | ||
import openfoodfacts.github.scrachx.openfood.features.analyticsusage.AnalyticsUsageDialogFragment | ||
import org.matomo.sdk.Matomo | ||
import org.matomo.sdk.TrackerBuilder | ||
import org.matomo.sdk.extra.TrackHelper | ||
|
||
|
||
object MatomoAnalytics { | ||
|
||
//TODO: change matomo url and id from properties | ||
private val tracker = TrackerBuilder | ||
.createDefault(BuildConfig.MATOMO_URL, 1) | ||
.build(Matomo.getInstance(OFFApplication.instance)) | ||
.apply { | ||
isOptOut = PreferenceManager.getDefaultSharedPreferences(OFFApplication.instance) | ||
.getBoolean(OFFApplication.instance.getString(R.string.pref_analytics_reporting_key), false) | ||
} | ||
.also { | ||
TrackHelper.track().download().with(it) | ||
} | ||
|
||
fun trackView(view: AnalyticsView) { | ||
TrackHelper.track() | ||
.screen(view.path) | ||
.with(tracker) | ||
} | ||
|
||
fun trackEvent(event: AnalyticsEvent) { | ||
TrackHelper.track() | ||
.event(event.category, event.action) | ||
.name(event.name) | ||
.value(event.value) | ||
.with(tracker) | ||
} | ||
|
||
fun showAnalyticsBottomSheetIfNeeded(childFragmentManager: FragmentManager) { | ||
if (PreferenceManager.getDefaultSharedPreferences(OFFApplication.instance).contains(OFFApplication.instance.getString(R.string.pref_analytics_reporting_key))) { | ||
//key already exists, do not show | ||
return | ||
} | ||
val bottomSheet = AnalyticsUsageDialogFragment() | ||
bottomSheet.show(childFragmentManager, AnalyticsUsageDialogFragment.TAG) | ||
} | ||
|
||
fun onAnalyticsEnabledToggled(enabled: Boolean) { | ||
tracker.isOptOut = !enabled | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/openfoodfacts/github/scrachx/openfood/analytics/SentryAnalytics.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package openfoodfacts.github.scrachx.openfood.analytics | ||
|
||
import androidx.preference.PreferenceManager | ||
import io.sentry.Sentry | ||
import io.sentry.android.core.SentryAndroid | ||
import openfoodfacts.github.scrachx.openfood.BuildConfig | ||
import openfoodfacts.github.scrachx.openfood.R | ||
import openfoodfacts.github.scrachx.openfood.app.OFFApplication | ||
|
||
|
||
object SentryAnalytics { | ||
|
||
// isCrashReportingEnabled is not dynamic, as sentry can not be enabled / disabled, so it takes the value at startup, and changes will only be taken into account after an app restart | ||
private val isCrashReportingEnabled by lazy { | ||
PreferenceManager.getDefaultSharedPreferences(OFFApplication.instance) | ||
.getBoolean(OFFApplication.instance.getString(R.string.pref_crash_reporting_key), true) | ||
} | ||
|
||
fun init() { | ||
if (isCrashReportingEnabled) { | ||
SentryAndroid.init(OFFApplication.instance) | ||
Sentry.configureScope { scope -> | ||
scope.setTag("flavor", BuildConfig.FLAVOR) | ||
} | ||
} | ||
} | ||
|
||
fun setBarcode(barcode: String) { | ||
setTag("barcode", barcode) | ||
} | ||
|
||
fun setTag(key: String, value: String) { | ||
if (isCrashReportingEnabled) { | ||
Sentry.setTag(key, value) | ||
} | ||
} | ||
|
||
fun record(exception: Throwable) { | ||
if (isCrashReportingEnabled) { | ||
Sentry.captureException(exception) | ||
} | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
app/src/main/java/openfoodfacts/github/scrachx/openfood/app/AnalyticsService.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.