Skip to content

Commit

Permalink
Merge pull request #13119 from woocommerce/issue/13074-product-global…
Browse files Browse the repository at this point in the history
…-unique-id-tracking

[Product Global Unique Identifier Support] Tracking
  • Loading branch information
toupper authored Dec 12, 2024
2 parents df6be4c + ca3343b commit fb53ca1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,10 @@ enum class AnalyticsEvent(override val siteless: Boolean = false) : IAnalyticsEv
PRODUCT_QUICK_INVENTORY_VIEW_PRODUCT_DETAILS_TAPPED,
PRODUCT_QUICK_INVENTORY_UPDATE_BOTTOM_SHEET_SHOWN,

// -- Product Search Via SKU
// -- Product Search Via SKU or Global Unique Identifier
PRODUCT_SEARCH_VIA_SKU_SUCCESS,
PRODUCT_SEARCH_VIA_SKU_FAILURE,
PRODUCT_SEARCH_VIA_GLOBAL_UNIQUE_IDENTIFIER_SUCCESS,

// -- Refunds
CREATE_ORDER_REFUND_NEXT_BUTTON_TAPPED,
Expand Down Expand Up @@ -588,6 +589,7 @@ enum class AnalyticsEvent(override val siteless: Boolean = false) : IAnalyticsEv
PRODUCT_DETAIL_VIEW_DOWNLOADABLE_FILES_TAPPED,
PRODUCT_PRICE_SETTINGS_DONE_BUTTON_TAPPED,
PRODUCT_INVENTORY_SETTINGS_DONE_BUTTON_TAPPED,
PRODUCT_INVENTORY_SETTINGS_GLOBAL_UNIQUE_IDENTIFIER_FIELD_EDITED,
PRODUCT_SHIPPING_SETTINGS_DONE_BUTTON_TAPPED,
PRODUCT_IMAGE_SETTINGS_DONE_BUTTON_TAPPED,
PRODUCT_CATEGORY_SETTINGS_DONE_BUTTON_TAPPED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,21 @@ class ProductInventoryViewModel @Inject constructor(
mapOf(AnalyticsTracker.KEY_HAS_CHANGED_DATA to hasChanges)
)
if (hasChanges && !hasSkuError() && !hasGlobalUniqueIdError()) {
trackGlobalUniqueIdChangeIfNecessary()
triggerEvent(ExitWithResult(inventoryData))
} else {
triggerEvent(Exit)
}
}

private fun trackGlobalUniqueIdChangeIfNecessary() {
if (inventoryData.globalUniqueId != originalInventoryData.globalUniqueId) {
analyticsTracker.track(
AnalyticsEvent.PRODUCT_INVENTORY_SETTINGS_GLOBAL_UNIQUE_IDENTIFIER_FIELD_EDITED
)
}
}

