Skip to content

Commit 14b9cb0

Browse files
authored
Merge pull request #217 from virtusize/bugfix/NSDK-408
NSDK-408 dublicated analytics events fix
2 parents c84066f + 35f5e5d commit 14b9cb0

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Use list notation, and following prefixes:
1010
- Bugfix - when fixing any major bug
1111
- Docs - for any improvement to documentation
1212

13+
### Changes:
14+
- Fix: Prevent send event userSawProduct when productValid = false or repeated product load
15+
1316
### 2.12.24
1417
- Fix: fixed VirtusizeInPageStandart view transalte issue
1518

Virtusize/Sources/VirtusizeRepository.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ internal class VirtusizeRepository: NSObject { // swiftlint:disable:this type_bo
5252
/// Note: No lock needed - protected by task cancellation in Virtusize.load()
5353
internal var serverStoreProductSet: Set<VirtusizeServerProduct> = []
5454

55+
/// The external product ID of the last product for which userSawProduct was sent
56+
private var lastUserSawProductExternalId: String?
57+
5558
/// Insert into serverStoreProductSet
5659
private func insertIntoProductSet(_ product: VirtusizeServerProduct) {
5760
serverStoreProductSet.insert(product)
@@ -83,13 +86,7 @@ internal class VirtusizeRepository: NSObject { // swiftlint:disable:this type_bo
8386
mutableProduct.productCheckData = product.productCheckData
8487
mutableProduct.imageURL = imageURL
8588

86-
// Send the API event where the user saw the product
87-
Task { // should NOT be awaited, to not block the main flow
88-
await VirtusizeAPIService.sendEvent(
89-
VirtusizeEvent(name: .userSawProduct),
90-
withContext: mutableProduct.jsonObject
91-
)
92-
}
89+
let isNewProduct = mutableProduct.externalId != lastUserSawProductExternalId
9390

9491
guard mutableProduct.productCheckData?.productDataId != nil else {
9592
DispatchQueue.main.async {
@@ -102,6 +99,17 @@ internal class VirtusizeRepository: NSObject { // swiftlint:disable:this type_bo
10299
return nil
103100
}
104101

102+
if isNewProduct && mutableProduct.productCheckData?.validProduct == true {
103+
lastUserSawProductExternalId = mutableProduct.externalId
104+
// Send the API event where the user saw the product
105+
Task { // should NOT be awaited, to not block the main flow
106+
await VirtusizeAPIService.sendEvent(
107+
VirtusizeEvent(name: .userSawProduct),
108+
withContext: mutableProduct.jsonObject
109+
)
110+
}
111+
}
112+
105113
if let sendImageToBackend = mutableProduct.productCheckData?.fetchMetaData,
106114
sendImageToBackend,
107115
imageURL != nil,
@@ -112,11 +120,13 @@ internal class VirtusizeRepository: NSObject { // swiftlint:disable:this type_bo
112120
}
113121

114122
// Send the API event where the user saw the widget button
115-
Task { // should NOT be awaited, to not block the main flow
116-
await VirtusizeAPIService.sendEvent(
117-
VirtusizeEvent(name: .userSawWidgetButton),
118-
withContext: mutableProduct.jsonObject
119-
)
123+
if isNewProduct && mutableProduct.productCheckData?.validProduct == true {
124+
Task { // should NOT be awaited, to not block the main flow
125+
await VirtusizeAPIService.sendEvent(
126+
VirtusizeEvent(name: .userSawWidgetButton),
127+
withContext: mutableProduct.jsonObject
128+
)
129+
}
120130
}
121131

122132
// Post notification on main thread

0 commit comments

Comments
 (0)