feat(Google Analytics Node): Overhaul for google analytics node

This commit is contained in:
Michael Kret
2023-01-20 17:00:47 +02:00
committed by GitHub
parent e810966a3b
commit 736e700902
22 changed files with 3173 additions and 309 deletions

View File

@@ -0,0 +1,125 @@
import { OptionsWithUri } from 'request';
import { IExecuteFunctions, IExecuteSingleFunctions, ILoadOptionsFunctions } from 'n8n-core';
import { IDataObject, NodeApiError } from 'n8n-workflow';
export async function googleApiRequest(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: IDataObject = {},
qs: IDataObject = {},
uri?: string,
option: IDataObject = {},
) {
const baseURL = 'https://analyticsreporting.googleapis.com';
let options: OptionsWithUri = {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
method,
body,
qs,
uri: uri || `${baseURL}${endpoint}`,
json: true,
};
options = Object.assign({}, options, option);
try {
if (Object.keys(body).length === 0) {
delete options.body;
}
if (Object.keys(qs).length === 0) {
delete options.qs;
}
return await this.helpers.requestOAuth2.call(this, 'googleAnalyticsOAuth2', options);
} catch (error) {
const errorData = (error.message || '').split(' - ')[1] as string;
if (errorData) {
const parsedError = JSON.parse(errorData.trim());
const [message, ...rest] = parsedError.error.message.split('\n');
const description = rest.join('\n');
const httpCode = parsedError.error.code;
throw new NodeApiError(this.getNode(), error, { message, description, httpCode });
}
throw new NodeApiError(this.getNode(), error, { message: error.message });
}
}
export async function googleApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions,
propertyName: string,
method: string,
endpoint: string,
body: IDataObject = {},
query: IDataObject = {},
uri?: string,
) {
const returnData: IDataObject[] = [];
let responseData;
do {
responseData = await googleApiRequest.call(this, method, endpoint, body, query, uri);
if (body.reportRequests && Array.isArray(body.reportRequests)) {
(body.reportRequests as IDataObject[])[0].pageToken =
responseData[propertyName][0].nextPageToken;
} else {
body.pageToken = responseData.nextPageToken;
}
returnData.push.apply(returnData, responseData[propertyName]);
} while (
(responseData.nextPageToken !== undefined && responseData.nextPageToken !== '') ||
responseData[propertyName]?.[0].nextPageToken !== undefined
);
return returnData;
}
export function simplify(responseData: any | [any]) {
const response = [];
for (const {
columnHeader: { dimensions, metricHeader },
data: { rows },
} of responseData) {
if (rows === undefined) {
// Do not error if there is no data
continue;
}
const metrics = metricHeader.metricHeaderEntries.map((entry: { name: string }) => entry.name);
for (const row of rows) {
const data: IDataObject = {};
if (dimensions) {
for (let i = 0; i < dimensions.length; i++) {
data[dimensions[i]] = row.dimensions[i];
for (const [index, metric] of metrics.entries()) {
data[metric] = row.metrics[0].values[index];
}
}
} else {
for (const [index, metric] of metrics.entries()) {
data[metric] = row.metrics[0].values[index];
}
}
response.push(data);
}
}
return response;
}
export function merge(responseData: [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];
}

View File

