-
Notifications
You must be signed in to change notification settings - Fork 0
SDK Initialization
Let's move to the actual programming and start with the SDK initialization. To properly initialize Gravitum Android SDK add the following code to the onCreate method on your Activity class.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Analytics.SetUserId(user_id);
Analytics.SetUserName(user_name);
Analytics.SetFacebookId(facebookId);
Analytics.SetDevicePushToken(device_push_token);
Calendar birth = Calendar.getInstance();
birth.set(1989, 5, 24);
Analytics.SetUserBirthday(new Date(birth.getTimeInMillis()));
Analytics.SetUserGender(Analytics.Gender.MALE);
Analytics.Init(this, APP_TOKEN, APP_SECRET, SENDER_ID);
}The main aspect is Analytics.Init method. That's the place where Gravitum Analytics initialization takes place.
Important: All the method calls before Analytics.Init are optional methods. These methods used to set additional user metadata. Let's take a brief review of the optional methods.
-
The very first optional method is SetUserId. You may provide your own user id in certain cases (e.g. you want to use Google Play Services/Game Center player id as the unique user id for Gravitum service or you have your own web server with the custom player authentication), otherwise, it will be generated automatically. Just make sure, that the Id you provide will be unique for each user.
-
To provide the user name use SetUserName method with the string name. Unlike the used Id, user name may not be unique. What name to provide is up to you without any restrictions.
-
Set user Facebook Id via SetFacebookId method call. The common approach if to get the user Facebook Id from Facebook SDK itself and provide to Gravitum Analytics.
-
If you have your own push notifications service implementation, you may use SetDevicePushToken method to provide the unique Id for each user device. If you don't pass any particular token, the Gravitum will generate push token by itself according to the Google Cloud Messaging SenderId you provided with the Init method. See Sample code to use OneSignal user ids here
-
Last two SetUserBirthday and SetUserGender speak for themselves. Provide the user birthday via java.util.Date object and user gender via Analytics.Gender enum constant.
Now we have done with SDK initialization and ready to go with the events tracking.
The simplest way to send events shown below. Use this method if you don't need to send the data with your event. When just the event Id will be enough for you. Take a look at the following code snippet
Analytics.SendEvent("first_start");To send custom event with data bundle use following code sample. You can provide any event data you want as a HashMap. What data to send with analytics event depends on your game project.
HashMap<String, Object> data = new HashMap<>();
data.put("str_data", "hello_data");
data.put("float_data", 3.14f);
data.put("int_data", 10101);
Analytics.SendEvent("simple_event", data);One more specific Analytics event type is Purchase event. The common approach is to track the In-App Purchases with such kind of events. So, send this event every success In-App Purchase. Provide the product Id, price and the currency code of the real In-App Purchase. All these events you will find in one place in Gravitum Dashboard. Purchase event use example is following
Analytics.SendPurchaseEvent("coins_pack", 0.99f, "USD");The last but not least is session tracking feature of Gravitum Service. To setup session tracking you have to add two method calls in the right places of your Activity class. The first one is Analytics.SessionStart. Add this method to onResume method as shown below.
@Override
public void onResume() {
super.onResume();
Analytics.SessionStart();
}Analytics.SessionEnd method should be called from onPause method of your main activity.
@Override
public void onPause() {
super.onPause();
Analytics.SessionEnd();
}Important: You shouldn't send any event for tracking session length. All the duration tracking events will be sent under the hood. Game session metrics will be available in your Gravitum Developer Dashboard. If you need more detailed information about each API, you can find all the SDK API References here