Sự kiện trong Ứng dụng
Tổng quan
Để biết thông tin giới thiệu về các sự kiện trong ứng dụng dành cho nhà phát triển, hãy xem phần Sự kiện trong ứng dụng.
Trước khi bạn bắt đầu
Bạn phải tích hợp SDK.
Ghi nhật ký sự kiện trong ứng dụng
SDK cho phép bạn ghi nhật ký các hành động của người dùng xảy ra trong ngữ cảnh ứng dụng của bạn. Chúng thường được gọi là sự kiện trong ứng dụng.
The logEvent
method
logEvent
methodThe 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
có 4 đối số:
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 parametersMap
- The fourth argument (
listener
) is an optionalAppsFlyerRequestListener
(useful for Handling event submission success/failure)
Example: Send "add to wishlist" event
Ví dụ: để ghi lại rằng một người dùng đã thêm một mục vào danh sách yêu thích của họ:
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:
- Tên sự kiện là
AFInAppEventType.ADD_TO_WISHLIST
- The event value is a
Map
containing these event parameters:- AFInAppEventParameterName.PRICE: Giá liên quan đến sự kiện
- AFInAppEventParameterName.CONTENT_ID: Mã định danh của mục được thêm vào
Implementing event structure definitions
Dựa trên định nghĩa ví dụ được cung cấp trong Tìm hiểu định nghĩa cấu trúc sự kiện, sự kiện phải được triển khai như sau:
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:
- Đã ghi nhận thành công sự kiện trong ứng dụng
- Đã xảy ra lỗi khi ghi nhận sự kiện trong ứng dụng
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)
}
})
Trong trường hợp xảy ra lỗi khi ghi sự kiện trong ứng dụng, mã lỗi và mô tả chuỗi sẽ được cung cấp, như được chỉ ra trong bảng sau.
Mã lỗi | Mô tả (NSError) |
---|---|
10 | "Event timeout. Check 'minTimeBetweenSessions' param" (Hết thời gian chờ của sự kiện. Kiểm tra thông số 'minTimeBetweenSessions') |
11 | "Skipping event because 'isStopTracking' enabled" (Bỏ qua sự kiện vì 'isStopTracking' đã được bật) |
40 | Network error: Error description comes from Android (Lỗi mạng: Mô tả lỗi đến từ Android) |
41 | "No dev key" (Không có dev key) |
50 | "Status code failure" (Lỗi mã trạng thái) + mã phản hồi thực tế từ máy chủ |
Recording offline events
SDK có thể ghi lại các sự kiện xảy ra khi không có kết nối internet. Xem phần Sự kiện trong ứng dụng ngoại tuyến để biết chi tiết.
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.
Nếu có nhiều sự kiện trong bộ nhớ tạm, chúng sẽ lần lượt được gửi đến máy chủ (không phải hàng loạt, một yêu cầu mạng cho mỗi sự kiện).
Ghi nhật ký doanh thu
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.
Giá trị doanh thu không được chứa dấu tách dấu phẩy, ký hiệu tiền tệ hoặc văn bản. Ví dụ một sự kiện doanh thu nên tương tự như 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)
Sự kiện mua ở trên có doanh thu $200, hiển thị dưới dạng doanh thu trong bảng điều khiển.
Lưu ý
Không thêm ký hiệu loại tiền tệ vào giá trị doanh thu.
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)
- Mã tiền tệ phải là mã ISO 4217 gồm 3 ký tự
- Loại tiền tệ mặc định là USD
Để tìm hiểu về cài đặt tiền tệ, hiển thị và chuyển đổi tiền tệ, hãy xem hướng dẫn về tiền tệ doanh thu của chúng tôi.
Logging negative revenue
Có thể có những tình huống mà bạn muốn ghi nhận doanh thu âm. Ví dụ: người dùng nhận được tiền hoàn lại hoặc hủy đăng ký.
Để ghi nhật ký doanh thu âm:
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)
Lưu ý
Lưu ý những điều sau về mã code ở trên:
- Giá trị doanh thu được bắt đầu bằng dấu trừ
- Tên sự kiện là tên sự kiện tùy chỉnh gọi là "cancel_purchase" - tên này giúp bạn dễ dàng xác định các sự kiện doanh thu âm trong bảng điều khiển và báo cáo dữ liệu thô
Xác thực giao dịch mua
AppsFlyer provides server verification for in-app purchases. The validateAndLogInAppPurchase
method takes care of validating and logging the purchase event.
Lưu ý
The legacy function
validateAndLogInAppPurchase
can be replaced by the newer and fully automatic purchase SDK connector. To learn how to integrate the connector, see in Github Android purchase SDK connector
The validateAndLogInAppPurchase
method
validateAndLogInAppPurchase
methodvalidateAndLogInAppPurchase
is exposed via AppsFlyerLib
.
validateAndLognInAppPurchase
có các đối số sau:
validateAndLogInAppPurchase(Context context,
java.lang.String publicKey,
java.lang.String signature,
java.lang.String purchaseData,
java.lang.String price, java.lang.String currency,
java.util.Map<java.lang.String,java.lang.String> additionalParameters)
context
: Ngữ cảnh Ứng dụng / Hoạt độngpublicKey
: Mã khóa Cấp phép lấy từ Google Play Consolesignature
:data.INAPP_DATA_SIGNATURE
fromonActivityResult
purchaseData
:data.INAPP_PURCHASE_DATA
fromonActivityResult
price
: Giá mua, phải được lấy từskuDetails.getStringArrayList("DETAILS_LIST")
currency
: Đơn vị tiền tệ mua hàng, phải được lấy từskuDetails.getStringArrayList("DETAILS_LIST")
additionalParameters
- Các thông số sự kiện bổ sung để ghi nhật ký
If the validation is successful, an af_purchase
event is logged with the values provided to validateAndLogInAppPurchase
.
Lưu ý
validateAndLogInAppPurchase
generates anaf_purchase
in-app event upon successful validation. Sending this event yourself will cause duplicate event reporting.
Example: Validate an in-app purchase
// Purchase object is returned by Google API in onPurchasesUpdated() callback
private void handlePurchase(Purchase purchase) {
Log.d(LOG_TAG, "Purchase successful!");
Map<String, String> eventValues = new HashMap<>();
eventValues.put("some_parameter", "some_value");
AppsFlyerLib.getInstance().validateAndLogInAppPurchase(getApplicationContext(),
PUBLIC_KEY,
purchase.getSignature(),
purchase.getOriginalJson(),
"10",
"USD",
eventValues);
}
// Purchase object is returned by Google API in onPurchasesUpdated() callback
private fun handlePurchase(Purchase purchase) {
Log.d(LOG_TAG, "Purchase successful!")
val eventValues = HashMap<String, String>()
eventValues.put("some_parameter", "some_value")
AppsFlyerLib.getInstance().validateAndLogInAppPurchase(this,
PUBLIC_KEY,
purchase.getSignature(),
purchase.getOriginalJson(),
"10",
"USD",
eventValues)
}
Handling purchase validation success/failure
use AppsFlyerInAppPurchaseValidatorListener
to subscribe to purchase validation successes/failures and registerValidatorListener
to register it in your Application class.
registerValidatorListener
is exposed via AppsFlyerLib
. To use AppsFlyerInAppPurchaseValidatorListener
, import it:
import com.appsflyer.AppsFlyerInAppPurchaseValidatorListener;
import com.appsflyer.AppsFlyerInAppPurchaseValidatorListener
AppsFlyerInAppPurchaseValidatorListener
có hai lệnh gọi lại:
onValidateInApp
: Được kích hoạt khi xác thực mua thành côngonValidateInAppFailure
: Được kích hoạt khi xác thực mua thất bại
registerValidatorListener
có 2 đối số:
context
: Ngữ cảnh Ứng dụngvalidationListener
: TheAppsFlyerInAppPurchaseValidatorListener
object you wish to register
AppsFlyerLib.getInstance().registerValidatorListener(this,new
AppsFlyerInAppPurchaseValidatorListener() {
public void onValidateInApp() {
Log.d(TAG, "Purchase validated successfully");
}
public void onValidateInAppFailure(String error) {
Log.d(TAG, "onValidateInAppFailure called: " + error);
}
});
AppsFlyerLib.getInstance().registerValidatorListener(this, object : AppsFlyerInAppPurchaseValidatorListener {
override fun onValidateInApp() {
Log.d(LOG_TAG, "Purchase validated successfully")
}
override fun onValidateInAppFailure(error: String) {
Log.d(LOG_TAG, "onValidateInAppFailure called: $error")
}
})
Xác thực mua hàng trong ứng dụng sẽ tự động gửi một sự kiện mua trong ứng dụng tới AppsFlyer. Vui lòng xem bên dưới dữ liệu mẫu được truyền trong tham số event_value:
{
"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
}
Hằng số sự kiện
Predefined event names
Để sử dụng các hằng số sau, hãy nhập 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
Tên của sự kiện | Tên hằng số 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" | AFInAppEventType.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" | ||
"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
Tên thông số sự kiện | Tên hằng số Android | Loại |
---|---|---|
"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 | int |
"af_projected_revenue" | PROJECTED_REVENUE | int |
"af_receipt_id" | RECEIPT_ID | String |
"af_tutorial_id" | TUTORIAL_ID | String |
"af_virtual_currency_name" | VIRTUAL_CURRENCY_NAME | String |
"af_deep_link" | 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 tuple có định dạng là (min,max) |
"af_preferred_star_ratings" | PREFERRED_STAR_RATINGS | String , int tuple có định dạng là (min,max) |
Đã cập nhật 28 ngày trước