このドキュメントが更新されました。
日本語への翻訳がまだ完了していません。
英語の最終更新: 2023/12/21
日本語の最終更新: 2023/12/14

Get Started with App Events (iOS)

This guide shows you how to add App Events to your new or existing app by integrating the Facebook SDK then logging these events.

Changes have been made to the Facebook iOS SDK. We recommend upgrading to the new version of the Facebook iOS SDK. See the Device Consent section for more information about this change.

Before You Start

You will need:

Step 1: Configure Your Facebook App

Go to the App Dashboard, click My Apps, and create a new app if you don't already have one. Navigate to Settings > Basic to view the App Details Panel with your App ID, your App Secret, and other details about your app.

Scroll down to the bottom of the page and click Add Platform. Choose iOS, add your app details, and save your changes.

Set up your app for advertising by adding the following details:

  • App Domains - Provide Apple App Store URL of your app.
  • Privacy Policy URL - Provide a Privacy Policy URL. Required to take your app public.
  • Terms of Service URL - Provide a Terms of Service URL.
  • Platform - Scroll to the bottom of the Settings panel to add the iOS Platform.

To learn more about adding details to your app, such as an icon or category, visit the App Development docs.

Step 2: Link your Ad and Business Accounts

To run ads and measure installs in the Ads Manager, associate at least one Ad Account and a Business Account with your App.

  1. In the App Dashboard click Settings > Advanced.
  2. In Authorized Ad Account IDs, add your Ad Account IDs. You can get your ad account IDs from your Ads Manager.
  3. In the Advertising Accounts Panel, click Get Started and follow the instructions to connect the app to a Business.

Step 3: Set Up Your Development Environment

The following procedure uses Swift Package Manager to set up your development environment in Xcode.

  1. In Xcode, click File > Add Packages....
  2. In the search field that appears, enter the repository URL: https://github.com/facebook/facebook-ios-sdk.
  3. In Dependency Rule, select Up to Next Major Version and enter a recent version. The most current release is listed at https://github.com/facebook/facebook-ios-sdk/releases/
  4. Choose the libraries you intend to use and the targets to which you want to add those libraries.
  5. Click on Add Package to complete your setup.

Step 4: Register and Configure Your App with Facebook

Add Your Bundle Identifier and enable Single Sign-On for your app.

バンドルIDは、次のボックスに表示されます。ボックスが空白の場合は、Xcodeプロジェクトの[iOS Application Target]でバンドルIDを探し、下のボックスに貼り付けてください。
バンドルID
バンドルIDは、設定ページのiOSセクションで後から変更できます。
シングルサインオンを有効にする
次の[シングルサインオン]を[はい]に設定して、アプリのシングルサインオンを有効にします。
シングルサインオン iOSお知らせから起動します

Step 5: Configure Your Project

Info.plistファイルに、アプリに関するデータが含まれているXMLスニペットを挿入します。

Facebookログインを統合すると、特定のアプリイベントは、アプリイベントの自動記録を無効にしない限り、イベントマネージャのために自動的に記録、収集されます。これは、アプリコードで変更できます。また、アプリダッシュボードかイベントマネージャの[アプリイベント]の下のトグルで切り替えることもできます。AutoLogAppEventsEnabledのフラグとトグルとの間で値が競合している場合、「Facebook SDKの自動イベント記録」のトグル値が優先されることに注意してください。収集される情報やアプリイベントの自動記録を無効にする方法については、アプリイベントの自動記録をご覧ください。

  1. Info.plistを右クリックし、[プログラムで開く]▸[ソースコード]を選択します。
  2. 次のXMLスニペットをコピーして、ファイルの本文(<dict>...</dict>)に貼り付けます。
    <key>CFBundleURLTypes</key>
    <array>
      <dict>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>fbAPP-ID</string>
      </array>
      </dict>
    </array>
    <key>FacebookAppID</key>
    <string>APP-ID</string>
    <key>FacebookClientToken</key>
    <string>CLIENT-TOKEN</string>
    <key>FacebookDisplayName</key>
    <string>APP-NAME</string>
  3. キー[CFBundleURLSchemes]<array><string>にあるAPP-IDは、自身のアプリIDに置き換えてください。
  4. キーFacebookAppID<string>にあるAPP-IDは、自身のアプリIDに置き換えてください。
  5. キーFacebookClientToken<string>にあるCLIENT-TOKENは、アプリダッシュボードの[設定 > 高度 > クライアントトークン]にある値に置き換えてください。
  6. キーFacebookDisplayName<string>にあるAPP-NAMEは、実際のアプリ名に置き換えてください。
  7. Facebookアプリへのアプリ切り替えを実行できるFacebookダイアログ(ログイン、シェア、アプリの招待など)のいずれかを使用するには、アプリのInfo.plistに以下を含める必要があります。
    <key>LSApplicationQueriesSchemes</key>
    <array>
      <string>fbapi</string>
      <string>fb-messenger-share-api</string>
    </array>

