Comparing version 1.0.0 to 1.1.0
@@ -7,3 +7,10 @@ const request = require('request'), | ||
const CREATE = "create", | ||
ESTIMATE = "estimate", | ||
LIST = "list", | ||
RESULT = "result", | ||
STOP = "stop"; | ||
const TARGET_ACTIONS = [CREATE, ESTIMATE, LIST, RESULT, STOP]; | ||
var CleverTapAPI = function (CleverTapAccountId, CleverTapAccountPasscode) { | ||
@@ -24,3 +31,10 @@ if(!CleverTapAccountId) { | ||
}; | ||
CleverTapAPI.prototype.TARGET_CREATE = CREATE; | ||
CleverTapAPI.prototype.TARGET_ESTIMATE = ESTIMATE; | ||
CleverTapAPI.prototype.TARGET_LIST = LIST; | ||
CleverTapAPI.prototype.TARGET_RESULT = RESULT; | ||
CleverTapAPI.prototype.TARGET_STOP = STOP; | ||
CleverTapAPI.prototype.TARGET_ACTIONS = TARGET_ACTIONS; | ||
CleverTapAPI.prototype.upload = function(data, options, callback) { | ||
@@ -89,2 +103,37 @@ | ||
CleverTapAPI.prototype.profile = function(options, callback) { | ||
if (typeof(options) === 'function' || !options) { | ||
callback = options; | ||
options = {}; | ||
} | ||
if(!options.email && !options.identity && !options.objectId) { | ||
if (callback) { | ||
callback({}); | ||
} | ||
} | ||
var endpoint = "profile.json"; | ||
if (options.email) { | ||
endpoint += "?email="+options.email; | ||
} | ||
else if (options.identity) { | ||
endpoint += "?identity="+options.identity; | ||
} | ||
else if (options.objectId) { | ||
endpoint += "?objectId="+options.objectId; | ||
} | ||
this._call(endpoint, options, (res) => { | ||
if (callback) { | ||
callback(res); | ||
} | ||
}); | ||
}; | ||
CleverTapAPI.prototype.profiles = function(query, options, callback) { | ||
@@ -100,2 +149,31 @@ return this._fetch("profiles", query, options, callback); | ||
CleverTapAPI.prototype.targets = function(options, callback) { | ||
if (typeof(options) === 'function' || !options) { | ||
callback = options; | ||
options = {}; | ||
} | ||
var action = options.action; | ||
if(!action || this.TARGET_ACTIONS.indexOf(action) < 0) { | ||
if (options.debug) { | ||
console.log(`Invalid push target action ${action}`); | ||
} | ||
if (callback) { | ||
callback(null); | ||
} | ||
} | ||
var endpoint = `targets/${action}.json`; | ||
this._call(endpoint, options, (res) => { | ||
if (callback) { | ||
callback(res); | ||
} | ||
}); | ||
}; | ||
CleverTapAPI.prototype._fetch = function (type, query, options, callback) { | ||
@@ -138,6 +216,2 @@ | ||
if (options.debug) { | ||
console.log( `CleverTap: fetching endpoint ${endpoint}`); | ||
} | ||
this._call(endpoint, options, (res) => { | ||
@@ -167,3 +241,2 @@ | ||
fetchNext(); | ||
}; | ||
@@ -174,2 +247,6 @@ | ||
if (options.debug) { | ||
console.log( `CleverTap: calling endpoint ${endpoint} with options ${JSON.stringify(options)}`); | ||
} | ||
callback = callback || function(res) {}; | ||
@@ -197,3 +274,3 @@ | ||
request(requestOptions, function (error, response, body){ | ||
if(error) { | ||
if (error) { | ||
callback(error); | ||
@@ -200,0 +277,0 @@ } else { |
@@ -10,2 +10,10 @@ /** | ||
const CREATE = "create", | ||
ESTIMATE = "estimate", | ||
LIST = "list", | ||
RESULT = "result", | ||
STOP = "stop"; | ||
const TARGET_ACTIONS = [CREATE, ESTIMATE, LIST, RESULT, STOP]; | ||
var CleverTap = function (CleverTapAccountId, CleverTapAccountPasscode) { | ||
@@ -80,10 +88,3 @@ if(!CleverTapAccountId) { | ||
if (!data || data.length <= 0) { | ||
throw new Error("CleverTap: upload data is empty"); | ||
} | ||
var error = _validate("upload", data); | ||
if(error) { | ||
throw new Error(error); | ||
} | ||
@@ -96,2 +97,11 @@ if (typeof(options) === 'function' || !options) { | ||
return new Promise( (resolve, reject) => { | ||
if (error) { | ||
console.error(error); | ||
if (callback && typeof callback === 'function') { | ||
callback(null); | ||
} | ||
reject(error); | ||
return; | ||
} | ||
this.api.upload(data, options, (res) => { | ||
@@ -119,2 +129,80 @@ // if there is only one reponse return response object rather than array | ||
profile(options, callback) | ||
Retrieve an individual user profile by ID. | ||
Supported ID values are email, a custom identity value you have set on the profile via the SDKs or the Server API, | ||
or the unique CleverTap objectID used by CleverTap to identify the user profile. | ||
The CleverTap objectID is available via the SDKs as well as displayed in the user profile on the CleverTap dashboard. | ||
options: Object; required value is one of email "email":"foo@foo.com", identity "identity":"1234567" or objectId "objectId":"-1a063854f83a4c6484285039ecff87cb". | ||
optional values "debug":1, debug enables some logging. | ||
callback:function(result:Object), optional | ||
returns a Promise, callback optional | ||
Sample response: | ||
{ | ||
"status": "success", | ||
"record": { | ||
"email": "saifali9690@gmail.com", | ||
"profileData": { | ||
"Last Score": 308, | ||
"High Score": 308, | ||
"Replayed": true | ||
}, | ||
"events": { | ||
"App Launched": { | ||
"count": 10, | ||
"first_seen": 1457271567, | ||
"last_seen": 1458041215 | ||
}, | ||
"Charged": { | ||
"count": 6, | ||
"first_seen": 1457962417, | ||
"last_seen": 1458041276 | ||
} | ||
} | ||
} | ||
} | ||
For more see https://support.clevertap.com/server/downloading-profiles-and-actions/#user-profile-by-id | ||
*/ | ||
CleverTap.prototype.profile = function(options, callback) { | ||
if (typeof(options) === 'function' || !options) { | ||
callback = options; | ||
options = {}; | ||
} | ||
var error = null; | ||
if(!options.email && !options.identity && !options.objectId) { | ||
error = "profile requires email, identity or objectId"; | ||
} | ||
return new Promise( (resolve, reject) => { | ||
if (error) { | ||
console.error(error); | ||
if (callback && typeof callback === 'function') { | ||
callback(null); | ||
} | ||
reject(error); | ||
return; | ||
} | ||
this.api.profile(options, (res) => { | ||
resolve(res); | ||
if(callback && typeof callback === 'function') { | ||
callback(res); | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
profiles(query, options, callback) | ||
@@ -167,10 +255,4 @@ | ||
CleverTap.prototype.profiles = function(query, options, callback) { | ||
if (!query) { | ||
throw new Error("CleverTap.profiles requires a query object"); | ||
} | ||
var error = _validate("profiles", query); | ||
if(error) { | ||
throw new Error(error); | ||
} | ||
@@ -183,2 +265,11 @@ if (typeof(options) === 'function' || !options) { | ||
return new Promise( (resolve, reject) => { | ||
if (error) { | ||
console.error(error); | ||
if (callback && typeof callback === 'function') { | ||
callback(null); | ||
} | ||
reject(error); | ||
return; | ||
} | ||
this.api.profiles(query, options, (res) => { | ||
@@ -257,10 +348,4 @@ | ||
CleverTap.prototype.events = function(query, options, callback) { | ||
if (!query) { | ||
throw new Error("CleverTap.events requires a query object"); | ||
} | ||
var error = _validate("profiles", query); | ||
if(error) { | ||
throw new Error(error); | ||
} | ||
@@ -273,2 +358,11 @@ if (typeof(options) === 'function' || !options) { | ||
return new Promise( (resolve, reject) => { | ||
if (error) { | ||
console.error(error); | ||
if (callback && typeof callback === 'function') { | ||
callback(null); | ||
} | ||
reject(error); | ||
return; | ||
} | ||
this.api.events(query, options, (res) => { | ||
@@ -286,5 +380,128 @@ | ||
/** | ||
Push Notifications | ||
CREATE: creates a push notification target. Sends the notification to the segment of users defined by your payload. | ||
ESTIMATE: estimates the reach of a potential push notification target. Returns an estimate of reach, will not create (send) the notification. | ||
LIST: lists the targets you have created via the API. | ||
RESULT: returns the status and stats of a target. | ||
STOP: stops a running target. | ||
For info on required payloads and more in general see https://support.clevertap.com/server/send-notifications/push/ | ||
*/ | ||
CleverTap.prototype.TARGET_CREATE = CREATE; | ||
CleverTap.prototype.TARGET_ESTIMATE = ESTIMATE; | ||
CleverTap.prototype.TARGET_LIST = LIST; | ||
CleverTap.prototype.TARGET_RESULT = RESULT; | ||
CleverTap.prototype.TARGET_STOP = STOP; | ||
CleverTap.prototype.TARGET_ACTIONS = TARGET_ACTIONS; | ||
CleverTap.prototype.targets = function(action, payload, options, callback) { | ||
if (typeof(options) === 'function' || !options) { | ||
callback = options; | ||
options = {}; | ||
} | ||
var error = null; | ||
if (!action || this.TARGET_ACTIONS.indexOf(action) < 0) { | ||
error = `Invalid push target action ${action}`; | ||
} | ||
var _action = action; | ||
if (action == this.TARGET_ESTIMATE) { | ||
payload['estimate_only'] = true; | ||
action = this.TARGET_CREATE; | ||
} | ||
options.action = action; | ||
if (!error) { | ||
error = _validate(_action, payload); | ||
} | ||
options.data = payload; | ||
return new Promise( (resolve, reject) => { | ||
if (error) { | ||
console.error(error); | ||
if (callback && typeof callback === 'function') { | ||
callback(null); | ||
} | ||
reject(error); | ||
return; | ||
} | ||
this.api.targets(options, (res) => { | ||
resolve(res); | ||
if(callback && typeof callback === 'function') { | ||
callback(res); | ||
} | ||
}); | ||
}); | ||
}; | ||
var _validate = (type, data) => { | ||
var error = null; | ||
if (type === CREATE || type === ESTIMATE) { | ||
if (!data) { | ||
return `Push targets action ${type} requires a payload`; | ||
} | ||
if (!data.name) { | ||
return `Push targets action ${type} requires a name`; | ||
} | ||
if (!data.where && !data.segment) { | ||
return `Push targets action ${type} requires a where or segment value`; | ||
} | ||
if (data.where && data.segment) { | ||
return `Push targets action ${type} does not support both a where value and a segment value, specify one or the other`; | ||
} | ||
if (data.segment) { | ||
if (data.segment !== "all") { | ||
return `Push targets action ${type} segment value must be all`; | ||
} | ||
} | ||
if (!data.content) { | ||
return `Push targets action ${type} requires a content dict`; | ||
} | ||
if (data.content) { | ||
if (!data.content.title || !data.content.body) { | ||
return `Push targets action ${type} content dict requires a title and a body`; | ||
} | ||
} | ||
if (!data.devices) { | ||
return `Push targets action ${type} requires a devices array`; | ||
} | ||
return error; | ||
} | ||
if (type === LIST) { | ||
// no-op | ||
return error; | ||
} | ||
if (type === RESULT || type === STOP) { | ||
if (!data || !data.id) { | ||
error = `Push targets action ${type} requires a target id`; | ||
} | ||
return error; | ||
} | ||
if (type === "events") { | ||
@@ -291,0 +508,0 @@ if(!data || typeof(data) !== "object") { |
{ | ||
"name": "clevertap", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "CleverTap API library for node", | ||
"main": "./lib/clevertap", | ||
"scripts": { | ||
"test": "mocha test" | ||
"test": "mocha test --timeout 5000", | ||
"testpush": "mocha test/push_test.js --timeout 5000" | ||
}, | ||
@@ -15,3 +16,6 @@ "repository": { | ||
"node", | ||
"CleverTap" | ||
"CleverTap", | ||
"mobile", | ||
"analytics", | ||
"push" | ||
], | ||
@@ -18,0 +22,0 @@ "author": "CleverTap", |
@@ -57,2 +57,41 @@ # clevertap-node | ||
// send a push notification | ||
var createPayload = { | ||
"name": "green freedom", | ||
"when": "now", | ||
"where": { | ||
"event_name": "App Launched", | ||
"from": 20160101, | ||
"to": 20160317, | ||
}, | ||
"content":{ | ||
"title":"Hello!", | ||
"body":"Strictly Green Lantern fans only!", | ||
"platform_specific": { | ||
"ios": { | ||
"deep_link": "judepereira.com", | ||
"sound_file": "judepereira.wav", | ||
"category": "reactive", | ||
"badge_count": 1, | ||
"foo": "bar_ios" | ||
}, | ||
"android": { | ||
"background_image": "http://judepereira.com/a.jpg", | ||
"default_sound": true, | ||
"deep_link": "judepereira.com", | ||
"foo": "bar_android" | ||
} | ||
} | ||
}, | ||
"devices": [ | ||
"ios" | ||
], | ||
} | ||
//callback style | ||
clevertap.targets(clevertap.TARGET_CREATE, createPayload, {"debug":1}, (res) => {console.log(res)} ); | ||
// or if you prefer Promises | ||
clevertap.targets(clevertap.TARGET_CREATE, createPayload, {"debug":1}).then( (res) => {console.log(res)} ); | ||
``` | ||
@@ -67,3 +106,4 @@ | ||
npm install | ||
npm test | ||
npm test // all tests | ||
npm run testpush // just push specific tests | ||
``` |
27914
630
108