Anura Docs

Callback Functions

Developing applications is not one size fits all. Therefore, we're providing a few JavaScript-based examples that will attempt to demonstrate when data is available and the various ways you can interact with the callback and response object functions to obtain data and make decisions within the context of your applications.

Be sure to apply your own programming logic and remove all unnecessary console logging before putting your code into a production environment!

We know not everyone is a developer, therefore, we've created a wizard where you're able to create your own semi-custom integrations! You can find it under the "Integration" tab in the Anura Dashboard or by going directly to the Anura Integration Wizard.

Request

We've already covered adding Anura Script to your application, but to reiterate, begin by adding Anura Script to the end of your page's <head> tag to create a request and generate a result.

JavaScript
<script type="text/javascript">
// FOR DEMONSTRATION PURPOSES ONLY!
(function(){
    var anura = document.createElement('script');
    if ('object' === typeof anura) {
        var request = {
            instance: 123456789,
            callback: 'anuraResponseCallback'
        };
        var params = [Math.floor(1E12*Math.random()+1)];
        for (var x in request) params.push(x+'='+encodeURIComponent(request[x]));
        anura.type = 'text/javascript';
        anura.async = true;
        anura.src = 'https://script.anura.io/request.js?'+params.join('&');
        var script = document.getElementsByTagName('script')[0];
        script.parentNode.insertBefore(anura, script);
    }
})();
</script>

The JavaScript example shown above makes use of a hypothetical callback function named 'anuraResponseCallback' which is required for accessing Anura Script's response data in our latter examples.

The callback parameter must be defined and accessible from the (global scope) window object to execute your custom callback function.

The callback parameter does not need to be defined when performing basic monitoring or when a callback function is not going to be used.

Response

The callback function is executed at the completion of the "Request. Response." process. Each successful response from Anura Script will include either an Anura assigned response ID or your unique, self-provided EXID. The response ID, or EXID, is then used to query the result from Anura's servers.

1. Response Only Callback

The most secure way to obtain result data from Anura is by performing result queries from a back-end server, which limits exposure of potentially sensitive data to the client. The response ID, or the self-provided EXID, are accessible by using the built-in getId() and getExId() response object functions respectively.

JavaScript
<script type="text/javascript">
// FOR DEMONSTRATION PURPOSES ONLY! DO NOT CONSOLE LOG IN PRODUCTION ENVIRONMENTS!
function anuraResponseCallback() {
    if (!!Anura) {
        // currently executing
        console.log(arguments.callee.name);
        // response object functions
        console.log('ID = ' + Anura.getAnura().getId());
        console.log('EXID = ' + Anura.getAnura().getExId());
        console.log('HAS RESULT = ' + Anura.getAnura().hasResult());
        console.log('ERROR = ' + Anura.getAnura().getError());
        // response object values
        console.log(Anura.getAnura().getResponseObject());
    }
}
</script>

The response ID, or EXID, is the only value returned when "full response" is disabled.

2. Response and Query Result Callback

Not everyone is able to perform result queries from a back-end server, which is why we've included the ability as a built-in function. However, be aware and understand that querying result values directly from a client-side environment is less secure and introduces potential risk, as modern web browsers can record and display information about network requests taking place in the background.

Expanding on our previous example, to query result data from the client-side, we would use the queryResult(callback) function. The callback parameter is used to execute a referenced callback function at the completion of the result query.

JavaScript
<script type="text/javascript">
// FOR DEMONSTRATION PURPOSES ONLY! DO NOT CONSOLE LOG IN PRODUCTION ENVIRONMENTS!
function anuraResponseCallback() {
    if (!!Anura) {
        // currently executing
        console.log(arguments.callee.name);
        // response object functions
        console.log('ID = ' + Anura.getAnura().getId());
        console.log('EXID = ' + Anura.getAnura().getExId());
        console.log('HAS RESULT = ' + Anura.getAnura().hasResult());
        console.log('ERROR = ' + Anura.getAnura().getError());
        // response object values
        console.log(Anura.getAnura().getResponseObject());
        // result callback
        function anuraResultCallback() {
            // currently executing
            console.log(arguments.callee.name);
            // result object functions
            console.log('HAS RESULT = ' + Anura.getAnura().hasResult());
            console.log('IS GOOD = ' + Anura.getAnura().isGood());
            console.log('IS WARNING = ' + Anura.getAnura().isWarning());
            console.log('IS BAD = ' + Anura.getAnura().isBad());
            console.log('RESULT = ' + Anura.getAnura().getResult());
            console.log('RULE SETS = ' + Anura.getAnura().getRuleSets());
            console.log('INVALID TRAFFIC TYPE = ' + Anura.getAnura().getInvalidTrafficType());
            console.log('IS MOBILE = ' + Anura.getAnura().isMobile());
            console.log('MOBILE = ' + Anura.getAnura().getMobile());
            console.log('HAS ADBLOCKER = ' + Anura.getAnura().hasAdBlocker());
            console.log('ADBLOCKER = ' + Anura.getAnura().getAdBlocker());
            console.log('ERROR = ' + Anura.getAnura().getError());
            // result object values
            console.log(Anura.getAnura().getResultObject());
        }
        // execute result query and callback
        Anura.getAnura().queryResult(anuraResultCallback);
    }
}
</script>

The response ID, or EXID, is automatically referenced from response data and does not need to be declared.

3. Response and Query Result Callback with Custom Variable

For your convenience, Anura Script offers the ability to declare a custom (global scope) window variable that response object functions will be assigned to. The variable can be static or dynamic depending on your needs. To illustrate, we've modified the previous example to use a custom variable named 'controller'. Notice how both examples are similar in structure, however, the former piece of JavaScript uses a variable named 'Anura', while the latter uses 'controller' for its assignment.

