iOS SDK インテグレーションガイド
このページでは、Rokt Ecommerce の iOS SDK を実装して、チェックアウト時により関連性の高い顧客体験を提供する方法を説明します。SDK を使用すると、設定されたページで発火し、ユーザーおよびトランザクションデータを Rokt に返すことで、これらの体験(確認ページでのオファー表示など)をトリガーおよび追跡し、パーソナライズおよび測定を行うことができます。
専任のアカウント担当者が、iOS SDK のアカウント設定をサポートします。彼らは、SDK を初期化するために必要な key と secret、および顧客に最も関連性の高い体験を提供するために必要な追加リソースを提供します。
これらの指示を完了するには、開発リソースが必要です。追加のサポートが必要な場合は、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」を「Up to Next Major Version」に変更します。 - 右下の「Add Package」ボタンをクリックし、「Package Product」として
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の代わりにimport mParticle_Apple_SDK_NoLocationを使用して 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")
// データ環境をenvironmentで指定する:
// 統合をテスト中の場合は.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"];
// データ環境をenvironmentで指定する:
// 統合をテスト中の場合は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 {
//失敗を処理する - 下記の「エラーハンドリング」セクションを参照
}
}
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 を参照
}
};
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は、特定のセッションで収集されたユーザー属性を、同じセッションでトリガーされたイベントと関連付けます。
ユーザー属性を収集するには、Rokt 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"];