Analytics.js

import PurchaseData from './models/analytics/PurchaseData.js';
import CustomEvent from './models/analytics/CustomEvent.js';
import SDK from './sdk.js';

/**
 * @class Analytics
 * @hideconstructor
 */
export default class Analytics {
    /**
     * Reports in-app purchase to Dashboard.
     *
     * @memberof Analytics
     * @param {PurchaseData} purchaseData - Purchase data.
     * @return {Promise<boolean>} Promise with true if operation was successful, false otherwise.
     */
    static trackPurchase(purchaseData) {
        return Analytics.trackPurchases([purchaseData]);
    }

    /**
     * Reports multiple in-app purchases to Dashboard.
     *
     * @memberof Analytics
     * @param {PurchaseData[]} purchaseEventsData - Purchase events data.
     * @return {Promise<boolean>} Promise with true if operation was successful, false otherwise.
     */
     static trackPurchases(purchaseEventsData) {
        purchaseEventsData = purchaseEventsData || [];

        return SDK.request(
            'trackAnalyticsEvents',
            SDK.superProps,
            purchaseEventsData.map((ev) => {
                ev = new PurchaseData(ev);
                return ev.getBaseEvent();
            })
        );
    }


    /**
     * Reports custom event to Dashboard.
     *
     * @memberof Analytics
     * @param {string} eventName - Custom event.
     * @param {Object<string, string>} eventProperties - Properties.
     * @return {Promise<boolean>} Promise with true if operation was successful, false otherwise.
     */
    static trackCustomEvent(eventName, eventProperties) {
        return Analytics.trackCustomEvents([
            new CustomEvent({
                name: eventName,
                customProperties: eventProperties
            })
        ]);
    }

    /**
     * Reports custom event to Dashboard.
     *
     * @memberof Analytics
     * @param {CustomEvent[]} events - Array of custom events.
     * @return {Promise<boolean>} Promise with true if operation was successful, false otherwise.
     */
     static trackCustomEvents(events) {
        events = Array.isArray(events)
            ? events.map((ev) => {
                ev = new CustomEvent(ev);
                return ev.getBaseEvent();
            })
            : [];

        return SDK.request(
            'trackAnalyticsEvents',
            SDK.superProps,
            events
        );
    }
}