Get started with Google Analytics

This quickstart shows you how to add Google Analytics to your app and begin logging events.

Google Analytics collects usage and behavior data for your app. The SDK logs two primary types of information:

  • Events: What is happening in your app, such as user actions, system events, or errors.
  • User properties: Attributes you define to describe segments of your user base, such as language preference or geographic location.

Analytics automatically logs some events and user properties; you don't need to add any code to enable them.

Before you begin

  1. If you haven't already, add Firebase to your Apple project and make sure that Google Analytics is enabled in your Firebase project:

    • If you're creating a new Firebase project, enable Google Analytics during the project creation workflow.

    • If you're using an existing Firebase project that doesn't have Google Analytics enabled, go to the Integrations tab of your > Project settings to enable it.

    When you enable Google Analytics in your project, your Firebase apps are linked to Google Analytics data streams.

  2. (Recommended). Add the AdSupport framework to your project to enable additional features such as audiences and campaign attribution.

Add the Analytics SDK to your app

Use Swift Package Manager to install and manage Firebase dependencies.

  1. In Xcode, with your app project open, navigate to File > Add Packages.
  2. When prompted, add the Firebase Apple platforms SDK repository:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. Choose the Analytics library.
  5. Add the -ObjC flag to the Other Linker Flags section of your target's build settings.
  6. For an optimal experience with Analytics, we recommend enabling Google Analytics in your Firebase project and adding the Firebase SDK for Google Analytics to your app. You can select either the library without IDFA collection or with IDFA collection.
  7. When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.

Learn more about IDFA, the device-level advertising identifier, in Apple's User Privacy and Data Use and App Tracking Transparency documentation.

Next, perform some configuration steps:

  1. Import the FirebaseCore module in your UIApplicationDelegate, as well as any other Firebase modules your app delegate uses. For example, to use Cloud Firestore and Authentication:

    SwiftUI

    import SwiftUI
    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

    Swift

    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

    Objective-C

    @import FirebaseCore;
    @import FirebaseFirestore;
    @import FirebaseAuth;
    // ...
          
  2. Configure a FirebaseApp shared instance in your app delegate's application(_:didFinishLaunchingWithOptions:) method:

    SwiftUI

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    Swift

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    Objective-C

    // Use Firebase library to configure APIs
    [FIRApp configure];
  3. If you're using SwiftUI, you must create an application delegate and attach it to your App struct via UIApplicationDelegateAdaptor or NSApplicationDelegateAdaptor. You must also disable app delegate swizzling. For more information, see the SwiftUI instructions.

    SwiftUI

    @main
    struct YourApp: App {
      // register app delegate for Firebase setup
      @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
      var body: some Scene {
        WindowGroup {
          NavigationView {
            ContentView()
          }
        }
      }
    }
          


(Optional) Disable Apple ad network attribution registration

For your convenience, the SDK automatically registers your app with Apple for ad network attribution with SKAdNetwork. If you wish to disable this feature, set the value of GOOGLE_ANALYTICS_REGISTRATION_WITH_AD_NETWORK_ENABLED to NO (Boolean) in your app’s info.plist file.

Start logging events

After you have configured the FirebaseApp instance, you can begin to log events with the logEvent() method.

Certain events are recommended for all apps; others are recommended for specific business types or verticals. You should send suggested events along with their prescribed parameters, to ensure maximum available detail in your reports and to benefit from future features and integrations as they become available. This section demonstrates logging a pre-defined event, for more information on logging events, see Log events.

The following example demonstrates how to log a recommended event to indicate a user has clicked on a specific element in your app:

Swift

Note: This Firebase product is not available on the macOS target.
Analytics.logEvent(AnalyticsEventSelectContent, parameters: [
  AnalyticsParameterItemID: "id-\(title!)",
  AnalyticsParameterItemName: title!,
  AnalyticsParameterContentType: "cont",
])

Objective-C

Note: This Firebase product is not available on the macOS target.
[FIRAnalytics logEventWithName:kFIREventSelectContent
                    parameters:@{
                                 kFIRParameterItemID:[NSString stringWithFormat:@"id-%@", self.title],
                                 kFIRParameterItemName:self.title,
                                 kFIRParameterContentType:@"image"
                                 }];

To view this event in the Xcode debug console, enable Analytics debugging:

  1. In Xcode, select Product > Scheme > Edit scheme...
  2. Select Run from the left menu.
  3. Select the Arguments tab.
  4. In the Arguments Passed On Launch section, add -FIRAnalyticsDebugEnabled.

Next steps