2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2020-09-02 03:25:11 -07:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2020-09-02 03:25:11 -07:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2022-08-01 13:47:55 -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',
|
2024-06-06 04:34:30 -07:00
|
|
|
icon: { light: 'file:customerio.svg', dark: 'file:customerio.dark.svg' },
|
2020-09-02 03:25:11 -07:00
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Customer.io API',
|
|
|
|
defaults: {
|
feat(editor): Node creator actions (#4696)
* WIP: Node Actions List UI
* WIP: Recommended Actions and preseting of fields
* WIP: Resource category
* :art: Moved actions categorisation to the server
* :label: Add missing INodeAction type
* :sparkles: Improve SSR categorisation, fix adding of mixed actions
* :recycle: Refactor CategorizedItems to composition api, style fixes
* WIP: Adding multiple nodes
* :recycle: Refactor rest of the NodeCreator component to composition API, conver globalLinkActions to composable
* :sparkles: Allow actions dragging, fix search and refactor passing of actions to categorized items
* :lipstick: Fix node actions title
* Migrate to the pinia store, add posthog feature and various fixes
* :bug: Fix filtering of trigger actions when not merged
* fix: N8N-5439 — Do not use simple node item when at NodeHelperPanel root
* :bug: Design review fixes
* :bug: Fix disabling of merged actions
* Fix trigger root filtering
* :sparkles: Allow for custom node actions parser, introduce hubspot parser
* :bug: Fix initial node params validation, fix position of second added node
* :bug: Introduce operations category, removed canvas node names overrride, fix API actions display and prevent dragging of action nodes
* :sparkles: Prevent NDV auto-open feature flag
* :bug: Inject recommened action for trigger nodes without actions
* Refactored NodeCreatorNode to Storybook, change filtering of merged nodes for the trigger helper panel, minor fixes
* Improve rendering of app nodes and animation
* Cleanup, any only enable accordion transition on triggerhelperpanel
* Hide node creator scrollbars in Firefox
* Minor styles fixes
* Do not copy the array in rendering method
* Removed unused props
* Fix memory leak
* Fix categorisation of regular nodes with a single resource
* Implement telemetry calls for node actions
* Move categorization to FE
* Fix client side actions categorisation
* Skip custom action show
* Only load tooltip for NodeIcon if necessary
* Fix lodash startCase import
* Remove lodash.startcase
* Cleanup
* Fix node creator autofocus on "tab"
* Prevent posthog getFeatureFlag from crashing
* Debugging preview env search issues
* Remove logs
* Make sure the pre-filled params are update not overwritten
* Get rid of transition in itemiterator
* WIP: Rough version of NodeActions keyboard navigation, replace nodeCreator composable with Pinia store module
* Rewrite to add support for ActionItem to ItemIterator and make CategorizedItems accept items props
* Fix category item counter & cleanup
* Add APIHint to actions search no-result, clean up NodeCreatorNode
* Improve node actions no results message
* Remove logging, fix filtering of recommended placeholder category
* Remove unused NodeActions component and node merging feature falg
* Do not show regular nodes without actions
* Make sure to add manual trigger when adding http node via actions hint
* Fixed api hint footer line height
* Prevent pointer-events od NodeIcon img and remove "this" from template
* Address PR points
* Fix e2e specs
* Make sure canvas ia loaded
* Make sure canvas ia loaded before opening nodeCreator in e2e spec
* Fix flaky workflows tags e2e getter
* Imrpove node creator click outside UX, add manual node to regular nodes added from trigger panel
* Add manual trigger node if dragging regular from trigger panel
2022-12-09 01:56:36 -08:00
|
|
|
name: 'Customer.io',
|
2020-09-02 03:25:11 -07:00
|
|
|
},
|
|
|
|
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',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-09-02 03:25:11 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Customer',
|
|
|
|
value: 'customer',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Event',
|
|
|
|
value: 'event',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Campaign',
|
|
|
|
value: 'campaign',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Segment',
|
|
|
|
value: 'segment',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'customer',
|
|
|
|
},
|
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();
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
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 {
|
|
|
|
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') {
|
2022-12-29 03:20:43 -08:00
|
|
|
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;
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i);
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (jsonParameters) {
|
2022-08-01 13:47:55 -07:00
|
|
|
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 !== '') {
|
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
|
|
} else {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Additional fields must be a valid JSON',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
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') {
|
|
|
|
if (operation === 'upsert') {
|
|
|
|
const id = this.getNodeParameter('id', i) as number;
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i);
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (jsonParameters) {
|
2022-08-01 13:47:55 -07:00
|
|
|
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 !== '') {
|
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
|
|
} else {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Additional fields must be a valid JSON',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.customProperties) {
|
2022-12-02 06:25:21 -08:00
|
|
|
const data: any = {};
|
2021-07-19 23:58:54 -07:00
|
|
|
//@ts-ignore
|
2022-08-01 13:47:55 -07:00
|
|
|
additionalFields.customProperties.customProperty.map((property) => {
|
2021-07-19 23:58:54 -07:00
|
|
|
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;
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i);
|
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) {
|
2022-08-01 13:47:55 -07:00
|
|
|
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 !== '') {
|
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
|
|
} else {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Additional fields must be a valid JSON',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-12-02 06:25:21 -08:00
|
|
|
const data: any = {};
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (additionalFields.customAttributes) {
|
|
|
|
//@ts-ignore
|
2022-08-01 13:47:55 -07:00
|
|
|
additionalFields.customAttributes.customAttribute.map((property) => {
|
2021-07-19 23:58:54 -07:00
|
|
|
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;
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonParameters = this.getNodeParameter('jsonParameters', i);
|
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) {
|
2022-08-01 13:47:55 -07:00
|
|
|
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 !== '') {
|
|
|
|
if (validateJSON(additionalFieldsJson) !== undefined) {
|
|
|
|
Object.assign(body, JSON.parse(additionalFieldsJson));
|
|
|
|
} else {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'Additional fields must be a valid JSON',
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-12-02 06:25:21 -08:00
|
|
|
const data: any = {};
|
2020-09-02 03:25:11 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.customAttributes) {
|
|
|
|
//@ts-ignore
|
2022-08-01 13:47:55 -07:00
|
|
|
additionalFields.customAttributes.customAttribute.map((property) => {
|
2021-07-19 23:58:54 -07:00
|
|
|
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
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
const endpoint = '/events';
|
2021-07-19 23:58:54 -07:00
|
|
|
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) {
|
2024-06-19 22:45:00 -07:00
|
|
|
if (this.continueOnFail(error)) {
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-09-02 03:25:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|