JavaScript
<script type="text/javascript">
// FOR DEMONSTRATION PURPOSES ONLY! DO NOT CONSOLE LOG IN PRODUCTION ENVIRONMENTS!
function anuraResponseCallback() {
    if (!!controller) {
        // currently executing
        console.log(arguments.callee.name);
        // response object functions
        console.log('ID = ' + controller.getAnura().getId());
        console.log('EXID = ' + controller.getAnura().getExId());
        console.log('HAS RESULT = ' + controller.getAnura().hasResult());
        console.log('ERROR = ' + controller.getAnura().getError());
        // response object values
        console.log(controller.getAnura().getResponseObject());
        // result callback
        function anuraResultCallback() {
            // currently executing
            console.log(arguments.callee.name);
            // result object functions
            console.log('HAS RESULT = ' + controller.getAnura().hasResult());
            console.log('IS GOOD = ' + controller.getAnura().isGood());
            console.log('IS WARNING = ' + controller.getAnura().isWarning());
            console.log('IS BAD = ' + controller.getAnura().isBad());
            console.log('RESULT = ' + controller.getAnura().getResult());
            console.log('RULE SETS = ' + controller.getAnura().getRuleSets());
            console.log('INVALID TRAFFIC TYPE = ' + controller.getAnura().getInvalidTrafficType());
            console.log('IS MOBILE = ' + controller.getAnura().isMobile());
            console.log('MOBILE = ' + controller.getAnura().getMobile());
            console.log('HAS ADBLOCKER = ' + controller.getAnura().hasAdBlocker());
            console.log('ADBLOCKER = ' + controller.getAnura().getAdBlocker());
            console.log('ERROR = ' + controller.getAnura().getError());
            // result object values
            console.log(controller.getAnura().getResultObject());
        }
        // execute result query and callback
        controller.getAnura().queryResult(anuraResultCallback);
    }
}
</script>

The variable parameter is defined in the initial Anura Script request. Refer to our Script Integration - Request documentation for further details.

4. Response and Query Result Callback with Return Arguments and Anonymous Function

If you're unable to interact with the (global scope) window variable, Anura Script does return a response object as the first argument for both response callback and queryResult callback functions.

JavaScript
<script type="text/javascript">
// FOR DEMONSTRATION PURPOSES ONLY! DO NOT CONSOLE LOG IN PRODUCTION ENVIRONMENTS!
function anuraResponseCallback(response) { // the response object is returned
    // currently executing
    console.log(arguments.callee.name);
    // response object functions
    console.log('ID = ' + response.getId());
    console.log('EXID = ' + response.getExId());
    console.log('HAS RESULT = ' + response.hasResult());
    console.log('ERROR = ' + response.getError());
    // response object values
    console.log(response.getResponseObject());
    // execute result query and callback
    response.queryResult(function(result){ // the result callback can be passed anonymously with the response object returned
        // currently executing
        console.log(arguments.callee.name);
        // result object functions
        console.log('HAS RESULT = ' + result.hasResult());
        console.log('IS GOOD = ' + result.isGood());
        console.log('IS WARNING = ' + result.isWarning());
        console.log('IS BAD = ' + result.isBad());
        console.log('RESULT = ' + result.getResult());
        console.log('RULE SETS = ' + result.getRuleSets());
        console.log('INVALID TRAFFIC TYPE = ' + result.getInvalidTrafficType());
        console.log('IS MOBILE = ' + result.isMobile());
        console.log('MOBILE = ' + result.getMobile());
        console.log('HAS ADBLOCKER = ' + result.hasAdBlocker());
        console.log('ADBLOCKER = ' + result.getAdBlocker());
        console.log('ERROR = ' + result.getError());
        // result object values
        console.log(result.getResultObject());
    });
}
</script>

5. Full Response Callback

A "full response", which includes result data, is available to be returned with the response for immediate use. However, "full response" should only be used on occasions when you're unable to connect to the result endpoint.

JavaScript
<script type="text/javascript">
// FOR DEMONSTRATION PURPOSES ONLY! DO NOT CONSOLE LOG IN PRODUCTION ENVIRONMENTS!
function anuraResponseCallback() {
    if (!!Anura) {
        // currently executing
        console.log(arguments.callee.name);
        // response object functions
        console.log('ID = ' + Anura.getAnura().getId());
        console.log('EXID = ' + Anura.getAnura().getExId());
        console.log('HAS RESULT = ' + Anura.getAnura().hasResult());
        console.log('IS GOOD = ' + Anura.getAnura().isGood());
        console.log('IS WARNING = ' + Anura.getAnura().isWarning());
        console.log('IS BAD = ' + Anura.getAnura().isBad());
        console.log('RESULT = ' + Anura.getAnura().getResult());
        console.log('RULE SETS = ' + Anura.getAnura().getRuleSets());
        console.log('INVALID TRAFFIC TYPE = ' + Anura.getAnura().getInvalidTrafficType());
        console.log('IS MOBILE = ' + Anura.getAnura().isMobile());
        console.log('MOBILE = ' + Anura.getAnura().getMobile());
        console.log('HAS ADBLOCKER = ' + Anura.getAnura().hasAdBlocker());
        console.log('ADBLOCKER = ' + Anura.getAnura().getAdBlocker());
        console.log('ERROR = ' + Anura.getAnura().getError());
        // response object values
        console.log(Anura.getAnura().getResponseObject());
    }
}
</script>

Talk to support about whether enabling "full response" is right for you.