Skip to main content

Amplitude’s Quickstart Guide lacks the most important part, after installing Amplitude SDK as a dependency, it gives this code to init it:

import AmplitudeSwift

let amplitude = Amplitude(
configuration: Configuration(
apiKey: "YOUR-API-KEY"
)
)

OK, … so what should I do with it (after I insert my own API Key)?

 

Should it be in the @main ‘s init? Should I put it in didFinishLaunchingWithOptions ? Include it as a Singleton? Or a static var? Or paste this let amplitude = Amplitude(configuration...) in each file?

 

P.S. I’ve looked at the SDK on GitHub and noticed that they use the static var approach. As I understand it, this way Amplitude won’t be initialized until that static var is accessed for the first time. So if my app has 5 consecutive screens and I track an Amplitude Event on Screen #4, — only then will Amplitude start, and only then will fire Session events (session start), which would be incorrect as the user started his session on Screen #1.

 

P.P.S. Crazy how such a leading company doesn’t have straightforward guide on how to include it’s SDK. Mixpanel has it in Step 2 https://docs.mixpanel.com/docs/quickstart/connect-your-data?sdk=swift Firebase has it in Step 5 https://firebase.google.com/docs/ios/setup Straight and clear.

 

If I’ve asked this Q in the wrong forum—please let me know ✌️


If I’ve asked this Q in the wrong forum—please let me know ✌️

I don't think their admins or moderators are very active on here.


If I’ve asked this Q in the wrong forum—please let me know ✌️

I don't think their admins or moderators are very active on here.

Yes, seems like it. Too bad. Don’t know how Amplitude got this far without essential documentation.


import Foundation

import AmplitudeSwift

 

 

class AnalyticsManager {

  static let shared = AnalyticsManager()

 

  let amplitude: Amplitude

  

    private init() {

      amplitude = Amplitude(configuration: Configuration(

          apiKey: ""

      ))

    }

  

  func logEvent() {

  ...

  }

}

Not sure if this helps you, but something like this where you initialize on the first view on Appear of your app.  You can then do AnalyticsManager.shared.logEvent…. anywhere to log an event. 


@kaspesi, thank you for this, I have a very similar Singleton in my app:

final class AmplitudeManager {
static let shared = Amplitude(configuration:
Configuration(apiKey: "My-Key",
trackingOptions: TrackingOptions().disableTrackCarrier().disableTrackCity().disableTrackDMA().disableTrackIpAddress(),
defaultTracking: DefaultTrackingOptions(sessions: true, appLifecycles: true)))

private init() { }
}

Not really sure where to initialize it. You suggest in .onAppear() of the first view. But it could also be initialized in the init(), or even better in didFinishLaunchingWithOptions, like Firebase Analytics and Mixpanel do their initialization.

 

I can even do something simple like:

@main
struct MyApp: App {
//init Amplitude
let amp = AmplitudeManager.shared

var body: some Scene {
WindowGroup {
ContentView()
}
}
}

And it would work.


Reply