Skip to content

Commit

Permalink
add new dimensions for week of year, month of year and year
Browse files Browse the repository at this point in the history
  • Loading branch information
diosmosis committed May 9, 2024
1 parent f906803 commit fffe113
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 34 deletions.
75 changes: 60 additions & 15 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,27 @@ function getReportData(request: GoogleAppsScript.Data_Studio.Request<ConnectorPa

let rowsToFetchAtATime = parseInt(env.MAX_ROWS_TO_FETCH_PER_REQUEST, 10) || 100000;

const hasDate = !!(request.fields && request.fields.find((f) => 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;
Expand Down Expand Up @@ -275,7 +289,7 @@ function getReportData(request: GoogleAppsScript.Data_Studio.Request<ConnectorPa
break; // nothing returned by request
}

const partialResponse = hasDate ? partialResponseRaw as Record<string, DataTableRow[]> : { [date]: partialResponseRaw as DataTableRow[] };
const partialResponse = dateMetricIfPresent ? partialResponseRaw as Record<string, DataTableRow[]> : { [date]: partialResponseRaw as DataTableRow[] };
Object.entries(partialResponse).forEach(([date, rows]) => {
if (!rows) {
rows = [];
Expand All @@ -285,7 +299,7 @@ function getReportData(request: GoogleAppsScript.Data_Studio.Request<ConnectorPa
response[date] = [];
}

if (hasDate) {
if (dateMetricIfPresent) {
response[date].push(...rows.map((r) => ({ ...r, date })));
} else {
response[date].push(...rows);
Expand Down Expand Up @@ -323,12 +337,43 @@ function addDimension(fields: GoogleAppsScript.Data_Studio.Fields, id: string, d
.setType(cc.FieldType.TEXT);
}

function addDateDimension(fields: GoogleAppsScript.Data_Studio.Fields) {
fields
.newDimension()
.setId('date')
.setName('Date')
.setType(cc.FieldType.YEAR_MONTH_DAY);
const DATE_METRIC_IDS = ['date', 'date_month', 'date_week', 'date_year'];

function addDateDimensions(
fields: GoogleAppsScript.Data_Studio.Fields,
includeOnly: string[] = DATE_METRIC_IDS,
) {
if (includeOnly.includes('date')) {
fields
.newDimension()
.setId('date')
.setName('Date')
.setType(cc.FieldType.YEAR_MONTH_DAY);
}

if (includeOnly.includes('date_month')) {
fields
.newDimension()
.setId('date_month')
.setName('Month')
.setType(cc.FieldType.YEAR_MONTH);
}

if (includeOnly.includes('date_week')) {
fields
.newDimension()
.setId('date_week')
.setName('Week (Mon - Sun)')
.setType(cc.FieldType.YEAR_WEEK);
}

if (includeOnly.includes('date_year')) {
fields
.newDimension()
.setId('date_year')
.setName('Year')
.setType(cc.FieldType.YEAR);
}
}

function metricsForEachGoal(metrics: Record<string, string>, goals: Record<string, Api.Goal>) {
Expand Down Expand Up @@ -401,8 +446,8 @@ function getFieldsFromReportMetadata(reportMetadata: Api.ReportMetadata, goals:
return;
}

if (metricId === 'date') {
addDateDimension(fields);
if (DATE_METRIC_IDS.includes(metricId)) {
addDateDimensions(fields, [metricId]);
}

if (reportMetadata.dimensions?.[metricId]) {
Expand Down Expand Up @@ -456,7 +501,7 @@ export function getSchema(request: GoogleAppsScript.Data_Studio.Request<Connecto
const fields = getFieldsFromReportMetadata(reportMetadata, goals, siteCurrency);

// add Date field to support time series'
addDateDimension(fields);
addDateDimensions(fields);

const result = { schema: fields.build() };

Expand Down Expand Up @@ -510,7 +555,7 @@ export function getData(request: GoogleAppsScript.Data_Studio.Request<ConnectorP

// perform any transformations on the value required by the Matomo type
let matomoType = reportMetadata?.metricTypes?.[name];
if (name === 'date') {
if (DATE_METRIC_IDS.includes(name)) {
matomoType = 'date';
}

Expand Down
40 changes: 21 additions & 19 deletions tests/appscript/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,26 +388,28 @@ describe('data', () => {
});
}

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 () => {
Expand Down

0 comments on commit fffe113

Please sign in to comment.