@@ -0,0 +1,305 @@
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeBaseDescription,
INodeTypeDescription,
} from 'n8n-workflow';
import { reportFields, reportOperations } from './ReportDescription';
import { userActivityFields, userActivityOperations } from './UserActivityDescription';
import { googleApiRequest, googleApiRequestAllItems, merge, simplify } from './GenericFunctions';
import moment from 'moment-timezone';
import { IData } from './Interfaces';
const versionDescription: 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',
noDataExpression: true,
options: [
{
name: 'Report',
value: 'report',
},
{
name: 'User Activity',
value: 'userActivity',
},
],
default: 'report',
},
//-------------------------------
// Reports Operations
//-------------------------------
...reportOperations,
...reportFields,
//-------------------------------
// User Activity Operations
//-------------------------------
...userActivityOperations,
...userActivityFields,
],
};
export class GoogleAnalyticsV1 implements INodeType {
description: INodeTypeDescription;
constructor(baseDescription: INodeTypeBaseDescription) {
this.description = {
...baseDescription,
...versionDescription,
};
}
methods = {
loadOptions: {
// Get all the dimensions to display them to user so that he can
// select them easily
async getDimensions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
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) {
if (
dimesion.attributes.type === 'DIMENSION' &&
dimesion.attributes.status !== 'DEPRECATED'
) {
returnData.push({
name: dimesion.attributes.uiName,
value: dimesion.id,
description: dimesion.attributes.description,
});
}
}
returnData.sort((a, b) => {
const aName = a.name.toLowerCase();
const bName = b.name.toLowerCase();
if (aName < bName) {
return -1;
}
if (aName > bName) {
return 1;
}
return 0;
});
return returnData;
},
// Get all the views to display them to user so that he can
// select them easily
async getViews(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
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();
const returnData: INodeExecutionData[] = [];
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
let method = '';
const qs: IDataObject = {};
let endpoint = '';
let responseData;
for (let i = 0; i < items.length; i++) {
try {
if (resource === 'report') {
if (operation === 'get') {
//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;
const returnAll = this.getNodeParameter('returnAll', 0);
const additionalFields = this.getNodeParameter('additionalFields', i);
const simple = this.getNodeParameter('simple', i) as boolean;
const body: IData = {
viewId,
};
if (additionalFields.useResourceQuotas) {
qs.useResourceQuotas = additionalFields.useResourceQuotas;
}
if (additionalFields.dateRangesUi) {
const dateValues = (additionalFields.dateRangesUi as IDataObject)
.dateRanges as IDataObject;
if (dateValues) {
const start = dateValues.startDate as string;
const end = dateValues.endDate as string;
Object.assign(body, {
dateRanges: [
{
startDate: moment(start).utc().format('YYYY-MM-DD'),
endDate: moment(end).utc().format('YYYY-MM-DD'),
},
],
});
}
}
if (additionalFields.metricsUi) {
const metrics = (additionalFields.metricsUi as IDataObject)
.metricValues as IDataObject[];
body.metrics = metrics;
}
if (additionalFields.dimensionUi) {
const dimensions = (additionalFields.dimensionUi as IDataObject)
.dimensionValues as IDataObject[];
if (dimensions) {
body.dimensions = dimensions;
}
}
if (additionalFields.dimensionFiltersUi) {
const dimensionFilters = (additionalFields.dimensionFiltersUi as IDataObject)
.filterValues as IDataObject[];
if (dimensionFilters) {
dimensionFilters.forEach((filter) => (filter.expressions = [filter.expressions]));
body.dimensionFilterClauses = { filters: dimensionFilters };
}
}
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 });
}
if (returnAll) {
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) {
responseData = simplify(responseData);
} else if (returnAll && responseData.length > 1) {
responseData = merge(responseData);
}
}
}
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);
const returnAll = this.getNodeParameter('returnAll', 0);
const additionalFields = this.getNodeParameter('additionalFields', i);
const body: IDataObject = {
viewId,
user: {
userId,
},
};
if (additionalFields.activityTypes) {
Object.assign(body, { activityTypes: additionalFields.activityTypes });
}
if (returnAll) {
responseData = await googleApiRequestAllItems.call(
this,
'sessions',
method,
endpoint,
body,
);
} else {
body.pageSize = this.getNodeParameter('limit', 0);
responseData = await googleApiRequest.call(this, method, endpoint, body);
responseData = responseData.sessions;
}
}
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: i } },
);
returnData.push(...executionData);
} catch (error) {
if (this.continueOnFail()) {
const executionErrorData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray({ error: error.message }),
{ itemData: { item: i } },
);
returnData.push(...executionErrorData);
continue;
}
throw error;
}
}
return this.prepareOutputData(returnData);
}
}

View File

@@ -0,0 +1,28 @@
import { IDataObject } from 'n8n-workflow';
export interface IData {
viewId: string;
dimensions?: IDimension[];
dimensionFilterClauses?: {
filters: IDimensionFilter[];
};
pageSize?: number;
metrics?: IMetric[];
dateRanges?: IDataObject[];
}
export interface IDimension {
name?: string;
histogramBuckets?: string[];
}
export interface IDimensionFilter {
dimensionName?: string;
operator?: string;
expressions?: string[];
}
export interface IMetric {
expression?: string;
alias?: string;
formattingType?: string;
}

View File

@@ -0,0 +1,339 @@
import { INodeProperties } from 'n8n-workflow';
export const reportOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['report'],
},
},
options: [
{
name: 'Get',
value: 'get',
description: 'Return the analytics data',
action: 'Get a report',
},
],
default: 'get',
},
];
export const reportFields: INodeProperties[] = [
{
displayName: 'View Name or ID',
name: 'viewId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getViews',
},
default: '',
required: true,
displayOptions: {
show: {
resource: ['report'],
operation: ['get'],
},
},
placeholder: '123456',
description:
'The View ID of Google Analytics. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: ['get'],
resource: ['report'],
},
},
default: false,
description: 'Whether to return all results 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: 'Max number of results to return',
},
{
displayName: 'Simplify',
name: 'simple',
type: 'boolean',
displayOptions: {
show: {
operation: ['get'],
resource: ['report'],
},
},
default: true,
description: 'Whether to return a simplified version of the response instead of the raw data',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: ['report'],
operation: ['get'],
},
},
options: [
{
displayName: 'Date Ranges',
name: 'dateRangesUi',
placeholder: 'Add Date Range',
type: 'fixedCollection',
default: {},
description: 'Date ranges in the request',
options: [
{
displayName: 'Date Range',
name: 'dateRanges',
values: [
{
displayName: 'Start Date',
name: 'startDate',
type: 'dateTime',
default: '',
},
{
displayName: 'End Date',
name: 'endDate',
type: 'dateTime',
default: '',
},
],
},
],
},
{
displayName: 'Dimensions',
name: 'dimensionUi',
type: 'fixedCollection',
default: {},
typeOptions: {
multipleValues: true,
},
placeholder: 'Add Dimension',
description:
'Dimensions are attributes of your data. For example, the dimension ga:city indicates the city, for example, "Paris" or "New York", from which a session originates.',
options: [
{
displayName: 'Dimension',
name: 'dimensionValues',
values: [
{
displayName: 'Name or ID',
name: 'name',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getDimensions',
},
default: '',
description:
'Name of the dimension to fetch, for example ga:browser. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
],
},
],
},
{
displayName: 'Dimension Filters',
name: 'dimensionFiltersUi',
type: 'fixedCollection',
default: {},
typeOptions: {
multipleValues: true,
},
placeholder: 'Add Dimension Filter',
description: 'Dimension Filters in the request',
options: [
{
displayName: 'Filters',
name: 'filterValues',
values: [
{
displayName: 'Dimension Name or ID',
name: 'dimensionName',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getDimensions',
},
default: '',
description:
'Name of the dimension to filter by. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
// https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#Operator
{
displayName: 'Operator',
name: 'operator',
type: 'options',
default: 'EXACT',
description: 'Operator to use in combination with value',
options: [
{
name: 'Begins With',
value: 'BEGINS_WITH',
},
{
name: 'Ends With',
value: 'ENDS_WITH',
},
{
name: 'Equal (Number)',
value: 'NUMERIC_EQUAL',
},
{
name: 'Exact',
value: 'EXACT',
},
{
name: 'Greater Than (Number)',
value: 'NUMERIC_GREATER_THAN',
},
{
name: 'Less Than (Number)',
value: 'NUMERIC_LESS_THAN',
},
{
name: 'Partial',
value: 'PARTIAL',
},
{
name: 'Regular Expression',
value: 'REGEXP',
},
],
},
{
displayName: 'Value',
name: 'expressions',
type: 'string',
default: '',
placeholder: 'ga:newUsers',
description:
'String or <a href="https://support.google.com/analytics/answer/1034324?hl=en">regular expression</a> to match against',
},
],
},
],
},
{
displayName: 'Hide Totals',
name: 'hideTotals',
type: 'boolean',
default: false,
description:
'Whether to hide the total of all metrics for all the matching rows, for every date range',
},
{
displayName: 'Hide Value Ranges',
name: 'hideValueRanges',
type: 'boolean',
default: false,
description: 'Whether to hide the minimum and maximum across all matching rows',
},
{
displayName: 'Include Empty Rows',
name: 'includeEmptyRows',
type: 'boolean',
default: false,
description:
'Whether the response exclude rows if all the retrieved metrics are equal to zero',
},
{
displayName: 'Metrics',
name: 'metricsUi',
type: 'fixedCollection',
default: {},
typeOptions: {
multipleValues: true,
},
placeholder: 'Add Metrics',
description: 'Metrics in the request',
options: [
{
displayName: 'Metric',
name: 'metricValues',
values: [
{
displayName: 'Alias',
name: 'alias',
type: 'string',
default: '',
description:
'An alias for the metric expression is an alternate name for the expression. The alias can be used for filtering and sorting.',
},
{
displayName: 'Expression',
name: 'expression',
type: 'string',
default: 'ga:newUsers',
description:
'<p>A metric expression in the request. An expression is constructed from one or more metrics and numbers.</p><p>Accepted operators include: Plus (+), Minus (-), Negation (Unary -), Divided by (/), Multiplied by (*), Parenthesis, Positive cardinal numbers (0-9), can include decimals and is limited to 1024 characters.</p><p>Example ga:totalRefunds/ga:users, in most cases the metric expression is just a single metric name like ga:users.</p><p>Adding mixed MetricType (E.g., CURRENCY + PERCENTAGE) metrics will result in unexpected results.</p>.',
},
{
displayName: 'Formatting Type',
name: 'formattingType',
type: 'options',
default: 'INTEGER',
description: 'Specifies how the metric expression should be formatted',
options: [
{
name: 'Currency',
value: 'CURRENCY',
},
{
name: 'Float',
value: 'FLOAT',
},
{
name: 'Integer',
value: 'INTEGER',
},
{
name: 'Percent',
value: 'PERCENT',
},
{
name: 'Time',
value: 'TIME',
},
],
},
],
},
],
},
{
displayName: 'Use Resource Quotas',
name: 'useResourceQuotas',
type: 'boolean',
default: false,
description: 'Whether to enable resource based quotas',
},
],
},
];

