Skip to content

Commit

Permalink
Changed: sync with Sifo Internet App when sync with backend fails (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardian4 committed May 23, 2024
1 parent 52924e5 commit 6e25dff
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions Sources/TSMobileAnalytics/TSMobileAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,19 @@ public final class TSMobileAnalytics {
}

private var shouldSyncWithSifoInternetApp: Bool {
guard Storage.shared.sdkVersion != Self.sdkVersion,
let cookieString = Storage.shared.cookieString,
!cookieString.isEmpty
else { return true }


if(!isSifoInternetAppInstalled){
return false
}

if(Storage.shared.sdkVersion != Self.sdkVersion){
return true
}

if (Storage.shared.cookieString?.isEmpty ?? true){
return true
}

return false
}

Expand Down Expand Up @@ -267,17 +275,14 @@ private extension TSMobileAnalytics {
guard isOldPanelUserInfoCached || isSifoInternetAppInstalled
else { return }

guard !IdentificationManager.shared.isSystemIdentifierTrackingEnabled,
!shouldSyncWithBackendFirst
else {
if (IdentificationManager.shared.isSystemIdentifierTrackingEnabled && shouldSyncWithBackendFirst) {
syncCookiesWithBackend()
return
}

if hostAppIncludesExpectedUrlScheme && shouldSyncWithSifoInternetApp {
syncTokenWithSifoInternetApp(additionals: additionals)
} else {
refreshCookies() { self.sendTagWithAppStart() }
if hostAppIncludesExpectedUrlScheme && shouldSyncWithSifoInternetApp {
syncTokenWithSifoInternetApp(additionals: additionals)
} else {
refreshCookies() { self.sendTagWithAppStart() }
}
}
}

Expand All @@ -296,10 +301,7 @@ private extension TSMobileAnalytics {
"Failed to sync with backend.",
"\(error)"],
verbosity: .error)

if !self.shouldSyncWithSifoInternetApp {
self.refreshCookies() { self.sendTagWithAppStart() }
}
onBackendSyncError()
case .success(let response):
Self.logger.log(
multipleLines: [
Expand All @@ -314,18 +316,28 @@ private extension TSMobileAnalytics {
"Failed to verify sync response.",
error.localizedDescription],
verbosity: .error)
onBackendSyncError()
return
}

Self.logger.log(
message: "Successfully synced with backend.",
verbosity: .info)

self.updateCookieString(from: response)
self.refreshCookies { self.sendTagWithAppStart() }
}
}
}

private func onBackendSyncError(){
// fallback to sync with panelist app
if self.shouldSyncWithSifoInternetApp {
syncTokenWithSifoInternetApp(additionals: additionals)
} else {
self.refreshCookies() { self.sendTagWithAppStart() }
}
}

func updateCookieString(from data: Data) {
guard let cookieString = JSONManager.urlEncodedJSON(from: data)
Expand Down

0 comments on commit 6e25dff

Please sign in to comment.