应用内事件

总览

有关面向开发者的应用内事件介绍,请参阅应用内事件

开始之前

您必须对接SDK

Integrate In-App Events with our SDK wizard

记录应用内事件

SDK允许您记录在您的应用程序情景中发生的用户操作。这些通常被称为应用内事件

The logEvent method

The logEvent method lets you log in-app events and send them to AppsFlyer for processing.

To access the logEvent method, import AppsFlyerLib:

import com.appsflyer.AppsFlyerLib;
import com.appsflyer.AppsFlyerLib

To access predefined event constants, import AFInAppEventType and AFInAppEventParameterName:

import com.appsflyer.AFInAppEventType; // Predefined event names
import com.appsflyer.AFInAppEventParameterName; // Predefined parameter names
import com.appsflyer.AFInAppEventType // Predefined event names
import com.appsflyer.AFInAppEventParameterName // Predefined parameter names

logEvent 需要4个参数:

void logEvent(Context context,
              java.lang.String eventName,
              java.util.Map<java.lang.String,java.lang.Object> eventValues,
              AppsFlyerRequestListener listener)
  • The first argument (context) is the Application/Activity Context
  • The second argument (eventName) is the In-app event name
  • The third argument (eventValues) is the event parameters Map
  • The fourth argument (listener) is an optional AppsFlyerRequestListener (useful for Handling event submission success/failure)

Example: Send "add to wishlist" event

例如,记录用户在他们的心愿清单上添加了一件商品:

Map<String, Object> eventValues = new HashMap<String, Object>();
eventValues.put(AFInAppEventParameterName.PRICE, 1234.56);
eventValues.put(AFInAppEventParameterName.CONTENT_ID,"1234567");

AppsFlyerLib.getInstance().logEvent(getApplicationContext(),
                                    AFInAppEventType.ADD_TO_WISHLIST , eventValues);
val eventValues = HashMap<String, Any>() 
eventValues.put(AFInAppEventParameterName.PRICE, 1234.56)
eventValues.put(AFInAppEventParameterName.CONTENT_ID,"1234567")

AppsFlyerLib.getInstance().logEvent(getApplicationContext() ,
                                    AFInAppEventType.ADD_TO_WISHLIST , eventValues)

In the above logEvent invocation:

Implementing event structure definitions

根据了解事件结构定义中提供的示例定义,应按如下方式实现事件:

Map<String, Object> eventValues = new HashMap<String, Object>();
eventValues.put(AFInAppEventParameterName.PRICE, <ITEM_PRICE>);
eventValues.put(AFInAppEventParameterName.CONTENT_TYPE, <ITEM_TYPE>);
eventValues.put(AFInAppEventParameterName.CONTENT_ID, <ITEM_SKU>);

AppsFlyerLib.getInstance().logEvent(getApplicationContext(),
                                    AFInAppEventType.CONTENT_VIEW, eventValues);
val eventValues = HashMap<String, Any>() 
eventValues.put(AFInAppEventParameterName.PRICE, <ITEM_PRICE>)
eventValues.put(AFInAppEventParameterName.CONTENT_TYPE, <ITEM_TYPE>)
eventValues.put(AFInAppEventParameterName.CONTENT_ID, <ITEM_SKU>)

AppsFlyerLib.getInstance().logEvent(getApplicationContext(),
                                    AFInAppEventType.CONTENT_VIEW, eventValues)

Handling event submission success and failure

You can provide logEvent with a AppsFlyerRequestListener object when recording in-app events. The handler allows you to define logic for two scenarios:

  • 成功记录应用内事件
  • 记录应用内事件时出错
AppsFlyerLib.getInstance().logEvent(getApplicationContext(),
                                    AFInAppEventType.PURCHASE,
                                    eventValues,
                                    new AppsFlyerRequestListener() {
                    @Override
                    public void onSuccess() {
                        Log.d(LOG_TAG, "Event sent successfully");
                    }
                    @Override
                    public void onError(int i, @NonNull String s) {
                        Log.d(LOG_TAG, "Event failed to be sent:\n" +
                                "Error code: " + i + "\n"
                                + "Error description: " + s);
                    }
                });
