@@ -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