Skip to content

SDK Initialization

Gravitum edited this page Nov 3, 2016 · 14 revisions

Programming guidelines

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 in 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);

    HashMap<String, Object> fields = new HashMap<>();
    fields.put("property1", "value1");
    fields.put("property2", "valu2");
    Analytics.SetCustomFields(fields);

    Calendar birth = Calendar.getInstance();
    birth.set(1989, 5, 24);
    Analytics.SetUserBirthday(new Date(birth.getTimeInMillis()));
    Analytics.SetUserGender(Analytics.Gender.MALE);

    Analytics.SetReceivedPushListener(new IReceivedPushMessageListener() {
            @Override
            public void onReceived(int id, String title, String subtitle, String content, boolean isAppForeground, JSONObject json) {
                // Your custom logic goes here
            }
        });
    Analytics.SetLaunchPushListener(new ILaunchedPushMessageListener() {
            @Override
            public void onLaunched(int id, String title, String subtitle, String content, JSONObject json) {
                // Your custom logic goes here
            }
        });

    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.

  • 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.

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 events with data bundle use the 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 the Purchase event. The common approach is to track the In-App Purchases with such kind of events. So, send this event on 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 events use example is following

Analytics.SendPurchaseEvent("coins_pack", 0.99f, "USD");

Session Tracking

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

Next Step - Push Integration (Optional)

At this point, the SDK is working silently under the hood and custom events and purchases are being sent to the server.

Gravitum uses its own platform to send push messages. Once you are seeing events arriving on the Gravitum admin dashboard, you can proceed to Gravitum Push Setup for push messaging.

Clone this wiki locally