AppsFlyerLib.getInstance().logEvent(getApplicationContext(),
                                    AFInAppEventType.PURCHASE,
                                    eventValues,
                                    object : AppsFlyerRequestListener {
            override fun onSuccess() {
                Log.d(LOG_TAG, "Event sent successfully")
            }
            override fun onError(errorCode: Int, errorDesc: String) {
                Log.d(LOG_TAG, "Event failed to be sent:\n" +
                        "Error code: " + errorCode + "\n"
                        + "Error description: " + errorDesc)
            }
        })

如果在记录应用内事件的时候发生错误,错误代码和字符串说明如下,请参考:

错误代码说明(NSError)
10"事件超时,检查'minTimeBetweenSessions'参数"
11“正在跳过事件,因为启用了'isStopTracking'”
40网络错误:错误说明来自Android
41“没有dev key”
50"Status code failure"(状态码错误)+ 实际服务器响应码

Recording offline events

SDK可以记录在没有互联网连接时发生的事件。详情参阅离线应用内事件

Logging events before calling start

If you initialized the SDK but didn't call start, the SDK will cache in-app events until start is invoked.

如果缓存中有多个事件,则会将它们一个接一个地发送到服务器(不分批,每个事件一个广告平台请求)。

记录收入

You can send revenue with any in-app event. Use the AFInAppEventParameterName.REVENUE event parameter 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

Map<String, Object> eventValues = new HashMap<String, Object>();
eventValues.put(AFInAppEventParameterName.CONTENT_ID, <ITEM_SKU>);
eventValues.put(AFInAppEventParameterName.CONTENT_TYPE, <ITEM_TYPE>);
eventValues.put(AFInAppEventParameterName.REVENUE, 200);

AppsFlyerLib.getInstance().logEvent(getApplicationContext(), 
                                    AFInAppEventType.PURCHASE, eventValues);
val eventValues = HashMap<String, Any>() 
eventValues.put(AFInAppEventParameterName.CONTENT_ID, <ITEM_SKU>)
eventValues.put(AFInAppEventParameterName.CONTENT_TYPE, <ITEM_TYPE>)
eventValues.put(AFInAppEventParameterName.REVENUE, 200)

AppsFlyerLib.getInstance().logEvent(getApplicationContext(), 
                                    AFInAppEventType.PURCHASE, eventValues)

上述purchase event包含200美金收入,会作为收入显示在Dashboard面板上。

📘

注意

不要在收入值中添加货币符号。

Configuring revenue currency

You can set the currency code for an event's revenue by using the af_currency predefined event parameter:

Map<String, Object> eventValues = new HashMap<String, Object>();
eventValues.put(AFInAppEventParameterName.CURRENCY, "USD");
eventValues.put(AFInAppEventParameterName.REVENUE, <TRANSACTION_REVENUE>);
AppsFlyerLib.getInstance().logEvent(getApplicationContext(), 
                                    AFInAppEventType.PURCHASE, eventValues);
val eventValues = HashMap<String, Any>() 
eventValues.put(AFInAppEventParameterName.REVENUE, <TRANSACTION_REVENUE>)
eventValues.put(AFInAppEventParameterName.CURRENCY,"USD")
AppsFlyerLib.getInstance().logEvent(getApplicationContext() , AFInAppEventType.PURCHASE , eventValues)
  • 货币代码应为3个字符的ISO 4217代码
  • 默认货币为美元

To learn about currency settings, display, and currency conversion, see our guide on revenue currency.

Logging negative revenue

在某些情况下,您想记录负收入。 例如,一名用户收到了一笔退款或者取消了订阅。

记录负收入:

