Amplify Analytics をセットアップする
Analytics カテゴリでは、アプリ用の分析データを収集できます。Analytics カテゴリは Amazon Pinpoint と Amazon Kinesis(Kinesis サポートは現在のところ Amplify JavaScript ライブラリでのみ利用可能)の組み込みサポートで提供されています。Analytics カテゴリは Amazon Cognito Identity プールを使用してアプリ内のユーザーを識別します。Cognito により、アプリ内の認証済みユーザーと非認証ユーザーの両方からデータを受け取ることができます。
目的
Amplify Analytics でアプリケーションをセットアップおよび設定し、分析イベントを記録します。
前提条件
An application with Amplify libraries integrated and a minimum target of any of the following:
- iOS 13.0, using Xcode 14.1 or later.
- macOS 10.15, using Xcode 14.1 or later.
- tvOS 13.0, using Xcode 14.3 or later.
- watchOS 9.0, using Xcode 14.3 or later.
- visionOS 1.0, using Xcode 15 or later. (Preview support - see below for more details.)
For a full example, please follow the project setup walkthrough.
Analytics バックエンドのセットアップ
プロジェクトのルートフォルダで以下のコマンドを実行します。CLI は Analytics カテゴリの設定オプション(Amazon Pinpoint リソース名と分析イベント設定など)のプロンプトを表示します。
Analytics カテゴリは、アプリが分析イベントを送信することを認可するために、バックグラウンドで Authentication カテゴリを利用しています。
amplify add analytics? Select an Analytics provider (Use arrow keys) `Amazon Pinpoint`? Provide your pinpoint resource name: `yourPinpointResourceName`? Apps need authorization to send analytics events. Do you want to allow guests and unauthenticated users to send analytics events? (we recommend you allow this when getting started) `Yes`バックエンドをデプロイするには、以下を実行します:
amplify pushUpon completion, amplifyconfiguration.json should be updated to reference provisioned backend analytics resources. Note that these files should already be a part of your project if you followed the Project setup walkthrough.
Amplify ライブラリをインストール
Swift Package Manager
-
To install Amplify Libraries in your application, open your project in Xcode and select File > Add Packages....
-
Enter the Amplify Library for Swift GitHub repo URL (
https://github.com/aws-amplify/amplify-swift) into the search bar and click Add Package.
- Lastly, choose AWSPinpointAnalyticsPlugin, AWSCognitoAuthPlugin, and Amplify. Then click Add Package.
Amplify Analytics を初期化
To use the Amplify Analytics and Authentication categories in your app, you need to create and configure their corresponding plugins by calling the Amplify.add(plugin:) and Amplify.configure() methods.
Add the following imports to the top of your main App file:
import Amplifyimport AWSCognitoAuthPluginimport AWSPinpointAnalyticsPluginAdd the following code to its initializer. If there is none, you can create a default init:
init() { do { try Amplify.add(plugin: AWSCognitoAuthPlugin()) try Amplify.add(plugin: AWSPinpointAnalyticsPlugin()) try Amplify.configure() print("Amplify configured with Auth and Analytics plugins") } catch { print("Failed to initialize Amplify with \(error)") }}Add the following imports to the top of your AppDelegate.swift file:
import Amplifyimport AWSCognitoAuthPluginimport AWSPinpointAnalyticsPluginAdd the following code to the application:didFinishLaunchingWithOptions method:
func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { do { try Amplify.add(plugin: AWSCognitoAuthPlugin()) try Amplify.add(plugin: AWSPinpointAnalyticsPlugin()) try Amplify.configure() print("Amplify configured with Auth and Analytics plugins") } catch { print("Failed to initialize Amplify with \(error)") }
return true}Upon building and running this application you should see the following in your console window:
Amplify configured with Auth and Analytics pluginTo record an event, create a BasicAnalyticsEvent and then call Amplify.Analytics.record(event:)
let properties: AnalyticsProperties = [ "eventPropertyStringKey": "eventPropertyStringValue", "eventPropertyIntKey": 123, "eventPropertyDoubleKey": 12.34, "eventPropertyBoolKey": true]
let event = BasicAnalyticsEvent( name: "eventName", properties: properties)
Amplify.Analytics.record(event: event)Analytics コンソールを表示
以前にリンクを保存していない場合でも、ターミナルからアクセスできます。コンソールを開くには、以下のコマンドを実行します。
amplify console analytics次のステップ:
おめでとうございます!Analytics のバックエンドをプロビジョニングし、Analytics ライブラリをインストールしました。Amplify Analytics のユースケースについては、以下のリンクをチェックしてください: