diff --git a/src/data.ts b/src/data.ts index 476b9db..b5a246f 100644 --- a/src/data.ts +++ b/src/data.ts @@ -212,13 +212,27 @@ function getReportData(request: GoogleAppsScript.Data_Studio.Request f.name === 'date')); + const dateMetricIfPresent = request.fields && request.fields.find((f) => DATE_METRIC_IDS.includes(f.name)); let period = 'range'; let date = `${request.dateRange.startDate},${request.dateRange.endDate}`; - if (hasDate) { - period = 'day'; + if (dateMetricIfPresent) { + switch (dateMetricIfPresent.name) { + case 'date_year': + period = 'year'; + break; + case 'date_month': + period = 'month'; + break; + case 'date_week': + period = 'week'; + break; + case 'date': + default: + period = 'day'; + break; + } // note: this calculation doesn't work every time, but it's good enough for determining row counts const MS_IN_DAY = 1000 * 60 * 60 * 24; @@ -275,7 +289,7 @@ function getReportData(request: GoogleAppsScript.Data_Studio.Request : { [date]: partialResponseRaw as DataTableRow[] }; + const partialResponse = dateMetricIfPresent ? partialResponseRaw as Record : { [date]: partialResponseRaw as DataTableRow[] }; Object.entries(partialResponse).forEach(([date, rows]) => { if (!rows) { rows = []; @@ -285,7 +299,7 @@ function getReportData(request: GoogleAppsScript.Data_Studio.Request ({ ...r, date }))); } else { response[date].push(...rows); @@ -323,12 +337,32 @@ function addDimension(fields: GoogleAppsScript.Data_Studio.Fields, id: string, d .setType(cc.FieldType.TEXT); } -function addDateDimension(fields: GoogleAppsScript.Data_Studio.Fields) { +const DATE_METRIC_IDS = ['date', 'date_month', 'date_week', 'date_year']; + +function addDateDimensions(fields: GoogleAppsScript.Data_Studio.Fields) { fields .newDimension() .setId('date') .setName('Date') .setType(cc.FieldType.YEAR_MONTH_DAY); + + fields + .newDimension() + .setId('date_month') + .setName('Month') + .setType(cc.FieldType.YEAR_MONTH); + + fields + .newDimension() + .setId('date_week') + .setName('Week (Mon - Sun)') + .setType(cc.FieldType.YEAR_WEEK); + + fields + .newDimension() + .setId('date_year') + .setName('Year') + .setType(cc.FieldType.YEAR); } function metricsForEachGoal(metrics: Record, goals: Record) { @@ -401,8 +435,8 @@ function getFieldsFromReportMetadata(reportMetadata: Api.ReportMetadata, goals: return; } - if (metricId === 'date') { - addDateDimension(fields); + if (DATE_METRIC_IDS.includes(metricId)) { + addDateDimensions(fields); } if (reportMetadata.dimensions?.[metricId]) { @@ -456,7 +490,7 @@ export function getSchema(request: GoogleAppsScript.Data_Studio.Request { }); } - it('should fetch data by day when the date dimension is requested', async () => { - let result = await Clasp.run('getData', { - configParams: { - idsite: env.APPSCRIPT_TEST_IDSITE, - report: JSON.stringify({ apiModule: 'Events', apiAction: 'getName' }), - filter_limit: 5, - }, - dateRange: { - startDate: RANGE_START_DATE_TO_TEST, - endDate: RANGE_END_DATE_TO_TEST, - }, - fields: [ - { name: 'date' }, - { name: 'nb_events' }, - { name: 'Events_EventAction' }, - { name: 'Events_EventName' }, - { name: 'max_event_value' }, - ], + ['date', 'date_week', 'date_month', 'date_year'].forEach((dimensionName) => { + it(`should fetch data by day when the ${dimensionName} dimension is requested`, async () => { + let result = await Clasp.run('getData', { + configParams: { + idsite: env.APPSCRIPT_TEST_IDSITE, + report: JSON.stringify({ apiModule: 'Events', apiAction: 'getName' }), + filter_limit: 5, + }, + dateRange: { + startDate: RANGE_START_DATE_TO_TEST, + endDate: RANGE_END_DATE_TO_TEST, + }, + fields: [ + { name: dimensionName }, + { name: 'nb_events' }, + { name: 'Events_EventAction' }, + { name: 'Events_EventName' }, + { name: 'max_event_value' }, + ], + }); + expect(result).toEqual(getExpectedResponse(result, 'data', `Events.getName_withDateDimension_${dimensionName}`)); }); - expect(result).toEqual(getExpectedResponse(result, 'data', 'Events.getName_withDateDimension')); }); it('should paginate correctly when fetch data by day when the date dimension is requested', async () => {