Map<String, Object> eventValues = new HashMap<String, Object>();
eventValues.put(AFInAppEventParameterName.REVENUE, -1234.56);
eventValues.put(AFInAppEventParameterName.CONTENT_ID,"1234567");
AppsFlyerLib.getInstance().logEvent(getApplicationContext(),
                                    "cancel_purchase",
                                    eventValues);
val eventValues = HashMap<String, Any>() 
eventValues.put(AFInAppEventParameterName.REVENUE, -1234.56)
eventValues.put(AFInAppEventParameterName.CONTENT_ID,"1234567")
AppsFlyerLib.getInstance().logEvent(getApplicationContext(),
                                    "cancel_purchase",
                                    eventValues)

📘

注意

请注意上面代码中的以下内容:

  • 收入值前面有一个减号
  • 事件名称是一个名为“cancel_purchase”的自定义事件名称 - 它帮助您在控制面板和原始数据报告中轻松识别负收入事件

验证购买

AppsFlyer provides server verification for in-app purchases. For more information see Validate and log purchase

事件常量

Predefined event names

要使用以下常量,请导入com.appsflyer.AFInAppEventType:

import com.appsflyer.AFInAppEventType;
import com.appsflyer.AFInAppEventType

Predefined event name constants follow a AFInAppEventType.EVENT_NAME naming convention. For example, AFInAppEventType.ADD_TO_CART

事件名称Android常量名称
"af_level_achieved"
AFInAppEventType.LEVEL_ACHIEVED
"af_add_payment_info"
AFInAppEventType.ADD_PAYMENT_INFO
"af_add_to_cart"
AFInAppEventType.ADD_TO_CART
"af_add_to_wishlist"
AFInAppEventType.ADD_TO_WISHLIST
"af_complete_registration"
AFInAppEventType.COMPLETE_REGISTRATION
"af_tutorial_completion"
AFInAppEventType.TUTORIAL_COMPLETION
"af_initiated_checkout"
AFInAppEventType.INITIATED_CHECKOUT
"af_purchase"
AFInAppEventType.PURCHASE
"af_rate"
AFInAppEventType.RATE
"af_search"
"af_spent_credits"
AFInAppEventType.SPENT_CREDITS
"af_achievement_unlocked"
AFInAppEventType.ACHIEVEMENT_UNLOCKED
"af_content_view"
AFInAppEventType.CONTENT_VIEW
"af_list_view"
AFInAppEventType.LIST_VIEW
"af_travel_booking"
AFInAppEventType.TRAVEL_BOOKING
"af_share"
AFInAppEventType.SHARE
"af_invite"
AFInAppEventType.INVITE
"af_login"
AFInAppEventType.LOGIN
"af_re_engage"
AFInAppEventType.RE_ENGAGE
"af_update"
AFInAppEventType.UPDATE
"af_location_coordinates"
AFInAppEventType.LOCATION_COORDINATES
"af_customer_segment"
AFInAppEventType.CUSTOMER_SEGMENT
"af_subscribe"
AFInAppEventType.SUBSCRIBE
"af_start_trial"
AFInAppEventType.START_TRIAL
"af_ad_click"
AFInAppEventType.AD_CLICK
"af_ad_view"
AFInAppEventType.AD_VIEW
"af_opened_from_push_notification"
AFInAppEventType.OPENED_FROM_PUSH_NOTIFICATION

Predefined event parameters

To use the following constants, import AFInAppEventParameterName:

import com.appsflyer.AFInAppEventParameterName;
import com.appsflyer.AFInAppEventParameterName

Predefined event parameter constants follow a AFInAppEventParameterName.PARAMETER_NAME naming convention. For example, AFInAppEventParameterName.CURRENCY

