Using the Android User Hook SDK

Loading Hook Points

After you have defined your hook points in the admin webpage, you will need to update your app to call the User Hook servers and load any hook points that apply to the current user.

We suggest that you load hook points when the user starts a new app session. When initializing User Hook in your Application class, the third parameter of the UserHook.initialize method is a flag to tell the SDK if it should automatically load hook points every time a new session is started.

The code below will cause hook points to be loaded each time a new session starts.

UserHook.initialize(this, "YOUR_USERHOOK_APP_ID", "YOUR_USERHOOK_APP_KEY", true);

Having the third parameter be false tells the SDK to not automatically load hook points when a session starts.

UserHook.initialize(this, "YOUR_USERHOOK_APP_ID", "YOUR_USERHOOK_APP_KEY", false);

You may also manually load hook points within your app. To do this, you will need to call the UserHook.fetchHookPoint method.

UserHook.fetchHookPoint(new UserHook.UHHookPointFetchListener() {
    @Override
    public void onSuccess(UHHookPoint hookPoint) {
        if (hookPoint != null) {
            hookPoint.execute(getActivity());
        }
    }

    @Override
    public void onError() {
        Log.e("userhook", "error fetching hookpoints");
    }
});

Every time you fetch hook points from the server, the server will look at the user’s meta data (session count, session time, custom fields, etc), compare it to the targeting rules of the active hook points, and return one hook point. If no hook point matches the current user, the server will return an empty response.

If the hook point returns an in-app message, such as a Rating Prompt, the prompt will automatically display. If the hook point returns a payload, that will be handled through the Payload Handler.

Payloads

When a payload is received from a hook point or push notification, the assigned UHPayloadListener is executed.

As an example, let’s imagine we have a hook point that will give the user 10 free coins. The payload created in the User Hook admin webpage can be represented as the following:

{
 'action' : 'give coins',
 'coins' : '10'
}

In the Application class, we will need to assign a UHHookPointActionListener after we have initialized the SDK.


UserHook.initialize(this, "YOUR_USERHOOK_APP_ID", "YOUR_USERHOOK_APP_KEY", true);

UserHook.setPayloadListener(new UserHook.UHPayloadListener() {
    @Override
    public void onAction(Activity activity, Map<String, Object> payload) {

        String action = (String) payload.get("action");
        if (action.equals("give coins")) {

            Integer coins = Integer.parse(payload.get("coins"));

            // logic to give the coins to the user
            ...
        }


    }
});

Sending Custom Fields

Besides any of the predefined targeting fields, you may also send custom fields that will be stored on the User Hook servers. These custom fields can be used when creating targeting rules for hook points.

Map<String,Object> customFields = new HashMap<>();
customFields.put("username","user1");

UserHook.updateCustomFields(customFields, new UserHook.UHSuccessListener() {
    @Override
    public void onSuccess() {
        Log.i("userhook", "custom fields updated");
    }
});

The above code shows an example of a custom field with the name username and a value of user1. Custom fields are stored as Strings or Numbers. If the value sent to the User Hook servers is convertible to a Number, the value is stored as a Number. Otherwise, the value will be stored as a String.

Logging In-App Purchases

In order to track user purchases, purchase information must be sent to the User Hook servers. Sending purchase data requires an item identifier (ie. sku) and a price. A null price may be sent to the server if you do not wish to track the total purchase amount of a user.

String sku = ...;
Number price = ...;

UserHook.updatePurchasedItem(sku, price, new UserHook.UHSuccessListener() {
    @Override
    public void onSuccess() {
        Log.i("userhook", "bought product: " + sku + " for: " + price);
    }
});

Showing Feedback View

To show the Feedback view, the User Hook SDK will create a new UHHostedPageActivity that is then presented to the user. This happens automatically when using the showFeedback method.

UserHook.showFeedback();

Setting Title of Feedback View

When the Feedback View is shown, you can customize the title that is shown at the top of the screen. If you wanted to have the screen say “Support” instead of feedback, you could change the title using the following code.

UserHook.setFeedbackScreenTitle("Support");

Sending Custom Feedback Fields

Feedback is sent to User Hook without the need of a user having to create an account. Some developers prefer to know additional information about the user submitting the feedback. Any time during your app lifecycle, you can assign additional fields that will be sent when a user submits new feedback. These additional fields will be displayed inside the feedback thread for that user in the User Hook admin page.


UserHook.setFeedbackScreenTitle("Feedback");
Map<String,String> customFields = new HashMap<>();
customFields.put("username","user123");
UserHook.setFeedbackCustomFields(customFields);

Showing New Feedback

When the User Hook SDK initializes and creates a new session for the user, it also checks for any new feedback responses from the app developer to the app user. If there is a new feedback message available, your app can react to this new notification by using a UHFeedbackListener. This listener is set in the Application class after initializing the SDK. By using a UHFeedbackListener you can prompt the app user to read the new feedback message.

UserHook.initialize(this, "YOUR_USERHOOK_APP_ID", "YOUR_USERHOOK_APP_KEY", true);

UserHook.setFeedbackListener(new UserHook.UHFeedbackListener() {
    @Override
    public void onNewFeedback(final Activity activity) {

        if (activity != null) {

            UserHook.showFeedbackPrompt("You have a new response to your recently submitted feedback. Do you want to read it now?", "Later", "Read Now");

        }

    }
});

If the user clicks on the positive button, the Feedback screen will be displayed. If the user clicks on the negative button, the prompt will be closed.

Showing Rating Prompt

Besides using a Message Hook Point to display a “Rate This App” prompt, you may also manually display a prompt.

This example is for how to display the rating prompt after a user action, such as clicking on a menu item for “Rate This App”. When using a Message Hook Point, displaying of the prompt is handled automatically by the SDK and the following code is not needed.

UserHook.showRatingPrompt("Do you mind leaving us a review in the app store?", "Sure", "Not Now");

You may also redirect the user to the Google Play rating page without using a prompt.

UserHoo.rateThisApp();

Static Pages

Static Pages are displayed within the app as a UHHostedPageActivity with an embedded web view. To display a static page, you need a UHPage object. This UHPage object can either be created inside the app or returned from the the User Hook servers as part of the UserHook.fetchPageNames call.

UHPage page = new UHPage("privacy","Privacy Policy");
Intent intent = new Intent(this, UHHostedPageActivity.class);
intent.putExtra(UHHostedPageActivity.TYPE_PAGE, page);
startActivity(intent);

To retrieve a list of all defined static pages, you will need to call UserHook.fetchPageNames.

UserHook.fetchPageNames(new UHOperation.UHArrayListener<UHPage>() {
    @Override
    public void onSuccess(List<UHPage> items) {

        // item is a list of all defined pages
        ...
    }
});