UNPKG

customerio-node

Version:

A node client for the Customer.io event API. http://customer.io

135 lines (134 loc) 5.95 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TrackClient = void 0; const request_1 = __importDefault(require("./request")); const regions_1 = require("./regions"); const utils_1 = require("./utils"); class TrackClient { constructor(siteid, apikey, defaults = {}) { if (defaults.region && !(defaults.region instanceof regions_1.Region)) { throw new Error('region must be one of Regions.US or Regions.EU'); } this.siteid = siteid; this.apikey = apikey; this.defaults = Object.assign(Object.assign({}, defaults), { region: defaults.region || regions_1.RegionUS }); this.request = new request_1.default({ siteid: this.siteid, apikey: this.apikey }, this.defaults); this.trackRoot = this.defaults.url ? this.defaults.url : this.defaults.region.trackUrl; } identify(customerId, data = {}) { if ((0, utils_1.isEmpty)(customerId)) { throw new utils_1.MissingParamError('customerId'); } return this.request.put(`${this.trackRoot}/customers/${encodeURIComponent(customerId)}`, data); } destroy(customerId) { if ((0, utils_1.isEmpty)(customerId)) { throw new utils_1.MissingParamError('customerId'); } return this.request.destroy(`${this.trackRoot}/customers/${encodeURIComponent(customerId)}`); } suppress(customerId) { if ((0, utils_1.isEmpty)(customerId)) { throw new utils_1.MissingParamError('customerId'); } return this.request.post(`${this.trackRoot}/customers/${encodeURIComponent(customerId)}/suppress`); } unsuppress(customerId) { if ((0, utils_1.isEmpty)(customerId)) { throw new utils_1.MissingParamError('customerId'); } return this.request.post(`${this.trackRoot}/customers/${encodeURIComponent(customerId)}/unsuppress`); } track(customerId, data = {}) { if ((0, utils_1.isEmpty)(customerId)) { throw new utils_1.MissingParamError('customerId'); } if ((0, utils_1.isEmpty)(data.name)) { throw new utils_1.MissingParamError('data.name'); } return this.request.post(`${this.trackRoot}/customers/${encodeURIComponent(customerId)}/events`, data); } trackAnonymous(anonymousId, data = {}) { if ((0, utils_1.isEmpty)(data.name)) { throw new utils_1.MissingParamError('data.name'); } let payload = Object.assign({}, data); if (!(0, utils_1.isEmpty)(anonymousId)) { payload['anonymous_id'] = anonymousId; } return this.request.post(`${this.trackRoot}/events`, payload); } trackPageView(customerId, path) { if ((0, utils_1.isEmpty)(customerId)) { throw new utils_1.MissingParamError('customerId'); } if ((0, utils_1.isEmpty)(path)) { throw new utils_1.MissingParamError('path'); } return this.request.post(`${this.trackRoot}/customers/${encodeURIComponent(customerId)}/events`, { type: 'page', name: path, }); } trackPush(data = {}) { return this.request.post(`${this.trackRoot}/push/events`, data); } addDevice(customerId, device_id, platform, data = {}) { if ((0, utils_1.isEmpty)(customerId)) { throw new utils_1.MissingParamError('customerId'); } if ((0, utils_1.isEmpty)(device_id)) { throw new utils_1.MissingParamError('device_id'); } if ((0, utils_1.isEmpty)(platform)) { throw new utils_1.MissingParamError('platform'); } let { last_used } = data, attributes = __rest(data, ["last_used"]); return this.request.put(`${this.trackRoot}/customers/${encodeURIComponent(customerId)}/devices`, { device: Object.assign(Object.assign({ id: device_id, platform }, (last_used && { last_used })), (Object.keys(attributes).length && { attributes })), }); } deleteDevice(customerId, deviceToken) { if ((0, utils_1.isEmpty)(customerId)) { throw new utils_1.MissingParamError('customerId'); } if ((0, utils_1.isEmpty)(deviceToken)) { throw new utils_1.MissingParamError('deviceToken'); } return this.request.destroy(`${this.trackRoot}/customers/${encodeURIComponent(customerId)}/devices/${encodeURIComponent(deviceToken)}`); } mergeCustomers(primaryIdType, primaryId, secondaryIdType, secondaryId) { if ((0, utils_1.isEmpty)(primaryId)) { throw new utils_1.MissingParamError('primaryId'); } if ((0, utils_1.isEmpty)(secondaryId)) { throw new utils_1.MissingParamError('secondaryId'); } if (!(0, utils_1.isIdentifierType)(primaryIdType) || !(0, utils_1.isIdentifierType)(secondaryIdType)) { throw new Error('primaryIdType and secondaryIdType must be one of "id", "cio_id", or "email"'); } return this.request.post(`${this.trackRoot}/merge_customers`, { primary: { [primaryIdType]: primaryId, }, secondary: { [secondaryIdType]: secondaryId, }, }); } } exports.TrackClient = TrackClient;