事件参数名称Android常量名称类型
"af_content"
CONTENT
String[]
"af_achievement_id"
ACHIEVEMENT_ID
String
"af_level"
LEVEL
String
"af_score"
SCORE
String
"af_success"
SUCCESS
String
"af_price"
PRICE
float
"af_content_type"
CONTENT_TYPE
String
"af_content_id"
CONTENT_ID
String
"af_content_list"
CONTENT_LIST
String[]
"af_currency"
CURRENCY
String
"af_quantity"
QUANTITY
int
"af_registration_method"
REGISTRATION_METHOD
String
"af_payment_info_available"
PAYMENT_INFO_AVAILABLE
String
"af_max_rating_value"
MAX_RATING_VALUE
String
"af_rating_value"
RATING_VALUE
String
"af_search_string"
SEARCH_STRING
String
"af_date_a"
DATE_A
String
"af_date_b"
DATE_B
String
"af_destination_a"
DESTINATION_A
String
"af_destination_b"
DESTINATION_B
String
"af_description"
DESCRIPTION
String
"af_class"
CLASS
String
"af_event_start"
EVENT_START
String
"af_event_end"
EVENT_END
String
"af_lat"
LAT
String
"af_long"
LONG
String
"af_customer_user_id"
CUSTOMER_USER_ID
String
"af_validated"
VALIDATED
boolean
"af_revenue"
REVENUE
float
"af_projected_revenue"
PROJECTED_REVENUE
float
"af_receipt_id"
RECEIPT_ID
String
"af_tutorial_id"
TUTORIAL_ID
String
"af_virtual_currency_name"
VIRTUAL_CURRENCY_NAME
String
"af_deep_link"String
"af_old_version"
OLD_VERSION
String
"af_new_version"
NEW_VERSION
String
"af_review_text"
REVIEW_TEXT
String
"af_coupon_code"
COUPON_CODE
String
"af_order_id"
ORDER_ID
String
"af_param_1"
PARAM_1
String
"af_param_2"
PARAM_2
String
"af_param_3"
PARAM_3
String
"af_param_4"
PARAM_4
String
"af_param_5"
PARAM_5
String
"af_param_6"
PARAM_6
String
"af_param_7"
PARAM_7
String
"af_param_8"
PARAM_8
String
"af_param_9"
PARAM_9
String
"af_param_10"
PARAM_10
String
"af_departing_departure_date"
DEPARTING_DEPARTURE_DATE
String
"af_returning_departure_date"
RETURNING_DEPARTURE_DATE
String
"af_destination_list"
DESTINATION_LIST
String[]
"af_city"
CITY
String
"af_region"
REGION
String
"af_country"
COUNTRY
String
"af_departing_arrival_date"
DEPARTING_ARRIVAL_DATE
String
"af_returning_arrival_date"
RETURNING_ARRIVAL_DATE
String
"af_suggested_destinations"
SUGGESTED_DESTINATIONS
String[]
"af_travel_start"
TRAVEL_START
String
"af_travel_end"
TRAVEL_END
String
"af_num_adults"
NUM_ADULTS
String
"af_num_children"
NUM_CHILDREN
String
"af_num_infants"
NUM_INFANTS
String
"af_suggested_hotels"
SUGGESTED_HOTELS
String[]
"af_user_score"
USER_SCORE
String
"af_hotel_score"
HOTEL_SCORE
String
"af_purchase_currency"
PURCHASE_CURRENCY
String
"af_preferred_neighborhoods"
PREFERRED_NEIGHBORHOODS
String[]
"af_preferred_num_stops"
PREFERRED_NUM_STOPS
String
"af_adrev_ad_type"
AD_REVENUE_AD_TYPE
String
"af_adrev_network_name"
AD_REVENUE_NETWORK_NAME
String
"af_adrev_placement_id"
AD_REVENUE_PLACEMENT_ID
String
"af_adrev_ad_size"
AD_REVENUE_AD_SIZE
String
"af_adrev_mediated_network_name"
AD_REVENUE_MEDIATED_NETWORK_NAME
String
"af_preferred_price_range"
PREFERRED_PRICE_RANGE
String, int元组格式为 (min,max)
"af_preferred_star_ratings"
PREFERRED_STAR_RATINGS
String, int元组格式为 (min,max)