UNPKG

@pinelab/vendure-plugin-metrics

Version:

Vendure plugin measuring and visualizing e-commerce metrics

44 lines (43 loc) 1.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AverageOrderValueMetric = void 0; const graphql_1 = require("../ui/generated/graphql"); /** * Calculates the average order value per month */ class AverageOrderValueMetric { constructor() { this.metricType = graphql_1.AdvancedMetricType.Currency; this.code = 'aov'; this.allowProductSelection = false; } getTitle(ctx) { return `Average order value`; } calculateDataPoints(ctx, orders) { let averageInclTax = 0; let averageExclTax = 0; if (orders.length) { // Only calculate if there are orders let totalWithTax = 0; let totalExclTax = 0; orders.forEach((o) => { totalWithTax += o.totalWithTax; totalExclTax += o.total; }); averageInclTax = Math.round(totalWithTax / orders.length) / 100; averageExclTax = Math.round(totalExclTax / orders.length) / 100; } return [ { legendLabel: 'AOV incl. tax', value: averageInclTax, }, { legendLabel: 'AOV excl. tax', value: averageExclTax, }, ]; } } exports.AverageOrderValueMetric = AverageOrderValueMetric;