Method signature

Method is used to initialize ads API. It inject a special ads widget which is used to show video ads via other ads SDK methods.

OKSDK.Ads.init(frameId, callbackFunction)

Where:

  • frameId - id of an iframe in which ads widget will be placed. Optional parameter. If not specified ok-ads-frame id will be used;
  • callbackFunction - callback function that will be called after any other ads SDK method call. Optional parameter. If not specified default callback function will be used.

Callback

We recommend to specify a callback function with following signature:

function adsCallback(message)

Message object is passed to this function an has a following structure:

{
    "data": {
        "call": {
            "method": "methodName",
            "arguments": ["argument_1", "argument_2"]
        },
        
        "result": {
            "status": "resultStatus",
            "code": "resultCode"
        }
    }
}

Where:

  • call - method call general info:
    • method - method name. Possible values: init, prepare, show;
    • arguments - additional arguments.
  • result - method call result:
    • status - result status. Possible values: ok, error;
    • code - additional result data.

Callback message

If ads API is successfully initialized following message will be passed to callback function:

{
    "call": {
        "method": "init"
    },
    "result": {
        "status": "ok",
        "code": "initialized"
    }
}

If ads API was not initialized properly a message will contain following data:

{
    "call": {
        "method": "init"
    },
    "result": {
        "status": "error",
        "code": "Error description"
    }
}

Example usage

Without callback function:

    OKSDK.Ads.init(frameId, callbackFunction);

With callback function:

    OKSDK.Ads.init(null, adsCallback);

    function adsCallback(message) {
        if (!message.data) {
            return;
        }

        var data = JSON.parse(message.data);

        if (!data.call || !data.call.method) {
            return;
        }

        if (!data.result || !data.result.status) {
            return;
        }

        switch (data.call.method) {
            case "init":
                if (data.result.status === "ok") {
                    console.log("OK Ads initialization complete");
                    OKSDK.Ads.State.init = true;
                } else {
                    console.log("OK Ads failed to initialize");
                    OKSDK.Ads.State.init = false;
                }
                break;
            case "prepare":
                if (data.result.status === "ok") {
                    if (data.result.code === "ready") {
                        console.log("Ad is ready to be shown");
                        OKSDK.Ads.State.ready = true;
                    }
                } else {
                    console.log("Ad is not ready to be shown. Status: " + data.result.status + ". Code: " + data.result.code);
                    OKSDK.Ads.State.ready = false;
                }
                break;
            case "show":
                ads_state.frame_element.style.display = "none";
                if (data.result.status === "ok") {
                    if (data.result.code === "complete") {
                        console.log("Ad is successfully shown");
                        OKSDK.Ads.State.ready = false;
                    }
                } else {
                    console.log("An ad can't be shown. Status: " + data.result.status + ". Code: " + data.result.code);
                    OKSDK.Ads.State.ready = false;
                }
                break;
        }
    }