Skip to content

Commit

Permalink
[Feature] Change month dimension to yearMonth and format
Browse files Browse the repository at this point in the history
  • Loading branch information
levinmr committed Oct 31, 2024
1 parent 40afa50 commit a91f411
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
28 changes: 14 additions & 14 deletions reports/usa.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "screenResolution"
Expand Down Expand Up @@ -135,7 +135,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "screenResolution"
Expand Down Expand Up @@ -245,7 +245,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "language"
Expand Down Expand Up @@ -401,7 +401,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "deviceCategory"
Expand Down Expand Up @@ -510,7 +510,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "deviceCategory"
Expand Down Expand Up @@ -619,7 +619,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "mobileDeviceModel"
Expand Down Expand Up @@ -728,7 +728,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "operatingSystem"
Expand Down Expand Up @@ -837,7 +837,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "operatingSystem"
Expand Down Expand Up @@ -946,7 +946,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "operatingSystemVersion"
Expand Down Expand Up @@ -1063,7 +1063,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "operatingSystemVersion"
Expand Down Expand Up @@ -1180,7 +1180,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "browser"
Expand Down Expand Up @@ -1289,7 +1289,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "browser"
Expand Down Expand Up @@ -1395,7 +1395,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "browser"
Expand Down Expand Up @@ -1548,7 +1548,7 @@
"query": {
"dimensions": [
{
"name": "month"
"name": "yearMonth"
},
{
"name": "browser"
Expand Down
23 changes: 16 additions & 7 deletions src/process_results/analytics_data_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand All @@ -189,6 +182,8 @@ class AnalyticsDataProcessor {

if (field === "date") {
modValue = this.#formatDate(value);
} else if (field === "yearMonth") {
modValue = this.#formatYearMonth(value);
} else {
modValue = value;
}
Expand All @@ -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;
8 changes: 8 additions & 0 deletions test/process_results/analytics_data_processor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)" }] }];
Expand Down

0 comments on commit a91f411

Please sign in to comment.