Mengintegrasikan SDK

Pelajari cara menginisialisasi dan memulai SDK Android.

Disarankan

Get started with our SDK integration wizard

  • Anda harus menginstal Android SDK .
  • Pastikan di aplikasi Anda build.gradle file, applicationIdnilai (di defaultConfig blok) mencocokkan ID aplikasi dari aplikasi di AppsFlyer.
  • Dapatkan kode pengembang AppsFlyer . Diperlukan agar inisialisasi SDK berhasil.

Menginisialisasi SDK Android

Disarankan untuk menginisialisasi SDK di kelas/subkelas Aplikasi global. Hal ini untuk memastikan SDK dapat dimulai dalam skenario apa pun (misalnya, deep linking).

Langkah 1: Impor AppsFlyerLib
Di kelas Aplikasi global Anda, impor AppsFlyerLib:

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

Langkah 2: Menginisialisasi SDK
Di Aplikasi global onCreate, call init dengan argumen berikut ini:

AppsFlyerLib.getInstance().init(<YOUR_DEV_KEY>, null, this);
AppsFlyerLib.getInstance().init(<YOUR_DEV_KEY>, null, this)
  1. Argumen pertama adalah kode pengembang AppsFlyer Anda.
  2. Argumen kedua adalah Dapat Diubah ke Null AppsFlyerConversionListener. Jika Anda tidak membutuhkan data konversi, kami merekomendasikan untuk meneruskan null sebagai argumen kedua. Untuk informasi selengkapnya, lihat Data konversi.
  3. Argumen ketiga adalah Konteks Aplikasi.

Memulai SDK Android

Di metode onCreate Aplikasi, setelah memanggil init, call start dan meneruskannya ke Konteks Aplikasi sebagai argumen pertama:

AppsFlyerLib.getInstance().start(this);
AppsFlyerLib.getInstance().start(this)

Deferring SDK start

OPSIONAL
Anda dapat menunda inisialisasi SDK dengan memanggil start dari kelas Aktivitas, bukannya memanggilnya di kelas Aplikasi. init masih harus dipanggil di kelas Aplikasi.

Penggunaan khusus dari permulaan SDK yang ditangguhkan adalah ketika aplikasi ingin meminta persetujuan dari pengguna untuk mengumpulkan data di Aktivitas Utama dan memanggil start setelah mendapatkan persetujuan pengguna.

⚠️

Pemberitahuan Penting

Jika aplikasi memanggil start dari suatu Aktivitas, itu harus meneruskan Konteks Aktivitas ke SDK.
Gagal melewati konteks aktivitas tidak akan memicu SDK, sehingga kehilangan data atribusi dan in-app event.

Starting with a response listener

Untuk menerima konfirmasi bahwa SDK dimulai secara sukses, menciptakan sebuah AppsFlyerRequestListener objek dan meneruskannya sebagai argumen ketiga dari start:

AppsFlyerLib.getInstance().start(getApplicationContext(), <YOUR_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, <YOUR_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() metode callback dipanggil untuk setiap 200 respons untuk permintaan atribusi yang dibuat oleh SDK.
  • The onError(String error) metode callback dipanggil untuk respons lain dan mengembalikan respons sebagai string kesalahan.

Contoh lengkap

Contoh berikut menunjukkan cara menginisialisasi dan memulai SDK dari kelas Aplikasi.

import android.app.Application;
import com.appsflyer.AppsFlyerLib;
// ...
public class AFApplication extends Application {
    // ...
    @Override
    public void onCreate() {
        super.onCreate();
        // ...
        AppsFlyerLib.getInstance().init(<YOUR_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(<YOUR_DEV_KEY>, null, this)
        AppsFlyerLib.getInstance().start(this)
        // ...
    }
    // ...
}

Tautan Github

Setting the Customer User ID

OPSIONAL

The Customer User ID (CUID) is a unique user identifier created by the app owner outside the SDK. It can be associated with in-app events if provided to the SDK. Once associated with the CUID, these events can be cross-referenced with user data from other devices and applications.

Set the customer User ID

Once the CUID is available, you can set it by calling  setCustomerUserId.


...
AppsFlyerLib.getInstance().init(<YOUR_DEV_KEY>, conversionListener, this);  
AppsFlyerLib.getInstance().start(this , <YOUR_DEV_KEY> );
...
// Do your magic to get the customerUserID...
...
AppsFlyerLib.getInstance().setCustomerUserId(<MY_CUID>);

The CUID can only be associated with in-app events after it was set. Since start was called before setCustomerUserID, the install event will not be associated with the CUID. If you need to associate the install event with the CUID, see the below section.

Associate the CUID with the install event

If it’s important for you to associate the install event with the CUID, you should set it before calling start.

You can set the CUID before start in two ways, depending on whether you start the SDK in the Application or the Activity class.

When starting from the application class

If you started the SDK from the Application class (see: Starting the Android SDK) and you want the CUID to be associated with the install event, put the SDK in waiting mode to prevent the install data from being sent to AppsFlyer before the CUID is provided.

To activate the waiting mode, set waitForCustomerUserId to true after init and before start.

⚠️

Penting

It's important to remember that putting the SDK in a waiting mode may block the SDK from sending the install event and consequently prevent attribution. This can occur, for example, when the user launches the application for the first time and then exits before the SDK can set the CUID.

AppsFlyerLib.getInstance().init(<YOUR_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.

Note

If you wish to remove the waiting mode from the SDK initialization flow, it is not enough to delete the call to waitForCustomerUserId(true). It is also required to replace it with waitForCustomerUserID(false). Simply removing the call is insufficient because the 'waitForCustomerUserId' boolean flag is stored in the Android Shared Preferences.

Example code

public class AFApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    AppsFlyerConversionListener conversionDataListener = 
    new AppsFlyerConversionListener() {
      ...
    };
    AppsFlyerLib.getInstance().init(<YOUR_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);
  }
}

When starting from the Activity class

If you started the SDK from an Activity (see: Deferring SDK start) class and you want the CUID to be associated with the install event, set the CUID beforestart.

Log sessions

The SDK sends an af_app_opened message whenever the app is opened or brought to the foreground. Before the message is sent, the SDK makes sure that the time passed since sending the last message is not smaller than a predefined interval.

Setting the time interval between app launches

Panggil setMinTimeBetweenSessions to set the minimal time interval that must lapse between two af_app_opened messages. The default interval is 5 seconds.

Logging sessions manually

You can log sessions manually by calling logSession.

mengaktifkan mode debug

OPSIONAL
Anda dapat mengaktifkan log debug dengan memanggil setDebugLog:

AppsFlyerLib.getInstance().setDebugLog(true);
AppsFlyerLib.getInstance().setDebugLog(true)

📘

Catatan

Untuk melihat log debug yang lengkap, pastikan untuk memanggil setDebugLog sebelum memanggil metode SDK lainnya.

Lihat contoh.

🚧

peringatan

Untuk menghindari kebocoran informasi sensitif, pastikan log debug dinonaktifkan sebelum mendistribusikan aplikasi.

Menguji integrasi

OPSIONAL
Untuk petunjuk pengujian integrasi mendetail, lihat panduan pengujian integrasi Android SDK .