Tích hợp SDK
Tìm hiểu cách khởi tạo và khởi động SDK Android.
Trước khi bạn bắt đầu
- Bạn phải cài đặt SDK Android.
- Đảm bảo rằng tệp tin
build.gradle
trong ứng dụng của bạn,applicationId
giá trị (trong khốidefaultConfig
) khớp với ID ứng dụng của ứng dụng trong AppsFlyer. - Lấy dev key của AppsFlyer. Bắt buộc phải có dev key này để khởi tạo thành công SDK.
- Các đoạn mã trong tài liệu này là cách thực hiện mẫu. Đảm bảo thay đổi
<AF_DEV_KEY>
và các trình giữ chỗ khác khi cần thiết. - Tất cả các bước trong tài liệu này là bắt buộc trừ khi có quy định khác.
Khởi tạo SDK Android
Bạn nên khởi tạo SDK trong lớp/lớp con Ứng dụng toàn cầu. Điều đó nhằm đảm bảo SDK có thể khởi động trong bất cứ trường hợp nào (ví dụ: liên kết sâu).
Bước 1: Nhập AppsFlyerLib
Trong lớp Ứng dụng toàn cầu của bạn, hãy nhập AppsFlyerLib
:
import com.appsflyer.AppsFlyerLib;
import com.appsflyer.AppsFlyerLib
Bước 2: Khởi tạo SDK
Trong Ứng dụng toàn cầu onCreate
, call init
với các đối số sau:
AppsFlyerLib.getInstance().init(<AF_DEV_KEY>, null, this);
AppsFlyerLib.getInstance().init(<AF_DEV_KEY>, null, this)
- Đối số đầu tiên là dev key AppsFlyer của bạn.
- Đối số thứ hai có thể là Rỗng
AppsFlyerConversionListener
. Nếu không cần dữ liệu chuyển đổi, bạn nên chuyểnnull
làm đối số thứ hai. Để biết thêm thông tin, hãy xem Dữ liệu chuyển đổi. - Đối số thứ ba là Ngữ cảnh Ứng dụng.
Khởi động SDK Android
Trong phương thức onCreate
của Ứng dụng, sau khi gọi ra init
, call start
và chuyển cho nó Bối cảnh của Ứng dụng làm đối số đầu tiên:
AppsFlyerLib.getInstance().start(this);
AppsFlyerLib.getInstance().start(this)
Deferring SDK start
Tùy chọn
Bạn có thể trì hoãn quá trình khởi tạo SDK bằng cách gọi ra start
từ một lớp Hoạt động, thay vì gọi ra trong lớp Ứng dụng. init
sẽ vẫn được gọi ra trong lớp Ứng dụng.
Cách sử dụng điển hình của khởi động SDK trì hoãn là khi một ứng dụng cần yêu cầu quyết định đồng ý của người dùng để thu thập dữ liệu trong Hoạt động Chính và gọi ra start
sau khi nhận được sự đồng ý của người dùng.
Thông báo quan trọng
Nếu ứng dụng gọi
start
từ một Hoạt động, Ngữ cảnh Hoạt động sẽ được chuyển tới SDK.
Việc không chuyển ngữ cảnh hoạt động sẽ không kích hoạt SDK, do đó làm mất dữ liệu phân bổ và sự kiện trong ứng dụng.
Starting with a response listener
Để nhận được xác nhận rằng SDK đã được khởi động thành công, hãy tạo một đối tượng AppsFlyerRequestListener
và chuyển nó làm đối số thứ ba của start
:
AppsFlyerLib.getInstance().start(getApplicationContext(), <AF_DEV_KEY>, new AppsFlyerRequestListener() {
@Override
public void onSuccess() {
Log.d(LOG_TAG, "Launch sent successfully, got 200 response code from server");
}
@Override
public void onError(int i, @NonNull String s) {
Log.d(LOG_TAG, "Launch failed to be sent:\n" +
"Error code: " + i + "\n"
+ "Error description: " + s);
}
});
AppsFlyerLib.getInstance().start(this, <AF_DEV_KEY>, object : AppsFlyerRequestListener {
override fun onSuccess() {
Log.d(LOG_TAG, "Launch sent successfully")
}
override fun onError(errorCode: Int, errorDesc: String) {
Log.d(LOG_TAG, "Launch failed to be sent:\n" +
"Error code: " + errorCode + "\n"
+ "Error description: " + errorDesc)
}
})
- The
onSuccess()
phương thức gọi lại được gọi ra cho mỗi phản hồi200
đối với yêu cầu phân bổ do SDK thực hiện. - The
onError(String error)
phương thức gọi lại được gọi ra cho bất kỳ phản hồi nào khác và trả về phản hồi dưới dạng chuỗi lỗi.
Ví dụ đầy đủ
Ví dụ sau đây trình bày cách khởi tạo và khởi động SDK từ lớp Ứng dụng.
import android.app.Application;
import com.appsflyer.AppsFlyerLib;
// ...
public class AFApplication extends Application {
// ...
@Override
public void onCreate() {
super.onCreate();
// ...
AppsFlyerLib.getInstance().init(<AF_DEV_KEY>, null, this);
AppsFlyerLib.getInstance().start(this);
// ...
}
// ...
}
import android.app.Application
import com.appsflyer.AppsFlyerLib
// ...
class AFApplication : Application() {
override fun onCreate() {
super.onCreate()
// ...
AppsFlyerLib.getInstance().init(<AF_DEV_KEY>, null, this)
AppsFlyerLib.getInstance().start(this)
// ...
}
// ...
}
Setting the Customer User ID
Tùy chọn
The Customer User ID (CUID) is a unique user identifier created outside the SDK by the app owner. If made available to the SDK, it can be associated with installs and other in-app events. These CUID-tagged events can be cross-referenced with user data from other devices and applications.
There are two ways to set the CUID, depending on whether or not the SDK is started from the Application
class or the Activity
class (see Starting the Android SDK).
Set the CUID in the Application class
If you started the SDK from the Application
class (see Starting the Android SDK), pause the SDK to prevent the install data from being sent to AppsFlyer before the CUID is provided.
To achieve the delay, set waitForCustomerUserId
to true
after init
and before start
.
AppsFlyerLib.getInstance().init(AF_DEV_KEY, getConversionListener(), getApplicationContext());
AppsFlyerLib.getInstance().waitForCustomerUserId(true);
AppsFlyerLib.getInstance().start(this);
After calling start
, you can add your custom code that makes the CUID available.
Once the CUID is available, the final step includes setting the CUID, releasing the SDK from the waiting mode, and sending the attribution data with the customer ID to AppsFlyer. This step is performed using the call to setCustomerIdAndLogSession
.
AppsFlyerLib.getInstance().setCustomerIdAndLogSession(<CUSTOMER_ID>, this);
Other than setCustomerIdAndLogSession
, do not use setCustomerUserId
or any other AppsFlyer SDK functionality, as the waiting SDK will ignore it.
Example code
public class AFApplication extends Application {
private static final String AF_DEV_KEY = <AF_DEV_KEY>;
@Override
public void onCreate() {
super.onCreate();
AppsFlyerConversionListener conversionDataListener =
new AppsFlyerConversionListener() {
...
};
AppsFlyerLib.getInstance().init(AF_DEV_KEY, getConversionListener(), getApplicationContext());
AppsFlyerLib.getInstance().waitForCustomerUserId(true);
AppsFlyerLib.getInstance().start(this);
// Do your magic to get the customerUserID
// any AppsFlyer SDK code invoked here will be discarded
// ...
// Once the customerUserID is available, call setCustomerIdAndLogSession().
// setCustomerIdAndLogSession() sets the CUID, releases the waiting mode,
// and sends the attribution data with the customer ID to AppsFlyer.
AppsFlyerLib.getInstance().setCustomerIdAndLogSession(<CUSTOMER_ID>, this);
}
}
Lưu ý
If you wish to remove the waiting mode from the SDK initialization fow, it is not enough to delete the call to
waitForCustomerUserId(true)
. It is also required to replace it withwaitForCustomerUserID(false)
. Simply removing the call is insufficient because the 'waitForCustomerUserId' boolean flag is stored in the Android Shared Preferences.
Set the CUID in an Activity class
If you started the SDK from an Activity
class (see Deferring SDK start), we recommend setting the CUID after init
and before start
. This ensures that the install event can be recorded with the CUID.
Sử dụng phương thức setCustomerUserId
function to set the CUID.
public void setCustomerUserId(String id);
Usage example:
// Do your magic to get the customerUserID
...
AppsFlyerLib.getInstance().init(AF_DEV_KEY, conversionListener, this);
AppsFlyerLib.getInstance().setCustomerUserId(<MY_CUID>);
...
//Now you can call start
AppsFlyerLib.getInstance().start(this , AF_DEV_KEY );
Bật chế độ gỡ lỗi
Tùy chọn
Bạn có thể bật nhật ký gỡ lỗi bằng cách gọi ra setDebugLog
:
AppsFlyerLib.getInstance().setDebugLog(true);
AppsFlyerLib.getInstance().setDebugLog(true)
Lưu ý
Để xem toàn bộ nhật ký gỡ lỗi, hãy gọi ra
setDebugLog
trước khi gọi ra các phương thức SDK khác.Xem ví dụ.
cảnh báo
Để tránh rò rỉ thông tin nhạy cảm, hãy đảm bảo rằng nhật ký gỡ lỗi đã được tắt trước khi phân bố ứng dụng.
Thử nghiệm tích hợp
Tùy chọn
Để được hướng dẫn kiểm tra tích hợp chi tiết, hãy xem phần hướng dẫn kiểm tra tích hợp SDK Android.
Đã cập nhật 7 ngày trước