2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function googleApiRequest(
|
|
|
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2021-01-05 10:16:25 -08:00
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
2022-08-17 08:50:24 -07:00
|
|
|
Accept: 'application/json',
|
2021-01-05 10:16:25 -08:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
2023-01-13 09:11:56 -08:00
|
|
|
uri: uri ?? `https://analyticsreporting.googleapis.com${endpoint}`,
|
2021-01-05 10:16:25 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function googleApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
): Promise<any> {
|
2021-01-05 10:16:25 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, query, uri);
|
|
|
|
if (body.reportRequests && Array.isArray(body.reportRequests)) {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.reportRequests[0].pageToken = responseData[propertyName][0].nextPageToken;
|
2021-01-05 10:16:25 -08:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.pageToken = responseData.nextPageToken;
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
} while (
|
2022-12-02 12:54:28 -08:00
|
|
|
(responseData.nextPageToken !== undefined && responseData.nextPageToken !== '') ||
|
|
|
|
responseData[propertyName]?.[0].nextPageToken !== undefined
|
2021-01-05 10:16:25 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function simplify(responseData: any | [any]) {
|
2021-05-08 13:26:06 -07:00
|
|
|
const response = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
for (const {
|
|
|
|
columnHeader: { dimensions },
|
|
|
|
data: { rows },
|
|
|
|
} of responseData) {
|
2021-06-25 06:38:32 -07:00
|
|
|
if (rows === undefined) {
|
|
|
|
// Do not error if there is no data
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-08 13:26:06 -07:00
|
|
|
for (const row of rows) {
|
|
|
|
const data: IDataObject = {};
|
|
|
|
if (dimensions) {
|
|
|
|
for (let i = 0; i < dimensions.length; i++) {
|
|
|
|
data[dimensions[i]] = row.dimensions[i];
|
2022-12-02 12:54:28 -08:00
|
|
|
data.total = row.metrics[0].values.join(',');
|
2021-05-08 13:26:06 -07:00
|
|
|
}
|
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
data.total = row.metrics[0].values.join(',');
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function merge(responseData: [any]) {
|
|
|
|
const response: { columnHeader: IDataObject; data: { rows: [] } } = {
|
2021-05-08 13:26:06 -07:00
|
|
|
columnHeader: responseData[0].columnHeader,
|
|
|
|
data: responseData[0].data,
|
|
|
|
};
|
|
|
|
const allRows = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
for (const {
|
|
|
|
data: { rows },
|
|
|
|
} of responseData) {
|
2021-05-08 13:26:06 -07:00
|
|
|
allRows.push(...rows);
|
|
|
|
}
|
|
|
|
response.data.rows = allRows as [];
|
|
|
|
return [response];
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|