Skip to content

Commit

Permalink
Merge pull request #291 from edx/test/segment_plus_firebase
Browse files Browse the repository at this point in the history
Firebase and Segment integration
  • Loading branch information
rnr authored Feb 26, 2024
2 parents 7db3b35 + fb22073 commit cf65ce9
Show file tree
Hide file tree
Showing 22 changed files with 312 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public enum SocialAuthMethod: String {

//sourcery: AutoMockable
public protocol AuthorizationAnalytics {
func setUserID(_ id: String)
func identify(id: String, username: String, email: String)
func userLogin(method: AuthMethod)
func signUpClicked()
func createAccountClicked()
Expand All @@ -41,7 +41,7 @@ public protocol AuthorizationAnalytics {

#if DEBUG
class AuthorizationAnalyticsMock: AuthorizationAnalytics {
public func setUserID(_ id: String) {}
func identify(id: String, username: String, email: String) {}
public func userLogin(method: AuthMethod) {}
public func signUpClicked() {}
public func createAccountClicked() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class SignInViewModel: ObservableObject {
isShowProgress = true
do {
let user = try await interactor.login(username: username, password: password)
analytics.setUserID("\(user.id)")
analytics.identify(id: "\(user.id)", username: user.username, email: user.email)
analytics.userLogin(method: .password)
router.showMainOrWhatsNewScreen(sourceScreen: sourceScreen)
} catch let error {
Expand Down Expand Up @@ -110,7 +110,7 @@ public class SignInViewModel: ObservableObject {
isShowProgress = true
do {
let user = try await interactor.login(externalToken: externalToken, backend: backend)
analytics.setUserID("\(user.id)")
analytics.identify(id: "\(user.id)", username: user.username, email: user.email)
analytics.userLogin(method: authMethod)
router.showMainOrWhatsNewScreen(sourceScreen: sourceScreen)
} catch let error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public class SignUpViewModel: ObservableObject {
fields: validateFields,
isSocial: externalToken != nil
)
analytics.setUserID("\(user.id)")
analytics.identify(id: "\(user.id)", username: user.username, email: user.email)
analytics.registrationSuccess()
isShowProgress = false
router.showMainOrWhatsNewScreen(sourceScreen: sourceScreen)
Expand Down Expand Up @@ -188,7 +188,7 @@ public class SignUpViewModel: ObservableObject {
do {
isShowProgress = true
let user = try await interactor.login(externalToken: response.token, backend: backend)
analytics.setUserID("\(user.id)")
analytics.identify(id: "\(user.id)", username: user.username, email: user.email)
analytics.userLogin(method: authMethod)
isShowProgress = false
router.showMainOrWhatsNewScreen(sourceScreen: sourceScreen)
Expand Down
26 changes: 14 additions & 12 deletions Authorization/AuthorizationTests/AuthorizationMock.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,10 @@ open class AuthorizationAnalyticsMock: AuthorizationAnalytics, Mock {



open func setUserID(_ id: String) {
addInvocation(.m_setUserID__id(Parameter<String>.value(`id`)))
let perform = methodPerformValue(.m_setUserID__id(Parameter<String>.value(`id`))) as? (String) -> Void
perform?(`id`)
open func identify(id: String, username: String, email: String) {
addInvocation(.m_identify__id_idusername_usernameemail_email(Parameter<String>.value(`id`), Parameter<String>.value(`username`), Parameter<String>.value(`email`)))
let perform = methodPerformValue(.m_identify__id_idusername_usernameemail_email(Parameter<String>.value(`id`), Parameter<String>.value(`username`), Parameter<String>.value(`email`))) as? (String, String, String) -> Void
perform?(`id`, `username`, `email`)
}

open func userLogin(method: AuthMethod) {
Expand Down Expand Up @@ -553,7 +553,7 @@ open class AuthorizationAnalyticsMock: AuthorizationAnalytics, Mock {


fileprivate enum MethodType {
case m_setUserID__id(Parameter<String>)
case m_identify__id_idusername_usernameemail_email(Parameter<String>, Parameter<String>, Parameter<String>)
case m_userLogin__method_method(Parameter<AuthMethod>)
case m_signUpClicked
case m_createAccountClicked
Expand All @@ -563,9 +563,11 @@ open class AuthorizationAnalyticsMock: AuthorizationAnalytics, Mock {

static func compareParameters(lhs: MethodType, rhs: MethodType, matcher: Matcher) -> Matcher.ComparisonResult {
switch (lhs, rhs) {
case (.m_setUserID__id(let lhsId), .m_setUserID__id(let rhsId)):
case (.m_identify__id_idusername_usernameemail_email(let lhsId, let lhsUsername, let lhsEmail), .m_identify__id_idusername_usernameemail_email(let rhsId, let rhsUsername, let rhsEmail)):
var results: [Matcher.ParameterComparisonResult] = []
results.append(Matcher.ParameterComparisonResult(Parameter.compare(lhs: lhsId, rhs: rhsId, with: matcher), lhsId, rhsId, "_ id"))
results.append(Matcher.ParameterComparisonResult(Parameter.compare(lhs: lhsId, rhs: rhsId, with: matcher), lhsId, rhsId, "id"))
results.append(Matcher.ParameterComparisonResult(Parameter.compare(lhs: lhsUsername, rhs: rhsUsername, with: matcher), lhsUsername, rhsUsername, "username"))
results.append(Matcher.ParameterComparisonResult(Parameter.compare(lhs: lhsEmail, rhs: rhsEmail, with: matcher), lhsEmail, rhsEmail, "email"))
return Matcher.ComparisonResult(results)

case (.m_userLogin__method_method(let lhsMethod), .m_userLogin__method_method(let rhsMethod)):
Expand All @@ -591,7 +593,7 @@ open class AuthorizationAnalyticsMock: AuthorizationAnalytics, Mock {

func intValue() -> Int {
switch self {
case let .m_setUserID__id(p0): return p0.intValue
case let .m_identify__id_idusername_usernameemail_email(p0, p1, p2): return p0.intValue + p1.intValue + p2.intValue
case let .m_userLogin__method_method(p0): return p0.intValue
case .m_signUpClicked: return 0
case .m_createAccountClicked: return 0
Expand All @@ -602,7 +604,7 @@ open class AuthorizationAnalyticsMock: AuthorizationAnalytics, Mock {
}
func assertionName() -> String {
switch self {
case .m_setUserID__id: return ".setUserID(_:)"
case .m_identify__id_idusername_usernameemail_email: return ".identify(id:username:email:)"
case .m_userLogin__method_method: return ".userLogin(method:)"
case .m_signUpClicked: return ".signUpClicked()"
case .m_createAccountClicked: return ".createAccountClicked()"
Expand All @@ -627,7 +629,7 @@ open class AuthorizationAnalyticsMock: AuthorizationAnalytics, Mock {
public struct Verify {
fileprivate var method: MethodType

public static func setUserID(_ id: Parameter<String>) -> Verify { return Verify(method: .m_setUserID__id(`id`))}
public static func identify(id: Parameter<String>, username: Parameter<String>, email: Parameter<String>) -> Verify { return Verify(method: .m_identify__id_idusername_usernameemail_email(`id`, `username`, `email`))}
public static func userLogin(method: Parameter<AuthMethod>) -> Verify { return Verify(method: .m_userLogin__method_method(`method`))}
public static func signUpClicked() -> Verify { return Verify(method: .m_signUpClicked)}
public static func createAccountClicked() -> Verify { return Verify(method: .m_createAccountClicked)}
Expand All @@ -640,8 +642,8 @@ open class AuthorizationAnalyticsMock: AuthorizationAnalytics, Mock {
fileprivate var method: MethodType
var performs: Any

public static func setUserID(_ id: Parameter<String>, perform: @escaping (String) -> Void) -> Perform {
return Perform(method: .m_setUserID__id(`id`), performs: perform)
public static func identify(id: Parameter<String>, username: Parameter<String>, email: Parameter<String>, perform: @escaping (String, String, String) -> Void) -> Perform {
return Perform(method: .m_identify__id_idusername_usernameemail_email(`id`, `username`, `email`), performs: perform)
}
public static func userLogin(method: Parameter<AuthMethod>, perform: @escaping (AuthMethod) -> Void) -> Perform {
return Perform(method: .m_userLogin__method_method(`method`), performs: perform)
Expand Down
4 changes: 4 additions & 0 deletions Core/Core.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
07DDFCBD29A780BB00572595 /* UINavigationController+Animation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07DDFCBC29A780BB00572595 /* UINavigationController+Animation.swift */; };
07E0939F2B308D2800F1E4B2 /* Data_Certificate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07E0939E2B308D2800F1E4B2 /* Data_Certificate.swift */; };
141F1D302B7328D4009E81EB /* WebviewCookiesUpdateProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 141F1D2F2B7328D4009E81EB /* WebviewCookiesUpdateProtocol.swift */; };
A51CDBE72B6D21F2009B6D4E /* SegmentConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51CDBE62B6D21F2009B6D4E /* SegmentConfig.swift */; };
142EDD6C2B831D1400F9F320 /* BranchSDK in Frameworks */ = {isa = PBXBuildFile; productRef = 142EDD6B2B831D1400F9F320 /* BranchSDK */; };
A53A32352B233DEC005FE38A /* ThemeConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = A53A32342B233DEC005FE38A /* ThemeConfig.swift */; };
A595689B2B6173DF00ED4F90 /* BranchConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = A595689A2B6173DF00ED4F90 /* BranchConfig.swift */; };
Expand Down Expand Up @@ -304,6 +305,7 @@
3B74C6685E416657F3C5F5A8 /* Pods-App-Core.releaseprod.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App-Core.releaseprod.xcconfig"; path = "Target Support Files/Pods-App-Core/Pods-App-Core.releaseprod.xcconfig"; sourceTree = "<group>"; };
60153262DBC2F9E660D7E11B /* Pods-App-Core.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App-Core.release.xcconfig"; path = "Target Support Files/Pods-App-Core/Pods-App-Core.release.xcconfig"; sourceTree = "<group>"; };
9D5B06CAA99EA5CD49CBE2BB /* Pods-App-Core.debugdev.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App-Core.debugdev.xcconfig"; path = "Target Support Files/Pods-App-Core/Pods-App-Core.debugdev.xcconfig"; sourceTree = "<group>"; };
A51CDBE62B6D21F2009B6D4E /* SegmentConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentConfig.swift; sourceTree = "<group>"; };
A53A32342B233DEC005FE38A /* ThemeConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThemeConfig.swift; sourceTree = "<group>"; };
A595689A2B6173DF00ED4F90 /* BranchConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BranchConfig.swift; sourceTree = "<group>"; };
A5F4E7B42B61544A00ACD166 /* BrazeConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BrazeConfig.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -783,6 +785,7 @@
DBF6F2402B014ADA0098414B /* FirebaseConfig.swift */,
A5F4E7B42B61544A00ACD166 /* BrazeConfig.swift */,
A595689A2B6173DF00ED4F90 /* BranchConfig.swift */,
A51CDBE62B6D21F2009B6D4E /* SegmentConfig.swift */,
DBF6F2492B0380E00098414B /* FeaturesConfig.swift */,
DBF6F2452B01DAFE0098414B /* AgreementConfig.swift */,
BAFB99812B0E2354007D09F9 /* FacebookConfig.swift */,
Expand Down Expand Up @@ -1029,6 +1032,7 @@
02B3E3B32930198600A50475 /* AVPlayerViewControllerExtension.swift in Sources */,
BAD9CA332B28A8F300DE790A /* AjaxProvider.swift in Sources */,
0295C885299B99DD00ABE571 /* RefreshableScrollView.swift in Sources */,
A51CDBE72B6D21F2009B6D4E /* SegmentConfig.swift in Sources */,
0282DA7328F98CC9003C3F07 /* WebUnitView.swift in Sources */,
E0D586362B314CD3009B4BA7 /* LogistrationBottomView.swift in Sources */,
0727878128D25EFD002E9142 /* SnackBarView.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Core/Core/Configuration/Config/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public protocol ConfigProtocol {
var discovery: DiscoveryConfig { get }
var braze: BrazeConfig { get }
var branch: BranchConfig { get }
var segment: SegmentConfig { get }
var program: DiscoveryConfig { get }
var URIScheme: String { get }
}
Expand Down
20 changes: 0 additions & 20 deletions Core/Core/Configuration/Config/FirebaseConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import FirebaseCore

private enum FirebaseKeys: String {
case enabled = "ENABLED"
Expand Down Expand Up @@ -76,25 +75,6 @@ public final class FirebaseConfig: NSObject {
public var isAnalyticsSourceFirebase: Bool {
return analyticsSource == AnalyticsSource.firebase
}

public var firebaseOptions: FirebaseOptions? {
if enabled,
requiredKeysAvailable,
let bundleID = bundleID,
let googleAppID = googleAppID,
let gcmSenderID = gcmSenderID {
let firebaseOptions = FirebaseOptions(googleAppID: googleAppID,
gcmSenderID: gcmSenderID)
firebaseOptions.apiKey = apiKey
firebaseOptions.projectID = projectID
firebaseOptions.bundleID = bundleID
firebaseOptions.clientID = clientID
firebaseOptions.storageBucket = storageBucket
firebaseOptions.databaseURL = databaseURL
}

return nil
}
}

private let firebaseKey = "FIREBASE"
Expand Down
31 changes: 31 additions & 0 deletions Core/Core/Configuration/Config/SegmentConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// SegmentConfig.swift
// Core
//
// Created by Anton Yarmolenka on 02/02/2024.
//

import Foundation

private enum SegmentKeys: String, RawStringExtractable {
case enabled = "ENABLED"
case writeKey = "SEGMENT_IO_WRITE_KEY"
}

public final class SegmentConfig: NSObject {
public var enabled: Bool = false
public var writeKey: String = ""

init(dictionary: [String: AnyObject]) {
super.init()
writeKey = dictionary[SegmentKeys.writeKey] as? String ?? ""
enabled = dictionary[SegmentKeys.enabled] as? Bool == true && !writeKey.isEmpty
}
}

private let segmentKey = "SEGMENT_IO"
extension Config {
public var segment: SegmentConfig {
SegmentConfig(dictionary: self[segmentKey] as? [String: AnyObject] ?? [:])
}
}
11 changes: 11 additions & 0 deletions Core/CoreTests/Configuration/ConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class ConfigTests: XCTestCase {
"BRANCH": [
"ENABLED": true,
"KEY": "testBranchKey"
],
"SEGMENT_IO": [
"ENABLED": true,
"SEGMENT_IO_WRITE_KEY": "testSegmentKey"
]
]

Expand Down Expand Up @@ -136,4 +140,11 @@ class ConfigTests: XCTestCase {
XCTAssertTrue(config.branch.enabled)
XCTAssertEqual(config.branch.key, "testBranchKey")
}

func testSegmentConfigInitialization() {
let config = Config(properties: properties)

XCTAssertTrue(config.segment.enabled)
XCTAssertEqual(config.segment.writeKey, "testSegmentKey")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import Foundation
import SwiftUI
import Core
import FirebaseCrashlytics

public class DiscussionTopicsViewModel: ObservableObject {

Expand Down
Loading

0 comments on commit cf65ce9

Please sign in to comment.