2020-09-02 03:25:11 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2020-09-02 03:25:11 -07:00
|
|
|
} from 'n8n-workflow';
|
2020-10-01 05:01:39 -07:00
|
|
|
import {
|
|
|
|
customerIoApiRequest,
|
|
|
|
validateJSON,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
import {
|
|
|
|
campaignFields,
|
|
|
|
campaignOperations,
|
|
|
|
} from './CampaignDescription';
|
|
|
|
import {
|
|
|
|
customerFields,
|
|
|
|
customerOperations,
|
|
|
|
} from './CustomerDescription';
|
|
|
|
import {
|
|
|
|
eventFields,
|
|
|
|
eventOperations,
|
|
|
|
} from './EventDescription';
|
|
|
|
import {
|
|
|
|
segmentFields,
|
|
|
|
segmentOperations,
|
|
|
|
} from './SegmentDescription';
|
2020-09-02 03:25:11 -07:00
|
|
|
|
|
|
|
|
|
|
|
export class CustomerIo implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Customer.io',
|
2020-09-02 03:32:12 -07:00
|
|
|
name: 'customerIo',
|
2021-06-12 12:00:37 -07:00
|
|
|
icon: 'file:customerio.svg',
|
2020-09-02 03:25:11 -07:00
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Customer.io API',
|
|
|
|
defaults: {
|
|
|
|
name: 'CustomerIo',
|
|
|
|
color: '#ffcd00',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'customerIoApi',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-09-02 03:25:11 -07:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Customer',
|
|
|
|
value: 'customer',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Event',
|
|
|
|
value: 'event',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Campaign',
|
|
|
|
value: 'campaign',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Segment',
|
|
|
|
value: 'segment',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'customer',
|
|
|
|
description: 'Resource to consume.',
|
|
|
|
},
|
2020-09-02 03:32:12 -07:00
|
|
|
// CAMPAIGN
|
|
|
|
...campaignOperations,
|
|
|
|
...campaignFields,
|
|
|
|
// CUSTOMER
|
|
|
|
...customerOperations,
|
|
|
|
...customerFields,
|
|
|
|
// EVENT
|
|
|
|
...eventOperations,
|
|
|
|
...eventFields,
|
|
|
|
// SEGMENT
|
|
|
|
...segmentOperations,
|
2020-10-22 06:46:03 -07:00
|
|
|
...segmentFields,
|
2020-09-02 03:25:11 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
const items = this.getInputData();
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
2020-09-02 03:32:12 -07:00
|
|
|
const body: IDataObject = {};
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2020-09-02 03:32:12 -07:00
|
|
|
let responseData;
|
2020-09-02 03:25:11 -07:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'campaign') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
const campaignId = this.getNodeParameter('campaignId', i) as number;
|
|
|
|
const endpoint = `/campaigns/${campaignId}`;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await customerIoApiRequest.call(this, 'GET', endpoint, body, 'beta');
|
|
|
|
responseData = responseData.campaign;
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
|
|
|
const endpoint = `/campaigns`;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await customerIoApiRequest.call(this, 'GET', endpoint, body, 'beta');
|
|
|
|
responseData = responseData.campaigns;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getMetrics') {
|
|
|
|
const campaignId = this.getNodeParameter('campaignId', i) as number;
|
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (jsonParameters) {
|
|
|
|
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFieldsJson !== '') {
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be a valid JSON');
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
const period = this.getNodeParameter('period', i) as string;
|
|
|
|
let endpoint = `/campaigns/${campaignId}/metrics`;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (period !== 'days') {
|
|
|
|
endpoint = `${endpoint}?period=${period}`;
|
|
|
|
}
|
|
|
|
if (additionalFields.steps) {
|
|
|
|
body.steps = additionalFields.steps as number;
|
|
|
|
}
|
|
|
|
if (additionalFields.type) {
|
|
|
|
if (additionalFields.type === 'urbanAirship') {
|
|
|
|
additionalFields.type = 'urban_airship';
|
|
|
|
} else {
|
|
|
|
body.type = additionalFields.type as string;
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await customerIoApiRequest.call(this, 'GET', endpoint, body, 'beta');
|
|
|
|
responseData = responseData.metric;
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'customer') {
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'upsert') {
|
|
|
|
const id = this.getNodeParameter('id', i) as number;
|
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (jsonParameters) {
|
|
|
|
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFieldsJson !== '') {
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be a valid JSON');
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.customProperties) {
|
|
|
|
const data: any = {}; // tslint:disable-line:no-any
|
|
|
|
//@ts-ignore
|
|
|
|
additionalFields.customProperties.customProperty.map(property => {
|
|
|
|
data[property.key] = property.value;
|
|
|
|
});
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.data = data;
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.email) {
|
|
|
|
body.email = additionalFields.email as string;
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.createdAt) {
|
|
|
|
body.created_at = new Date(additionalFields.createdAt as string).getTime() / 1000;
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/customers/${id}`;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await customerIoApiRequest.call(this, 'PUT', endpoint, body, 'tracking');
|
2020-09-02 03:32:12 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = Object.assign({ id }, body);
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
|
|
|
const id = this.getNodeParameter('id', i) as number;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.id = id;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/customers/${id}`;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
await customerIoApiRequest.call(this, 'DELETE', endpoint, body, 'tracking');
|
2020-09-02 03:32:12 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = {
|
|
|
|
success: true,
|
|
|
|
};
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'event') {
|
|
|
|
if (operation === 'track') {
|
|
|
|
const customerId = this.getNodeParameter('customerId', i) as number;
|
|
|
|
const eventName = this.getNodeParameter('eventName', i) as string;
|
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.name = eventName;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (jsonParameters) {
|
|
|
|
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFieldsJson !== '') {
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be a valid JSON');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
const data: any = {}; // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
if (additionalFields.customAttributes) {
|
|
|
|
//@ts-ignore
|
|
|
|
additionalFields.customAttributes.customAttribute.map(property => {
|
|
|
|
data[property.key] = property.value;
|
|
|
|
});
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.type) {
|
|
|
|
data.type = additionalFields.type as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
body.data = data;
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const endpoint = `/customers/${customerId}/events`;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
await customerIoApiRequest.call(this, 'POST', endpoint, body, 'tracking');
|
|
|
|
responseData = {
|
|
|
|
success: true,
|
|
|
|
};
|
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'trackAnonymous') {
|
|
|
|
const eventName = this.getNodeParameter('eventName', i) as string;
|
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i) as boolean;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.name = eventName;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (jsonParameters) {
|
|
|
|
const additionalFieldsJson = this.getNodeParameter('additionalFieldsJson', i) as string;
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFieldsJson !== '') {
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Additional fields must be a valid JSON');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
const data: any = {}; // tslint:disable-line:no-any
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.customAttributes) {
|
|
|
|
//@ts-ignore
|
|
|
|
additionalFields.customAttributes.customAttribute.map(property => {
|
|
|
|
data[property.key] = property.value;
|
|
|
|
});
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
body.data = data;
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
const endpoint = `/events`;
|
|
|
|
await customerIoApiRequest.call(this, 'POST', endpoint, body, 'tracking');
|
|
|
|
|
|
|
|
responseData = {
|
|
|
|
success: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resource === 'segment') {
|
|
|
|
const segmentId = this.getNodeParameter('segmentId', i) as number;
|
|
|
|
const customerIds = this.getNodeParameter('customerIds', i) as string;
|
|
|
|
|
|
|
|
body.id = segmentId;
|
|
|
|
body.ids = customerIds.split(',');
|
|
|
|
|
|
|
|
let endpoint = '';
|
|
|
|
|
|
|
|
if (operation === 'add') {
|
|
|
|
endpoint = `/segments/${segmentId}/add_customers`;
|
2020-09-02 03:25:11 -07:00
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
endpoint = `/segments/${segmentId}/remove_customers`;
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await customerIoApiRequest.call(this, 'POST', endpoint, body, 'tracking');
|
2020-09-02 03:32:12 -07:00
|
|
|
|
|
|
|
responseData = {
|
|
|
|
success: true,
|
|
|
|
};
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
2020-09-02 03:25:11 -07:00
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push(responseData as unknown as IDataObject);
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|