Feat: New analytics using GA4 [DEV]#20582
Conversation
|
TODO for me: Update the analytics usage in the whole app i.e. find the places where it is not there as of now and fix it |
There was a problem hiding this comment.
This is AWESOME
Before we look into this further, we need commits to:
- Remove all past consent for analytics, and use another preference key
- Modify our consent dialog to be opt-in, not opt-out (flip a checkbox default state)
EDIT: Split the refactorings out into 'move' commits whenever you can. so we don't lose git history
6764e26 to
360b343
Compare
360b343 to
bb640a0
Compare
bb640a0 to
d97afae
Compare
|
@david-allison what's blocking here now? |
d97afae to
32db738
Compare
BrayanDSO
left a comment
There was a problem hiding this comment.
Great work. Getting analytics back is going to be amazing. Thanks!
david-allison
left a comment
There was a problem hiding this comment.
Sorry to nitpick. From what I see in the diff, this no longer matches the description
- The issue should not be closed
- This functionality is not enabled
- The old implementation is not removed
| * analytics client id. Keeping it isolated avoids exposing the id through | ||
| * preference screens, backups, or bulk prefs operations on the main file. | ||
| */ | ||
| private const val ANALYTICS_PREFS = "analyticsPrefs" |
There was a problem hiding this comment.
How would this be impacted by profiles?
| @Volatile private var optIn = false | ||
|
|
||
| private val serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) | ||
| private val clientId: String by lazy { getOrCreateClientId(AnkiDroidApp.instance) } |
There was a problem hiding this comment.
Note that we have rare Android-level bugs where AnkiDroidApp.instance is not defined.
This needs to be handled: analytics is a startup concern which must not crash
| apiSecret = BuildConfig.ANALYTICS_API_KEY | ||
| appName = appContext.getString(R.string.app_name) | ||
| appVersion = BuildConfig.VERSION_NAME | ||
| enabled = true |
There was a problem hiding this comment.
Why does this not use optIn?
|
|
||
| serviceScope.launch { | ||
| runCatching { analytics?.flush() }.onFailure { e -> | ||
| Timber.e(e, "Failed to flush analytics") |
There was a problem hiding this comment.
| Timber.e(e, "Failed to flush analytics") | |
| Timber.w(e, "Failed to flush analytics") |
| private fun getRootCause(t: Throwable): Throwable { | ||
| var result = t | ||
| while (result.cause != null && result.cause !== result) { | ||
| result = result.cause!! | ||
| } | ||
| return result | ||
| } |
There was a problem hiding this comment.
I would make this an extension method and move it to common
Throwable.getRootCause
| var isEnabled: Boolean | ||
| get() = optIn | ||
| set(value) { | ||
| AnkiDroidApp.instance.sharedPrefs().edit { | ||
| putBoolean(ANALYTICS_OPTIN_KEY, value) | ||
| } | ||
| } |
| val analytics = analytics ?: return | ||
| analytics | ||
| .exception(clientId) | ||
| .description(description.take(150)) |
| } | ||
| } | ||
|
|
||
| fun sendAnalyticsScreenView(screen: Any) = sendAnalyticsScreenView(screen.javaClass.simpleName) |
There was a problem hiding this comment.
document usage and what this outputs: com.ichi2.anki.DeckPicker or DeckPicker
| desugar-jdk-libs-nio = "2.1.5" | ||
| drawer = "1.0.3" | ||
| espresso = '3.7.0' | ||
| googleAnalyticsKt = "1.2.1" |
There was a problem hiding this comment.
Add a comment linking the GitHub repo
| return samplePercentage | ||
| } | ||
|
|
||
| private fun initializePrefKeys(context: Context) { |
There was a problem hiding this comment.
Quickly document what this does
32db738 to
59e60b1
Compare
59e60b1 to
76fb29e
Compare
Community & Privacy Context
Before diving into the technical details, we want to be completely transparent about what this PR does and does not do. We have had analytics in the app for years to help us understand feature usage, but the system recently broke. This PR is strictly a bugfix to restore that existing functionality.
Because community trust is our top priority, please note:
Technical Implementation
I am updating the analytics usage in project, I forked library that Mike had implemented and after going through it, I create my own library with Kt and implemented whatever was there in java one(help needed here as the library needs more test which I am adding but any help is welcome).
Steps implemented:
Fixes
Approach
NA, the commits are faily self explaining
How Has This Been Tested?
Pixel 10
Learning (optional, can help others)
A lot of it tbh, the migration in Kotlin took me almost 2 days, then how to publish the library on maven (I had published the library on jitpack before but never on maven so this was new, Mike can give me valuable input since he is maintaining a lot fo things)
Checklist
Please, go through these checks before submitting the PR.