Amplify Analyticsのセットアップ
Analytics カテゴリを使用すると、アプリのアナリティクスデータを収集できます。Analytics カテゴリは Amazon Pinpoint と Amazon Kinesis に組み込みサポートを備えています (Kinesis サポートは現在 Amplify JavaScript ライブラリでのみ利用可能です)。Analytics カテゴリは Amazon Cognito Identity プールを使用してアプリのユーザーを識別します。Cognito を使用すると、アプリの認証済みユーザーと認証されていないユーザーからデータを受け取ることができます。
目標
Amplify Analytics を使用してアプリケーションをセットアップおよび設定し、アナリティクスイベントを記録します。
前提条件
- Install and configure Amplify CLI
- An iOS application targeting at least iOS 11.0 with Amplify libraries integrated
- For a full example of creating a iOS Xcode project, please follow project setup walkthrough
Analytics バックエンドのセットアップ
プロジェクトのルートフォルダで以下のコマンドを実行します。CLI は Analytics カテゴリの設定オプション (Amazon Pinpoint リソース名やアナリティクスイベント設定など) をプロンプトします。
Analytics カテゴリは、アプリがアナリティクスイベントを送信することを許可するために、バックグラウンドで認証カテゴリを使用します。
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 ライブラリをインストール
-
To install Amplify Libraries in your application, open your project in Xcode and select File > Add Packages....
-
Enter the Amplify iOS 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.
To install the Amplify Analytics and Authentication to your application, add both "AmplifyPlugins/AWSPinpointAnalyticsPlugin" and "AmplifyPlugins/AWSCognitoAuthPlugin" to your Podfile (Because IAM credential is required to access AWS Pinpoint Service, "AWSCognitoAuthPlugin" also needs to be installed). Your Podfile should look similar to:
target 'MyAmplifyApp' do use_frameworks! pod 'Amplify' pod 'AmplifyPlugins/AWSPinpointAnalyticsPlugin' pod 'AmplifyPlugins/AWSCognitoAuthPlugin'endTo install, download and resolve these pods, execute the command:
pod install --repo-updateNow you can open your project by opening the .xcworkspace file using the following command:
xed .Amplify Analytics を初期化
To initialize the Amplify Analytics and Authentication categories, you are required to use the Amplify.add() method for each category you want. When you are done calling add() on each category, you finish configuring Amplify by calling Amplify.configure().
Add the following imports to the top of your AppDelegate.swift file:
import Amplifyimport AWSPinpointAnalyticsPluginimport Amplifyimport AmplifyPluginsAdd the following code
Create a custom AppDelegate, and add to your application:didFinishLaunchingWithOptions method
class AppDelegate: NSObject, UIApplicationDelegate { 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 }}Then in the App scene, use UIApplicationDelegateAdaptor property wrapper to use your custom AppDelegate
@mainstruct MyAmplifyApp: App { @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene { WindowGroup { ContentView() } }}Add to your AppDelegate's 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, specify your event using BasicAnalyticsEvent and call Amplify.Analytics.record()
func recordEvents() { let properties: AnalyticsProperties = [ "eventPropertyStringKey": "eventPropertyStringValue", "eventPropertyIntKey": 123, "eventPropertyDoubleKey": 12.34, "eventPropertyBoolKey": true ] let event = BasicAnalyticsEvent(name: "eventName", properties: properties) Amplify.Analytics.record(event: event)}アナリティクスコンソールを表示
以前のリンクを保存していない場合でも、ターミナルから到達できます。以下のコマンドを実行してコンソールを開きます。
amplify console analytics次のステップ:
おめでとうございます! Analytics バックエンドをプロビジョニングし、Analytics ライブラリをインストールしました。Amplify Analytics のユースケースについては、以下のリンクをご覧ください。