AppCenter Analytics – Part 2

In this article, we’ll look how to get analytics data with AppCenter open api.

If you want to integrate AppCenter utilities into your back-end system, the appcenter open api layer will help you.

Firstly you need to get APIToken for open api authorization. You can learn by following the steps on the site

It makes a lot of sense to make a website where we can track app analytics and app error details.  In this sample, we’ll get all analytic data very simply by making an API call.

I want to use pure html and js btw 🙂 “it smells oldschool”

I want to see,

  • Count of active events in the time range
  • Device models in the time range
  • Count of devices for an event by interval in the time range

Firstly, we need to create an XMLHttpRequest function

function Request(callback, url){
var ul = document.getElementById("myul");
ul.innerHTML = "";
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200)
{
callback(req);
}
}
req.open( "GET", url, true );
req.setRequestHeader("X-API-Token", "Your API Token");
req.send( null );
}

and we need to create 3 js functions. GetEvents, GetModels and GetDeviceCountByEvent

function GetEvents(req){
                var json = JSON.parse(req.responseText);
                var ul = document.getElementById("myul");
                for (let index = 0; index < json.events.length; index++) {
                        var li = document.createElement("li");
                        li.appendChild(document.createTextNode(json.events[index].name));
                        ul.appendChild(li);
                }
            }
function GetModels(req){
                var json = JSON.parse(req.responseText);
                var ul = document.getElementById("myul");
                for (let index = 0; index < json.models.length; index++) {
                        var li = document.createElement("li");
                        li.appendChild(document.createTextNode(json.models[index].model_name + " : " + json.models[index].count));
                        ul.appendChild(li);
                }
            }
function GetDeviceCountByEvent(req){
                var json = JSON.parse(req.responseText);
                var ul = document.getElementById("myul");
                for (let index = 0; index < json.devices_count.length; index++) {
                        var li = document.createElement("li");
                        li.appendChild(document.createTextNode(json.devices_count[index].datetime + " : " + json.devices_count[index].count));
                        ul.appendChild(li);
                }
            }

and a little bit html code


<div>
            <input type="button" value="Get Events" onclick="Request(GetEvents, eventsurl)" />
            <input type="button" value="Get Models" onclick="Request(GetModels, modelsurl)" />
            <input type="button" value="Get Device Count By Event" onclick="Request(GetDeviceCountByEvent, devicecounturl)" />

<div>

<ul id="myul">
                </ul>

            </div>

        </div>

Let’s try!

Get Device Count By Event

Get Models

Get Events

Next app center articles will be more complicated. I think distribute, test or export

Download full code

Yiğit

Xamarin Developer, Consultant & Architect. Community Leader and Director of Xamarin Türkiye

Post A Reply