From e46a70a73edbcd1a981eddbe54c822e7b7b4be41 Mon Sep 17 00:00:00 2001 From: Michael Levin Date: Thu, 31 Oct 2024 16:51:42 -0400 Subject: [PATCH] [Feature] Change month dimension to yearMonth and format --- reports/usa.json | 56 +++++++++---------- .../analytics_data_processor.js | 23 +++++--- .../analytics_data_processor.test.js | 8 +++ 3 files changed, 52 insertions(+), 35 deletions(-) diff --git a/reports/usa.json b/reports/usa.json index a0cbaaeb..79e65905 100644 --- a/reports/usa.json +++ b/reports/usa.json @@ -26,7 +26,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "screenResolution" @@ -107,7 +107,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -135,7 +135,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "screenResolution" @@ -216,7 +216,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -245,7 +245,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "language" @@ -373,7 +373,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -401,7 +401,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "deviceCategory" @@ -482,7 +482,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -510,7 +510,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "deviceCategory" @@ -591,7 +591,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -619,7 +619,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "mobileDeviceModel" @@ -700,7 +700,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -728,7 +728,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "operatingSystem" @@ -809,7 +809,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -837,7 +837,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "operatingSystem" @@ -918,7 +918,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -946,7 +946,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "operatingSystemVersion" @@ -1035,7 +1035,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -1063,7 +1063,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "operatingSystemVersion" @@ -1152,7 +1152,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -1180,7 +1180,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "browser" @@ -1261,7 +1261,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -1289,7 +1289,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "browser" @@ -1370,7 +1370,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -1395,7 +1395,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "browser" @@ -1523,7 +1523,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, @@ -1548,7 +1548,7 @@ "query": { "dimensions": [ { - "name": "month" + "name": "yearMonth" }, { "name": "browser" @@ -1684,7 +1684,7 @@ "orderBys": [ { "dimension": { - "dimensionName": "month" + "dimensionName": "yearMonth" }, "desc": true }, diff --git a/src/process_results/analytics_data_processor.js b/src/process_results/analytics_data_processor.js index 49adfc5a..790553f2 100644 --- a/src/process_results/analytics_data_processor.js +++ b/src/process_results/analytics_data_processor.js @@ -162,13 +162,6 @@ class AnalyticsDataProcessor { return data; } - #formatDate(date) { - if (date == "(other)") { - return date; - } - return [date.substr(0, 4), date.substr(4, 2), date.substr(6, 2)].join("-"); - } - #processRow({ hostname, row, data }) { const point = {}; @@ -189,6 +182,8 @@ class AnalyticsDataProcessor { if (field === "date") { modValue = this.#formatDate(value); + } else if (field === "yearMonth") { + modValue = this.#formatYearMonth(value); } else { modValue = value; } @@ -212,6 +207,20 @@ class AnalyticsDataProcessor { const name = data[targetKey][index].name; return this.#mapping[name] || name; } + + #formatDate(date) { + if (date == "(other)") { + return date; + } + return [date.substr(0, 4), date.substr(4, 2), date.substr(6, 2)].join("-"); + } + + #formatYearMonth(value) { + const year = parseInt(value.substring(0, 4)); + const monthIndex = parseInt(value.slice(3)) - 1; + const date = new Date(year, monthIndex); + return date.toLocaleString("en-us", { month: "long", year: "numeric" }); + } } module.exports = AnalyticsDataProcessor; diff --git a/test/process_results/analytics_data_processor.test.js b/test/process_results/analytics_data_processor.test.js index 05aaa102..ca35955b 100644 --- a/test/process_results/analytics_data_processor.test.js +++ b/test/process_results/analytics_data_processor.test.js @@ -84,6 +84,14 @@ describe("AnalyticsDataProcessor", () => { expect(result.data[0].date).to.equal("2017-01-30"); }); + it("should format yearMonth values", () => { + data.dimensionHeaders = [{ name: "yearMonth" }]; + data.rows = [{ dimensionValues: [{ value: "202010" }] }]; + + const result = subject.processData({ report, data }); + expect(result.data[0].yearMonth).to.equal("October 2020"); + }); + it("should not format dates with value (other)", () => { data.dimensionHeaders = [{ name: "date" }]; data.rows = [{ dimensionValues: [{ value: "(other)" }] }];