Skip to content

Repository files navigation

Sift Android SDK

Introduction

The Sift Android SDK collects and sends Android device information and application interaction events to Sift for use in improving fraud detection accuracy.

Installing Sift

Add Sift to your application’s build.gradle file:

dependencies {
...
compile 'com.siftscience:sift-android:1.0.0'...
}

You may also need to add the following packagingOptions to the main android block:

android {
...
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
...
}

Compatibility

  • minSdk is 19 (Android 4.4+).
  • Generated classes under com.sift.api.representations should be accessed via getters/setters.
  • If your integration directly references generated model fields, migrate to accessors:
// Beforeevent.userId = "SOME_USER_ID";
// Afterevent.setUserId("SOME_USER_ID");

Integrating Sift

There are two different integration paths to take for incorporating Sift into your application. The first one will be detailed below in the Application Integration section. Follow these instructions if your application flow is primarily based on Activities. If your application flow is based on a combination of Activities and Fragments, please refer to the the Custom Integration section.

Application Integration

Add Sift to your Application file

Create an Application file if you haven’t already. Create an internal class that implements the ActivityLifecycleCallbacks interface and register Sift as shown below:

importsiftscience.android.Sift;
publicclassAppextendsApplication {
@OverridepublicvoidonCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(newActivityLifecycleCallbacksHandler());
}
privatestaticfinalclassActivityLifecycleCallbacksHandlerimplementsActivityLifecycleCallbacks {
publicvoidonActivityCreated(Activityactivity, Bundlebundle) {
Sift.open(activity, newSift.Config.Builder()
.withAccountId("YOUR_ACCOUNT_ID")
.withBeaconKey("YOUR_BEACON_KEY")
// Uncomment to disallow location collection// .withDisallowLocationCollection(true)
.build());
Sift.collect();
}
publicvoidonActivityPaused(Activityactivity) {
Sift.pause();
}
publicvoidonActivityResumed(Activityactivity) {
Sift.resume(activity);
}
publicvoidonActivityDestroyed(Activityactivity) {
Sift.close();
}
}
}

Set the user id

As soon as your application is aware of the user id, set it on the Sift instance using the code below. All subsequent events will include the user id.

Sift.setUserId("SOME_USER_ID");

If the user logs out of your application, you should unset the user id:

Sift.unsetUserId();

Custom Integration

Initialize Sift in your main Activity

Configure the Sift object in the onCreate method of your application's main Activity (the one that begins the application). If the user id is known at this point, you can set it here. Otherwise, you should set it as soon as it is known. In the main Activity, also override onPause, onResume, and onDestroy as shown:

importsiftscience.android.Sift;
publicclassMainActivityextendsAppCompatActivity {
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_sift);
Sift.open(this, newSift.Config.Builder()
.withAccountId("YOUR_ACCOUNT_ID")
.withBeaconKey("YOUR_BEACON_KEY")
// Uncomment to disallow location collection// .withDisallowLocationCollection(true)
.build());
Sift.collect();
}
@OverrideprotectedvoidonPause() {
super.onPause();
Sift.pause();
}
@OverrideprotectedvoidonResume() {
super.onResume();
Sift.resume(this);
}
@OverrideprotectedvoidonDestroy() {
super.onDestroy();
Sift.close();
}
}

Add Sift to your application flow

For each Activity or Fragment that represents a unique page in your application flow, override onStart, onPause, onResume, and onDestroy:

publicclassOtherActivityextendsAppCompatActivity {
@OverrideprotectedvoidonStart(BundlesavedInstanceState) {
super.onStart();
Sift.open(this);
// For Fragments, use Sift.open(this.getActivity()) insteadSift.collect();
}
@OverrideprotectedvoidonPause() {
super.onPause();
Sift.pause();
}
@OverrideprotectedvoidonResume() {
super.onResume();
Sift.resume(this);
}
@OverrideprotectedvoidonDestroy() {
super.onDestroy();
Sift.close();
}
}

Set the user id

As soon as your application is aware of the user id, set it on the Sift instance using the code below. All subsequent events will include the user id.

Sift.setUserId("SOME_USER_ID");

If the user logs out of your application, you should unset the user id:

Sift.unsetUserId();

Submitting an issue

If you find problems or issues with the SDK please create an issue. We ask you provide the following in your submission.

  • Context: Explain how you discovered this issue, include steps to reproduce, and code samples.

Thanks for your help!

About

Sift Android SDK

Resources

Stars

12 stars

Watchers

24 watching

Forks

Releases

Packages

Used by

Contributors

Languages