This is the Best Way to Integrate Google Analytics with SwiftUI


Are you eager to know how users are using your app? Of course, developers should get a deeper understanding of their customers and optimize their apps. The best way to figure out this problem is Google Analytics, which is a free tool that you need to analyze data for your business in apps.

Create a Property

Property is a data stream console used to collect, analyze and present data and charts in real time. Developers should create a separate property for each app to avoid confusion.

Create Property in Console

Property Configuration

Property Details and Business Information are required. But developers can modify both of them after creation.

Set Property Name

Business Information is optional, and can be modified after creation.

Provide More Information About Your Business

Choose iOS Platform

Google Analytics works for both websites and apps. Here, select the iOS app, because we integrate Google Analytics with an iOS SwiftUI app in this tutorial.

Select “iOS app”

Register Your App

The iOS bundle ID must be the same as the bundle ID in the Xcode project. And both the iOS bundle ID and App Name is immutable after creation. App Store ID is optional and zero by default.

Register your app

Link a New Firebase Project to Your Property by Google Automatically

Google Analytics creates a dedicated and free Firebase project for each property to store and analyze data. This process is automatic and fast, developers just need to wait for a while.

Link a new Firebase project to your property by Google automatically

Download and Put GoogleService-Info.plist to Your Xcode Project

GoogleService-Info.plist contains everything the Google Analytics SDK needs. Usually, developers don’t need to care about and modify the content.

Download and put GoogleService-Info.plist to your Xcode project

Import Google Analytics SDK to Your Xcode Project

There are two recommended ways to import Google Analytics SDK. One is CocoaPods described in the official document, and the other is Swift Package Manager, which is a better choice since Xcode 11. Swift Package Manager also works well together with CocoaPods, so developers can enjoy it without any changes to their existing project with CocoaPods.

By CocoaPods

Notice that the package name is Firebase/Analytics, without a specified version.

Import Firebase/Analytics by CocoaPods

By Swift Package Manager (Recommended)

1. Open a Xcode project (or workspace with CocoaPods)

2. Menu “File” > “Swift Packages” > “Add package dependency”

Add Package Dependency

3. Paste “”, and click “Next”

Paste Google Analytics SDK Repository URL

4. Select “Versions: Up to Next Major 8.0.0”, and click “Next”

Choose the Lastest Version

5. Check “FirebaseAnalytics” only, and click “Finished”.

Check FirebaseAnalytics Only

Initialize Google Analytics SDK on Startup

The initialization code in the Google Analytics guide does not update for SwiftUI, whose lifecycle is different from UIKit completely. To connect Firebase when your app starts up, consider the code below.

import SwiftUI
import Firebase

struct IntegrateGAwithSwiftUIApp: App {
    init() {
        // Google Analytics initiliaze here
    var body: some Scene {
        WindowGroup {

MOST IMPORTANT: Add Additional Linker Flag

  1. Click the project in the Project navigator
  2. Select the app target on the left
  3. Select the Build Settings tab
  4. Add “-ObjC” to Others Linker Flags
Add -ObjC to Other Linker Flags

Checking If Google Analytics SDK Works Well in Your App

Startup your app on a simulator or device. Google Analytics SDK tries to communicate with its server. The blue box turns green when everything goes well.

Checking if your Google Analytics SDK works well
Google Analytics SDK works well

Realtime Data Stream

Change the property to GAIntegration. A user launched the app in last 30 minutes.

Realtime Data Stream


In summary, Google Analytics is powerful and easy to integrate with SwiftUI. Don’t miss any step above, otherwise, Google Analytics works unexpectedly. Now, update your apps with Google Analytics to understand your users deeply and in time.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *