Facebook Analytics Quickstart Guide for iOS

This Quickstart shows how to integrate Facebook Analytics into a new or existing Swift or Objective-C app.

1. Login

Please log in to Facebook to create apps or register as a developer.

2. Set up Your Development Environment

In the following list, select the appropriate SDK for the language you will use in this app. If your app uses Swift, select SDK: Cocoapods. If your app uses Objective-C, select SDK: FB SDK.

a. Install Cocoapods.

If you already installed Cocoapods and created a pod file, go to the next step to install the Facebook SDK. To install Cocoapods, open a terminal window and run the following command in a terminal window:
sudo gem install cocoapods

b. Install the Facebook SDK.

  1. Create a new Swift project and go to the project directory. If you are using an existing project, go to project directory of the existing project.
  2. In a terminal window, run the following command to create a pod file for the project:
    pod init
  3. Add the following line to the pod file of your project:
    pod 'FacebookCore'
  4. In the terminal window, run the following command to install the Facebook SDK for Swift:
    pod install

c. Modify the AppDelegate.swift file:

  1. Add the following import statement:
  2. import FacebookCore
    	
  3. Add the following statement to the applicationDidBecomeActive function:
  4. AppEventsLogger.activate(application)
    	

a. Download the Facebook SDK for iOS.

Download the SDK and unzip the archive to ~/Documents/FacebookSDK.

b. In xCode, create a single view Objective-C project.

c. Add the necessary Frameworks to your Xcode Project.

  1. Open ~/Documents/FacebookSDK
  2. Drag the following frameworks to Frameworks in the Project Navigator.
    • Bolts
    • FBSDKCoreKit
  3. Add the path to ~/Documents/FacebookSDK to Framework Search Paths in the Build Settings of your project.

When you use the Facebook SDK, events in your app are automatically logged and collected for Facebook Analytics unless you disable automatic event logging. For details about what information is collected and how to disable automatic event logging, see Automatic App Event Logging.

d. Make the following changes to the AppDelegate.m file.

  1. At the top, add an import statement that points to <path_to>/FBSDKCoreKit.framework/Headers/FBSDKCoreKit.h.
  2. Replace the (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions function with the following code:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; // Add any custom logic here. return YES; }

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

    BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey] ]; // Add any custom logic here. return handled; }

3. Configure Your Information Property List.

a. Enter you Bundle Identifier (Bundle ID) in the Box.

You need to login to complete this step.

b. In Xcode, right-click info.plist and choose Open As Source Code.

c. Copy the XML snippet into the body of your file just before the final </dict> element.

<key>CFBundleURLTypes</key>
<array>
  <dict>
  <key>CFBundleURLSchemes</key>
  <array>
    <string>fb[APP_ID]</string>
  </array>
  </dict>
</array>
<key>FacebookAppID</key>
<string>[APP_ID]</string>
<key>FacebookDisplayName</key>
<string>[APP_NAME]</string>

4. Build and then run the project in the Simulator

In Xcode, select an iOS simulator and click Run. Xcode builds your project and then launches the most recent version of your app running in Simulator.

5. See the Results in the Dashboard.

To see events, open Facebook Analytics. Choose your app and navigate to Activity > Event Debugging.
You should see your app launch event. If not, click the refresh button to load the latest events for your app.
Note: Events may take up to 20 minutes to appear in the dashboard.
Once you've verified that your events are being delivered, you're ready to create your first funnel to help you measure conversion for a sequence of events in your app.

6. Add More Events

Use the App Event Builder to generate the code in your language to implement either a custom or pre-defined app event that you can then add to your app.

To Generate Code for a Pre-defined Event

  1. Choose the Pre-Defined Event tab.
  2. In Event Name, choose a pre-defined event.
  3. Choose Get Code.
  4. In the window, select a language to see the code to copy and paste into your app.

Automatically Logged Events

After you integrate the Facebook SDK into your app, the following events are automatically logged:

EventDetails

App Install

The first time a new user activates an app or the first time an app starts on a particular device.

App Launch

When the Facebook SDK is initialized in your app for each session.

In-App Purchase

When a purchase processed by the Apple App Store or Google Play has been completed. If you use other payments platforms, you will need to add purchase event code manually.

Pre-defined Events

The following table contains the pre-defined events. Use the App Event Builder to get the code for these events.

Event NameDescription

Achieved Level

Log this event when the user has achieved the level in the app. The level is specified in a parameter.

Add Payment Info

Log this event when the user has entered their payment info.

Add To Cart

Log this event when the user has added an item to their cart. The valueToSum parameter contains the price of the item.

Add To Wishlist

Log this event when the user add an item to their wish list. The valueToSum parameter contains the price of the item.

Complete Registration

Log this event when a user has completed registration with the app.

Complete Tutorial

Log this event when the user has completed a tutorial in the app.

Purchase

Log this event after the user completes a purchase using your own purchase method. If you use Facebook to manage your in-app purchases, the purchase events are automatically logged.

Rate

Log this event when the user gives a rating. The valueToSum parameter is the rating given by the user.

Search

Log this event when a user performs a search within the app.

Spent Credits

Log this event when the user has spent their app credits. The valueToSum parameter is the number of credits spent.

Unlock Achievement

Log this event when the user unlocks an achievement in the app.

View Content

Log this event when a user has viewed a form of content in the app.

Test your events with the App Ads Helper

The App Ads Helper allows you to test your app events to ensure that your app is sending events to Facebook.

a. Open the App Ads Helper.

b. In Select an App, choose your app and choose Submit.

c. Go to the bottom and choose Test App Events.

d. Start your app and send an event. The event appears on the web page.

Take a Guided Tour of Facebook Analytics

Now that you're logging app events, visit our demo app to take a guided tour of all of the key features that Facebook Analytics offers you and your business.

The demo app does not have an Event Debugging link. This link is only available in apps that you create.

Next Steps

Be sure to check out our other documentation pages for more advanced guides.