アプリ内イベント
iOS SDKでアプリ内イベントを設定する方法を説明します。
概要
本ドキュメントは、iOS SDKでアプリ内イベントを実装するためのガイドです。開発者向けのアプリ内イベントについての概要については、 アプリ内イベント をご覧ください。
始める前に
SDKを実装する必要があります。
アプリ内イベントの記録
SDKでは、アプリで発生したユーザーアクションを記録できます。これは、一般的にアプリ内イベントと呼ばれます。
The logEvent
method
logEvent
methodThe logEvent
method lets you log in-app events and send them to AppsFlyer for processing.
AppsFlyerLib
exposes logEvent
, predefined event name constants and predefined event parameter constants.
logEvent
は3つの引数を取ります。
- (void)logEventWithEventName:(NSString *)eventName
eventValues:(NSDictionary<NSString * , id> * _Nullable)eventValues
completionHandler:(void (^ _Nullable)(NSDictionary<NSString *, id> * _Nullable dictionary, NSError * _Nullable error))completionHandler;
- The first argument (
eventName
) is the event name - The second argument (
eventValues
) is the event parametersNSDictionary
- The third argument (
completionHandler
) is an optional completion handler (useful for Handling event submission success/failure)
注意
eventValues
(second argument) must be a validNSDictionary
. For more information, see Foundation JSONSerialization.
Example: Send "add to wishlist" event
例えば、ユーザーが商品をウィッシュリストに追加したことを記録する方法は次のとおりです:
[[AppsFlyerLib shared] logEvent: AFEventAddToWishlist withValues: @{
AFEventParamPrice: @20,
AFEventParamContentId: @"123456"
}]
AppsFlyerLib.shared().logEvent(AFEventAddToWishlist,
withValues: [
AFEventParamPrice: 20,
AFEventParamContentId: "123456"
]);
In the above logEvent
invocation:
- イベント名は、
AFEventAddToWishlist
- The event value is a
NSDictionary
containing these event parameters:- AFEventParamPrice:ユーザーがウィッシュリストに追加した商品の価格
- AFEventParamContentId:追加された商品の識別子
Implementing in-app event definitions
イベント構成の定義にて提供されている定義の例に基づいて、イベントは次のように実装する必要があります。
[[AppsFlyerLib shared] logEvent: AFEventContentView withValues: @{
AFEventParamContentId: <ITEM_SKU>,
AFEventParamContentType: <ITEM_TYPE>,
AFEventParamPrice: <ITEM_PRICE>
}]
AppsFlyerLib.shared().logEvent(AFEventAddToCart,
withValues: [
AFEventParamContent: <ITEM_NAME>
AFEventParamContentId: <ITEM_SKU>
AFEventParamPrice: <ITEM_PRICE>
]);
Handling event submission success and failure
You can pass a completionHandler
to logEvent
when recording in-app events. The handler allows you to define logic for two scenarios:
- アプリ内イベントが正常に記録された
- アプリ内イベントの記録中にエラーが発生しました
[[AppsFlyerLib shared] logEventWithEventName:AFEventPurchase
eventValues: @{
AFEventParamRevenue: @200,
AFEventParamCurrency: @"USD",
AFEventParamQuantity: @2,
AFEventParamContentId: @"092",
AFEventParamReceiptId: @"9277"
}
completionHandler:^(NSDictionary<NSString *,id> * _Nullable dictionary, NSError * _Nullable error){
if(dictionary != nil) {
NSLog(@"In app callback success:");
for(id key in dictionary){
NSLog(@"Callback response: key=%@ value=%@", key, [dictionary objectForKey:key]);
}
}
if(error != nil) {
NSLog(@"In app callback error:", error);
}
}];
AppsFlyerLib.shared().logEvent(name: "In app event name", values: ["id": 12345, "name": "John doe"], completionHandler: { (response: [String : Any]?, error: Error?) in
if let response = response {
print("In app event callback Success: ", response)
}
if let error = error {
print("In app event callback ERROR:", error)
}
})
}
アプリ内イベントの記録時にエラーが発生した場合には、次の表のようにエラーコードと文字列での説明が提供されます。
エラーコード | 説明 (NSError) |
---|---|
10 | "Event timeout. Check 'minTimeBetweenSessions' param" |
11 | "Skipping event because 'isStopTracking' enabled" |
40 | ネットワークエラー:Android経由のエラーの説明 |
41 | "No dev key" |
50 | "Status code failure" + サーバーからの実際のエラーコード |
Recording offline events
SDKはインターネットに接続されていないときに発生するイベントを記録することができます。詳細はオフライン時のアプリ内イベントを参照してください。
Logging events before calling start
start
If you initialized the SDK but didn't call start
, the SDK will cache in-app events until start
is invoked.
キャッシュに複数のイベントがある場合は、それらは次々とサーバーに送信されます(非バッチ、イベント毎に1つのネットワークリエスト)。
注意
If the SDK is initialized,
logEvent
invocations will call SKAdNetwork'supdateConversionValue
even ifstart
wasn't called orisStopped
is set totrue
(in both SDK and S2S modes).
収益の記録
af_revenue
is the only event parameter that AppsFlyer counts as real revenue in the dashboard and reports.
You can send revenue with any in-app event. Use the AFEventParameterRevenue
constant to include revenue in the in-app event. You can populate it with any numeric value, positive or negative.
コンマの値区切り、通貨記号、テキストなどは収益の値に含めないでください。たとえば、収益の値は「1234.56」のように設定してください。
Example: Purchase event with revenue
[[AppsFlyerLib shared] logEvent: AFEventPurchase
withValues:@{
AFEventParamContentId:@"1234567",
AFEventParamContentType : @"category_a",
AFEventParamRevenue: @200,
AFEventParamCurrency:@"USD"
}];
AppsFlyerLib.shared().logEvent(AFEventPurchase,
withValues: [
AFEventParamContentId:"1234567",
AFEventParamContentType : "category_a",
AFEventParamRevenue: 200,
AFEventParamCurrency:"USD"
]);
注意
収益値 (REVENUE) に通貨記号を含めないでください。数値のみを挿入してください。
Configuring revenue currency
You can set the currency code for an event's revenue by using the af_currency
predefined event parameter:
[[AppsFlyerLib shared] logEvent: AFEventPurchase
withValues:@{
AFEventParamRevenue: @200,
AFEventParamCurrency:@"USD"
}];
AppsFlyerLib.shared().logEvent(AFEventPurchase,
withValues: [
AFEventParamRevenue: 200,
AFEventParamCurrency:"USD"
]);
- 通貨コードは3桁のISO 4217コードを使用してください。
- デフォルトの通貨はUSDです
通貨単位の設定、管理画面上での表示、通貨換算については、収益通貨に関するガイドをご覧ください。
Logging negative revenue
稀に、マイナスの収益を計測したいケースがあるかと思います。たとえば、ユーザーは払い戻しを受け取ったり、サブスクリプションをキャンセルしたときです。
マイナスの収益を記録する方法:
[[AppsFlyerLib shared] logEvent: @"cancel_purchase"
withValues:@{
AFEventParamContentId:@"1234567",
AFEventParamContentType : @"category_a",
AFEventParamRevenue: @-1.99,
AFEventParamCurrency:@"USD"
}];
AppsFlyerLib.shared().logEvent("cancel_purchase",
withValues: [
AFEventParamContentId:"1234567",
AFEventParamContentType : "category_a",
AFEventParamRevenue: -1.99,
AFEventParamCurrency:"USD"
]);
上記のコードについて次のことに注意してください。
- 収益値の前にはマイナス記号が追加されています
- The event name is a custom event called
cancel_purchase
- that's how the marketer identifies negative revenue events in the dashboard and raw-data reports
購入の検証
AppsFlyer provides server verification for in-app purchases. The validateAndLogInAppPurchase
method takes care of validating and logging the purchase event.
Purchase validation using validateAndLogInAppPurchase
validateAndLoginInAppPurchase
はこれらの引数を取ります:
- (void) validateAndLogInAppPurchase:(NSString *) productIdentifier,
price:(NSString *) price
currency:(NSString *) currency
transactionId:(NSString *) tranactionId
additionalParameters:(NSDictionary *) params
success:(void (^)(NSDictionary *response)) successBlock
failure:(void (^)(NSError *error, id reponse)) failedBlock;
validateAndLog(inAppPurchase: String?,
price: String?,
currency: String?,
transactionId: String?,
additionalParameters: [AnyHashable : Any]?,
success: ([AnyHashable : Any]) -> Void)?,
failure: ((Error?, Any?) -> Void)?)
Upon successful validation, a NSDictionary
is returned with the receipt validation data (provided by Apple servers).
注意
Calling
validateAndLogInAppPurchase
generates anaf_purchase
in-app event upon successful validation. Sending this event yourself creates duplicate event reporting.
Example: Validate in-app purchase
[[AppsFlyerLib shared] validateAndLogInAppPurchase:@"ProductIdentifier" price:@"price"
currency:@"USD"
transactionId:@"transactionID"
additionalParameters:@{@"test": @"val" , @"test1" : @"val 1"}
success:^(NSDictionary *result){
NSLog(@"Purchase succeeded And verified! response: %@", result[@"receipt"]);
} failure:^(NSError *error, id response) {
NSLog(@"response = %@", response);
if([response isKindOfClass:[NSDictionary class]]) {
if([response[@"status"] isEqualToString:@"in_app_arr_empty"]){
// retry with 'SKReceiptRefreshRequest' because
// Apple has returned an empty response
// <YOUR CODE HERE>
}
} else {
//handle other errors
return;
}
}];
AppsFlyerLib.shared().validateAndLogInAppPurchase (
inAppPurchase: "productIdentifier",
price: "price",
currency: "currency",
transactionId: "transactionId",
additionalParameters: [:],
success: {
guard let dictionary = $0 as? [String:Any] else { return }
dump(dictionary)
},
failure: { error, result in
guard let emptyInApp = result as? [String:Any],
let status = emptyInApp["status"] as? String,
status == "in_app_arr_empty" else {
// Try to handle other errors
return
}
})
サンドボックスモードでの購入検証テスト
サンドボックス環境で購入検証を行うには、以下のコードを追加してください:
[AppsFlyerLib shared].useReceiptValidationSandbox = YES;
AppsFlyerLib.shared().useReceiptValidationSandbox = true
注意
このコードは、本番環境のビルドから削除する必要があります。
Validating an in-app purchase automatically generates and sends an in-app purchase event to AppsFlyer. Its eventValues
will look something like this:
{
"some_parameter": "some_value", // from additional_event_values
"af_currency": "USD", // from currency
"af_content_id" :"test_id", // from purchase
"af_revenue": "10", // from revenue
"af_quantity": "1", // from purchase
"af_validated": true // flag that AF verified the purchase
}
イベント定数
Predefined event names
Predefined event name constants follow a AFEventEventName
naming convention. For example, AFEventAddToCart
.
イベント名 | iOS 定数名 |
---|---|
"af_level_achieved" | AFEventLevelAchieved |
"af_add_payment_info" | AFEventAddPaymentInfo |
"af_add_to_cart" | AFEventAddToCart |
"af_add_to_wishlist" | AFEventAddToWishlist |
"af_complete_registration" | AFEventCompleteRegistration |
"af_tutorial_completion" | AFEventTutorial_completion |
"af_initiated_checkout" | AFEventInitiatedCheckout |
"af_purchase" | AFEventPurchase |
"af_rate" | AFEventRate |
"af_search" | AFEventSearch |
"af_spent_credits" | AFEventSpentCredits |
"af_achievement_unlocked" | AFEventAchievementUnlocked |
"af_content_view" | AFEventContentView |
"af_list_view" | AFEventListView |
"af_travel_booking" | AFEventTravelBooking |
AFEventShare | |
"af_invite" | AFEventInvite |
"af_login" | AFEventLogin |
"af_re_engage" | AFEventReEngage |
"af_update" | AFEventUpdate |
"af_opened_from_push_notification" | AFEventOpenedFromPushNotification |
"af_location_coordinates" | AFEventLocation |
"af_customer_segment" | AFEventCustomerSegment |
"af_subscribe" | AFEventSubscribe |
"af_start_trial" | AFEventStartTrial |
"af_ad_click" | AFEventAdClick |
"af_ad_view" | AFEventAdView |
Predefined event parameters
Predefined event parameter constants follow a AFEventParamParameterName
naming convention. For example, AFEventParamRevenue
.
イベントパラメーター名 | iOS 定数名 |
---|---|
"af_content" | AFEventParamContent |
"af_achievement_id" | AFEventParamAchievementId |
"af_level" | AFEventParamLevel |
"af_score" | AFEventParamScore |
"af_success" | AFEventParamSuccess |
"af_price" | AFEventParamPrice |
"af_content_type" | AFEventParamContentType |
"af_content_id" | AFEventParamContentId |
"af_content_list" | AFEventParamContentList |
"af_currency" | AFEventParamCurrency |
"af_quantity" | AFEventParamQuantity |
"af_registration_method" | AFEventParamRegistrationMethod |
"af_payment_info_available" | AFEventParamPaymentInfoAvailable |
"af_max_rating_value" | AFEventParamMaxRatingValue |
"af_rating_value" | AFEventParamRatingValue |
"af_search_string" | AFEventParamSearchString |
"af_date_a" | AFEventParamDateA |
"af_date_b" | AFEventParamDateB |
"af_destination_a" | AFEventParamDestinationA |
"af_destination_b" | AFEventParamDestinationB |
"af_description" | AFEventParamDescription |
"af_class" | AFEventParamClass |
"af_event_start" | AFEventParamEventStart |
"af_event_end" | AFEventParamEventEnd |
"af_lat" | AFEventParamLat |
"af_long" | AFEventParamLong |
"af_customer_user_id" | AFEventParamCustomerUserId |
"af_validated" | AFEventParamValidated |
"af_revenue" | AFEventParamRevenue |
"af_projected_revenue" | AFEventProjectedParamRevenue |
"af_receipt_id" | AFEventParamReceiptId |
"af_tutorial_id" | AFEventParamTutorialId |
"af_virtual_currency_name" | AFEventParamVirtualCurrencyName |
"af_deep_link" | AFEventParamDeepLink |
"af_old_version" | AFEventParamOldVersion |
"af_new_version" | AFEventParamNewVersion |
"af_review_text" | AFEventParamReviewText |
"af_coupon_code" | AFEventParamCouponCode |
"af_order_id" | AFEventParamOrderId |
"af_param_1" | AFEventParam1 |
"af_param_2" | AFEventParam2 |
"af_param_3" | AFEventParam3 |
"af_param_4" | AFEventParam4 |
"af_param_5" | AFEventParam5 |
"af_param_6" | AFEventParam6 |
"af_param_7" | AFEventParam7 |
"af_param_8" | AFEventParam8 |
"af_param_9" | AFEventParam9 |
"af_param_10" | AFEventParam10 |
"af_departing_departure_date" | AFEventParamDepartingDepartureDate |
"af_returning_departure_date" | AFEventParamReturningDepartureDate |
"af_destination_list" | AFEventParamDestinationList //array of string |
"af_city" | AFEventParamCity |
"af_region" | AFEventParamRegion |
"af_country" | AFEventParamCountry |
"af_departing_arrival_date" | AFEventParamDepartingArrivalDate |
"af_returning_arrival_date" | AFEventParamReturningArrivalDate |
"af_suggested_destinations" | AFEventParamSuggestedDestinations //array of string |
"af_travel_start" | AFEventParamTravelStart |
"af_travel_end" | AFEventParamTravelEnd |
"af_num_adults" | AFEventParamNumAdults |
"af_num_children" | AFEventParamNumChildren |
"af_num_infants" | AFEventParamNumInfants |
"af_suggested_hotels" | AFEventParamSuggestedHotels //array of string |
"af_user_score" | AFEventParamUserScore |
"af_hotel_score" | AFEventParamHotelScore |
"af_purchase_currency" | AFEventParamPurchaseCurrency |
"af_preferred_star_ratings" | AFEventParamPreferredStarRatings //array of int (basically a tuple (min,max) but we'll use array of int and instruct the developer to use two values" |
"af_preferred_price_range" | AFEventParamPreferredPriceRange //array of int (basically a tuple (min,max) but we'll use array of int and instruct the developer to use two values" |
"af_preferred_neighborhoods" | AFEventParamPreferredNeighborhoods //array of string |
"af_preferred_num_stops" | AFEventParamPreferredNumStops |
"af_adrev_ad_type" | AFEventParamAdRevenueAdType |
"af_adrev_network_name" | AFEventParamAdRevenueNetworkName |
"af_adrev_placement_id" | AFEventParamAdRevenuePlacementId |
"af_adrev_ad_size" | AFEventParamAdRevenueAdSize |
"af_adrev_mediated_network_name" | AFEventParamAdRevenueMediatedNetworkName |
更新済 約1年前