Info.plistにキーとしてFacebookAutoLogAppEventsEnabledを追加することで、アプリイベントの自動収集をtrueまたはfalseに直接設定できるようになります。

Mac Catalystアプリでログインが機能するには、プロジェクトにキーチェーン共有機能を含める必要があります。
  1. アプリターゲットを設定する際に、[署名と機能]タブの[+ 機能]ボタンを選択します。
  2. キーチェーン共有機能を見つけて選択します。
  3. ターゲットとしてキーチェーン共有機能がリストされていることを確認します。

Step 6: Connect Your App Delegate and Scene Delegate

以下のコードで、 AppDelegate.swift 内のコードを置き換えます。このコードは、アプリの起動時にSDKを初期化し、ログインアクションやシェアアクションが実行されたときにネイティブFacebookアプリのログインとシェアリングをSDKが処理できるようにします。このコードを使用しない場合は、ユーザーはFacebookにログインした状態で、ログイン用のアプリ内ブラウザーを使用する必要があります。
    
// AppDelegate.swift
import UIKit
import FacebookCore

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {    
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {          
        ApplicationDelegate.shared.application(
            application,
            didFinishLaunchingWithOptions: launchOptions
        )

        return true
    }
          
    func application(
        _ app: UIApplication,
        open url: URL,
        options: [UIApplication.OpenURLOptionsKey : Any] = [:]
    ) -> Bool {
        ApplicationDelegate.shared.application(
            app,
            open: url,
            sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
            annotation: options[UIApplication.OpenURLOptionsKey.annotation]
        )
    }  
}

iOS 13では、URLを開く機能が SceneDelegateに移動されました。iOS 13以上を使用している場合は、次のメソッドを SceneDelegate に追加して、ログインや共有機能のような操作を意図したとおりに行えるようにしてください。
// SceneDelegate.swift
import FacebookCore
  ...
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else {
        return
    }

    ApplicationDelegate.shared.application(
        UIApplication.shared,
        open: url,
        sourceApplication: nil,
        annotation: [UIApplication.OpenURLOptionsKey.annotation]
    )
}

Step 7: Add App Events

There are three ways events are tracked in your app:

iOS 14のApp Store Connect

アプリが収集してFacebookに送信するイベントによっては、App Store Connect質問表でそのようなデータタイプを公開するよう求められる場合があります。アプリのプライバシーポリシーでこの点が反映されていることを確認するのは、開発者の責任です。公開する必要があるデータタイプの詳細については、Apple App Storeでのプライバシーに関する詳細情報の記事をご覧ください。

Automatically Logged Events

Facebook SDKを使用すると、イベントの自動記録を無効にしない限り、アプリの特定のイベントは自動的に記録、収集され、Facebookイベントマネージャに使用されます。このようなイベントは、ターゲット設定、効果測定、最適化などあらゆるユースケースに関連します。

具体的には、アプリのインストール、アプリの起動、購入という3つの主要イベントが自動で記録されます。自動記録が有効である場合、広告主はこれらのイベントや、ログインインプレッションイベントなどほかのFacebook内部イベントを無効化できます。一方、自動記録を無効にした場合に、インストールや購入などの特定のイベントを記録するには、該当するイベントの記録をアプリ内で手動実装する必要があります。

