Unity Android SDK

Before beginning the implementation of Polling SDK into your Unity project, you must import the following package into your Unity project.


You can add it through Package Manager, or manually downloading .unitypackage and dragging it into your project. Upon adding this package a selection dialog for files will popup. Choose all files.

Two new folders will be created/merged at Assets directory:

  • Polling
  • Plugins/Android

Class Details

Below, you can see the documentation for the Polling classes.

class Identifier

A class to use on Survey for identifying your app and the customer id.

Params:

  • string customerId
  • string apiKey

Identifier identifier = new Identifier(customerId, apiKey);

class CallbackHandler

A class to assign callbacks that will be triggered by all Survey overlay/request functions on a specific GameObject with its specific callbacks assigned. onReward is used only for overlays (and ignored for data requests).

Params:

  • GameObject target
  • Action<string> onSuccess
  • Action<string> onFailure
  • Action<string> onReward

CallbackHandler callbacks = new CallbackHandler(this.gameObject, OnSuccess, OnFailure, OnReward);

class Survey

A class to invoke surveys overlays or retrieve API data from Polling.com.

Params:

  • class Identifier
  • class CallbackHandler

// Create identifier with customerId and apiKey
Identifier identifier = new Identifier(customerId, apiKey);

// The target GameObject parameter should always be where functions are
CallbackHandler callbacks = new CallbackHandler(this.gameObject, OnSuccess, OnFailure, OnReward);

// Create a new Survey instance, that can be used through the whole game
Survey survey = new Survey(identifier, callbacks);

method AvailableSurveys

Retrieves a Polling.com overlay with surveys available. You can choose the overlay format between a Dialog popup or a Bottom sheet. If you choose None, AvailableSurveys() is called.

Params:

  • enum: None | Dialog | Bottom ViewType

survey.AvailableSurveys(ViewType.Bottom);

method SingleSurvey

Retrieves a Polling.com overlay for a single survey by using its uuid. You can choose the overlay format between a Dialog popup or a Bottom sheet. If no ViewType is provided (or if None is selected), the method will return the raw JSON response.

Params:

  • string surveyUuid
  • enum: None | Dialog | Bottom ViewType (Optional)

survey.SingleSurvey("a123b325-....", ViewType.Dialog);
// OR
survey.SingleSurvey("a123b325-....");

method CompletedSurveys

Retrieves data in a JSON response for all completed surveys.


survey.CompletedSurveys();

method OnReward

A function to be used as a default extension for OnReward callback. Check code example.

Params:

  • string response

// Check full example for detailed usage on callback
List<Reward> rewards = survey.OnReward(response);

class Reward

A class to deserialize rewards callbacks.

Attributes:

  • string completed_at
  • string reward_amount
  • string reward_name
  • string name
  • string started_at
  • string uuid

List<Reward> rewards = survey.OnReward(response);
Reward reward = rewards.First();
Debug.Log(String.Format("{0} ({1}) awarded to Player", reward.reward_amount, reward.reward_name));

Full Example

Full code example


// C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Polling;

public class PollingHandler : MonoBehaviour
{
    public string customerId;
    private string apiKey = "cli_XXXX...";

    Survey survey;

    void Start()
    {
        // Create identifier with customerId and apiKey
        Identifier identifier = new Identifier(customerId, apiKey);

        // The target GameObject parameter should always be where functions are
        CallbackHandler callbacks = new CallbackHandler(this.gameObject, OnSuccess, OnFailure, OnReward);

        // Create a new Survey instance, that can be used through the whole game
        survey = new Survey(identifier, callbacks);
    }

    // All response callbacks
    private void OnSuccess(string response)
    {
        Debug.Log("SUCCESS: " + response);
    }

    private void OnFailure(string error)
    {
        Debug.Log("ERROR: " + error);
    }

    private void OnReward(string response)
    {
        // For this, you have two options:

        // 1. Implement survey.OnReward(response) which returns you
        // data into a List of Reward class
        List<Reward> rewards = survey.OnReward(response);

        // 2. Implement your own way of dealing with rewards json.
        //-------------------------------------------------------------

        // Both options, you will need to implement how rewards are handled to
        // your specific game economy.
        HandleRewards(rewards); // hypothetical function
    }

    // All below you can assign to a Button or subscribe to an event, for example.

    // Available surveys
    public void AvailableSurveysApi()
    {
        survey.AvailableSurveys();
    }

    public void BottomOverlayAvailableSurveys()
    {
        survey.AvailableSurveys(ViewType.Bottom);
    }

    public void PopupOverlayAvailableSurveys()
    {
        survey.AvailableSurveys(ViewType.Dialog);
    }

    // Single survey
    public void SingleSurveyApi()
    {
        survey.SingleSurvey("a123b345...");
    }

    public void BottomOverlaySingleSurvey()
    {
        survey.SingleSurvey("a123b345...", ViewType.Bottom);
    }

    public void PopupOverlaySingleSurvey()
    {
        survey.SingleSurvey("a123b345...", ViewType.Dialog);
    }

    // Completed surveys
    public void CompletedSurveysApi()
    {
        survey.CompletedSurveys();
    }
}