Skip to content

Commit

Permalink
add temporary metrics to showColumns API parameter when fetching data
Browse files Browse the repository at this point in the history
  • Loading branch information
diosmosis committed Jun 28, 2024
1 parent 60ec074 commit f054ad3
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ const pastScriptRuntimeLimitErrorMessage = 'It\'s taking too long to get the req
+ 'your Matomo, but if it continues to occur for this report, then you may be requesting too much data. In this '
+ 'case, limit the data you are requesting to see it in Looker Studio.';

function getReportData(request: GoogleAppsScript.Data_Studio.Request<ConnectorParams>, requestedFields: { name: string }[]) {
function getReportData(
request: GoogleAppsScript.Data_Studio.Request<ConnectorParams>,
requestedFields: { name: string }[],
requiredTemporaryMetrics: string[],
) {
const idSite = request.configParams.idsite;
const report = request.configParams.report;
const segment = request.configParams.segment || '';
Expand All @@ -61,10 +65,17 @@ function getReportData(request: GoogleAppsScript.Data_Studio.Request<ConnectorPa
'Actions.getPageUrlsFollowingSiteSearch',
];

let showRawMetrics = requiredTemporaryMetrics.length ? '1' : '0';

let showColumns;
// showColumns does not work correctly with some API methods
if (!SHOW_COLUMNS_UNSUPPORTED_METHODS.includes(apiMethod)) {
showColumns = (requestedFields.map(({name}) => name)).join(',');
let showColumnsMetrics = requestedFields.map(({name}) => name);
if (requiredTemporaryMetrics.length) {
showColumnsMetrics.push(...requiredTemporaryMetrics);
}

showColumns = showColumnsMetrics.join(',');
}

let rowsToFetchAtATime = parseInt(env.MAX_ROWS_TO_FETCH_PER_REQUEST, 10) || 100000;
Expand Down Expand Up @@ -120,6 +131,7 @@ function getReportData(request: GoogleAppsScript.Data_Studio.Request<ConnectorPa
filter_limit: `${limitToUse}`,
filter_offset: `${offset}`,
showColumns,
showRawMetrics,
};

if (reportParams.apiModule !== 'Goals') {
Expand Down Expand Up @@ -216,10 +228,13 @@ export function getData(request: GoogleAppsScript.Data_Studio.Request<ConnectorP
}
requestedFields = requestedFields.filter(({ name }) => fields.getFieldById(name));

// get all temporary metrics required by requested processed metrics
const temporaryMetrics = fields.asArray().filter(f => f.isDefault()).map(f => f.getId());

// field instances can be garbage collected if we don't request them specifically first
const requestedFieldObjects = requestedFields.map(({ name }) => fields.getFieldById(name));

let reportData = getReportData(request, requestedFields);
let reportData = getReportData(request, requestedFields, temporaryMetrics);
if (reportData === null) {
const reportParams = JSON.parse(request.configParams.report);
throwUnexpectedError(`The "${reportParams.apiModule}.${reportParams.apiAction}" report cannot be found in the Matomo's report metadata.`);
Expand Down

0 comments on commit f054ad3

Please sign in to comment.