Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOB-10781] updates cooldown period constant #892

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion swift-sdk/Core/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum Const {
static let deepLinkRegex = "/a/[a-zA-Z0-9]+"
static let href = "href"
static let exponentialFactor = 2.0
static let criteriaFetchingCooldown = 120.0 // 120 seconds = 120,000 milliseconds
static let criteriaFetchingCooldown = 120000.0 // 120 seconds = 120,000 milliseconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very nice catch @evantk91 👏


enum Http {
static let GET = "GET"
Expand Down
33 changes: 17 additions & 16 deletions swift-sdk/Internal/AnonymousUserManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class AnonymousUserManager: AnonymousUserManagerProtocol {
private var config: IterableConfig
private(set) var lastCriteriaFetch: Double = 0

// Tracks an anonymous event and store it locally
/// Tracks an anonymous event and store it locally
public func trackAnonEvent(name: String, dataFields: [AnyHashable: Any]?) {
var body = [AnyHashable: Any]()
body.setValue(for: JsonKey.eventName, value: name)
Expand All @@ -41,11 +41,12 @@ public class AnonymousUserManager: AnonymousUserManagerProtocol {
storeEventData(type: EventType.customEvent, data: body)
}

/// Tracks an anonymous user update event and store it locally
public func trackAnonUpdateUser(_ dataFields: [AnyHashable: Any]) {
storeEventData(type: EventType.updateUser, data: dataFields, shouldOverWrite: true)
}

// Tracks an anonymous purchase event and store it locally
/// Tracks an anonymous purchase event and store it locally
public func trackAnonPurchaseEvent(total: NSNumber, items: [CommerceItem], dataFields: [AnyHashable: Any]?) {
var body = [AnyHashable: Any]()
body.setValue(for: JsonKey.Body.createdAt, value:IterableUtil.secondsFromEpoch(for: dateProvider.currentDate))
Expand All @@ -57,22 +58,22 @@ public class AnonymousUserManager: AnonymousUserManagerProtocol {
storeEventData(type: EventType.purchase, data: body)
}

// Tracks an anonymous cart event and store it locally
/// Tracks an anonymous cart event and store it locally
public func trackAnonUpdateCart(items: [CommerceItem]) {
var body = [AnyHashable: Any]()
body.setValue(for: JsonKey.Body.createdAt, value: IterableUtil.secondsFromEpoch(for: dateProvider.currentDate))
body.setValue(for: JsonKey.Commerce.items, value: convertCommerceItemsToDictionary(items))
storeEventData(type: EventType.updateCart, data: body)
}

// Tracks an anonymous token registration event and store it locally
/// Tracks an anonymous token registration event and store it locally
public func trackAnonTokenRegistration(token: String) {
var body = [AnyHashable: Any]()
body.setValue(for: JsonKey.token, value: token)
storeEventData(type: EventType.tokenRegistration, data: body)
}

// Stores an anonymous sessions locally. Updates the last session time each time when new session is created
/// Stores an anonymous sessions locally. Updates the last session time each time when new session is created
public func updateAnonSession() {
if var sessions = localStorage.anonymousSessions {
sessions.itbl_anon_sessions.totalAnonSessionCount += 1
Expand All @@ -86,14 +87,14 @@ public class AnonymousUserManager: AnonymousUserManagerProtocol {
}
}

// Syncs unsynced data which might have failed to sync when calling syncEvents for the first time after criterias met
/// Syncs unsynced data which might have failed to sync when calling syncEvents for the first time after criterias met
public func syncNonSyncedEvents() {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { // little delay necessary in case it takes time to store userIdAnon in localstorage
self.syncEvents()
}
}

// Syncs locally saved data through track APIs
/// Syncs locally saved data through track APIs
public func syncEvents() {
if let events = localStorage.anonymousUserEvents {
for var eventData in events {
Expand Down Expand Up @@ -144,26 +145,26 @@ public class AnonymousUserManager: AnonymousUserManagerProtocol {
localStorage.anonymousUserUpdate = nil
}

// Gets the anonymous criteria
/// Gets the anonymous criteria and updates the last criteria fetch time in milliseconds
public func getAnonCriteria() {
lastCriteriaFetch = Date().timeIntervalSince1970 * 1000
updateLastCriteriaFetch(currentTime: Date().timeIntervalSince1970 * 1000)

IterableAPI.implementation?.getCriteriaData { returnedData in
self.localStorage.criteriaData = returnedData
};
}

// Gets the last criteria fetch time in milliseconds
/// Gets the last criteria fetch time in milliseconds
public func getLastCriteriaFetch() -> Double {
return lastCriteriaFetch
}

// Sets the last criteria fetch time in milliseconds
/// Sets the last criteria fetch time in milliseconds
public func updateLastCriteriaFetch(currentTime: Double) {
lastCriteriaFetch = currentTime
}

// Creates a user after criterias met and login the user and then sync the data through track APIs
/// Creates a user after criterias met and login the user and then sync the data through track APIs
private func createAnonymousUser(_ criteriaId: String) {
var anonSessions = convertToDictionary(data: localStorage.anonymousSessions?.itbl_anon_sessions)
let userId = IterableUtil.generateUUID()
Expand Down Expand Up @@ -195,7 +196,7 @@ public class AnonymousUserManager: AnonymousUserManagerProtocol {
}
}

// Checks if criterias are being met and returns criteriaId if it matches the criteria.
/// Checks if criterias are being met and returns criteriaId if it matches the criteria.
private func evaluateCriteriaAndReturnID() -> String? {
guard let criteriaData = localStorage.criteriaData else { return nil }

Expand All @@ -214,7 +215,7 @@ public class AnonymousUserManager: AnonymousUserManagerProtocol {
return CriteriaCompletionChecker(anonymousCriteria: criteriaData, anonymousEvents: events).getMatchedCriteria()
}

// Stores event data locally
/// Stores event data locally
private func storeEventData(type: String, data: [AnyHashable: Any], shouldOverWrite: Bool = false) {
// Early return if no AUT consent was given
if !self.localStorage.anonymousUsageTrack {
Expand All @@ -233,7 +234,7 @@ public class AnonymousUserManager: AnonymousUserManagerProtocol {
}
}

// Stores User Update data
/// Stores User Update data
private func processAndStoreUserUpdate(data: [AnyHashable: Any]) {
var userUpdate = localStorage.anonymousUserUpdate ?? [:]

Expand All @@ -246,7 +247,7 @@ public class AnonymousUserManager: AnonymousUserManagerProtocol {
localStorage.anonymousUserUpdate = userUpdate
}

// Stores all other event data
/// Stores all other event data
private func processAndStoreEvent(type: String, data: [AnyHashable: Any]) {
var eventsDataObjects: [[AnyHashable: Any]] = localStorage.anonymousUserEvents ?? []

Expand Down
Loading