Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent duplicate identity calls and only send deltas (#1452)
- Loading branch information
1 parent
28f9d1b
commit 55a3c1d
Showing
6 changed files
with
231 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import Foundation | ||
import KsApi | ||
|
||
private enum KSRAnalyticsIdentityTraitValue: Equatable { | ||
case string(String) | ||
case bool(Bool) | ||
|
||
var value: Any { | ||
switch self { | ||
case let .string(value): return value | ||
case let .bool(value): return value | ||
} | ||
} | ||
} | ||
|
||
public struct KSRAnalyticsIdentityData: Equatable { | ||
public let userId: Int | ||
private let name: String | ||
private let notifications: User.Notifications | ||
|
||
init(_ user: User) { | ||
self.userId = user.id | ||
self.name = user.name | ||
self.notifications = user.notifications | ||
} | ||
|
||
public static func == (lhs: KSRAnalyticsIdentityData, rhs: KSRAnalyticsIdentityData) -> Bool { | ||
let uniqueTraits = lhs.uniqueTraits(comparedTo: rhs) | ||
|
||
return uniqueTraits.isEmpty | ||
} | ||
|
||
func uniqueTraits(comparedTo otherData: KSRAnalyticsIdentityData?) -> [String: Any] { | ||
var newTraits: [String: Any] = [:] | ||
let otherTraits = otherData?.traits ?? [:] | ||
|
||
for key in self.traits.keys where self.traits[key] != otherTraits[key] { | ||
newTraits[key] = self.traits[key]?.value | ||
} | ||
|
||
return newTraits | ||
} | ||
|
||
fileprivate var traits: [String: KSRAnalyticsIdentityTraitValue] { | ||
let notifications = self.notifications.encode() | ||
.mapValues { ($0 as? Bool).flatMap(KSRAnalyticsIdentityTraitValue.bool) } | ||
.compactMapValues { $0 } | ||
|
||
return [ | ||
"name": .string(self.name) | ||
] | ||
.withAllValuesFrom(notifications) | ||
} | ||
} | ||
|
||
extension KSRAnalyticsIdentityData: Codable { | ||
private enum CodingKeys: String, CodingKey { | ||
case userId | ||
case name | ||
case notifications | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.userId, forKey: .userId) | ||
try container.encode(self.name, forKey: .name) | ||
|
||
let data = try JSONSerialization.data(withJSONObject: self.notifications.encode(), options: []) | ||
try container.encode(data, forKey: .notifications) | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let values = try decoder.container(keyedBy: CodingKeys.self) | ||
|
||
self.userId = try values.decode(Int.self, forKey: .userId) | ||
self.name = try values.decode(String.self, forKey: .name) | ||
|
||
let data = try values.decode(Data.self, forKey: .notifications) | ||
self.notifications = try JSONDecoder().decode(User.Notifications.self, from: data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
@testable import KsApi | ||
@testable import Library | ||
import Prelude | ||
import XCTest | ||
|
||
final class KSRAnalyticsIdentityDataTests: XCTestCase { | ||
func testInitialization() { | ||
let user = User.template | ||
|> User.lens.name .~ "Test User" | ||
|> User.lens.notifications.mobileBackings .~ false | ||
|> User.lens.notifications.messages .~ true | ||
|
||
let data = KSRAnalyticsIdentityData(user) | ||
|
||
XCTAssertEqual(data.userId, 1) | ||
XCTAssertEqual(data.uniqueTraits(comparedTo: nil)["name"] as? String, "Test User") | ||
XCTAssertEqual(data.uniqueTraits(comparedTo: nil)["notify_mobile_of_backings"] as? Bool, false) | ||
XCTAssertEqual(data.uniqueTraits(comparedTo: nil)["notify_of_messages"] as? Bool, true) | ||
} | ||
|
||
func testUniqueTraits() { | ||
let user1 = User.template | ||
|> User.lens.name .~ "Test User 1" | ||
|> User.lens.notifications.mobileBackings .~ false | ||
|> User.lens.notifications.messages .~ true | ||
|
||
let user2 = User.template | ||
|> User.lens.name .~ "Test User 2" | ||
|> User.lens.notifications.mobileBackings .~ false | ||
|> User.lens.notifications.messages .~ true | ||
|> User.lens.notifications.friendActivity .~ true | ||
|
||
let data1 = KSRAnalyticsIdentityData(user1) | ||
let data2 = KSRAnalyticsIdentityData(user2) | ||
|
||
let uniqueTraits = data2.uniqueTraits(comparedTo: data1) | ||
|
||
XCTAssertEqual(uniqueTraits.keys.count, 2) | ||
|
||
XCTAssertEqual(uniqueTraits["name"] as? String, "Test User 2") | ||
XCTAssertEqual(uniqueTraits["notify_of_friend_activity"] as? Bool, true) | ||
} | ||
|
||
func testEquality() { | ||
let user1 = User.template | ||
|> User.lens.name .~ "Test User 1" | ||
|> User.lens.notifications.mobileBackings .~ false | ||
|> User.lens.notifications.messages .~ true | ||
|
||
let user2 = User.template | ||
|> User.lens.name .~ "Test User 1" | ||
|> User.lens.notifications.mobileBackings .~ false | ||
|> User.lens.notifications.messages .~ true | ||
|
||
XCTAssertEqual(KSRAnalyticsIdentityData(user1), KSRAnalyticsIdentityData(user2)) | ||
|
||
let user3 = User.template | ||
|> User.lens.name .~ "Test User 2" | ||
|> User.lens.notifications.mobileBackings .~ false | ||
|> User.lens.notifications.messages .~ true | ||
|
||
let user4 = User.template | ||
|> User.lens.name .~ "Test User 3" | ||
|> User.lens.notifications.mobileBackings .~ true | ||
|> User.lens.notifications.messages .~ true | ||
|
||
XCTAssertNotEqual(KSRAnalyticsIdentityData(user3), KSRAnalyticsIdentityData(user4)) | ||
} | ||
|
||
func testEncodingDecoding() { | ||
let data1 = KSRAnalyticsIdentityData(.template) | ||
|
||
guard let encoded = try? JSONEncoder().encode(data1) else { | ||
XCTFail("Failed to encode") | ||
return | ||
} | ||
|
||
let decoded = try? JSONDecoder().decode(KSRAnalyticsIdentityData.self, from: encoded) | ||
|
||
XCTAssertEqual(decoded, data1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters