2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2021-01-05 10:16:25 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { reportFields, reportOperations } from './ReportDescription';
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { userActivityFields, userActivityOperations } from './UserActivityDescription';
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { googleApiRequest, googleApiRequestAllItems, merge, simplify } from './GenericFunctions';
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment-timezone';
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IData } from './Interfaces';
|
2021-01-05 10:16:25 -08:00
|
|
|
|
|
|
|
export class GoogleAnalytics implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Google Analytics',
|
|
|
|
name: 'googleAnalytics',
|
|
|
|
icon: 'file:analytics.svg',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Use the Google Analytics API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Google Analytics',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'googleAnalyticsOAuth2',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-01-05 10:16:25 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Report',
|
|
|
|
value: 'report',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'User Activity',
|
|
|
|
value: 'userActivity',
|
|
|
|
},
|
|
|
|
],
|
2021-05-08 13:26:06 -07:00
|
|
|
default: 'report',
|
2021-01-05 10:16:25 -08:00
|
|
|
},
|
|
|
|
//-------------------------------
|
|
|
|
// Reports Operations
|
|
|
|
//-------------------------------
|
|
|
|
...reportOperations,
|
|
|
|
...reportFields,
|
|
|
|
|
|
|
|
//-------------------------------
|
|
|
|
// User Activity Operations
|
|
|
|
//-------------------------------
|
|
|
|
...userActivityOperations,
|
|
|
|
...userActivityFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the dimensions to display them to user so that he can
|
|
|
|
// select them easily
|
2022-08-17 08:50:24 -07:00
|
|
|
async getDimensions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2021-01-05 10:16:25 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const { items: dimensions } = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'',
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
'https://www.googleapis.com/analytics/v3/metadata/ga/columns',
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const dimesion of dimensions) {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (
|
|
|
|
dimesion.attributes.type === 'DIMENSION' &&
|
|
|
|
dimesion.attributes.status !== 'DEPRECATED'
|
|
|
|
) {
|
2021-01-05 10:16:25 -08:00
|
|
|
returnData.push({
|
|
|
|
name: dimesion.attributes.uiName,
|
|
|
|
value: dimesion.id,
|
|
|
|
description: dimesion.attributes.description,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 06:38:32 -07:00
|
|
|
|
|
|
|
returnData.sort((a, b) => {
|
2022-08-17 08:50:24 -07:00
|
|
|
const aName = a.name.toLowerCase();
|
|
|
|
const bName = b.name.toLowerCase();
|
|
|
|
if (aName < bName) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (aName > bName) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-06-25 06:38:32 -07:00
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
2021-01-05 10:16:25 -08:00
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
// Get all the views to display them to user so that he can
|
|
|
|
// select them easily
|
2022-08-17 08:50:24 -07:00
|
|
|
async getViews(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2021-01-05 10:16:25 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const { items } = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'',
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
'https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties/~all/profiles',
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const item of items) {
|
|
|
|
returnData.push({
|
|
|
|
name: item.name,
|
|
|
|
value: item.id,
|
|
|
|
description: item.websiteUrl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-01-05 10:16:25 -08:00
|
|
|
|
|
|
|
let method = '';
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
let endpoint = '';
|
|
|
|
let responseData;
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (resource === 'report') {
|
|
|
|
if (operation === 'get') {
|
2021-07-19 23:58:54 -07:00
|
|
|
//https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet
|
|
|
|
method = 'POST';
|
|
|
|
endpoint = '/v4/reports:batchGet';
|
|
|
|
const viewId = this.getNodeParameter('viewId', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IData = {
|
|
|
|
viewId,
|
|
|
|
};
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.useResourceQuotas) {
|
|
|
|
qs.useResourceQuotas = additionalFields.useResourceQuotas;
|
|
|
|
}
|
|
|
|
if (additionalFields.dateRangesUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const dateValues = (additionalFields.dateRangesUi as IDataObject)
|
|
|
|
.dateRanges as IDataObject;
|
2021-07-19 23:58:54 -07:00
|
|
|
if (dateValues) {
|
|
|
|
const start = dateValues.startDate as string;
|
|
|
|
const end = dateValues.endDate as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
Object.assign(body, {
|
|
|
|
dateRanges: [
|
|
|
|
{
|
|
|
|
startDate: moment(start).utc().format('YYYY-MM-DD'),
|
|
|
|
endDate: moment(end).utc().format('YYYY-MM-DD'),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.metricsUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const metrics = (additionalFields.metricsUi as IDataObject)
|
|
|
|
.metricValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
body.metrics = metrics;
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.dimensionUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const dimensions = (additionalFields.dimensionUi as IDataObject)
|
|
|
|
.dimensionValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (dimensions) {
|
|
|
|
body.dimensions = dimensions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (additionalFields.dimensionFiltersUi) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const dimensionFilters = (additionalFields.dimensionFiltersUi as IDataObject)
|
|
|
|
.filterValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (dimensionFilters) {
|
2022-08-17 08:50:24 -07:00
|
|
|
dimensionFilters.forEach((filter) => (filter.expressions = [filter.expressions]));
|
2021-07-19 23:58:54 -07:00
|
|
|
body.dimensionFilterClauses = { filters: dimensionFilters };
|
|
|
|
}
|
2021-06-25 06:51:02 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.includeEmptyRows) {
|
|
|
|
Object.assign(body, { includeEmptyRows: additionalFields.includeEmptyRows });
|
|
|
|
}
|
|
|
|
if (additionalFields.hideTotals) {
|
|
|
|
Object.assign(body, { hideTotals: additionalFields.hideTotals });
|
|
|
|
}
|
|
|
|
if (additionalFields.hideValueRanges) {
|
|
|
|
Object.assign(body, { hideTotals: additionalFields.hideTotals });
|
|
|
|
}
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'reports',
|
|
|
|
method,
|
|
|
|
endpoint,
|
|
|
|
{ reportRequests: [body] },
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
method,
|
|
|
|
endpoint,
|
|
|
|
{ reportRequests: [body] },
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.reports;
|
|
|
|
}
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (simple) {
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = simplify(responseData);
|
2022-12-02 12:54:28 -08:00
|
|
|
} else if (returnAll && responseData.length > 1) {
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = merge(responseData);
|
|
|
|
}
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'userActivity') {
|
|
|
|
if (operation === 'search') {
|
|
|
|
//https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/userActivity/search
|
|
|
|
method = 'POST';
|
|
|
|
endpoint = '/v4/userActivity:search';
|
|
|
|
const viewId = this.getNodeParameter('viewId', i);
|
|
|
|
const userId = this.getNodeParameter('userId', i);
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
viewId,
|
|
|
|
user: {
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if (additionalFields.activityTypes) {
|
|
|
|
Object.assign(body, { activityTypes: additionalFields.activityTypes });
|
|
|
|
}
|
2021-01-05 10:16:25 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'sessions',
|
|
|
|
method,
|
|
|
|
endpoint,
|
|
|
|
body,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
body.pageSize = this.getNodeParameter('limit', 0);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body);
|
|
|
|
responseData = responseData.sessions;
|
|
|
|
}
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2021-01-05 10:16:25 -08:00
|
|
|
}
|
|
|
|
}
|