Skip to content

Commit 75754e2

Browse files
authored
Merge pull request #7717 from woocommerce/issue/7714-widget-analytics
Widget: Analytics
2 parents eee1d55 + e9f4e0a commit 75754e2

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

WooCommerce/Classes/Analytics/WooAnalytics.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import UIKit
33
import WordPressShared
4+
import WidgetKit
45

56
public class WooAnalytics: Analytics {
67

@@ -180,7 +181,10 @@ private extension WooAnalytics {
180181
}
181182

182183
@objc func trackApplicationOpened() {
183-
track(.applicationOpened)
184+
WidgetCenter.shared.getCurrentConfigurations { [weak self] configurationResult in
185+
guard let self = self else { return }
186+
self.track(.applicationOpened, withProperties: self.applicationOpenedProperties(configurationResult))
187+
}
184188
applicationOpenedTime = Date()
185189
}
186190

@@ -211,6 +215,27 @@ private extension WooAnalytics {
211215
updatedProperties[PropertyKeys.wpcomStoreKey] = site?.isWordPressStore
212216
return updatedProperties
213217
}
218+
219+
/// Builds the necesary properties for the `application_opened` event.
220+
///
221+
func applicationOpenedProperties(_ configurationResult: Result<[WidgetInfo], Error>) -> [String: [String]] {
222+
guard let installedWidgets = try? configurationResult.get() else {
223+
return ["widgets": []]
224+
}
225+
226+
// Translate the widget kind into a name recognized by tracks.
227+
let widgetAnalyticNames: [String] = installedWidgets.map { widgetInfo in
228+
switch widgetInfo.kind {
229+
case WooConstants.storeInfoWidgetKind:
230+
return WooAnalyticsEvent.Widgets.Name.todayStats.rawValue
231+
default:
232+
DDLogWarn("⚠️ Make sure the widget: \(widgetInfo.kind), has the correct tracks name.")
233+
return widgetInfo.kind
234+
}
235+
}
236+
237+
return ["widgets": widgetAnalyticNames]
238+
}
214239
}
215240

216241

WooCommerce/Classes/Analytics/WooAnalyticsEvent.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,3 +1605,22 @@ extension WooAnalyticsEvent {
16051605
}
16061606
}
16071607
}
1608+
1609+
// MARK: - Widgets {
1610+
extension WooAnalyticsEvent {
1611+
enum Widgets {
1612+
enum Key: String {
1613+
case name = "name"
1614+
}
1615+
1616+
enum Name: String {
1617+
case todayStats = "today_stats"
1618+
}
1619+
1620+
/// Event when a widget is tapped and opens the app.
1621+
///
1622+
static func widgetTapped(name: Name) -> WooAnalyticsEvent {
1623+
WooAnalyticsEvent(statName: .widgetTapped, properties: [Key.name.rawValue: name.rawValue])
1624+
}
1625+
}
1626+
}

WooCommerce/Classes/Analytics/WooAnalyticsStat.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ public enum WooAnalyticsStat: String {
688688
case loginJetpackConnectDismissed = "login_jetpack_connect_dismissed"
689689
case loginJetpackConnectionURLFetchFailed = "login_jetpack_connection_url_fetch_failed"
690690
case loginJetpackConnectionVerificationFailed = "login_jetpack_connection_verification_failed"
691+
692+
// MARK: Widgets
693+
case widgetTapped = "widget_tapped"
691694
}
692695

693696
public extension WooAnalyticsStat {

WooCommerce/Classes/AppDelegate.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
180180
handleWebActivity(userActivity)
181181
}
182182

183+
trackWidgetTappedIfNeeded(userActivity: userActivity)
184+
183185
return true
184186
}
185187
}
@@ -374,6 +376,17 @@ private extension AppDelegate {
374376
func startABTesting() async {
375377
await ABTest.start()
376378
}
379+
380+
/// Tracks if the application was opened via a widget tap.
381+
///
382+
func trackWidgetTappedIfNeeded(userActivity: NSUserActivity) {
383+
switch userActivity.activityType {
384+
case WooConstants.storeInfoWidgetKind:
385+
ServiceLocator.analytics.track(event: .Widgets.widgetTapped(name: .todayStats))
386+
default:
387+
break
388+
}
389+
}
377390
}
378391

379392

0 commit comments

Comments
 (0)