iOS SDK 統合ガイド
このページでは、Rokt EcommerceのiOS SDKを実装して、チェ ックアウト時により関連性の高い顧客体験を提供する方法を説明します。SDKは、設定されたページでトリガーを発動し、ユーザーおよびトランザクションデータをRoktに渡してパーソナライズと測定を可能にすることで、こうした体験をトリガーおよび追跡します(確認ページ上でのオファーの表示など)。
担当のアカウント代表者が、iOS SDK用にアカウントを設定するお手伝いをします。SDKを初期化するために必要なキーとシークレット、およびお客様に最も関連性の高い体験を表示するために必要な追加リソースを提供します。
これらの手順を完了するには開発リソースが必要です。追加の支援が必要な場合は、Roktのアカウントマネージャーにお問い合わせください。Shopifyストアでは、Rokt Ecommerceアプリを使用して数秒でRokt配置を設定できます—コーディングは不要です!
1. iOSアプリにRokt SDKを追加する
SPMまたはCocoaPodsのいずれかを使用して、Rokt SDKをアプリケーションに含めます:
CocoaPods
CocoaPodsを使用してSDKを統合するには、Podfileに以下を指定してください:
pod 'mParticle-Apple-SDK', '~> 8.0'
pod 'mParticle-Rokt','~> 8.0'
SPM
Swift Package Managerを使用してSDKを統合するには:
- Xcodeでプロジェクトを開き、「Package Dependencies」タブに移動します。
- パッケージリストの下にある**+**ボタンをクリックします。
- 右上の検索ボックスにリポジトリURL
https://github.com/mParticle/mparticle-apple-sdk
を入力し、パッケージ一覧からmparticle-apple-sdk
を選択し、「Dependency Rule」を「アップ・トゥ・ネクスト・メジャー・バージョン」に変更します。 - 右下の「Add Package」ボタンをクリックし、「パッケージ製品」として
mParticle-Apple-SDK
を選択します。位置情報追跡サポートを含まないSDKバージョンを使用したい場合は、mParticle-Apple-SDK-NoLocation
を選択します。 - Rokt KitリポジトリURL
https://github.com/mparticle-integrations/mparticle-apple-integration-rokt.git
についてもステップ3と4を繰り返します。mParticle-Apple-SDK-NoLocation
パッケージ製品を選択した場合は、import mParticle_Apple_SDK_NoLocation
を使用してSDKをインポートする必要があります。import mParticle_Apple_SDK
の代わりに使用します。
2. Rokt SDK の初期化
SDK を初期化するには、AppDelegate ファイルに次の初期化スニペットを挿入します:
your-key
とyour-secret
は、専任の Rokt チームから提供されたキーとシークレットに置き換えてください。
- Swift
- Objective-C
import mParticle_Apple_SDK
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// SDK を初期化
let options = MParticleOptions(key: "your-key",
secret: "your-secret")
// データ環境を指定してください:
// 統合テスト中の場合は .development に設定します。
// 生産データに対応する統合が準備できたら .production に設定します。
// デフォルトは .autoDetect で、環境を自動的に検出しようとします
options.environment = .development
// 現在のユーザーを識別:
let identifyRequest = MPIdentityApiRequest.withEmptyUser()
// 未ハッシュのメールアドレスを使用している場合は 'email' に設定します。
identifyRequest.email = "j.smith@example.com"
// ユーザーがメールアドレスで識別される場合は、追加属性を設定します。
options.identifyRequest = identifyRequest
options.onIdentifyComplete = {(result: MPIdentityApiResult?, error: Error?) in
if let user = result?.user {
user.setUserAttribute("example attribute key", value: "example attribute value")
} else {
//失敗処理 - 下記の「エラーハンドリング」セクションを参照
}
}
MParticle.sharedInstance().start(with: options)
return true
}
#import <mParticle_Apple_SDK/mParticle.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// SDKを初期化
MParticleOptions *options = [MParticleOptions optionsWithKey:@"your-key"
secret:@"your-secret"];
// データ環境を指定:
// 統合をまだテストしている場合は MPEnvironmentDevelopment に設定
// 本番データに対する統合が準備完了している場合は MPEnvironmentProduction に設定
// デフォルトは MPEnvironmentAutoDetect で、自動的に環境を検出しようとします
options.environment = MPEnvironmentDevelopment;
// 現在のユーザーを識別:
// ユーザーのメールアドレスがない場合は、null 値を渡すことができます
MPIdentityApiRequest *identifyRequest = [MPIdentityApiRequest requestWithEmptyUser];
// 未ハッシュのメールアドレスを使用している場合は、'email'に設定
identifyRequest.email = @"j.smith@example.com";
options.identifyRequest = identifyRequest;
// ユーザーがメールアドレスで識別されている場合、追加のユーザー属性を設定
options.onIdentifyComplete = ^(MPIdentityApiResult *_Nullable apiResult, NSError *_Nullable error) {
if (apiResult) {
[apiResult.user setUserAttribute:@"example attribute key"
value:@"example attribute value"];
} else {
//エラー処理 - 詳細は https://docs.mparticle.com/developers/sdk/ios/idsync/#error-handling
}
};
[[MParticle sharedInstance] startWithOptions:options];
return YES;
}
2.1 初期化時にユーザーを識別する
SDKが初期化されると、現在のユーザーを識別することができ、収集されたデータが正確にユーザーに帰属し、ユーザーの行動に基づいて最も関連性の高い広告が表示されるようにします。
SDK初期化スクリプトには、identifyRequestというオブジェクトが含まれます:
- Swift
- Objective-C
let identifyRequest = MPIdentityApiRequest.withEmptyUser()
identifyRequest.email = "j.smith@example.com"
options.identifyRequest = identifyRequest
MPIdentityApiRequest *identifyRequest = [MPIdentityApiRequest requestWithEmptyUser];
identifyRequest.email = @"j.smith@example.com";
options.identifyRequest = identifyRequest;
2.2 追加のユーザー属性を設定
初期化スクリプトには、ユーザーがメールアドレスで正常に識別された場合に、追加のユーザー属性を設定することを許可するコールバック関数が含まれています:
- Swift
- Objective-C
options.onIdentifyComplete = {(result: MPIdentityApiResult?, error: Error?) in
if let user = result?.user {
user.setUserAttribute("example attribute key", value: "example attribute value")
} else {
//handle failure - see "Error Handling" section below
}
}
options.onIdentifyComplete = ^(MPIdentityApiResult *_Nullable apiResult, NSError *_Nullable error) {
if (apiResult) {
[apiResult.user setUserAttribute:@"example attribute key"
value:@"example attribute value"];
} else {
//handle failure - see https://docs.mparticle.com/developers/sdk/ios/idsync/#error-handling
}
};
3. データが利用可能になったときにユーザーを識別する
SDKを初期化した後にユーザーがメールアドレスを提供するたびに(例:ログインまたは購入時)、identify メソッドを呼び出して Rokt にメールを渡す必要があります。これにより、データが現在のユーザーに正しく帰属されることが保証されます。
3.1 identifyRequest
を作成する
ユーザーを識別するには、まずユーザーのメールアドレスを含む identifyRequest
オブジェクトを作成します。
ハッシュ化されていないメールアドレスを提供する場合は、以 下を使用してください:
- Swift
- Objective-C
let identifyRequest = MPIdentifyApiRequest.withEmptyUser()
identifyRequest.email = "j.smith@example.com"
MPIdentifyApiRequest *identifyRequest = [MPIdentifyApiRequest requestWithEmptyUser];
identifyRequest.email = @"j.smith@example.com";
3.2 追加のユーザー属性を設定する
次に、コールバック関数を作成してユーザーを識別する際に追加のユーザー属性を設定するオプションがあります。identifyRequest
が成功した場合、setUserAttribute
で設定したユーザー属性がユーザーに割り当てられます。
- Swift
- Objective-C
let identifyCallback = {(result: MPIdentifyApiResult?) in
if let user = result?.user {
user.setUserAttribute("example attribute key", value: "example attribute value")
}
}
id identityCallback = ^(MPIdentifyApiResult *_Nullable apiResult) {
if (apiResult) {
[apiResult.user setUserAttribute:@"example attribute key"
value:@"example attribute key"];
}
}
3.3 identify メソッドを呼び出す
最後に、identifyRequest
と identityCallback
を作成した後、追加の属性を設定するために、作成した identifyRequest
と identityCallback
オブジェクトを渡して identify メソッドを呼び出します。
- Swift
- Objective-C
MParticle.sharedInstance().identity.identify(identifyRequest, completion: identityCallback)
[[[MParticle sharedInstance] identity] identify:identifyRequest completion:identityCallback];
例えば、Jane Smith という名前のユーザーを j.smith@example.com というメールアドレスで識別する場合(メールアドレスのハッシュ化は必要ありません)、以下のように使用します。
- Swift
- Objective-C
let identifyRequest = MPIdentifyApiRequest.withEmptyUser()
identifyRequest.email = "j.smith@example.com"
let identifyCallback = {(result: MPIdentifyApiResult?) in
if let user = result?.user {
user.setUserAttribute("example attribute key", value: "example attribute value")
}
}
MParticle.sharedInstance().identity.identify(identifyRequest, completion: identityCallback)
MPIdentifyApiRequest *identifyRequest = [MPIdentifyApiRequest requestWithEmptyUser];
identifyRequest.email = @"j.smith@example.com";
id identityCallback = ^(MPIdentifyApiResult *_Nullable apiResult) {
if (apiResult) {
[apiResult.user setUserAttribute:@"example attribute key"
value:@"example attribute key"];
}
}
[[[MParticle sharedInstance] identity] identify:identifyRequest completion:identityCallback];
4. ユーザー属性の追跡
Rokt SDK を使用して、イベントとは別にユーザー属性を収集できます。ユーザー属性は、イベントを追跡する際のカスタム属性とは別扱いです。SDK は、あるセッションで収集されたユーザー属性を、同じセッション内でトリガーされたイベントと関連付けます。
ユーザー属性を収集するには、Adjust SDK を初期化した直後、すぐに以下のコードをアプリ内で実行し、イベントを記録する前に行います。
- Swift
- Objective-C
import mParticle_Apple_SDK
// 現在のユーザーを取得します。これは、SDK の初期化時にユーザーを識別した場合、または identify メソッドを呼び出した場合にのみ成功します。
let currentUser = MParticle.sharedInstance().identity.currentUser
// `currentUser` に現在のユーザーを正常に設定した後、次のようにユーザー属性を設定できます:
currentUser?.setUserAttribute("custom-attribute-name", value: "custom-attribute-value")
// 注意: すべてのユーザー属性(リスト属性やタグを含む)は、異なる名称を持たなければなりません。
// Rokt は、可能な限り次のユーザー属性を設定することをお勧めします:
currentUser?.setUserAttribute("firstname", value: "John")
currentUser?.setUserAttribute("lastname", value: "Doe")
// 電話番号は '1234567890'、または '+1 (234) 567-8901' のいずれかの形式でフォーマットできます
currentUser?.setUserAttribute("mobile", value: "3125551515")
currentUser?.setUserAttribute("age", value: "33")
currentUser?.setUserAttribute("gender", value: "M")
currentUser?.setUserAttribute("city", value: "Brooklyn")
currentUser?.setUserAttribute("state", value: "NY")
currentUser?.setUserAttribute("zip", value: "123456")
currentUser?.setUserAttribute("dob", value: "yyyymmdd")
currentUser?.setUserAttribute("title", value: "Mr")
currentUser?.setUserAttribute("language", value: "en")
currentUser?.setUserAttribute("value", value: "52.25")
currentUser?.setUserAttribute("predictedltv", value: "136.23")
// ユーザー属性を作成して、値のリストを含めることができます
currentUser?.setUserAttributeList("favorite-genres", values: ["documentary", "comedy", "romance", "drama"])
// ユーザー属性を削除するには、removeUserAttribute メソッドを呼び出し、属性名を渡します。すべてのユーザー属性は同じキー空間を共有します。
currentUser?.removeAttribute("attribute-to-remove")
#import <mParticle_Apple_SDK/mParticle.h>
// 現在のユーザーを取得します。これは、SDK の初期化時にユーザーを識別した場合、または identify メソッドを呼び出した場合にのみ成功します。
MParticleUser *currentUser = [[[MParticle sharedInstance] identity] currentUser];
// 現在のユーザーを `currentUser` に正常に設定した後、次のようにユーザー属性を設定できます:
[currentUser setUserAttribute:@"custom-attribute-name" value:@"custom-attribute-name"];
// 注: すべてのユーザー属性(リスト属性やタグを含む)には、異なる名前を付ける必要があります。
// Rokt は、可能な限り次のユーザー属性を設定することを推奨しています:
[currentUser setUserAttribute:@"firstname" value:@"John"];
[currentUser setUserAttribute:@"lastname" value:@"Doe"];
// 電話番号は '1234567890' または '+1 (234) 567-8901' の形式でフォーマットできます
[currentUser setUserAttribute:@"mobile" value:@"3125551515"];
[currentUser setUserAttribute:@"age" value:@"33"];
[currentUser setUserAttribute:@"gender" value:@"M"];
[currentUser setUserAttribute:@"city" value:@"Brooklyn"];
[currentUser setUserAttribute:@"state" value:@"NY"];
[currentUser setUserAttribute:@"zip" value:@"123456"];
[currentUser setUserAttribute:@"dob" value:@"yyyymmdd"];
[currentUser setUserAttribute:@"title" value:@"Mr"];
[currentUser setUserAttribute:@"language" value:@"en"];
[currentUser setUserAttribute:@"value" value:@"52.25"];
[currentUser setUserAttribute:@"predictedltv" value:@"136.23"];
// 値のリストを含むユーザー属性を作成することができます
[currentUser setUserAttribute:@"favorite-genres" values:@[@"documentary", @"comedy", @"romance", @"drama"]];
// ユーザー属性を削除するには、removeUserAttribute を呼び出し、属性名を渡します。すべてのユーザー属性は同じキー空間を共有します。
[currentUser removeUserAttribute:@"attribute-to-remove"];