Setting up GA4 for iOS apps.
Setting up Google Analytics 4 (GA4) in an iOS app involves the Google Analytics for Firebase SDK because GA4 is built on top of Firebase.
If you don’t have a Firebase project yet, go to the Firebase console at https://console.firebase.google.com/ and create one. In your Firebase project, add an iOS app. You will need to provide your iOS app’s bundle ID during this process.
During the process of adding an iOS app to your Firebase project, you’ll be prompted to download a GoogleService-Info.plist file. Download this file and add it to your iOS project in Xcode.
You then need to install the Firebase SDK which can be installed using CocoaPods, Carthage or Swift Package Manager (SPM). If you’re using CocoaPods, add the following to your Podfile:
pod 'Firebase/Analytics'Afterwards, run pod install to install the Firebase Analytics SDK.
If you are using Swift Package Manager, you can add the Firebase SDK by adding the package dependency in Xcode:
- File > Swift Packages > Add Package Dependency
- Enter
https://github.com/firebase/firebase-ios-sdk.gitin the “Choose Package Repository” dialog - In the next page, specify the version you want to depend on.
- And finally, select the “FirebaseAnalytics” library and finish the setup.
In your AppDelegate’s didFinishLaunchingWithOptions method, import Firebase and configure it:
import Firebase
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
return true
}
Now, you can use GA4 to log events. Here’s an example of logging an event:
Analytics.logEvent(AnalyticsEventSelectContent, parameters: [
AnalyticsParameterItemID: "id-\(title)",
AnalyticsParameterItemName: title,
AnalyticsParameterContentType: "cont",
])This logs a “select_content” event with several parameters.