iOS SDK インテグレーションガイド
このページでは、Rokt Ads の iOS SDK を実装して、チェックアウト時により関連性の高い顧客体験を提供する方法を説明します。SDK を使用すると、設定されたページで発火し、ユー ザーとトランザクションデータを Rokt に渡してパーソナライズと測定を行うことにより、これらの体験(確認ページでのオファー表示など)をトリガーおよび追跡できます。
専任のアカウント担当者が、iOS SDK のアカウント設定をサポートします。彼らは、SDK を初期化するために必要な key と secret、および顧客に最も関連性の高い体験を提供するために必要な追加リソースを提供します。
これらの手順を完了するには、開発リソースが必要です。追加のサポートが必要な場合は、Rokt のアカウントマネージャーにお問い合わせください。
1. Rokt SDK を iOS アプリに追加する
SPM または CocoaPods のいずれかを使用して、ア プリケーションに Rokt SDK を含めます:
CocoaPods
SDKをCocoaPodsを使用して統合するには、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_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
}
#```objective-c
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 = MPIdentityApiRequest.withEmptyUser()
identifyRequest.email = "j.smith@example.com"
MPIdentityApiRequest *identifyRequest = [MPIdentityApiRequest 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 = MPIdentityApiRequest.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)
MPIdentityApiRequest *identifyRequest = [MPIdentityApiRequest 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"];
5. 画面ビューのキャプチャ
追跡できる最も基本的なイベントタイプの1つが、画面ビューです。
Rokt SDKと初めて統合する際には、まず画面ビューの追跡を実装してください。カスタムイベントやコマースイベントの追跡については、付録を参照してください。
画面ビューの追跡
追跡できる最も基本的なイベントタイプの1つが、画面ビューです。画面ビューをログに記録するには、.logScreen メソッドを呼び出し、画面の名前を文字列として渡します。
渡す名前は、'homepage' や 'product detail page' のような限られたページセットの1つであるべきです。また、eventInfo 配列に追加のカスタム属性を含めることもできます。
- Swift
- Objective-C
MParticle.sharedInstance().logScreen(
"homepage",
eventInfo: ["custom-attribute": customValue]
)
[[MParticle sharedInstance] logScreen:@"homepage"
eventInfo:@{@"custom-attribute": customValue}];
6. コンバージョンのトラッキング
コンバージョンをトラッキングするには、顧客がコンバートした後に読み込まれる最も適切なページ(購入確認ページや「ありがとう」ページなど)で、次のコードスニペットを実行します。
コードスニペットをサイトに貼り付ける際には、以下の点に注意してください:
setUserAttribute呼び出しのサンプルユーザー属性を、ユーザーまたは顧客の実際の値に置き換えます。Roktは少なくとも以下のユーザー属性を設定することを推奨します:firstnamelastnamezipcodemobile
- サンプルのコンバージョンイベント属性を、コンバージョンイベントからの実際の値に置き換えます。
コンバージョンイベントをログに記録する際には、できるだけ多くのユーザー属性とイベント属性を含めることで、Roktがキャンペーンを最適化する能力を向上させること ができます。
- Swift
- Objective-C
let currentUser = MParticle.sharedInstance().identity.currentUser
currentUser?.setUserAttribute("firstname", value: "John")
currentUser?.setUserAttribute("lastname", value: "Doe")
currentUser?.setUserAttribute("zipcode", value: "98103")
currentUser?.setUserAttribute("mobile", value: "3125551515")
if let event = MPEvent(name: "conversion", type: .transaction) {
event.customAttributes = [
"conversiontype": "signup", // コンバージョンのタイプ
"confirmationref": "54321", // トランザクションID / 注文ID
"amount": "", // トランザクションの金額 例: 300.5
"currency": "", // トランザクションの通貨 例: USD
// 独自のカスタムイベント属性をトラッキングできます!
"CUSTOM_EVENT_ATTRIBUTE_NAME" : "CUSTOM_EVENT_ATTRIBUTE_VALUE"
]
MParticle.sharedInstance().logEvent(event)
}
MParticleUser *currentUser = [[[MParticle sharedInstance] identity] currentUser];
[currentUser setUserAttribute:@"firstname" value:@"John"];
[currentUser setUserAttribute:@"lastname" value:@"Doe"];
[currentUser setUserAttribute:@"zip" value:@"98103"];
[currentUser setUserAttribute:@"mobile" value:@"3125551515"];
MPEvent *event = [[MPEvent alloc] initWithName:@"conversion" type:MPEventTypeTransaction];
```objectivec
if (event) {
event.customAttributes = @{
@"conversiontype": @"signup", // コンバージョンの種類
@"confirmationref": @"54321", // トランザクションID / 注文ID
@"amount": @"", // トランザクション金額 例: 300.5
@"currency": @"", // トランザクション通貨 例: USD
// 独自のカスタムイベント属性を追跡できます!
@"CUSTOM_EVENT_ATTRIBUTE_NAME" : @"CUSTOM_EVENT_ATTRIBUTE_VALUE"
};
[[MParticle sharedInstance] logEvent:event];
}
付録
カスタムイベントを追跡する
logEvent を呼び出し、イベント名、イベントタイプ、およびイベントの任意のカスタム属性を含む配列を渡すことで、カスタム イベントを定義および追跡できます。
- Swift
- Objective-C
if let event = MPEvent(name: "Video Watched", type: .navigation) {
event.customAttributes = ["category": "Destination Intro", "title": "Paris"]
MParticle.sharedInstance().logEvent(event)
}
MPEvent *event = [[MPEvent alloc] initWithName:@"Video Watched" type:MPEventTypeNavigation];
if (event) {
event.customAttributes = @{
@"category": @"Destination Intro",
@"title": @"Paris"
};
[[MParticle sharedInstance] logEvent:event];
}
サポートされているカスタムイベントタイプは次のとおりです:
- Swift
- Objective-C
navigation- アプリ内でのユーザーのナビゲーションフローやページ遷移を追跡location- ユーザーの位置情報に基づくインタラクションや移動を記録search- 検索クエリや検索関連のユーザーアクションをキャプチャtransaction- 金銭取引や購入関連の活動をログuserContent- レビュー、コメント、投稿などのユーザー生成コンテンツを追跡userPreference- ユーザーの設定、好み、カスタマイズの選択を記録social- ソーシャルメディアのインタラクションや共有活動をキャプチャ
MPEventTypeNavigation- アプリ内でのユーザーのナビゲーションフローやページ遷移をトラッキングMPEventTypeLocation- ユーザーの位置情報に基づくインタラクションや移動を記録MPEventTypeSearch- 検索クエリや検索関連のユーザーアクションをキャプチャMPEventTypeTransaction- 金銭取引や購入関連のアクティビティをログMPEventTypeUserContent- レビュー、コメント、投稿などのユーザー生成コンテンツをトラッキングMPEventTypeUserPreference- ユーザー設定、好み、カスタマイズ選択を記録MPEventTypeSocial- ソーシャルメディアでのインタラクションや共有アクティビティをキャプチャ
コマースイベントをトラッキング
コマースイベントをトラッキングするには、以下の3つのステップが必要です:
MPProduct.initを使用して購入される商品を変数内で定義- トランザクションを要約するためのMPTransactionAttributesオブジェクトを作成
- 商品アクションとイベントのタイプを定義し、
logEventメソッドを呼び出し、商品定義とトランザクションの要約を含める
- Swift
- Objective-C
// 1. Define the product(s)
let product = MPProduct.init(name: "Double Room",
sku: "econ-1",
quantity: 4,
price: 100.00)
// 2. Summarize the transaction
let attributes = MPTransactionAttributes.init()
attributes.transactionId = "foo-transaction-id"
attributes.revenue = 430.00
attributes.tax = 30.00
// 3. Log the commerce event.
// Several types of "actions" are supported, such as AddToCart and Checkout. This example uses the Purchase product action.
let action = MPCommerceEventAction.purchase;
let event = MPCommerceEvent.init(action: action, product: product)
event.transactionAttributes = attributes
MParticle.sharedInstance().logEvent(event)
// 1. Define the product(s)
MPProduct *product = [[MPProduct alloc] initWithName:@"Double Room"
sku:@"econ-1"
quantity:@4
price:@100.00];
// 2. Summarize the transaction
MPTransactionAttributes *attributes = [[MPTransactionAttributes alloc] init];
attributes.transactionId = @"foo-transaction-id";
attributes.revenue = @430.00;
attributes.tax = @30.00;
// 3. コマースイベントをログに記録する
MPCommerceEvent *event = [[MPCommerceEvent alloc] initWithAction:MPCommerceEventActionPurchase
product:product];
event.transactionAttributes = attributes;
[[MParticle sharedInstance] logEvent:event];
商品アクションをログに記録する際には、以下のいずれかのプロダクトアクションタイプを指定する必要があります:
- Swift
- Objective-C
AddToCartRemoveFromCartCheckoutCheckoutOptionClickViewDetailPurchaseRefundAddToWishlistRemoveFromWishlistUnknown
MPCommerceEventActionAddToCartMPCommerceEventActionRemoveFromCartMPCommerceEventActionCheckoutMPCommerceEventActionCheckoutOptionMPCommerceEventActionClickMPCommerceEventActionViewDetailMPCommerceEventActionPurchaseMPCommerceEventActionRefundMPCommerceEventActionAddToWishlistMPCommerceEventActionRemoveFromWishlistMPCommerceEventActionUnknown