Skip to content

Commit 37a913b

Browse files
Merge pull request #8753 from woocommerce/feat/8679-rest-api-track-ab-variant
[REST API] Track assigned A/B test variant
2 parents b3f30fb + bc70ac9 commit 37a913b

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

WooCommerce/Classes/Analytics/WooAnalyticsEvent+ApplicationPassword.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extension WooAnalyticsEvent {
55
private enum Key {
66
static let scenario = "scenario"
77
static let cause = "cause"
8+
static let experimentVariant = "experiment_variant"
89
}
910

1011
enum Scenario: String {
@@ -19,6 +20,13 @@ extension WooAnalyticsEvent {
1920
case other = "other"
2021
}
2122

23+
/// Tracks the REST API A/B test variation
24+
///
25+
static func restAPILoginExperiment(variation: String) -> WooAnalyticsEvent {
26+
WooAnalyticsEvent(statName: .trackRestAPILoginExperimentVariation,
27+
properties: [Key.experimentVariant: variation])
28+
}
29+
2230
/// Tracks when generating application password succeeds
2331
///
2432
static func applicationPasswordGeneratedSuccessfully(scenario: Scenario) -> WooAnalyticsEvent {

WooCommerce/Classes/Analytics/WooAnalyticsStat.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ public enum WooAnalyticsStat: String {
821821
// MARK: Application password Events
822822
case applicationPasswordsNewPasswordCreated = "application_passwords_new_password_created"
823823
case applicationPasswordsGenerationFailed = "application_passwords_generation_failed"
824+
case trackRestAPILoginExperimentVariation = "rest_api_login_experiment"
824825
}
825826

826827
public extension WooAnalyticsStat {

WooCommerce/Classes/ViewRelated/AppCoordinator.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ private extension AppCoordinator {
142142
} else {
143143
configureAndDisplayAuthenticator()
144144
}
145+
146+
analytics.track(event: .ApplicationPassword.restAPILoginExperiment(variation: ABTest.applicationPasswordAuthentication.variation.analyticsValue))
145147
}
146148

147149
/// Configures the WPAuthenticator and sets the authenticator UI as the window's root view.

WooCommerce/WooCommerceTests/AppCoordinatorTests.swift

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,25 @@ final class AppCoordinatorTests: XCTestCase {
318318
appCoordinator.start()
319319

320320
// Then
321-
XCTAssertEqual(analytics.receivedEvents, [WooAnalyticsStat.loginOnboardingShown.rawValue])
321+
_ = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == WooAnalyticsStat.loginOnboardingShown.rawValue}))
322+
}
323+
324+
func test_trackRestAPILoginExperimentVariation_is_tracked_after_presenting_onboarding() throws {
325+
// Given
326+
stores.deauthenticate()
327+
let analytics = MockAnalyticsProvider()
328+
let appCoordinator = makeCoordinator(window: window,
329+
stores: stores,
330+
authenticationManager: authenticationManager,
331+
analytics: WooAnalytics(analyticsProvider: analytics))
332+
333+
// When
334+
appCoordinator.start()
335+
336+
// Then
337+
let indexOfEvent = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == "rest_api_login_experiment" }))
338+
let eventProperties = try XCTUnwrap(analytics.receivedProperties[indexOfEvent])
339+
XCTAssertNotNil(eventProperties["experiment_variant"] as? String)
322340
}
323341

324342
// MARK: - Login reminder analytics
@@ -340,10 +358,12 @@ final class AppCoordinatorTests: XCTestCase {
340358
pushNotesManager.sendLocalNotificationResponse(response)
341359

342360
// Then
343-
XCTAssertEqual(analytics.receivedEvents, [WooAnalyticsStat.loginLocalNotificationTapped.rawValue])
344-
let actionPropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["action"] as? String)
361+
let indexOfEvent = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == WooAnalyticsStat.loginLocalNotificationTapped.rawValue}))
362+
let eventProperties = try XCTUnwrap(analytics.receivedProperties[indexOfEvent])
363+
364+
let actionPropertyValue = try XCTUnwrap(eventProperties["action"] as? String)
345365
XCTAssertEqual(actionPropertyValue, "contact_support")
346-
let typePropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["type"] as? String)
366+
let typePropertyValue = try XCTUnwrap(eventProperties["type"] as? String)
347367
XCTAssertEqual(typePropertyValue, "site_address_error")
348368
}
349369

@@ -364,10 +384,12 @@ final class AppCoordinatorTests: XCTestCase {
364384
pushNotesManager.sendLocalNotificationResponse(response)
365385

366386
// Then
367-
XCTAssertEqual(analytics.receivedEvents, [WooAnalyticsStat.loginLocalNotificationTapped.rawValue])
368-
let actionPropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["action"] as? String)
387+
let indexOfEvent = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == WooAnalyticsStat.loginLocalNotificationTapped.rawValue}))
388+
let eventProperties = try XCTUnwrap(analytics.receivedProperties[indexOfEvent])
389+
390+
let actionPropertyValue = try XCTUnwrap(eventProperties["action"] as? String)
369391
XCTAssertEqual(actionPropertyValue, "login_with_wpcom")
370-
let typePropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["type"] as? String)
392+
let typePropertyValue = try XCTUnwrap(eventProperties["type"] as? String)
371393
XCTAssertEqual(typePropertyValue, "site_address_error")
372394
}
373395

@@ -388,10 +410,12 @@ final class AppCoordinatorTests: XCTestCase {
388410
pushNotesManager.sendLocalNotificationResponse(response)
389411

390412
// Then
391-
XCTAssertEqual(analytics.receivedEvents, [WooAnalyticsStat.loginLocalNotificationTapped.rawValue])
392-
let actionPropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["action"] as? String)
413+
let indexOfEvent = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == WooAnalyticsStat.loginLocalNotificationTapped.rawValue}))
414+
let eventProperties = try XCTUnwrap(analytics.receivedProperties[indexOfEvent])
415+
416+
let actionPropertyValue = try XCTUnwrap(eventProperties["action"] as? String)
393417
XCTAssertEqual(actionPropertyValue, "default")
394-
let typePropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["type"] as? String)
418+
let typePropertyValue = try XCTUnwrap(eventProperties["type"] as? String)
395419
XCTAssertEqual(typePropertyValue, "site_address_error")
396420
}
397421

@@ -412,8 +436,10 @@ final class AppCoordinatorTests: XCTestCase {
412436
pushNotesManager.sendLocalNotificationResponse(response)
413437

414438
// Then
415-
XCTAssertEqual(analytics.receivedEvents, [WooAnalyticsStat.loginLocalNotificationDismissed.rawValue])
416-
let typePropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["type"] as? String)
439+
let indexOfEvent = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == WooAnalyticsStat.loginLocalNotificationDismissed.rawValue}))
440+
let eventProperties = try XCTUnwrap(analytics.receivedProperties[indexOfEvent])
441+
442+
let typePropertyValue = try XCTUnwrap(eventProperties["type"] as? String)
417443
XCTAssertEqual(typePropertyValue, "site_address_error")
418444
}
419445
}

0 commit comments

Comments
 (0)