Source: managers/analytics.js

import Manager from "./manager";

/**
 * @class Analytics
 * @hideconstructor
 */
export default class Analytics extends Manager {
    constructor(client) {
        super(client);
    }

    /**
     * @memberOf Analytics
     * @typedef {Object} errorResponse
     * @property {string} error - Error type. Enum: "UnknownError" "BadRequest" "ResourceExists" "Unauthorized" "NotFound" "PartialSuccess"
     * @property {string} message - Error description.
     * @property {Object.<string, string>} context - Error context. 
     */

    /**
     * @memberOf Analytics
     * @typedef {Object} customEvent
     * @property {string} name - Event name.
     * @property {Object.<string, string>} custom_data - The custom data from the event. 
     */

     /**
     * Track custom events
     *
     * @memberOf Analytics
     * @param {event[]} customEvent - Array of custom events.
     * @returns {Promise<errorResponse>} - A Promise with an error response.
     */
    trackCustomEvents(events) {
        events = events || [];

        events.forEach((item) => item.app_platform = "Web");

        return this.client.authenticatedRequest("/analytics/track-custom-event", {
            method: "POST",
            body: events,
        });
    }

    /**
     * @memberOf Analytics
     * @typedef {Object} purchaseEvent
     * @property {Object.<string, string>} custom_data - The custom data from the event. 
     * @property {string} product_id - The SKU of the product.  
     * @property {string} product_type - The type of the product. Either "item" or "subscription"  
     * @property {string} product_title - The display name for the product.  
     * @property {number} price - The product price in the original currency.  
     * @property {string} price_currency - The purchase currency.  
     * @property {string} purchase_date - The datetime of the event in ISO 8601 format. Only needed if differs from the current date-time.  
     * @property {string} transaction_identifier - A unique transaction identifier.  
     */

    /**
     * Track purchase events
     *
     * @memberOf Analytics
     * @param {event[]} purchaseEvent - Array of purchase events.
     * @returns {Promise<errorResponse>} - A Promise with an error response.
     */
    trackPurchaseEvents(events) {
        events = events || [];

        events.forEach((item) => item.app_platform = "Web");

        return this.client.authenticatedRequest("/analytics/track-purchase", {
            method: "POST",
            body: events,
        });
    }
}