イベント詳細

アプリのインストール

新しい利用者がアプリを初めてアクティベートしたとき、または特定のデバイスでアプリが初めて起動されたとき。

アプリの起動

利用者がアプリを起動し、Facebook SDKが初期化されてイベントがログに記録されたとき。ただし、最初のアプリの起動イベントから60秒以内に2回目のアプリの起動イベントが発生した場合、2回目のイベントはログ記録されません。

アプリ内購入

Apple App StoreまたはGoogle Playにより処理された購入が完了したとき。ほかの支払いプラットフォームを使用している場合、購入イベントコードを手動で追加する必要があります。

Facebook SDKクラッシュレポート

(Facebookの使用に限定。)

Facebook SDKが原因でアプリがクラッシュした場合、クラッシュレポートが生成されて、アプリの再起動時にFacebookに送られます。このレポートは、ユーザーデータを含んでおらず、FacebookがSDKの品質と安定性を確認する上で役立つものです。このイベントのログ記録をオプトアウトするには、自動でログ記録されるイベントを無効にしてください。

Get Device Consent

Starting with iOS 14.5, you will need to set isAdvertiserTrackingEnabled and log each time you give a device permission to share data with Facebook.

If a device provides consent, set Settings.shared.isAdvertiserTrackingEnabled = true.

If a device does not allow tracking, set Settings.shared.isAdvertiserTrackingEnabled = false.

Disable Automatically Logged Events

To disable automatic event logging, open the application's Info.plist as code in Xcode and add the following XML to the property dictionary:

<key>FacebookAutoLogAppEventsEnabled</key>
<false/>

In some cases, you want to delay the collection of automatically logged events, such as to obtain User consent or fulfill legal obligations, instead of disable it. In this case, set Settings.shared.isAutoLogAppEventsEnabled = true to re-enable auto-logging after the end-user provides consent.

To suspend collection again for any reasons, set Settings.shared.isAutoLogAppEventsEnabled = false.

You can also disable automatic In-App Purchase event logging using the app dashboard. Go to the iOS card under Basic>Settings and toggle the switch to No.

Disable Collection of Advertiser IDs

To disable collection of advertiser-id, open the application's .plist as code in Xcode and add the following XML to the property dictionary:

<key>FacebookAdvertiserIDCollectionEnabled</key>
<false/>

In some cases, you want to delay the collection of advertiser_id, such as to obtain User consent or fulfill legal obligations, instead of disabling it. In this case, set Settings.shared.isAdvertiserIDCollectionEnabled = true after the end-user provides consent.

To suspend collection for any reason, set Settings.shared.isAdvertiserIDCollectionEnabled = false.

Manually Log Events

To log a custom event, just pass the name of the event as an AppEvents.Name:

AppEvents.shared.logEvent(AppEvents.Name("battledAnOrc"))

Event Parameters

Facebook has created a set of useful event parameters for inclusion with standard events or with your own custom events. You can also provide your own parameters.

These pre-defined parameters are intended to provide guidance on common logging patterns, and may have a more readable form in reporting and other UIs. Log the set of parameters you're interested in seeing broken down. The recommended description for these are guidance only - you can use these parameters for whatever makes sense for your app.

The parameters are passed via a dictionary where the key holds the parameter name as an AppEvents.ParameterName, and the value must be either a String or a number (Int, Double, etc.).

Step 8: Test Your Events

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

  1. Open the App Ads Helper.
  2. In Select an App, choose your app and choose Submit.
  3. Scroll to the bottom and choose Test Event.
  4. Start your app and send an event. The event should appear on the page.

If you plan to optimize/track your events in SKAdNetwork campaigns, you also need to properly configure event priority (also known as conversion value) in order for Facebook to correctly receive the conversions. More details can be found here.

Learn More

Example Apps

We have created some examples for different app types to make it easier for you to see how you can use app events. Each of the example apps provides a screen by screen breakdown of the different events and parameters that can be collected. At the end of each section, there is a table listing the recommended events and parameters for each app. And, if necessary, you can create your own events and parameters.