models/analytics/PurchaseData.js

/** PurchaseData */

export default class PurchaseData {
    /**
     * Creates a PurchaseData
     * @param {Object} args - Purchase arguments.
     * @param {string} args.product_id - The SKU of the product.
     * @param {string} args.product_title - The display name for the product.
     * @param {string} args.product_type - The type of the product. Either "item" or "subscription"
     * @param {number} args.price - The product price in the original currency.
     * @param {string} args.price_currency - The purchase currency.
     * @param {string} args.purchase_date - The datetime of the event in ISO 8601 format. Only needed if differs from the current date-time.
     * @param {string} args.transaction_identifier - A unique transaction identifier.
     */
    constructor(args) {
        this.product_id = args.product_id || null;
        this.product_title = args.product_title || null;
        this.product_type = args.product_type || null;
        this.price = args.price || null;
        this.price_currency = args.price_currency || null;
        this.purchase_date = args.purchase_date || null;
        this.transaction_identifier = args.transaction_identifier || null;
    }

    /**
     * Parse data to get instance of THAnalyticsBaseEvent
     * @ignore
     * @return {THAnalyticsBaseEvent}.
     */
    getBaseEvent () {
        let ev = new THAnalyticsBaseEvent({
            customProperties: this,
            deviceTime: (new Date()).getTime(),
            deviceTimeType: THDeviceTimeType.LOCAL_TIME
        });

        if (ev.customProperties && ev.customProperties.custom_data) {
            ev.customProperties = {
                ...ev.customProperties,
                ...ev.customProperties.custom_data
            };

            delete ev.customProperties.custom_data;
        }

        return ev;
  }
}