This repository was archived by the owner on Aug 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Create Analytics module #11
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1686243
Create Analytics module with a first version of TracksTracker
hamorillo eee9c81
Update gitignore to properly omit build folders
hamorillo 832b1de
Introduce GravatarAndroidLibraryConventionPlugin
hamorillo d7ff5c5
Declare AppEvent as a sealed class
hamorillo 16d0f16
Injecting TracksClient into TracksTracker
hamorillo 4b085a4
Add "userId" as Tracker parameter
hamorillo 66fe71e
Added some unit tests to TrackTracker
hamorillo 35ae904
Remove unnecessary manifest
hamorillo b4e3303
Extract common code in test to a @Before method
hamorillo 93412be
Include analytics module inside AppModule instead load it independently
hamorillo 81cbb12
Using WPCOM userType
hamorillo 6a17b89
Refactor TrackTrackerTest
hamorillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -9,6 +9,6 @@ local.properties | |
| *.iml | ||
|
|
||
| # generated files | ||
| /build | ||
| build/ | ||
| /captures | ||
| .kotlin/ | ||
This file contains hidden or 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 @@ | ||
| /build |
This file contains hidden or 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,18 @@ | ||
| plugins { | ||
| alias(libs.plugins.gravatar.android.library) | ||
| } | ||
|
|
||
| android { | ||
| namespace = "com.gravatar.analytics" | ||
| } | ||
|
|
||
| dependencies { | ||
|
|
||
| implementation(project.dependencies.platform(libs.koin.bom)) | ||
| implementation(libs.koin.core) | ||
| implementation(libs.automattic.tracks) | ||
|
|
||
| testImplementation(libs.junit) | ||
| testImplementation(libs.koin.test.junit4) | ||
| testImplementation(libs.mockk.android) | ||
| } |
Empty file.
This file contains hidden or 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,21 @@ | ||
| # Add project specific ProGuard rules here. | ||
| # You can control the set of applied configuration files using the | ||
| # proguardFiles setting in build.gradle. | ||
| # | ||
| # For more details, see | ||
| # http://developer.android.com/guide/developing/tools/proguard.html | ||
|
|
||
| # If your project uses WebView with JS, uncomment the following | ||
| # and specify the fully qualified class name to the JavaScript interface | ||
| # class: | ||
| #-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
| # public *; | ||
| #} | ||
|
|
||
| # Uncomment this to preserve the line number information for | ||
| # debugging stack traces. | ||
| #-keepattributes SourceFile,LineNumberTable | ||
|
|
||
| # If you keep the line number information, uncomment this to | ||
| # hide the original source file name. | ||
| #-renamesourcefileattribute SourceFile |
This file contains hidden or 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,5 @@ | ||
| package com.gravatar.analytics | ||
|
|
||
| interface Event { | ||
| val name: String | ||
| } | ||
This file contains hidden or 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,7 @@ | ||
| package com.gravatar.analytics | ||
|
|
||
| abstract class Tracker { | ||
| abstract var userId: String? | ||
| abstract fun trackEvent(event: Event) | ||
| abstract fun flush() | ||
| } |
13 changes: 13 additions & 0 deletions
13
analytics/src/main/kotlin/com/gravatar/analytics/di/AnalyticsModule.kt
This file contains hidden or 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,13 @@ | ||
| package com.gravatar.analytics.di | ||
|
|
||
| import com.automattic.android.tracks.TracksClient | ||
| import com.gravatar.analytics.Tracker | ||
| import com.gravatar.analytics.tracks.TracksTracker | ||
| import org.koin.core.module.dsl.bind | ||
| import org.koin.core.module.dsl.singleOf | ||
| import org.koin.dsl.module | ||
|
|
||
| val analyticsModule = module { | ||
| single { TracksClient.getClient(get()) } | ||
| singleOf(::TracksTracker) { bind<Tracker>() } | ||
| } |
27 changes: 27 additions & 0 deletions
27
analytics/src/main/kotlin/com/gravatar/analytics/tracks/TracksTracker.kt
This file contains hidden or 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,27 @@ | ||
| package com.gravatar.analytics.tracks | ||
|
|
||
| import com.automattic.android.tracks.TracksClient | ||
| import com.gravatar.analytics.Event | ||
| import com.gravatar.analytics.Tracker | ||
| import java.util.UUID | ||
|
|
||
| internal class TracksTracker(private val tracksClient: TracksClient) : Tracker() { | ||
| override var userId: String? = null | ||
| private val anonId: String = generateNewAnonID() | ||
|
|
||
| override fun trackEvent(event: Event) { | ||
| val userType = userId?.let { | ||
| TracksClient.NosaraUserType.WPCOM | ||
| } ?: TracksClient.NosaraUserType.ANON | ||
| tracksClient.track(event.name, userId ?: anonId, userType) | ||
| } | ||
|
|
||
| override fun flush() { | ||
| tracksClient.flush() | ||
| } | ||
| } | ||
|
|
||
| private fun generateNewAnonID(): String { | ||
| // Generate a new UUID and return it as a string. | ||
| return UUID.randomUUID().toString() | ||
| } |
23 changes: 23 additions & 0 deletions
23
analytics/src/test/kotlin/com/gravatar/analytics/di/AnalyticsModuleTest.kt
This file contains hidden or 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,23 @@ | ||
| package com.gravatar.analytics.di | ||
|
|
||
| import android.content.Context | ||
| import com.gravatar.analytics.tracks.TracksTracker | ||
| import org.junit.Test | ||
| import org.koin.core.annotation.KoinExperimentalAPI | ||
| import org.koin.test.KoinTest | ||
| import org.koin.test.verify.definition | ||
| import org.koin.test.verify.injectedParameters | ||
| import org.koin.test.verify.verify | ||
|
|
||
| class AnalyticsModuleTest : KoinTest { | ||
|
|
||
| @OptIn(KoinExperimentalAPI::class) | ||
| @Test | ||
| fun checkAllModules() { | ||
| analyticsModule.verify( | ||
| injections = injectedParameters( | ||
| definition<TracksTracker>(Context::class) | ||
| ) | ||
AdamGrzybkowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
analytics/src/test/kotlin/com/gravatar/analytics/tracks/TrackTrackerTest.kt
This file contains hidden or 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,53 @@ | ||
| package com.gravatar.analytics.tracks | ||
|
|
||
| import com.automattic.android.tracks.TracksClient | ||
| import com.gravatar.analytics.Event | ||
| import io.mockk.mockk | ||
| import io.mockk.verify | ||
| import io.mockk.verifySequence | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
|
|
||
| class TrackTrackerTest { | ||
|
|
||
| private lateinit var tracker: TracksTracker | ||
| private lateinit var mockClient: TracksClient | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| mockClient = mockk<TracksClient>(relaxed = true) | ||
| tracker = TracksTracker(mockClient) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when trackEvent is invoked without userId then call track on client with ANON`() { | ||
| val event = object : Event { | ||
| override val name: String = "test_event" | ||
| } | ||
|
|
||
| tracker.trackEvent(event) | ||
|
|
||
| verify { mockClient.track(event.name, any(), TracksClient.NosaraUserType.ANON) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `when trackEvent is invoked with userId then call track on client with GRAVATAR`() { | ||
| val event = object : Event { | ||
| override val name: String = "test_event_with_user" | ||
| } | ||
| tracker.userId = "someUserId" | ||
|
|
||
| tracker.trackEvent(event) | ||
|
|
||
| verify { mockClient.track(event.name, "someUserId", TracksClient.NosaraUserType.WPCOM) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `when flush is invoked then call flush on client`() { | ||
| tracker.flush() | ||
|
|
||
| verifySequence { | ||
| mockClient.flush() | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
| package com.gravatar.app.analytics | ||
|
|
||
| import com.gravatar.analytics.Event | ||
|
|
||
| sealed class AppEvent : Event { | ||
|
|
||
| data object Test : AppEvent() { | ||
| override val name: String = "gravatarandroid_test_test" | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will likely need a way to pass some params as well.