You can add 8digits Android SDK to your project by adding 8digits-sdk.jar file to your project.
You should create new instance of client in your main activity.
this.eightdigitsClient = EightDigitsClient.createInstance(this, "<API_URL>", "<TRACKING_CODE>");
8Digits client is a singleton class, if you want to get instance of class in other activities, you should type code below in your activity's onCreate event.
this.eightdigitsClient = EightDigitsClient.getInstance();
After getting instance of client, you should create authToken
to make requests. To create authToken
this.eightdigitsClient.auth("<API_KEY>");
Creating authToken
in your main activity is enough. You don't have to call this method in other activities.
After creating your session (Creating auth token), you should call newVisit method which creates sessionCode and hitCode. 8digits SDK will associate hitCode
and sessionCode
with your view to use later when sending events.
this.eightdigitsClient.newVisit("<Title of Visit>", "<Path>");
Before calling newVisit method, you should call setLocation("<Latitude>", "<Longtitude>")
method to set location of visitor.
If you want to create new hitCode you can call newScreen
method of client.
int hitCode = this.eightdigitsClient.newScreen("<Screen Name>", "<Screen Path>");
Every time user navigate to your activity, you need create new hitCode. Doing this is so easy in 8digits SDK. You just need to call onRestart
method of client in your Activity's onRestart method. You can see example usage below.
@Override
protected void onRestart() {
super.onRestart();
this.eightdigitsClient.onRestart("<Title of Visit>", "<Path>");
}
To create a new event, you can use newEvent
method. 8digits SDK automatically adds hitCode to your new event request. SDK, sends events to the server asynchronously and does not affect your application's user experience.
this.eightdigitsClient.newEvent("<Event Key>", "<Event Value>");
You can get user badges with score
method. This method just takes one callback argument. Callback is a instance of EightDigitsResultListener
class. You can see example below.
this.eightdigitsClient.score(new EightDigitsResultListener() {
@Override
public void handleResult(JSONObject result) {
// TODO Auto-generated method stub
}
});
You need to control error
key in handleResult method. If result has error key, this means your api request is failed.
You can get user badges with visitorBadges
method. This method just takes one callback argument. Callback is a instance of EightDigitsResultListener
class. You can call this method how you call score method. You can see example above.
You can get account badges with badges
method. This method just takes one callback argument. Callback is a instance of EightDigitsResultListener
class. You can call this method how you call score method. You can see example above.
To display an image of badge in an ImageView, you should use badgeImage
method. First parameter is your image view from your view, second argument is badge's id.
You should end user hits in your activities onDestroy method. You can do this by calling, endScreen
method. You don't need to send any parameter to this method.
this.eightdigitsClient.endScreen();
You should end user visit. You should call this method only in your main activity's onDestroy method.
this.eightdigitsClient.endVisit();
To set attribute of visitor, you should use setVisitorAttribute
method, to set avatar of visitor you should use setVisitorAvatar
method.
this.eightdigitsClient.setVisitorAttribute("fullName", "Foo Bar");
this.eightdigitsClient.setVisitorAvatar("http://foo.com/images/bar.jpg");
You can set visitor GSM by calling identifyAndSetVisitorGSM
method after creating visit.
this.eightdigitsClient.identifyAndSetVisitorGSM();
If you want to disable logging for EightDigitsClient class you can call setLoggingEnabled
method. To disable logging, after creating an instance of EightDigitsClient class, you should call method as shown below.
this.eightdigitsClient.setLoggingEnabled(false);
Default value of logging is true which means every http request to 8digits API servers will be logged to LogCat. We suggest disabling logging when you release your application to market because of security reasons.
Gurkan Oluc (@grkn) gurkan@8digits.com