private fun clearSkuError() {
viewState = viewState.copy(skuErrorMessage = 0)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.woocommerce.android.ui.products.inventory

import com.woocommerce.android.analytics.AnalyticsEvent
import com.woocommerce.android.analytics.AnalyticsTrackerWrapper
import com.woocommerce.android.model.Product
import com.woocommerce.android.ui.orders.creation.CheckDigitRemoverFactory
import com.woocommerce.android.ui.orders.creation.GoogleBarcodeFormatMapper
Expand All @@ -12,6 +14,7 @@ import javax.inject.Inject
class FetchProductByIdentifier @Inject constructor(
private val productRepository: ProductListRepository,
private val checkDigitRemoverFactory: CheckDigitRemoverFactory,
private val tracker: AnalyticsTrackerWrapper
) {
suspend operator fun invoke(
codeScannerResultCode: String,
Expand Down Expand Up @@ -69,6 +72,12 @@ class FetchProductByIdentifier @Inject constructor(
)?.firstOrNull()
}

if (product != null) {
tracker.track(
AnalyticsEvent.PRODUCT_SEARCH_VIA_GLOBAL_UNIQUE_IDENTIFIER_SUCCESS
)
}

return product
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,16 @@ class ProductInventoryViewModelTest : BaseUnitTest() {
mapOf(AnalyticsTracker.KEY_HAS_CHANGED_DATA to true)
)
}

@Test
fun `Send tracks event upon exit if there was a change on the product global unique id`() {
// when
viewModel.onProductUniqueGlobalIdChanged("1234")
viewModel.onExit()

// then
verify(analyticsTracker).track(
AnalyticsEvent.PRODUCT_INVENTORY_SETTINGS_GLOBAL_UNIQUE_IDENTIFIER_FIELD_EDITED
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.woocommerce.android.ui.products.inventory

import com.woocommerce.android.analytics.AnalyticsEvent
import com.woocommerce.android.analytics.AnalyticsTrackerWrapper
import com.woocommerce.android.ui.orders.creation.CheckDigitRemover
import com.woocommerce.android.ui.orders.creation.CheckDigitRemoverFactory
import com.woocommerce.android.ui.orders.creation.GoogleBarcodeFormatMapper
Expand All @@ -22,7 +24,8 @@ import kotlin.test.assertEquals
class FetchProductByIdentifierTest : BaseUnitTest() {
private val repo: ProductListRepository = mock()
private val checkDigitRemoverFactory: CheckDigitRemoverFactory = mock()
private val sut = FetchProductByIdentifier(repo, checkDigitRemoverFactory)
private val analyticsTracker: AnalyticsTrackerWrapper = mock()
private val sut = FetchProductByIdentifier(repo, checkDigitRemoverFactory, analyticsTracker)

@Test
fun `given barcode scan result, when product found, should return success`() = testBlocking {
Expand Down Expand Up @@ -93,25 +96,46 @@ class FetchProductByIdentifierTest : BaseUnitTest() {
}

@Test
fun `given barcode scan result, when product found when searching by sku and global unique id, should return global unique id`() = testBlocking {
val skuProduct = ProductTestUtils.generateProduct(productId = 2)
fun `given barcode scan result, when product found when searching by global unique id and sku, should return the global unique id`() = testBlocking {
val skuProduct = ProductTestUtils.generateProduct(productId = 1)
whenever(
repo.searchProductList(
searchQuery = "123",
skuSearchOptions = WCProductStore.SkuSearchOptions.ExactSearch
)
).thenReturn(listOf(skuProduct))

val globalUniqueIdReturnedProduct = ProductTestUtils.generateProduct(productId = 1)
val globalUniqueIdProduct = ProductTestUtils.generateProduct(productId = 2)
whenever(
repo.searchProductListByGlobalUniqueId(globalUniqueId = "123")
).thenReturn(listOf(globalUniqueIdProduct))

val result = sut("123", GoogleBarcodeFormatMapper.BarcodeFormat.FormatCode39)

assertTrue(result.isSuccess)
assertEquals(result.getOrNull(), globalUniqueIdProduct)
}

@Test
fun `given barcode scan result, when product found when searching by global unique id, should track event`() = testBlocking {
whenever(
repo.searchProductList(
searchQuery = "123",
skuSearchOptions = WCProductStore.SkuSearchOptions.ExactSearch
)
).thenReturn(emptyList())

whenever(
repo.searchProductListByGlobalUniqueId(globalUniqueId = "123")
).thenReturn(listOf(globalUniqueIdReturnedProduct))
).thenReturn(ProductTestUtils.generateProductList())

val result = sut("123", GoogleBarcodeFormatMapper.BarcodeFormat.FormatCode39)

verify(analyticsTracker).track(
AnalyticsEvent.PRODUCT_SEARCH_VIA_GLOBAL_UNIQUE_IDENTIFIER_SUCCESS
)

assertTrue(result.isSuccess)
assertEquals(result.getOrNull(), globalUniqueIdReturnedProduct)
}

@Test
Expand Down

0 comments on commit fb53ca1

Please sign in to comment.