Skip to content

Commit

Permalink
Update user agents
Browse files Browse the repository at this point in the history
  • Loading branch information
S2Ler committed Jul 6, 2023
1 parent 0fbfc92 commit c4920c1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct ActiveNavigationEventDetails: NavigationEventDetails {
init(dataSource: ActiveNavigationEventsManagerDataSource, session: SessionState, defaultInterface: Bool, appMetadata: [String: String?]? = nil) {
coordinate = dataSource.router.rawLocation?.coordinate
startTimestamp = session.departureTimestamp
sdkIdentifier = defaultInterface ? "mapbox-navigation-ui-ios" : "mapbox-navigation-ios"
sdkIdentifier = URLSession.navigationSdkIdentifierForTelemetry
profile = dataSource.routeProgress.routeOptions.profileIdentifier.rawValue
simulation = dataSource.locationManagerType is SimulatedLocationManager.Type

Expand Down
6 changes: 4 additions & 2 deletions Sources/MapboxCoreNavigation/BillingHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ final class BillingHandler {
private(set) static var shared: BillingHandler = {
let accessToken = NavigationSettings.shared.directions.credentials.accessToken
precondition(accessToken != nil, "A Mapbox access token is required. Go to <https://account.mapbox.com/access-tokens/>. In Info.plist, set the MBXAccessToken key to your access token.")
let service = ProductionBillingService(accessToken: accessToken ?? "",
userAgent: URLSession.userAgent)
let service = ProductionBillingService(
accessToken: accessToken ?? "",
userAgent: URLSession.navigationSdkCoreUserAgentFragment
)
return .init(service: service)
}()

Expand Down
23 changes: 19 additions & 4 deletions Sources/MapboxCoreNavigation/BundleAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ extension Bundle {
/**
Indicates whether the application depends on MapboxNavigation in addition to MapboxCoreNavigation.
*/
class var usesDefaultUserInterface: Bool {
return mapboxNavigationIfInstalled != nil
}

static let usesDefaultUserInterface: Bool = mapboxNavigationIfInstalled != nil

/**
Returns a dictionary of `MBXInfo.plist` in Mapbox Core Navigation.
*/
Expand Down Expand Up @@ -130,4 +128,21 @@ extension Bundle {
return Bundle.mapboxCoreNavigation.object(forInfoDictionaryKey: key) as? String
}
}

internal var bundleName: String {
if let cfBundleName = object(forInfoDictionaryKey: "CFBundleName") as? String {
return cfBundleName
} else if let bundleIdentifier {
return bundleIdentifier
} else {
return URL(fileURLWithPath: bundlePath).lastPathComponent
}
}

internal var bundleShortVersion: String? {
object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}

internal static let navigationSdkCoreIdentifier: String = "mapbox-navigation-ios"
internal static let navigationSdkIdentifier: String = "mapbox-navigation-ui-ios"
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ class NavigationCommonEventsManager: NavigationTelemetryManager {
}

private static func createEventsServerOptions(accessToken: String) -> EventsServerOptions {
EventsServerOptions(token: accessToken, userAgentFragment: NavigationCommonEventsManager.userAgent, deferredDeliveryServiceOptions: nil)
.init(
token: accessToken,
userAgentFragment: URLSession.navigationSdkUserAgentFragmentForTelemetry,
deferredDeliveryServiceOptions: nil
)
}

private static var userAgent: String = {
Bundle.usesDefaultUserInterface ? "mapbox-navigation-ui-ios" : "mapbox-navigation-ios"
}()

deinit {
suspendNotifications()
}
Expand All @@ -177,7 +177,10 @@ class NavigationCommonEventsManager: NavigationTelemetryManager {

func start() {
let shortVersion = Bundle.navigationSDKVersion
eventsAPI.sendTurnstileEvent(sdkIdentifier: NavigationCommonEventsManager.userAgent, sdkVersion: shortVersion)
eventsAPI.sendTurnstileEvent(
sdkIdentifier: URLSession.navigationSdkIdentifierForTelemetry,
sdkVersion: shortVersion
)
}

// MARK: Sending Feedback Events
Expand Down
90 changes: 36 additions & 54 deletions Sources/MapboxCoreNavigation/URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,12 @@ extension URLSession {
The user agent string for any HTTP requests performed directly within MapboxCoreNavigation or MapboxNavigation.
*/
public static let userAgent: String = {
// Bundles in order from the application level on down
#if SWIFT_PACKAGE
let bundles: [Bundle?] = [
.main,
.mapboxNavigationIfInstalled,
.mapboxCoreNavigation
var bundleComponents: [String] = [
mainBundleUserAgentFragment,
navigationSdkCoreUserAgentFragment,
]
#else
let bundles: [Bundle?] = [
.main,
.mapboxNavigationIfInstalled,
.mapboxCoreNavigation,
.init(for: Directions.self),
]
#endif

let bundleComponents = bundles.compactMap { (bundle) -> String? in
guard let name = bundle?.object(forInfoDictionaryKey: "CFBundleName") as? String ?? bundle?.bundleIdentifier else { return nil }

let defaultMapboxNavigationBundleName = "MapboxNavigation"
let defaultMapboxCoreNavigationBundleName = "MapboxCoreNavigation"

#if SWIFT_PACKAGE
let mapboxNavigationName = "MapboxNavigation_MapboxNavigation"
#else
let mapboxNavigationName = defaultMapboxNavigationBundleName
#endif

#if SWIFT_PACKAGE
let mapboxCoreNavigationName = "MapboxNavigation_MapboxCoreNavigation"
#else
let mapboxCoreNavigationName = defaultMapboxCoreNavigationBundleName
#endif

var bundleName: String {
switch name {
case mapboxNavigationName:
return defaultMapboxNavigationBundleName
case mapboxCoreNavigationName:
return defaultMapboxCoreNavigationBundleName
default:
return name
}
}

var stringForShortVersion: String? {
switch name {
case mapboxNavigationName, mapboxCoreNavigationName:
return Bundle.navigationSDKVersion
default:
return bundle?.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}
}
guard let version = stringForShortVersion else { return nil }
return "\(bundleName)/\(version)"
if Bundle.usesDefaultUserInterface {
bundleComponents.append(navigationSdkUserAgentFragment)
}

let system: String
Expand Down Expand Up @@ -109,4 +60,35 @@ extension URLSession {

return components.joined(separator: " ")
}()

internal static let navigationSdkCoreUserAgentFragment: String = {
"\(Bundle.navigationSdkCoreIdentifier)/\(Bundle.navigationSDKVersion)"
}()

internal static let navigationSdkUserAgentFragment: String = {
"\(Bundle.navigationSdkIdentifier)/\(Bundle.navigationSDKVersion)"
}()

internal static let navigationSdkUserAgentFragmentForTelemetry: String = {
if Bundle.usesDefaultUserInterface {
navigationSdkUserAgentFragment
} else {
navigationSdkCoreUserAgentFragment
}
}()

internal static let navigationSdkIdentifierForTelemetry: String = {
if Bundle.usesDefaultUserInterface {
return Bundle.navigationSdkIdentifier
} else {
return Bundle.navigationSdkCoreIdentifier
}
}()

internal static let mainBundleUserAgentFragment: String = {
let bundle = Bundle.main
let name = bundle.bundleName
let version = bundle.bundleShortVersion
return "\(bundle.bundleName)/\(bundle.bundleShortVersion ?? "n/a")"
}()
}

0 comments on commit c4920c1

Please sign in to comment.