Merge pull request #824 from android/tj/notification-analytics

Track when deep link notifications are opened
pull/828/head
Adetunji Dahunsi 1 year ago committed by GitHub
commit 59048107c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -41,5 +41,6 @@ dependencies {
implementation(project(":core:domain"))
implementation(project(":core:model"))
implementation(project(":core:notifications"))
implementation(project(":core:analytics"))
implementation(libs.kotlinx.datetime)
}

@ -0,0 +1,30 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.testing.util
import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsEvent
import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper
class TestAnalyticsHelper : AnalyticsHelper {
private val events = mutableListOf<AnalyticsEvent>()
override fun logEvent(event: AnalyticsEvent) {
events.add(event)
}
fun hasLogged(event: AnalyticsEvent) = events.contains(event)
}

@ -19,6 +19,9 @@ package com.google.samples.apps.nowinandroid.feature.foryou
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsEvent
import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsEvent.Param
import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsHelper
import com.google.samples.apps.nowinandroid.core.data.repository.NewsResourceQuery
import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository
import com.google.samples.apps.nowinandroid.core.data.repository.UserNewsResourceRepository
@ -42,6 +45,7 @@ import javax.inject.Inject
class ForYouViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle,
syncManager: SyncManager,
private val analyticsHelper: AnalyticsHelper,
private val userDataRepository: UserDataRepository,
userNewsResourceRepository: UserNewsResourceRepository,
getFollowableTopics: GetFollowableTopicsUseCase,
@ -127,6 +131,7 @@ class ForYouViewModel @Inject constructor(
if (newsResourceId == deepLinkedNewsResource.value?.id) {
savedStateHandle[LINKED_NEWS_RESOURCE_ID] = null
}
analyticsHelper.logNewsDeepLinkOpen(newsResourceId = newsResourceId)
viewModelScope.launch {
userDataRepository.setNewsResourceViewed(
newsResourceId = newsResourceId,
@ -141,3 +146,16 @@ class ForYouViewModel @Inject constructor(
}
}
}
private fun AnalyticsHelper.logNewsDeepLinkOpen(newsResourceId: String) =
logEvent(
AnalyticsEvent(
type = "news_deep_link_opened",
extras = listOf(
Param(
key = LINKED_NEWS_RESOURCE_ID,
value = newsResourceId,
),
),
),
)

@ -17,6 +17,8 @@
package com.google.samples.apps.nowinandroid.feature.foryou
import androidx.lifecycle.SavedStateHandle
import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsEvent
import com.google.samples.apps.nowinandroid.core.analytics.AnalyticsEvent.Param
import com.google.samples.apps.nowinandroid.core.data.repository.CompositeUserNewsResourceRepository
import com.google.samples.apps.nowinandroid.core.domain.GetFollowableTopicsUseCase
import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
@ -30,6 +32,7 @@ import com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRe
import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository
import com.google.samples.apps.nowinandroid.core.testing.repository.emptyUserData
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
import com.google.samples.apps.nowinandroid.core.testing.util.TestAnalyticsHelper
import com.google.samples.apps.nowinandroid.core.testing.util.TestSyncManager
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
import com.google.samples.apps.nowinandroid.feature.foryou.navigation.LINKED_NEWS_RESOURCE_ID
@ -44,6 +47,7 @@ import org.junit.Rule
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
/**
* To learn more about how this test handles Flows created with stateIn, see
@ -54,6 +58,7 @@ class ForYouViewModelTest {
val mainDispatcherRule = MainDispatcherRule()
private val syncManager = TestSyncManager()
private val analyticsHelper = TestAnalyticsHelper()
private val userDataRepository = TestUserDataRepository()
private val topicsRepository = TestTopicsRepository()
private val newsRepository = TestNewsRepository()
@ -74,6 +79,7 @@ class ForYouViewModelTest {
viewModel = ForYouViewModel(
syncManager = syncManager,
savedStateHandle = savedStateHandle,
analyticsHelper = analyticsHelper,
userDataRepository = userDataRepository,
userNewsResourceRepository = userNewsResourceRepository,
getFollowableTopics = getFollowableTopicsUseCase,
@ -253,7 +259,6 @@ class ForYouViewModelTest {
assertEquals(
NewsFeedUiState.Success(
feed = emptyList(),
),
viewModel.feedState.value,
)
@ -484,6 +489,20 @@ class ForYouViewModelTest {
viewModel.deepLinkedNewsResource.value,
)
assertTrue(
analyticsHelper.hasLogged(
AnalyticsEvent(
type = "news_deep_link_opened",
extras = listOf(
Param(
key = LINKED_NEWS_RESOURCE_ID,
value = sampleNewsResources.first().id,
),
),
),
),
)
collectJob.cancel()
}
}

Loading…
Cancel
Save