Using the Unity 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.

You may fetch hook points any time during the lifecycle of your app using the following code.

UserHook.Instance.fetchHookPoints();

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 IUserHookPayloadHandler 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'
}

When you initialize User Hook, probably in your first scene, you will need to assign a payload handler.


// In the Start() of the scene where User Hook is initialized

void Start() {
    UserHook.Instance.setPayloadHandler(new CustomPayloadHandler());
}



// CustomPayloadHandler class
using System;
using System.Collections.Generic;
using UnityEngine;

public class CustomPayloadHandler : IUserHookPayloadHandler
{
    public CustomPayloadHandler () {}

    public void handlePayload(Dictionary<string, string> payload) {

        // do any custom logic to handle all payloads returned by User Hook
        if (payload["action"] == "give coins") {
            Debug.Log("coins = " + payload["coins"];

            // logic give the user free coins
        }

    }
}

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.

// inside a script attached to your scene

public void updateCustomFields() {
    Dictionary<string, string> customFields = new Dictionary<string,string> ();
    customFields.put("username","user1");

    UserHook.Instance.updateCustomFields (customFields, new UserHookResponseHandler (this.gameObject.name, "afterUpdateFields"));
}

public void afterUpdateFields() {
    Debug.Log("custom fields updated");
}

The above code shows an example of a custom field with the name username and a value of user1. Once the custom fields have been saved to the server, the “afterUpdateFields” method will be called.

UserHookResponseHandler

All network functions in User Hook occur asynchronously. The UserHookResponseHandler is a way to assign the method that will be executed after the network call is complete. The handler takes two arguments: the name of the game object that owns the target method and the actual name of the method.

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.


public void trackPurchase() {
    string sku = ...;
    double price = ...;

    UserHook.Instance.updatePurchasedItems(sku, price, new UserHookResponseHandler (this.gameObject.name, "afterUpdateItem"));
}

public void afterUpateItem() {
    Debug.Log('item was purchased');
}

Showing Feedback View

To show the Feedback view, the User Hook SDK will create a new native view and display it to the user.

UserHook.Instance.displayFeedback();

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


void Start() {

Dictionary<string,string> feedbackFields = new Dictionary<string, string>();
feedbackFields.Add("username","unity user 1");
UserHook.Instance.setFeedbackCustomFields(feedbackFields);

}

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 IUserHookNewFeedbackHandler. This listener is set when initializing the SDK. By using a IUserHookNewFeedbackHandler you can prompt the app user to read the new feedback message.

// scene where User Hook is initialized
void Start() {
    UserHook.Instance.setNewFeedbackHandler(new CustomNewFeedbackHandler());
}


// Custom New Feedback Handler class


public class CustomNewFeedbackHandler : IUserHookNewFeedbackHandler
{
    public CustomNewFeedbackHandler () {}

    public void handleResponse ()
    {
        UserHook.Instance.DisplayFeedbackPrompt ("You have a new response to your recently submitted feedback. Do you want to read it now?", "Yes", "No");
    }
}

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.Instance.DisplayRatePrompt("Do you want to rate our app?","Yes","No");

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

UserHook.Instance.rateThisApp();

Static Pages

Static Pages are displayed within the app as an embedded web view. To display a static page, you need to know the page slug as defined in the User Hook admin webpage. When displaying a static page, you also pass in the name of the page that will be displayed at the top of the screen.

UserHook.Instance.DisplayStaticPage(pageSlug, pageName);

You may also dynamically fetch the names of all defined static pages and use an IUserHookStaticPageHandler to handle the response.


// load static page names and handle response
UserHook.Instance.fetchPageNames(new CustomPageHandler());




// Custom Page Handler Class
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CustomPageHandler : IUserHookStaticPageHandler
{
    public CustomPageHandler () { }

    public void handlePages (List<UserHookPage> pages)
    {
        // setup a button for the static page
        // this demo is only setup to handle 1 page in the response, 
        // but the server will return all the pages that are defined for your app

        // find out button object in the scene
        GameObject gameObject = GameObject.Find("Static Page Button");

        // loop through all returned pages
        foreach (UserHookPage page in pages) {
            if (gameObject != null) {

                // assign our onClick action to the button
                Button button = gameObject.GetComponent<Button>();
                button.onClick.AddListener(delegate {
                    UserHook.Instance.DisplayStaticPage(page.slug, page.name);
                });

                // update the button title with the name of the page
                Text text = gameObject.GetComponentInChildren<Text>();
                text.text = page.name;
            }

        }

    }
}