Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.
Gen1 DocsLegacy

Page updated Mar 26, 2026

Amplify Analytics のセットアップ

AWS will end support for Amazon Pinpoint on October 30, 2026,, and is no longer accepting any new users as of May 20 (see the linked doc). The guidance is to use AWS End User Messaging for push notifications and SMS, Amazon Simple Email Service for sending emails, Amazon Connect for campaigns, journeys, endpoints, and engagement analytics. Pinpoint recommends Amazon Kinesis for event collection and mobile analytics.

Amplify を使用すると、アプリのアナリティクスデータを収集できます。Analytics を使用するには、AWS Cloud Development Kit (AWS CDK) を使用して Amazon Kinesis または Amazon Pinpoint を有効にする必要があります。Analytics カテゴリは Amazon Cognito ID プールを使用してアプリのユーザーを_識別_します。Cognito により、アプリの認証済みユーザーと未認証ユーザーからデータを受け取ることができます。

前提条件

Amplify ライブラリが統合されたアプリケーションと、以下のいずれかの最小ターゲット:

  • iOS 13.0 (使用: Xcode 14.1 以降)
  • macOS 10.15 (使用: Xcode 14.1 以降)
  • tvOS 13.0 (使用: Xcode 14.3 以降)
  • watchOS 9.0 (使用: Xcode 14.3 以降)
  • visionOS 1.0 (使用: Xcode 15 以降) (プレビューサポート - 詳細は以下を参照)

visionOS サポートは現在プレビュー中であり、最新の Amplify リリースを使用することで利用できます。 新しい Xcode および visionOS バージョンがリリースされると、必要に応じて、ベストエフォート方式でサポートが更新されます。

Analytics バックエンドのセットアップ

AWS CDK を使用して、Amazon Pinpoint を搭載するアナリティクスリソースを作成します。

amplify/backend.ts
import { defineBackend } from "@aws-amplify/backend"
import { auth } from "./auth/resource";
import { data } from "./data/resource";
import { Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";
import { CfnApp } from "aws-cdk-lib/aws-pinpoint";
import { Stack } from "aws-cdk-lib/core";
const backend = defineBackend({
auth,
data,
// additional resources
});
const analyticsStack = backend.createStack("analytics-stack");
// create a Pinpoint app
const pinpoint = new CfnApp(analyticsStack, "Pinpoint", {
name: "myPinpointApp",
});
// create an IAM policy to allow interacting with Pinpoint
const pinpointPolicy = new Policy(analyticsStack, "PinpointPolicy", {
policyName: "PinpointPolicy",
statements: [
new PolicyStatement({
actions: ["mobiletargeting:UpdateEndpoint", "mobiletargeting:PutEvents"],
resources: [pinpoint.attrArn + "/*"],
}),
],
});
// apply the policy to the authenticated and unauthenticated roles
backend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);
backend.auth.resources.unauthenticatedUserIamRole.attachInlinePolicy(pinpointPolicy);
// patch the custom Pinpoint resource to the expected output configuration
backend.addOutput({
analytics: {
amazon_pinpoint: {
app_id: pinpoint.ref,
aws_region: Stack.of(pinpoint).region,
}
},
});

Amplify ライブラリのインストール

  1. アプリケーションに Amplify ライブラリをインストールするには、Xcode でプロジェクトを開き、File > Add Packages... を選択します。

  2. Amplify Library for Swift GitHub リポジトリ URL (https://github.com/aws-amplify/amplify-swift) を検索バーに入力し、Add Package をクリックします。

注: Dependency Rule ドロップダウンから Up to Next Major Version を選択する必要があります。

  1. 最後に、AWSPinpointAnalyticsPluginAWSCognitoAuthPlugin、および Amplify をターゲットに追加します。その後、Add Package をクリックします。

Amplify Analytics の初期化

設定ファイルをインポートしてアプリに読み込みます。Amplify 設定ステップをアプリのルートエントリポイントに追加することをお勧めします。

次のコマンドを実行して amplify_outputs.json ファイルが生成されていることを確認してください:

Terminal
npx ampx sandbox

次に、ファイルをプロジェクトに移動します。これは、ファイルを Xcode プロジェクトにドラッグアンドドロップすることで実行できます。

アプリで Amplify Analytics と Authentication カテゴリを使用するには、Amplify.add(plugin:) および Amplify.configure(with:) メソッドを呼び出して、対応するプラグインを作成して設定する必要があります。

メイン App ファイルの先頭に以下のインポートを追加します:

import Amplify
import AWSCognitoAuthPlugin
import AWSPinpointAnalyticsPlugin

初期化子に次のコードを追加します。ない場合は、デフォルトの init を作成できます:

init() {
do {
try Amplify.add(plugin: AWSCognitoAuthPlugin())
try Amplify.add(plugin: AWSPinpointAnalyticsPlugin())
try Amplify.configure(with: .amplifyOutputs)
print("Amplify configured with Auth and Analytics plugins")
} catch {
print("Failed to initialize Amplify with \(error)")
}
}

AppDelegate.swift ファイルの先頭に以下のインポートを追加します:

import Amplify
import AWSCognitoAuthPlugin
import AWSPinpointAnalyticsPlugin

application:didFinishLaunchingWithOptions メソッドに次のコードを追加します:

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
do {
try Amplify.add(plugin: AWSCognitoAuthPlugin())
try Amplify.add(plugin: AWSPinpointAnalyticsPlugin())
try Amplify.configure(with: .amplifyOutputs)
print("Amplify configured with Auth and Analytics plugins")
} catch {
print("Failed to initialize Amplify with \(error)")
}
return true
}

このアプリケーションをビルドして実行すると、コンソール ウィンドウに以下が表示されます:

Amplify configured with Auth and Analytics plugin

次のステップ:

おめでとうございます! これで Analytics バックエンドがプロビジョニングされ、Analytics ライブラリがインストールされました。次のリンクをチェックして、Amplify Analytics のユースケースを確認してください:

リファレンス

Amazon Pinpoint Construct ライブラリ