Skip to content
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,61 @@ extension WooAnalyticsEvent {
WooAnalyticsEvent(statName: .backgroundUpdatesDisabled, properties: [:])
}
}

enum POSCatalogSync {

private enum Keys {
static let duration = "duration"
static let source = "source"
static let taskType = "task_type"
}

// Scheduling events
static func fullSyncScheduled() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .posFullCatalogSyncScheduled, properties: [:])
}

static func incrementalSyncScheduled() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .posIncrementalSyncScheduled, properties: [:])
}

static func schedulingError(_ error: Error, taskType: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .posCatalogSyncSchedulingError, properties: [Keys.taskType: taskType], error: error)
}

// Sync completion events
static func fullSyncCompleted(source: String, duration: TimeInterval? = nil) -> WooAnalyticsEvent {
var properties: [String: String] = [Keys.source: source]
if let duration = duration {
properties[Keys.duration] = String(duration)
}
return WooAnalyticsEvent(statName: .posFullCatalogSyncCompleted, properties: properties)
}

static func incrementalSyncCompleted(duration: TimeInterval) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .posIncrementalSyncCompleted, properties: [Keys.duration: duration])
}

// Sync error events
static func fullSyncError(_ error: Error, source: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .posFullCatalogSyncError, properties: [Keys.source: source], error: error)
}

static func incrementalSyncError(_ error: Error) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .posIncrementalSyncError, properties: [:], error: error)
}

// Task management events
static func taskExpired(taskType: String) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .posCatalogSyncTaskExpired, properties: [Keys.taskType: taskType])
}

static func syncRecovered() -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .posCatalogSyncRecovered, properties: [:])
}

static func recoveryError(_ error: Error) -> WooAnalyticsEvent {
WooAnalyticsEvent(statName: .posCatalogSyncRecoveryError, properties: [:], error: error)
}
}
}
12 changes: 12 additions & 0 deletions WooCommerce/Classes/Analytics/WooAnalyticsStat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,18 @@ enum WooAnalyticsStat: String {
case pushNotificationOrderBackgroundSynced = "push_notification_order_background_synced"
case pushNotificationOrderBackgroundSyncError = "push_notification_order_background_sync_error"
case backgroundUpdatesDisabled = "background_updates_disabled"

// MARK: POS Catalog Sync events
case posFullCatalogSyncScheduled = "pos_full_catalog_sync_scheduled"
case posIncrementalSyncScheduled = "pos_incremental_sync_scheduled"
case posCatalogSyncSchedulingError = "pos_catalog_sync_scheduling_error"
case posFullCatalogSyncCompleted = "pos_full_catalog_sync_completed"
case posIncrementalSyncCompleted = "pos_incremental_sync_completed"
case posFullCatalogSyncError = "pos_full_catalog_sync_error"
case posIncrementalSyncError = "pos_incremental_sync_error"
case posCatalogSyncTaskExpired = "pos_catalog_sync_task_expired"
case posCatalogSyncRecovered = "pos_catalog_sync_recovered"
case posCatalogSyncRecoveryError = "pos_catalog_sync_recovery_error"

// MARK: Point of Sale events
case pointOfSaleTabSelected = "main_tab_pos_selected"
Expand Down
16 changes: 15 additions & 1 deletion WooCommerce/Classes/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

/// Handles events to background refresh the app.
///
private let appRefreshHandler = BackgroundTaskRefreshDispatcher()
internal let appRefreshHandler = BackgroundTaskRefreshDispatcher()

/// Manages POS catalog background synchronization.
///
internal let posCatalogSyncManager = POSCatalogSyncBackgroundTaskManager()

private var subscriptions: Set<AnyCancellable> = []

Expand Down Expand Up @@ -127,6 +131,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// Register for background app refresh events.
appRefreshHandler.registerSystemTaskIdentifier()

// Register POS catalog sync background tasks.
posCatalogSyncManager.registerBackgroundTasks()

// Schedule initial POS catalog syncs.
posCatalogSyncManager.scheduleFullCatalogSync()
posCatalogSyncManager.scheduleIncrementalSync()

// Recover any incomplete POS catalog syncs from previous app sessions.
posCatalogSyncManager.recoverIncompletesyncs()

return true
}
Expand Down
2 changes: 2 additions & 0 deletions WooCommerce/Classes/Extensions/UserDefaults+Woo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ extension UserDefaults {

// Hide stores from store picker
case hiddenStoreIDs

case lastPOSIncrementalSyncTimestamp
}
}

Expand Down
Loading