-
Notifications
You must be signed in to change notification settings - Fork 111
/
WooAnalyticsEvent.swift
3223 lines (2826 loc) · 145 KB
/
WooAnalyticsEvent.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Foundation
import Yosemite
import WooFoundation
/// This struct represents an analytics event. It is a combination of `WooAnalyticsStat` and
/// its properties.
///
/// This was mostly created to promote static-typing via constructors.
///
/// ## Adding New Events
///
/// 1. Add the event name (`String`) to `WooAnalyticsStat`.
/// 2. Create an `extension` of `WooAnalyticsStat` if necessary for grouping.
/// 3. Add a `static func` constructor.
///
/// Here is an example:
///
/// ~~~
/// extension WooAnalyticsEvent {
/// enum LoginStep: String {
/// case start
/// case success
/// }
///
/// static func login(step: LoginStep) -> WooAnalyticsEvent {
/// let properties = [
/// "step": step.rawValue
/// ]
///
/// return WooAnalyticsEvent(name: "login", properties: properties)
/// }
/// }
/// ~~~
///
/// Examples of tracking calls (in the client App or Pod):
///
/// ~~~
/// Analytics.track(event: .login(step: .start))
/// Analytics.track(event: .loginStart)
/// ~~~
///
struct WooAnalyticsEvent {
init(statName: WooAnalyticsStat, properties: [String: WooAnalyticsEventPropertyType], error: Error? = nil) {
self.statName = statName
self.properties = properties
self.error = error
}
let statName: WooAnalyticsStat
let properties: [String: WooAnalyticsEventPropertyType]
let error: Error?
}
// MARK: - In-app Feedback and Survey
extension WooAnalyticsEvent {
/// The action performed on the In-app Feedback Card.
public enum AppFeedbackPromptAction: String {
case shown
case liked
case didntLike = "didnt_like"
}
/// Where the feedback was shown. This is shared by a couple of events.
public enum FeedbackContext: String {
/// Shown in Stats but is for asking general feedback.
case general
/// Shown in products banner for general feedback.
case productsGeneral = "products_general"
/// Shown in shipping labels banner for Milestone 3 features.
case shippingLabelsRelease3 = "shipping_labels_m3"
/// Shown in beta feature banner for order add-ons.
case addOnsI1 = "add-ons_i1"
/// Shown in orders banner for order creation release.
case orderCreation = "order_creation"
/// Shown in beta feature banner for coupon management.
case couponManagement = "coupon_management"
/// Shown in store setup task list
case storeSetup = "store_setup"
/// Shown in Product details form for a AI generated product
case productCreationAI = "product_creation_ai"
/// Shown in the order form after adding a shipping line
case orderFormShippingLines = "order_form_shipping_lines"
}
/// The action performed on the survey screen.
public enum SurveyScreenAction: String {
case opened
case canceled
case completed
}
/// The action performed on "New Features" banners like in Products.
public enum FeatureFeedbackBannerAction: String {
case gaveFeedback = "gave_feedback"
case dismissed
}
/// The action performed on a shipment tracking number like in a shipping label card in order details.
public enum ShipmentTrackingMenuAction: String {
case track
case copy
}
/// The result of a shipping labels API GET request.
public enum ShippingLabelsAPIRequestResult {
case success
case failed(error: Error)
fileprivate var rawValue: String {
switch self {
case .success:
return "success"
case .failed:
return "failed"
}
}
}
static func appFeedbackPrompt(action: AppFeedbackPromptAction) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .appFeedbackPrompt, properties: ["action": action.rawValue])
}
static func surveyScreen(context: FeedbackContext, action: SurveyScreenAction) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .surveyScreen, properties: ["context": context.rawValue, "action": action.rawValue])
}
static func featureFeedbackBanner(context: FeedbackContext, action: FeatureFeedbackBannerAction) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .featureFeedbackBanner, properties: ["context": context.rawValue, "action": action.rawValue])
}
static func shipmentTrackingMenu(action: ShipmentTrackingMenuAction) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .shipmentTrackingMenuAction, properties: ["action": action.rawValue])
}
static func shippingLabelsAPIRequest(result: ShippingLabelsAPIRequestResult) -> WooAnalyticsEvent {
switch result {
case .success:
return WooAnalyticsEvent(statName: .shippingLabelsAPIRequest, properties: ["action": result.rawValue])
case .failed(let error):
return WooAnalyticsEvent(statName: .shippingLabelsAPIRequest, properties: [
"action": result.rawValue,
"error": error.localizedDescription
])
}
}
static func ordersListLoaded(totalDuration: TimeInterval,
pageNumber: Int,
filters: FilterOrderListViewModel.Filters?,
totalCompletedOrders: Int?) -> WooAnalyticsEvent {
let properties: [String: WooAnalyticsEventPropertyType?] = [
"status": (filters?.orderStatus ?? []).map { $0.rawValue }.joined(separator: ","),
"page_number": Int64(pageNumber),
"total_duration": Double(totalDuration),
"date_range": filters?.dateRange?.analyticsDescription ?? String(),
"total_completed_orders": totalCompletedOrders
]
return WooAnalyticsEvent(statName: .ordersListLoaded, properties: properties.compactMapValues { $0 })
}
static func ordersListLoadError(_ error: Error) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .ordersListLoadError, properties: [:], error: error)
}
}
// MARK: - Issue Refund
//
extension WooAnalyticsEvent {
// Namespace
enum IssueRefund {
/// The state of the "refund shipping" button
enum ShippingSwitchState: String {
case on
case off
}
// The method used for the refund
enum RefundMethod: String {
case items = "ITEMS"
case amount = "AMOUNT"
}
static func createRefund(orderID: Int64, fullyRefunded: Bool, method: RefundMethod, gateway: String, amount: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .refundCreate, properties: [
"order_id": "\(orderID)",
"is_full": "\(fullyRefunded)",
"method": method.rawValue,
"gateway": gateway,
"amount": amount
])
}
static func createRefundSuccess(orderID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .refundCreateSuccess, properties: ["order_id": "\(orderID)"])
}
static func createRefundFailed(orderID: Int64, error: Error) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .refundCreateFailed, properties: [
"order_id": "\(orderID)",
"error_description": error.localizedDescription,
])
}
static func selectAllButtonTapped(orderID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .createOrderRefundSelectAllItemsButtonTapped, properties: ["order_id": "\(orderID)"])
}
static func quantityDialogOpened(orderID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .createOrderRefundItemQuantityDialogOpened, properties: ["order_id": "\(orderID)"])
}
static func nextButtonTapped(orderID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .createOrderRefundNextButtonTapped, properties: ["order_id": "\(orderID)"])
}
static func summaryButtonTapped(orderID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .createOrderRefundSummaryRefundButtonTapped, properties: ["order_id": "\(orderID)"])
}
static func shippingSwitchTapped(orderID: Int64, state: ShippingSwitchState) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .createOrderRefundShippingOptionTapped, properties: ["order_id": "\(orderID)", "action": state.rawValue])
}
}
}
// MARK: - Variations
//
extension WooAnalyticsEvent {
// Namespace
enum Variations {
/// Common event keys
///
private enum Keys {
static let productID = "product_id"
static let variationID = "product_variation_id"
static let serverTime = "time"
static let errorDescription = "error_description"
static let field = "field"
static let variationsCount = "variations_count"
static let hasChangedData = "has_changed_data"
}
enum BulkUpdateField: String {
case regularPrice = "regular_price"
}
static func addFirstVariationButtonTapped(productID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .addFirstVariationButtonTapped, properties: [Keys.productID: "\(productID)"])
}
static func addMoreVariationsButtonTapped(productID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .addMoreVariationsButtonTapped, properties: [Keys.productID: "\(productID)"])
}
static func createVariation(productID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .createProductVariation, properties: [Keys.productID: "\(productID)"])
}
static func createVariationSuccess(productID: Int64, time: Double) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .createProductVariationSuccess, properties: [Keys.productID: "\(productID)", Keys.serverTime: "\(time)"])
}
static func createVariationFail(productID: Int64, time: Double, error: Error) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .createProductVariationFailed, properties: [Keys.productID: "\(productID)",
Keys.serverTime: "\(time)",
Keys.errorDescription: error.localizedDescription])
}
static func removeVariationButtonTapped(productID: Int64, variationID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .removeProductVariationButtonTapped, properties: [Keys.productID: "\(productID)", Keys.variationID: "\(variationID)"])
}
static func editAttributesButtonTapped(productID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .editProductAttributesButtonTapped, properties: [Keys.productID: "\(productID)"])
}
static func addAttributeButtonTapped(productID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .addProductAttributeButtonTapped, properties: [Keys.productID: "\(productID)"])
}
static func updateAttribute(productID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .updateProductAttribute, properties: [Keys.productID: "\(productID)"])
}
static func updateAttributeSuccess(productID: Int64, time: Double) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .updateProductAttributeSuccess, properties: [Keys.productID: "\(productID)", Keys.serverTime: "\(time)"])
}
static func updateAttributeFail(productID: Int64, time: Double, error: Error) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .updateProductAttributeFail, properties: [Keys.productID: "\(productID)",
Keys.serverTime: "\(time)",
Keys.errorDescription: error.localizedDescription])
}
static func renameAttributeButtonTapped(productID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .renameProductAttributeButtonTapped, properties: [Keys.productID: "\(productID)"])
}
static func removeAttributeButtonTapped(productID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .removeProductAttributeButtonTapped, properties: [Keys.productID: "\(productID)"])
}
static func editVariationAttributeOptionsRowTapped(productID: Int64, variationID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .editProductVariationAttributeOptionsRowTapped, properties: [Keys.productID: "\(productID)",
Keys.variationID: "\(variationID)"])
}
static func editVariationAttributeOptionsDoneButtonTapped(productID: Int64, variationID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .editProductVariationAttributeOptionsDoneButtonTapped, properties: [Keys.productID: "\(productID)",
Keys.variationID: "\(variationID)"])
}
static func bulkUpdateSectionTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationBulkUpdateSectionTapped, properties: [:])
}
static func bulkUpdateFieldTapped(field: BulkUpdateField) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationBulkUpdateFieldTapped, properties: [Keys.field: field.rawValue])
}
static func bulkUpdateFieldSuccess(field: BulkUpdateField) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationBulkUpdateFieldSuccess, properties: [Keys.field: field.rawValue])
}
static func bulkUpdateFieldFailed(field: BulkUpdateField, error: Error) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationBulkUpdateFieldFail, properties: [Keys.field: field.rawValue], error: error)
}
static func productVariationGenerationRequested() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationGenerationRequested, properties: [:])
}
static func productVariationGenerationConfirmed(count: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationGenerationConfirmed, properties: [Keys.variationsCount: count])
}
static func productVariationGenerationLimitReached(count: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationGenerationLimitReached, properties: [Keys.variationsCount: count])
}
static func productVariationGenerationSuccess() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationGenerationSuccess, properties: [:])
}
static func productVariationGenerationFailure() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationGenerationFailure, properties: [:])
}
/// Tracks when the merchant taps the Quantity Rules row for a product variation.
///
static func quantityRulesTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationDetailViewQuantityRulesTapped, properties: [:])
}
/// For Woo Subscriptions products, tracks when the subscription free trial setting is tapped.
///
static func freeTrialSettingsTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationViewSubscriptionFreeTrialTapped, properties: [:])
}
/// For Woo Subscriptions products, tracks when the subscription free trial setting is tapped.
///
static func expirationDateSettingsTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationViewSubscriptionExpirationDateTapped, properties: [:])
}
static func quantityRulesDoneButtonTapped(hasChangedData: Bool) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productVariationDetailsViewQuantityRulesDoneButtonTapped,
properties: [Keys.hasChangedData: hasChangedData])
}
}
}
// MARK: - Order Detail Add-ons
//
extension WooAnalyticsEvent {
// Namespace
enum OrderDetailAddOns {
/// Common event keys
///
private enum Keys {
static let state = "state"
static let addOns = "add_ons"
}
static func betaFeaturesSwitchToggled(isOn: Bool) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .settingsBetaFeaturesOrderAddOnsToggled, properties: [Keys.state: isOn ? "on" : "off"])
}
static func orderAddOnsViewed(addOnNames: [String]) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderDetailAddOnsViewed, properties: [Keys.addOns: addOnNames.joined(separator: ",")])
}
}
}
// MARK: - Product Detail
//
extension WooAnalyticsEvent {
/// Namespace
enum ProductDetail {
/// Common event keys
///
private enum Keys {
static let hasChangedData = "has_changed_data"
}
static func loaded(hasLinkedProducts: Bool, hasMinMaxQuantityRules: Bool, horizontalSizeClass: UIUserInterfaceSizeClass) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productDetailLoaded, properties: ["has_linked_products": hasLinkedProducts,
"has_minmax_quantity_rules": hasMinMaxQuantityRules,
"horizontal_size_class": horizontalSizeClass.nameForAnalytics])
}
/// Tracks when the merchant previews a product draft.
///
static func previewTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productDetailPreviewTapped, properties: [:])
}
/// Tracks when the product preview fails due to a HTTP error.
///
static func previewFailed(statusCode: Int) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productDetailPreviewFailed, properties: ["status_code": Int64(statusCode)])
}
/// Tracks when the merchant taps the Bundled Products row (applicable for bundle-type products only).
///
static func bundledProductsTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productDetailViewBundledProductsTapped, properties: [:])
}
/// Tracks when the merchant taps the Components row (applicable for composite-type products only).
///
static func componentsTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productDetailViewComponentsTapped, properties: [:])
}
/// Tracks when the merchant taps the Quantity Rules row.
///
static func quantityRulesTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productDetailViewQuantityRulesTapped, properties: [:])
}
/// For Woo Subscriptions products, tracks when the subscription free trial setting is tapped.
///
static func freeTrialSettingsTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productDetailsViewSubscriptionFreeTrialTapped, properties: [:])
}
/// For Woo Subscriptions products, tracks when the subscription free trial setting is tapped.
///
static func expirationDateSettingsTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productDetailsViewSubscriptionExpirationDateTapped, properties: [:])
}
static func quantityRulesDoneButtonTapped(hasChangedData: Bool) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productDetailsViewQuantityRulesDoneButtonTapped,
properties: [Keys.hasChangedData: hasChangedData])
}
/// For Woo Subscriptions products, tracks when the subscription expiration details screen is closed.
///
static func expirationDetailsScreenClosed(hasChangedData: Bool) -> WooAnalyticsEvent {
WooAnalyticsEvent(
statName: .productSubscriptionExpirationDoneButtonTapped,
properties: [Keys.hasChangedData: hasChangedData]
)
}
/// For Woo Subscriptions products, tracks when the subscription free trial screen is closed.
///
static func freeTrialDetailsScreenClosed(hasChangedData: Bool) -> WooAnalyticsEvent {
WooAnalyticsEvent(
statName: .productSubscriptionFreeTrialDoneButtonTapped,
properties: [Keys.hasChangedData: hasChangedData]
)
}
}
}
// MARK: - Product Detail Add-ons
//
extension WooAnalyticsEvent {
/// Common event keys
///
private enum Keys {
static let productID = "product_id"
}
// Namespace
enum ProductDetailAddOns {
static func productAddOnsButtonTapped(productID: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .productDetailViewProductAddOnsTapped, properties: [Keys.productID: "\(productID)"])
}
}
}
// MARK: - Order General
//
extension WooAnalyticsEvent {
// Namespace
enum Orders {
/// Possible Order Flows
///
enum Flow: String {
case creation
case editing
case list
case orderDetails = "order_details"
}
/// Possible item types to add to an Order
///
enum ProductType: String {
case product
case variation
}
enum OrderProductAdditionVia: String {
case scanning
case manually
}
enum GlobalKeys {
static let millisecondsSinceOrderAddNew = "milliseconds_since_order_add_new"
}
/// The raw value is the analytics event property value.
enum BundleProductConfigurationSource: String {
case productCard = "product_card"
case productSelector = "product_selector"
}
/// The raw value is the analytics event property value.
enum BundleProductConfigurationChangedField: String {
case quantity
case variation
}
private enum Keys {
static let changedField = "changed_field"
static let flow = "flow"
static let hasDifferentShippingDetails = "has_different_shipping_details"
static let orderStatus = "order_status"
static let productCount = "product_count"
static let customAmountsCount = "custom_amounts_count"
static let hasAddOns = "has_addons"
static let hasBundleProductConfiguration = "has_bundle_configuration"
static let hasCustomerDetails = "has_customer_details"
static let hasFees = "has_fees"
static let hasShippingMethod = "has_shipping_method"
static let isGiftCardRemoved = "removed"
static let errorContext = "error_context"
static let errorDescription = "error_description"
static let to = "to"
static let from = "from"
static let orderID = "id"
static let productTypes = "product_types"
static let hasMultipleShippingLines = "has_multiple_shipping_lines"
static let shippingLinesCount = "shipping_lines_count"
static let hasMultipleFeeLines = "has_multiple_fee_lines"
static let itemType = "item_type"
static let source = "source"
static let addedVia = "added_via"
static let isFilterActive = "is_filter_active"
static let searchFilter = "search_filter"
static let couponsCount = "coupons_count"
static let type = "type"
static let usesGiftCard = "use_gift_card"
static let taxStatus = "tax_status"
static let expanded = "expanded"
static let horizontalSizeClass = "horizontal_size_class"
static let shippingMethod = "shipping_method"
}
static func ordersSelected(horizontalSizeClass: UIUserInterfaceSizeClass) -> WooAnalyticsEvent {
return WooAnalyticsEvent(statName: .ordersSelected,
properties: [
Keys.horizontalSizeClass: horizontalSizeClass.nameForAnalytics
])
}
static func ordersReselected(horizontalSizeClass: UIUserInterfaceSizeClass) -> WooAnalyticsEvent {
return WooAnalyticsEvent(statName: .ordersReselected,
properties: [
Keys.horizontalSizeClass: horizontalSizeClass.nameForAnalytics
])
}
static func orderOpen(order: Order,
horizontalSizeClass: UIUserInterfaceSizeClass) -> WooAnalyticsEvent {
return WooAnalyticsEvent(statName: .orderOpen,
properties: [
"id": order.orderID,
"status": order.status.rawValue,
Keys.horizontalSizeClass: horizontalSizeClass.nameForAnalytics
])
}
static func orderAddNew() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderAddNew, properties: [:])
}
static func orderProductsLoaded(order: Order, products: [Product], addOnGroups: [AddOnGroup]) -> WooAnalyticsEvent {
let productTypes = productTypes(order: order, products: products)
let hasAddOns = hasAddOns(order: order, products: products, addOnGroups: addOnGroups)
return WooAnalyticsEvent(statName: .orderProductsLoaded, properties: [Keys.orderID: order.orderID,
Keys.productTypes: productTypes,
Keys.hasAddOns: hasAddOns])
}
private static func hasAddOns(order: Order, products: [Product], addOnGroups: [AddOnGroup]) -> Bool {
for item in order.items {
guard let product = products.first(where: { $0.productID == item.productID }) else {
continue
}
if item.addOns.isNotEmpty {
return true
}
let itemHasAddOns = AddOnCrossreferenceUseCase(orderItemAttributes: item.attributes,
product: product,
addOnGroups: addOnGroups)
.addOns().isNotEmpty
if itemHasAddOns {
return true
}
}
return false
}
private static func productTypes(order: Order, products: [Product]) -> String {
let productIDs = order.items.map { $0.productID }
return productIDs.compactMap { productID in
products.first(where: { $0.productID == productID })?.productType.rawValue
}.uniqued().sorted().joined(separator: ",")
}
static func orderAddNewFromBarcodeScanningTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderListProductBarcodeScanningTapped, properties: [:])
}
static func productAddNewFromBarcodeScanningTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderCreationProductBarcodeScanningTapped, properties: [:])
}
static func orderEditButtonTapped(hasMultipleShippingLines: Bool, hasMultipleFeeLines: Bool) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderEditButtonTapped, properties: [
Keys.hasMultipleShippingLines: hasMultipleShippingLines,
Keys.hasMultipleFeeLines: hasMultipleFeeLines
])
}
static func orderEditButtonTappedWhileDisabledForCurrencyConflict() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderEditButtonTappedWhileDisabledForCurrencyConflict, properties: [:])
}
static func orderProductAdd(flow: Flow,
source: BarcodeScanning.Source,
addedVia: OrderProductAdditionVia,
productCount: Int = 1,
includesBundleProductConfiguration: Bool) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderProductAdd, properties: [
Keys.flow: flow.rawValue,
Keys.productCount: Int64(productCount),
Keys.source: source.rawValue,
Keys.addedVia: addedVia.rawValue,
Keys.hasBundleProductConfiguration: includesBundleProductConfiguration
])
}
static func orderProductQuantityChange(flow: Flow) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderProductQuantityChange, properties: [Keys.flow: flow.rawValue])
}
static func orderProductRemove(flow: Flow) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderProductRemove, properties: [Keys.flow: flow.rawValue])
}
static func orderCustomerAdd(flow: Flow, hasDifferentShippingDetails: Bool) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderCustomerAdd, properties: [
Keys.flow: flow.rawValue,
Keys.hasDifferentShippingDetails: hasDifferentShippingDetails
])
}
static func orderFeeAdd(flow: Flow, taxStatus: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderFeeAdd, properties: [Keys.flow: flow.rawValue, Keys.taxStatus: taxStatus])
}
static func orderFeeUpdate(flow: Flow, taxStatus: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderFeeUpdate, properties: [Keys.flow: flow.rawValue, Keys.taxStatus: taxStatus])
}
static func orderFeeRemove(flow: Flow) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderFeeRemove, properties: [Keys.flow: flow.rawValue])
}
static func orderCouponAdd(flow: Flow) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderCouponAdd, properties: [Keys.flow: flow.rawValue])
}
static func orderCouponRemove(flow: Flow) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderCouponRemove, properties: [Keys.flow: flow.rawValue])
}
static func orderGoToCouponsButtonTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderGoToCouponsButtonTapped, properties: [:])
}
static func orderTaxHelpButtonTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderTaxHelpButtonTapped, properties: [:])
}
static func taxEducationalDialogEditInAdminButtonTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .taxEducationalDialogEditInAdminButtonTapped, properties: [:])
}
static func productDiscountAdd(type: FeeOrDiscountLineDetailsViewModel.FeeOrDiscountType) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderProductDiscountAdd, properties: [Keys.type: type.rawValue])
}
static func productDiscountRemove() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderProductDiscountRemove, properties: [:])
}
static func productDiscountAddButtonTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderProductDiscountAddButtonTapped, properties: [:])
}
static func productDiscountEditButtonTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderProductDiscountEditButtonTapped, properties: [:])
}
static func orderAddShippingTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderAddShippingTapped, properties: [:])
}
static func orderShippingMethodSelected(methodID: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderShippingMethodSelected, properties: [Keys.shippingMethod: methodID])
}
static func orderShippingMethodAdd(flow: Flow, methodID: String, shippingLinesCount: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderShippingMethodAdd, properties: [Keys.flow: flow.rawValue,
Keys.shippingMethod: methodID,
Keys.shippingLinesCount: shippingLinesCount])
}
static func orderShippingMethodRemove(flow: Flow) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderShippingMethodRemove, properties: [Keys.flow: flow.rawValue])
}
static func orderCustomerNoteAdd(flow: Flow, orderID: Int64, orderStatus: OrderStatusEnum) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderNoteAdd, properties: [Keys.flow: flow.rawValue,
"parent_id": orderID,
"status": orderStatus.rawValue,
"type": "customer"])
}
static func orderStatusChange(flow: Flow, orderID: Int64?, from oldStatus: OrderStatusEnum, to newStatus: OrderStatusEnum) -> WooAnalyticsEvent {
let properties: [String: WooAnalyticsEventPropertyType?] = [
Keys.flow: flow.rawValue,
Keys.orderID: orderID,
Keys.from: oldStatus.rawValue,
Keys.to: newStatus.rawValue
]
return WooAnalyticsEvent(statName: .orderStatusChange, properties: properties.compactMapValues { $0 })
}
static func orderTotalsExpansionChanged(flow: Flow,
expanded: Bool) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderFormTotalsPanelToggled, properties: [
Keys.flow: flow.rawValue,
Keys.expanded: expanded
])
}
static func orderCreateButtonTapped(order: Order,
status: OrderStatusEnum,
productCount: Int,
customAmountsCount: Int,
hasCustomerDetails: Bool,
hasFees: Bool,
hasShippingMethod: Bool,
products: [Product],
horizontalSizeClass: UIUserInterfaceSizeClass) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderCreateButtonTapped, properties: [
Keys.orderStatus: status.rawValue,
Keys.productCount: Int64(productCount),
Keys.customAmountsCount: Int64(customAmountsCount),
Keys.hasCustomerDetails: hasCustomerDetails,
Keys.hasFees: hasFees,
Keys.hasShippingMethod: hasShippingMethod,
Keys.productTypes: productTypes(order: order, products: products),
Keys.horizontalSizeClass: horizontalSizeClass.nameForAnalytics
])
}
static func orderCreationCollectPaymentTapped(order: Order,
status: OrderStatusEnum,
productCount: Int,
customAmountsCount: Int,
hasCustomerDetails: Bool,
hasFees: Bool,
hasShippingMethod: Bool,
products: [Product],
horizontalSizeClass: UIUserInterfaceSizeClass) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .collectPaymentTapped, properties: [
Keys.flow: Flow.creation.rawValue,
Keys.orderStatus: status.rawValue,
Keys.productCount: Int64(productCount),
Keys.customAmountsCount: Int64(customAmountsCount),
Keys.hasCustomerDetails: hasCustomerDetails,
Keys.hasFees: hasFees,
Keys.hasShippingMethod: hasShippingMethod,
Keys.productTypes: productTypes(order: order, products: products),
Keys.horizontalSizeClass: horizontalSizeClass.nameForAnalytics
])
}
static func orderCreationSuccess(millisecondsSinceSinceOrderAddNew: Int64?,
couponsCount: Int64,
usesGiftCard: Bool,
shippingLinesCount: Int64) -> WooAnalyticsEvent {
var properties: [String: WooAnalyticsEventPropertyType] = [Keys.couponsCount: couponsCount,
Keys.usesGiftCard: usesGiftCard,
Keys.shippingLinesCount: shippingLinesCount]
if let lapseSinceLastOrderAddNew = millisecondsSinceSinceOrderAddNew {
properties[GlobalKeys.millisecondsSinceOrderAddNew] = lapseSinceLastOrderAddNew
}
return WooAnalyticsEvent(statName: .orderCreationSuccess, properties: properties)
}
static func orderCreationFailed(usesGiftCard: Bool, errorContext: String, errorDescription: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderCreationFailed, properties: [
Keys.usesGiftCard: usesGiftCard,
Keys.errorContext: errorContext,
Keys.errorDescription: errorDescription
])
}
static func orderSyncFailed(flow: Flow, usesGiftCard: Bool, errorContext: String, errorDescription: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderSyncFailed, properties: [
Keys.flow: flow.rawValue,
Keys.usesGiftCard: usesGiftCard,
Keys.errorContext: errorContext,
Keys.errorDescription: errorDescription
])
}
static func orderCreationProductSelectorItemSelected(productType: ProductType) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderCreationProductSelectorItemSelected, properties: [
Keys.itemType: productType.rawValue
])
}
static func orderCreationProductSelectorItemUnselected(productType: ProductType) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderCreationProductSelectorItemUnselected, properties: [
Keys.itemType: productType.rawValue
])
}
static func orderCreationProductSelectorConfirmButtonTapped(productCount: Int, sources: [String], isFilterActive: Bool) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderCreationProductSelectorConfirmButtonTapped, properties: [
Keys.productCount: Int64(productCount),
Keys.source: sources.joined(separator: ","),
Keys.isFilterActive: isFilterActive
])
}
static func orderCreationProductSelectorSearchTriggered(searchFilter: ProductSearchFilter) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderCreationProductSelectorSearchTriggered, properties: [
Keys.searchFilter: searchFilter.rawValue
])
}
static func orderCreationProductSelectorClearSelectionButtonTapped(productType: ProductType) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderCreationProductSelectorClearSelectionButtonTapped, properties: [
Keys.source: productType.rawValue + "_selector"
])
}
static func orderFormAddGiftCardCTAShown(flow: Flow) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderFormAddGiftCardCTAShown, properties: [Keys.flow: flow.rawValue])
}
static func orderFormAddGiftCardCTATapped(flow: Flow) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderFormAddGiftCardCTATapped, properties: [Keys.flow: flow.rawValue])
}
static func orderFormGiftCardSet(flow: Flow, isRemoved: Bool) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderFormGiftCardSet, properties: [Keys.flow: flow.rawValue, Keys.isGiftCardRemoved: isRemoved])
}
/// Tracked when the user taps to collect a payment
///
static func collectPaymentTapped(flow: Flow) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .collectPaymentTapped,
properties: [Keys.flow: flow.rawValue])
}
/// Tracked when accessing the system plugin list without it being in sync.
///
static func pluginsNotSyncedYet() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .pluginsNotSyncedYet, properties: [:])
}
/// Tracked when subscriptions are displayed in order details.
///
static func subscriptionsShown() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderDetailsSubscriptionsShown, properties: [:])
}
/// Tracked when gift cards are displayed in order details.
///
static func giftCardsShown() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderDetailsGiftCardShown, properties: [:])
}
/// Tracks when shipping is displayed in order details.
///
static func shippingShown(shippingLinesCount: Int64) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderDetailsShippingMethodsShown, properties: [Keys.shippingLinesCount: shippingLinesCount])
}
/// Tracked when the Configure button is shown in the order form.
///
static func orderFormBundleProductConfigureCTAShown(flow: Flow, source: BundleProductConfigurationSource) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderFormBundleProductConfigureCTAShown, properties: [
Keys.flow: flow.rawValue,
Keys.source: source.rawValue
])
}
/// Tracked when the Configure button is tapped in the order form.
///
static func orderFormBundleProductConfigureCTATapped(flow: Flow, source: BundleProductConfigurationSource) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderFormBundleProductConfigureCTATapped, properties: [
Keys.flow: flow.rawValue,
Keys.source: source.rawValue
])
}
/// Tracked the user changes any field for any bundle item in the configuration form from the order form.
///
static func orderFormBundleProductConfigurationChanged(changedField: BundleProductConfigurationChangedField) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderFormBundleProductConfigurationChanged, properties: [
Keys.changedField: changedField.rawValue
])
}
/// Tracked when the user taps to save a valid bundle product configuration from the order form.
///
static func orderFormBundleProductConfigurationSaveTapped() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderFormBundleProductConfigurationSaveTapped, properties: [:])
}
}
}
// MARK: - Order Details Edit
//
extension WooAnalyticsEvent {
// Namespace
enum OrderDetailsEdit {
/// Possible types of edit
///
enum Subject: String {
fileprivate static let key = "subject"
case customerNote = "customer_note"
case shippingAddress = "shipping_address"
case billingAddress = "billing_address"
}
static func orderDetailEditFlowStarted(subject: Subject) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderDetailEditFlowStarted, properties: [Subject.key: subject.rawValue])
}
static func orderDetailEditFlowCompleted(subject: Subject) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderDetailEditFlowCompleted, properties: [Subject.key: subject.rawValue])
}
static func orderDetailEditFlowFailed(subject: Subject) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderDetailEditFlowFailed, properties: [Subject.key: subject.rawValue])
}
static func orderDetailEditFlowCanceled(subject: Subject) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderDetailEditFlowCanceled, properties: [Subject.key: subject.rawValue])
}
static func orderDetailPaymentLinkShared() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .orderDetailPaymentLinkShared, properties: [:])
}
}
}
// MARK: - What's New Component