Analytics

Analytics

Analytics API

The main purpose of the Analytics API is to provide a convenient method of generating and synchronising Analytics events to meldCX Analytics cloud.

The main method which allows to do this is "registerEvent(event)"

An equivalent supported approach is to send the analytics events directly to the local webserver using:

"POST http://127.0.0.1:9086[/custom/event/path]"

The below example shows these 2 equivalent approaches side by side

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const path = '/custom/event/path';
     const data = {custom: 'data'};

     // Option 1 for registering the same analytics event
     const res1 = await agent.Analytics.registerEvent({
         path,
         headers: [
             {
                 name: 'Custom-Header',
                 value: 'header-value'
             }
         ],
         data
     });

     // Option 2 for registering the same analytics event
     const res2 = await fetch('http://127.0.0.1:9086' + path, {
         method: 'POST',
         headers: {
            'Custom-Header': 'header-value'
         },
         body: JSON.stringify(data)
     }).then(r => r.json())
};

init();

Methods

getEvents(options) → {Promise.<Array.<module:Analytics~Event>>}

Get all the analytics events

Parameters:
Name Type Description
options EventOptions

The options to filter event list

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const result = await agent.Analytics.getEvents({path: '/test/data', sent: false});

     console.log(result);

     //
     //    Will Print
     //    [{
     //        "id", "760f8fbc-b001-49b5-802b-ed5e596fe066",
     //        "path": "/test/data",
     //        "headers": [{"name": "header1", "value": "value1"}],
     //        "data": {"test": "data"},
     //        "createdAt": 123456789900,
     //        "sentAt": null
     //    }]
     //
};

init();

getStatus() → {Promise.<module:Analytics~Status>}

Get the current status of Analytics API

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const result = await agent.Analytics.getStatus();

     console.log(result);

     //
     //    Will Print
     //    {
     //        "lastSent": 123456789900,
     //        "syncEnabled": true,
     //        "events": {
     //            "total": 0,
     //            "sent": 0
     //         }
     //    }
     //
};

init();

pauseSync()

Pause the synchronisation of the analytics data to the cloud

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     await agent.Analytics.pauseSync();
};

init();

registerEvent(event) → {Promise.<module:Analytics~EventResult>}

Register a new analytics event

Parameters:
Name Type Description
event EventData

The event data to register

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const result = await agent.Analytics.registerEvent({path: '/test/data', data: {test: data}});

     console.log(result);

     //
     //    Will Print
     //    {
     //        "id": "760f8fbc-b001-49b5-802b-ed5e596fe066"
     //    }
     //
};

init();

resumeSync()

Resume the synchronisation of the analytics data to the cloud

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     await agent.Analytics.resumeSync();
};

init();