Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Matomo integration * Show bottom sheet asking for analytics service * rebase Co-authored-by: Philippe Auriach <p.auriach@gmail.com>
- Loading branch information
1 parent
35503a2
commit 0952459
Showing
29 changed files
with
420 additions
and
46 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
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
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
package openfoodfacts.github.scrachx.openfood.analytics | ||
|
||
sealed class AnalyticsEvent(val category: String, val action: String, val name: String?, val value: Float?) { | ||
|
||
data class ScannedBarcode(val barcode: String) : AnalyticsEvent("scanner", "scanned", barcode, null) | ||
|
||
data class ScannedBarcodeResultExpanded(val barcode: String?) : AnalyticsEvent("scanner", "result-expanded", barcode, null) | ||
|
||
data class AllergenAlertCreated(val allergenTag: String) : AnalyticsEvent("allergen-alerts", "created", allergenTag, null) | ||
|
||
data class ProductCreated(val barcode: String?) : AnalyticsEvent("products", "created", barcode, null) | ||
|
||
data class ProductEdited(val barcode: String?) : AnalyticsEvent("products", "edited", barcode, null) | ||
|
||
data class ProductIngredientsPictureEdited(val barcode: String?) : AnalyticsEvent("products", "edited-ingredients-picture", barcode, null) | ||
|
||
object UserLogin : AnalyticsEvent("user-account", "login", null, null) | ||
|
||
object UserLogout : AnalyticsEvent("user-account", "logout", null, null) | ||
|
||
object RobotoffLoginPrompt : AnalyticsEvent("user-account", "login-prompt", "robotoff", null) | ||
|
||
object RobotoffLoggedInAfterPrompt : AnalyticsEvent("user-account", "logged-in-after-prompt", "robotoff", null) | ||
|
||
object ShoppingListCreated : AnalyticsEvent("shopping-lists", "created", null, null) | ||
|
||
object ShoppingListExported : AnalyticsEvent("shopping-lists", "exported", null, null) | ||
|
||
data class IngredientAnalysisEnabled(val type: String) : AnalyticsEvent("ingredient-analysis", "enabled", type, null) | ||
|
||
data class IngredientAnalysisDisabled(val type: String) : AnalyticsEvent("ingredient-analysis", "disabled", type, null) | ||
|
||
data class AddProductToComparison(val barcode: String) : AnalyticsEvent("products", "compare-add", barcode, null) | ||
|
||
data class CompareProducts(val count: Float) : AnalyticsEvent("products", "compare-multiple", null, count) | ||
} |
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
Oops, something went wrong.