mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 14:44:05 -08:00
⚡ Add pagination to resource report for Google Analytics (#1755)
This commit is contained in:
parent
77da5b70bc
commit
3a1a5fd49c
|
@ -27,11 +27,13 @@ export async function googleApiRequest(this: IExecuteFunctions | IExecuteSingleF
|
|||
};
|
||||
|
||||
options = Object.assign({}, options, option);
|
||||
|
||||
try {
|
||||
if (Object.keys(body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
if (Object.keys(qs).length === 0) {
|
||||
delete options.qs;
|
||||
}
|
||||
//@ts-ignore
|
||||
return await this.helpers.requestOAuth2.call(this, 'googleAnalyticsOAuth2', options);
|
||||
|
||||
|
@ -45,12 +47,11 @@ export async function googleApiRequestAllItems(this: IExecuteFunctions | ILoadOp
|
|||
const returnData: IDataObject[] = [];
|
||||
|
||||
let responseData;
|
||||
body.pageSize = 100;
|
||||
|
||||
do {
|
||||
responseData = await googleApiRequest.call(this, method, endpoint, body, query, uri);
|
||||
if (body.reportRequests && Array.isArray(body.reportRequests)) {
|
||||
body.reportRequests[0].pageToken = responseData['nextPageToken'];
|
||||
body.reportRequests[0]['pageToken'] = responseData[propertyName][0].nextPageToken;
|
||||
} else {
|
||||
body.pageToken = responseData['nextPageToken'];
|
||||
}
|
||||
|
@ -58,17 +59,17 @@ export async function googleApiRequestAllItems(this: IExecuteFunctions | ILoadOp
|
|||
} while (
|
||||
(responseData['nextPageToken'] !== undefined &&
|
||||
responseData['nextPageToken'] !== '') ||
|
||||
(responseData['reports'] &&
|
||||
responseData['reports'][0].nextPageToken &&
|
||||
responseData['reports'][0].nextPageToken !== undefined)
|
||||
(responseData[propertyName] &&
|
||||
responseData[propertyName][0].nextPageToken &&
|
||||
responseData[propertyName][0].nextPageToken !== undefined)
|
||||
);
|
||||
|
||||
return returnData;
|
||||
}
|
||||
|
||||
export function simplify(responseData: any) { // tslint:disable-line:no-any
|
||||
const { columnHeader: { dimensions }, data: { rows } } = responseData[0];
|
||||
responseData = [];
|
||||
export function simplify(responseData: any | [any]) { // tslint:disable-line:no-any
|
||||
const response = [];
|
||||
for (const { columnHeader: { dimensions }, data: { rows } } of responseData) {
|
||||
for (const row of rows) {
|
||||
const data: IDataObject = {};
|
||||
if (dimensions) {
|
||||
|
@ -79,7 +80,21 @@ export function simplify(responseData: any) { // tslint:disable-line:no-any
|
|||
} else {
|
||||
data['total'] = row.metrics[0].values.join(',');
|
||||
}
|
||||
responseData.push(data);
|
||||
response.push(data);
|
||||
}
|
||||
return responseData;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export function merge(responseData: [any]) { // tslint:disable-line:no-any
|
||||
const response: { columnHeader: IDataObject, data: { rows: [] } } = {
|
||||
columnHeader: responseData[0].columnHeader,
|
||||
data: responseData[0].data,
|
||||
};
|
||||
const allRows = [];
|
||||
for (const { data: { rows } } of responseData) {
|
||||
allRows.push(...rows);
|
||||
}
|
||||
response.data.rows = allRows as [];
|
||||
return [response];
|
||||
}
|
||||
|
|
|
@ -24,13 +24,14 @@ import {
|
|||
import {
|
||||
googleApiRequest,
|
||||
googleApiRequestAllItems,
|
||||
merge,
|
||||
simplify,
|
||||
} from './GenericFunctions';
|
||||
|
||||
import * as moment from 'moment-timezone';
|
||||
|
||||
import {
|
||||
IData
|
||||
IData,
|
||||
} from './Interfaces';
|
||||
|
||||
export class GoogleAnalytics implements INodeType {
|
||||
|
@ -158,6 +159,7 @@ export class GoogleAnalytics implements INodeType {
|
|||
method = 'POST';
|
||||
endpoint = '/v4/reports:batchGet';
|
||||
const viewId = this.getNodeParameter('viewId', i) as string;
|
||||
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
||||
const additionalFields = this.getNodeParameter(
|
||||
'additionalFields',
|
||||
i,
|
||||
|
@ -211,11 +213,17 @@ export class GoogleAnalytics implements INodeType {
|
|||
Object.assign(body, { hideTotals: additionalFields.hideTotals });
|
||||
}
|
||||
|
||||
if (returnAll === true) {
|
||||
responseData = await googleApiRequestAllItems.call(this, 'reports', method, endpoint, { reportRequests: [body] }, qs);
|
||||
} else {
|
||||
responseData = await googleApiRequest.call(this, method, endpoint, { reportRequests: [body] }, qs);
|
||||
responseData = responseData.reports;
|
||||
}
|
||||
|
||||
if (simple === true) {
|
||||
responseData = simplify(responseData);
|
||||
} else if (returnAll === true && responseData.length > 1) {
|
||||
responseData = merge(responseData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,6 +49,47 @@ export const reportFields = [
|
|||
placeholder: '123456',
|
||||
description: 'The View ID of Google Analytics',
|
||||
},
|
||||
{
|
||||
displayName: 'Return All',
|
||||
name: 'returnAll',
|
||||
type: 'boolean',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
resource: [
|
||||
'report',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: false,
|
||||
description: 'If all results should be returned or only up to a given limit.',
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'limit',
|
||||
type: 'number',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
resource: [
|
||||
'report',
|
||||
],
|
||||
returnAll: [
|
||||
false,
|
||||
],
|
||||
},
|
||||
},
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
maxValue: 1000,
|
||||
},
|
||||
default: 1000,
|
||||
description: 'How many results to return.',
|
||||
},
|
||||
{
|
||||
displayName: 'Simple',
|
||||
name: 'simple',
|
||||
|
|
Loading…
Reference in a new issue