diff --git a/core/testing/build.gradle.kts b/core/testing/build.gradle.kts index 2fea5beb3..bae91fdb9 100644 --- a/core/testing/build.gradle.kts +++ b/core/testing/build.gradle.kts @@ -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) } diff --git a/core/testing/src/main/java/com/google/samples/apps/nowinandroid/core/testing/util/TestAnalyticsHelper.kt b/core/testing/src/main/java/com/google/samples/apps/nowinandroid/core/testing/util/TestAnalyticsHelper.kt new file mode 100644 index 000000000..005784c21 --- /dev/null +++ b/core/testing/src/main/java/com/google/samples/apps/nowinandroid/core/testing/util/TestAnalyticsHelper.kt @@ -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() + override fun logEvent(event: AnalyticsEvent) { + events.add(event) + } + + fun hasLogged(event: AnalyticsEvent) = events.contains(event) +} diff --git a/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt b/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt index 0e910ea06..49196a999 100644 --- a/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt +++ b/feature/foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModel.kt @@ -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,17 @@ class ForYouViewModel @Inject constructor( if (newsResourceId == deepLinkedNewsResource.value?.id) { savedStateHandle[LINKED_NEWS_RESOURCE_ID] = null } + analyticsHelper.logEvent( + AnalyticsEvent( + type = "news_deep_link_opened", + extras = listOf( + Param( + key = LINKED_NEWS_RESOURCE_ID, + value = newsResourceId, + ), + ), + ), + ) viewModelScope.launch { userDataRepository.setNewsResourceViewed( newsResourceId = newsResourceId, diff --git a/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt b/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt index 6a2ea4a02..d1f2f69b7 100644 --- a/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt +++ b/feature/foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt @@ -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() } }