Skip to content

Commit

Permalink
Analytics - only fetch config before flushing events cache (#1337)
Browse files Browse the repository at this point in the history
* Only retrieve config when flushing cache, not per sendEvent analytics call
  • Loading branch information
scannillo authored Jun 18, 2024
1 parent 5f4ffed commit 720486f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Braintree iOS SDK Release Notes

## unreleased
* BraintreeCore
* For analytics, only call `fetchOrReturnRemoteConfig()` when batch uploading, not on each analytic event enqueue

## 6.21.0 (2024-06-12)
* BraintreePayPal
* Add PayPal App Switch vault flow (BETA)
Expand Down
73 changes: 36 additions & 37 deletions Sources/BraintreeCore/Analytics/BTAnalyticsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,52 +103,51 @@ class BTAnalyticsService: Equatable {

await BTAnalyticsService.events.append(event)

do {
let configuration = try await apiClient.fetchConfiguration()

// TODO: - Refactor to make HTTP non-optional property and instantiate in init()
if self.http == nil {
self.http = BTHTTP(authorization: self.apiClient.authorization, customBaseURL: BTAnalyticsService.url)
}

// A special value passed in by unit tests to prevent BTHTTP from actually posting
if let http = self.http, http.customBaseURL?.absoluteString == "test://do-not-send.url" {
return
}
// TODO: - Refactor to make HTTP non-optional property and instantiate in init()
if self.http == nil {
self.http = BTHTTP(authorization: self.apiClient.authorization, customBaseURL: BTAnalyticsService.url)
}

// A special value passed in by unit tests to prevent BTHTTP from actually posting
if let http = self.http, http.customBaseURL?.absoluteString == "test://do-not-send.url" {
return
}

if shouldBypassTimerQueue {
await self.sendQueuedAnalyticsEvents()
return
}

if BTAnalyticsService.timer == nil {
BTAnalyticsService.timer = DispatchSource.makeTimerSource(queue: self.http?.dispatchQueue)
BTAnalyticsService.timer?.schedule(
deadline: .now() + .seconds(self.timerInterval),
repeating: .seconds(self.timerInterval),
leeway: .seconds(1)
)

if shouldBypassTimerQueue {
await self.sendQueuedAnalyticsEvents(configuration: configuration)
return
}

if BTAnalyticsService.timer == nil {
BTAnalyticsService.timer = DispatchSource.makeTimerSource(queue: self.http?.dispatchQueue)
BTAnalyticsService.timer?.schedule(
deadline: .now() + .seconds(self.timerInterval),
repeating: .seconds(self.timerInterval),
leeway: .seconds(1)
)

BTAnalyticsService.timer?.setEventHandler {
Task {
await self.sendQueuedAnalyticsEvents(configuration: configuration)
}
BTAnalyticsService.timer?.setEventHandler {
Task {
await self.sendQueuedAnalyticsEvents()
}

BTAnalyticsService.timer?.resume()
}
} catch {
return
BTAnalyticsService.timer?.resume()
}
}

// MARK: - Helpers

func sendQueuedAnalyticsEvents(configuration: BTConfiguration) async {
func sendQueuedAnalyticsEvents() async {
if await !BTAnalyticsService.events.isEmpty {
let postParameters = await createAnalyticsEvent(config: configuration, sessionID: apiClient.metadata.sessionID, events: BTAnalyticsService.events.allValues)
http?.post("v1/tracking/batch/events", parameters: postParameters) { _, _, _ in }
await BTAnalyticsService.events.removeAll()
do {
let configuration = try await apiClient.fetchConfiguration()
let postParameters = await createAnalyticsEvent(config: configuration, sessionID: apiClient.metadata.sessionID, events: BTAnalyticsService.events.allValues)
http?.post("v1/tracking/batch/events", parameters: postParameters) { _, _, _ in }
await BTAnalyticsService.events.removeAll()
} catch {
return
}
}
}

Expand Down

0 comments on commit 720486f

Please sign in to comment.