View File

@@ -0,0 +1,136 @@
import { INodeProperties } from 'n8n-workflow';
export const userActivityOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['userActivity'],
},
},
options: [
{
name: 'Search',
value: 'search',
description: 'Return user activity data',
action: 'Search user activity data',
},
],
default: 'search',
},
];
export const userActivityFields: INodeProperties[] = [
{
displayName: 'View Name or ID',
name: 'viewId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getViews',
},
default: '',
required: true,
displayOptions: {
show: {
resource: ['userActivity'],
operation: ['search'],
},
},
placeholder: '123456',
description:
'The View ID of Google Analytics. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'User ID',
name: 'userId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: ['userActivity'],
operation: ['search'],
},
},
placeholder: '123456',
description: 'ID of a user',
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: ['search'],
resource: ['userActivity'],
},
},
default: false,
description: 'Whether to return all results or only up to a given limit',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: ['search'],
resource: ['userActivity'],
returnAll: [false],
},
},
typeOptions: {
minValue: 1,
maxValue: 500,
},
default: 100,
description: 'Max number of results to return',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
operation: ['search'],
resource: ['userActivity'],
},
},
options: [
{
displayName: 'Activity Types',
name: 'activityTypes',
type: 'multiOptions',
options: [
{
name: 'Ecommerce',
value: 'ECOMMERCE',
},
{
name: 'Event',
value: 'EVENT',
},
{
name: 'Goal',
value: 'GOAL',
},
{
name: 'Pageview',
value: 'PAGEVIEW',
},
{
name: 'Screenview',
value: 'SCREENVIEW',
},
],
description: 'Type of activites requested',
default: [],
},
],
},
];