2021-01-05 10:16:25 -08:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError,
|
2021-01-05 10:16:25 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
export async function googleApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string,
|
|
|
|
endpoint: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: uri || `https://analyticsreporting.googleapis.com${endpoint}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
try {
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
2021-05-08 13:26:06 -07:00
|
|
|
if (Object.keys(qs).length === 0) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
2021-01-05 10:16:25 -08:00
|
|
|
//@ts-ignore
|
|
|
|
return await this.helpers.requestOAuth2.call(this, 'googleAnalyticsOAuth2', options);
|
|
|
|
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function googleApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, query, uri);
|
|
|
|
if (body.reportRequests && Array.isArray(body.reportRequests)) {
|
2021-05-08 13:26:06 -07:00
|
|
|
body.reportRequests[0]['pageToken'] = responseData[propertyName][0].nextPageToken;
|
2021-01-05 10:16:25 -08:00
|
|
|
} else {
|
|
|
|
body.pageToken = responseData['nextPageToken'];
|
|
|
|
}
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
} while (
|
|
|
|
(responseData['nextPageToken'] !== undefined &&
|
|
|
|
responseData['nextPageToken'] !== '') ||
|
2021-05-08 13:26:06 -07:00
|
|
|
(responseData[propertyName] &&
|
|
|
|
responseData[propertyName][0].nextPageToken &&
|
|
|
|
responseData[propertyName][0].nextPageToken !== undefined)
|
2021-01-05 10:16:25 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2021-05-08 13:26:06 -07:00
|
|
|
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) {
|
|
|
|
for (let i = 0; i < dimensions.length; i++) {
|
|
|
|
data[dimensions[i]] = row.dimensions[i];
|
|
|
|
data['total'] = row.metrics[0].values.join(',');
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-05 10:16:25 -08:00
|
|
|
data['total'] = row.metrics[0].values.join(',');
|
|
|
|
}
|
2021-05-08 13:26:06 -07:00
|
|
|
response.push(data);
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
2021-05-08 13:26:06 -07:00
|
|
|
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];
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|