Comparing version 2.0.2 to 3.1.0
@@ -24,3 +24,2 @@ var Model = require('../Model'); | ||
v1: [ | ||
'alertPreferences', | ||
'blocked', | ||
@@ -27,0 +26,0 @@ 'reportPreferences', |
@@ -15,4 +15,4 @@ var Transaction = require('../Transaction'); | ||
Tracking.prototype.createTransaction = function(transactionID, opts){ | ||
return new Transaction(this.GS, transactionID, opts); | ||
Tracking.prototype.createTransaction = function(transactionID, opts, trackingData){ | ||
return new Transaction(this.GS, transactionID, opts, trackingData); | ||
}; | ||
@@ -24,10 +24,16 @@ | ||
Tracking.prototype.trackEvent = function(name, data, cb) { | ||
if (typeof data === 'function') { | ||
Tracking.prototype.trackEvent = function(name, data, trackingData, cb) { | ||
if (!cb && !trackingData && typeof data === 'function') { | ||
cb = data; | ||
data = {}; | ||
trackingData = {}; | ||
} | ||
var event = new Event(this.GS, name, data); | ||
if (!cb && typeof trackingData === 'function') { | ||
cb = trackingData; | ||
trackingData = {}; | ||
} | ||
var event = new Event(this.GS, name, data, trackingData); | ||
event.track(cb); | ||
}; |
var config = require('../config'); | ||
var Event = module.exports = function(GS, name, data) { | ||
var Event = module.exports = function(GS, name, data, trackingData) { | ||
this.GS = GS; | ||
this.body = { | ||
event: { | ||
name: name, | ||
data: data || {} | ||
} | ||
}; | ||
this.name = name; | ||
this.data = data || {}; | ||
this.trackingData = trackingData || {}; | ||
}; | ||
Event.prototype.track = function(cb) { | ||
cb = cb || function() {}; | ||
if (!cb) cb = function() {}; | ||
if (!this.body.event.name){ | ||
return cb && cb(new Error('Event name not given')); | ||
if (!this.name){ | ||
return cb(new Error('Event name not given')); | ||
} | ||
if (typeof this.personID !== 'undefined') this.body.person_id = this.personID; | ||
var body = this.trackingData; | ||
body.event = { | ||
name: this.name, | ||
data: this.data | ||
}; | ||
this.GS._exec(config.endpoint + '/tracking/v1', '/event', 'POST', {}, this.body, cb); | ||
this.GS._exec(config.endpoint + '/tracking/v1', '/event', 'POST', {}, body, cb); | ||
}; |
@@ -8,2 +8,3 @@ var Account = require('./api/Account'); | ||
var utils = require('./utils'); | ||
var version = require('../package.json').version; | ||
@@ -21,2 +22,3 @@ var GoSquared = module.exports = function(opts) { | ||
this.version = version; | ||
this.log = this.opts.log; | ||
@@ -47,3 +49,6 @@ | ||
body: data, | ||
timeout: this.opts.requestTimeout | ||
timeout: this.opts.requestTimeout, | ||
headers: { | ||
'User-Agent': 'node-gosquared/' + version | ||
} | ||
}; | ||
@@ -50,0 +55,0 @@ |
@@ -1,2 +0,1 @@ | ||
var crypto = require('crypto'); | ||
var utils = require('./utils'); | ||
@@ -15,4 +14,19 @@ var Transaction = require('./Transaction'); | ||
set: function(v) { | ||
v = v.toString(); | ||
this._id = ''+v; | ||
if (!v) return; | ||
if (typeof v === 'object') { | ||
// try to grab the id property out of the object | ||
if (v.id) { | ||
this._id = v.id; | ||
// no id, try email | ||
} else if (v.email) { | ||
this._id = 'email:' + v.email; | ||
} | ||
} else { | ||
this._id = v; | ||
} | ||
// ensure it's a string | ||
if (this._id) this._id = this._id.toString(); | ||
}, | ||
@@ -23,63 +37,75 @@ enumerable: true | ||
if (id) this.id = id; | ||
this.anonymousID = undefined; | ||
}; | ||
Person.prototype._exec = function(url, params, data, cb) { | ||
var GS = this.GS; | ||
GS._exec(config.endpoint + '/tracking/v1', url, 'POST', params, data, cb); | ||
this.GS._exec(config.endpoint + '/tracking/v1', url, 'POST', params, this._addIDs(data), cb); | ||
}; | ||
// identify is a quick way to both alias and set properties on a person | ||
Person.prototype.identify = function(newID, props, cb) { | ||
// if we don't have an anonymous ID, just go straight to adding the data as properties | ||
if (!this.id) { | ||
this.id = newID; | ||
return this.setProperties(props, cb); | ||
Person.prototype._verifyID = function(cb) { | ||
if (!this.id && !this.anonymousID) { | ||
setImmediate(function() { | ||
cb(new Error('Missing ID and anonymousID')); | ||
}); | ||
return false; | ||
} | ||
if (!cb && typeof props === 'function') { | ||
cb = props; | ||
props = {}; | ||
} | ||
return true; | ||
}; | ||
if (typeof newID === 'object') { | ||
props = newID; | ||
newID = props.id; | ||
} | ||
Person.prototype._addIDs = function(trackingData) { | ||
if (!trackingData) trackingData = {}; | ||
trackingData.person_id = this.id; | ||
trackingData.visitor_id = this.anonymousID; | ||
return trackingData; | ||
} | ||
// can use identify without a newID | ||
if (!newID) { | ||
return this.setProperties(props, cb); | ||
// identify is very similar to setProperties but requires an identifying property (id or email) | ||
Person.prototype.identify = function(props, cb) { | ||
this.id = props; | ||
if (!this.id) { | ||
setImmediate(function() { | ||
cb(new Error('Missing ID and email')); | ||
}); | ||
return; | ||
} | ||
var data = { visitor_id: this.id, person_id: ''+newID }; | ||
if (props) data.properties = props; | ||
this._exec('/identify', {}, data, cb); | ||
this._exec('/identify', {}, { properties: props }, cb); | ||
}; | ||
Person.prototype.alias = function(newID, cb) { | ||
var data = { visitor_id: this.id, person_id: ''+newID }; | ||
this.id = newID; | ||
this._exec('/alias', {}, data, cb); | ||
}; | ||
Person.prototype.setProperties = function(props, cb) { | ||
this.id = props; | ||
Person.prototype.setProperties = function(props, cb) { | ||
var data = { person_id: this.id, properties: props }; | ||
this._exec('/properties', {}, data, cb); | ||
if (!this._verifyID(cb)) return; | ||
this._exec('/properties', {}, { properties: props }, cb); | ||
}; | ||
Person.prototype.trackEvent = function(name, data, cb) { | ||
if (typeof data === 'function') { | ||
Person.prototype.trackEvent = function(name, data, trackingData, cb) { | ||
if (!cb && !trackingData && typeof data === 'function') { | ||
cb = data; | ||
data = {}; | ||
trackingData = {}; | ||
} | ||
var event = new Event(this.GS, name, data); | ||
event.personID = this.id; | ||
if (!cb && typeof trackingData === 'function') { | ||
cb = trackingData; | ||
trackingData = {}; | ||
} | ||
if (!this._verifyID(cb)) return; | ||
var event = new Event(this.GS, name, data, this._addIDs(trackingData)); | ||
event.track(cb); | ||
}; | ||
Person.prototype.createTransaction = function(transactionID, opts) { | ||
var t = new Transaction(this.GS, transactionID, opts); | ||
t.personID = this.id; | ||
return t; | ||
Person.prototype.createTransaction = function(transactionID, opts, trackingData) { | ||
// not too sure how to nicely error here if theres no ID. | ||
// the transaction will work, but won't be associated with a person | ||
// should we throw? | ||
// if (!this._verifyID(cb)) return; | ||
return new Transaction(this.GS, transactionID, opts, this._addIDs(trackingData)); | ||
}; |
var utils = require('./utils'); | ||
var config = require('../config'); | ||
var Transaction = module.exports = function(GS, transactionID, opts){ | ||
var Transaction = module.exports = function(GS, transactionID, opts, trackingData){ | ||
this.GS = GS; | ||
this.id = transactionID; | ||
this.items = []; | ||
this.params = {}; | ||
if (typeof transactionID === 'object') { | ||
if (transactionID && typeof transactionID === 'object') { | ||
opts = transactionID; | ||
@@ -15,7 +14,4 @@ this.id = transactionID.id; | ||
if (typeof opts === 'object') { | ||
this.opts = opts; | ||
} else { | ||
this.opts = {}; | ||
} | ||
this.opts = opts || {}; | ||
this.trackingData = trackingData || {} | ||
}; | ||
@@ -25,3 +21,3 @@ | ||
itemOpts = itemOpts || (typeof itemName === 'object' ? itemName : {}); | ||
if(typeof itemName !== 'object' && typeof itemName !== 'undefined') itemOpts.name = '' + itemName; | ||
if (typeof itemName !== 'object' && typeof itemName !== 'undefined') itemOpts.name = itemName + ''; | ||
@@ -39,5 +35,5 @@ this.items.push(itemOpts); | ||
Transaction.prototype.track = function(cb){ | ||
var GS = this.GS, err; | ||
var GS = this.GS; | ||
if(typeof cb !== 'function'){ | ||
if (typeof cb !== 'function'){ | ||
cb = function(){}; | ||
@@ -47,3 +43,3 @@ } | ||
if (typeof this.id === 'undefined') { | ||
err = new Error('Transaction ID not given'); | ||
var err = new Error('Transaction ID not given'); | ||
err.transaction = this; | ||
@@ -56,3 +52,3 @@ return cb(err); | ||
if (!it.name) { | ||
err = new Error('Item name not given'); | ||
var err = new Error('Item name not given'); | ||
err.item = it; | ||
@@ -64,12 +60,12 @@ err.transaction = this; | ||
var body = { | ||
person_id: this.personID, | ||
transaction: { | ||
id: this.id, | ||
items: this.items, | ||
opts: this.opts | ||
} | ||
var body = this.trackingData; | ||
body.transaction = { | ||
id: this.id, | ||
items: this.items, | ||
opts: this.opts | ||
}; | ||
GS._exec(config.endpoint + '/tracking/v1', '/transaction', 'POST', this.params, body, cb); | ||
GS._exec(config.endpoint + '/tracking/v1', '/transaction', 'POST', {}, body, cb); | ||
}; |
{ | ||
"name": "gosquared", | ||
"version": "2.0.2", | ||
"version": "3.1.0", | ||
"description": "GoSquared for your Node.JS application", | ||
@@ -40,11 +40,11 @@ "main": "lib/GoSquared.js", | ||
"devDependencies": { | ||
"async": "^0.9.0", | ||
"mocha": "~1.8.1" | ||
"async": "^1.4.0", | ||
"mocha": "^2.2.5" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha", | ||
"test-tracking": "./node_modules/mocha/bin/mocha test/tracking", | ||
"test-retrieval": "./node_modules/mocha/bin/mocha test/retrieval", | ||
"test-account": "./node_modules/mocha/bin/mocha test/account" | ||
"test": "mocha", | ||
"test-tracking": "mocha test/tracking", | ||
"test-retrieval": "mocha test/retrieval", | ||
"test-account": "mocha test/account" | ||
} | ||
} |
# node-gosquared | ||
[![Travis](https://api.travis-ci.org/gosquared/node-gosquared.svg)](https://travis-ci.org/gosquared/node-gosquared) | ||
[![Dependencies](https://david-dm.org/gosquared/node-gosquared.svg)](https://david-dm.org/gosquared/node-gosquared) | ||
[![Join the chat at https://gitter.im/gosquared/node-gosquared](https://img.shields.io/badge/gitter-join%20chat-blue.svg)](https://gitter.im/gosquared/node-gosquared) | ||
[![NPM](https://nodei.co/npm/gosquared.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/gosquared) | ||
The official GoSquared Node.js module for integrating the [GoSquared API][docs] into your Node.JS app with ease. | ||
@@ -64,3 +70,3 @@ | ||
[reporting-docs]: https://gosquared.com/developer/api/ | ||
[tracking-docs]: https://beta.gosquared.com/docs/tracking/api/ | ||
[docs]: https://beta.gosquared.com/docs | ||
[tracking-docs]: https://gosquared.com/docs/tracking/api/ | ||
[docs]: https://gosquared.com/docs |
@@ -17,41 +17,92 @@ var utils = require('./utils'); | ||
describe('People', function() { | ||
it('identify', function(done) { | ||
var p = gosquared.createPerson(); | ||
var props = { | ||
name: 'Node GoSquared', | ||
email: 'test-node@gosquared.com', | ||
custom: { | ||
test_node_gosquared: 'identify' | ||
} | ||
}; | ||
p.identify('test-node-gosquared', props, testSuccess(done)); | ||
}); | ||
describe('#identify', function() { | ||
it('works', function(done) { | ||
var p = gosquared.createPerson(); | ||
var props = { | ||
id: 'test-node-gosquared', | ||
name: 'Node GoSquared', | ||
email: 'test-node@gosquared.com', | ||
custom: { | ||
test_node_gosquared: 'identify' | ||
} | ||
}; | ||
p.identify(props, testSuccess(done)); | ||
}); | ||
it('props', function(done) { | ||
var p = gosquared.createPerson('test-node-gosquared'); | ||
var props = { | ||
custom: { | ||
test_node_gosquared: 'props' | ||
} | ||
}; | ||
p.setProperties(props, testSuccess(done)); | ||
it('works with an email', function(done) { | ||
var p = gosquared.createPerson(); | ||
var props = { | ||
name: 'Node GoSquared', | ||
email: 'test-node@gosquared.com', | ||
custom: { | ||
test_node_gosquared: 'identify' | ||
} | ||
}; | ||
p.identify(props, testSuccess(done)); | ||
}); | ||
it('fails without an id and email', function(done) { | ||
var p = gosquared.createPerson(); | ||
var props = { | ||
name: 'Node GoSquared', | ||
custom: { | ||
test_node_gosquared: 'identify' | ||
} | ||
}; | ||
p.identify(props, testFail(done)); | ||
}); | ||
}); | ||
it('alias', function(done) { | ||
var p = gosquared.createPerson(); | ||
p.identify('test-node-gosquared-pre-alias', { | ||
name: 'Node GoSquared Pre Alias' | ||
describe('#setProperties', function() { | ||
it('works', function(done) { | ||
var p = gosquared.createPerson(); | ||
var props = { | ||
id: 'test-node-gosquared', | ||
custom: { | ||
test_node_gosquared: 'props' | ||
} | ||
}; | ||
p.setProperties(props, testSuccess(done)); | ||
}); | ||
p.alias('test-node-gosquared-alias', testSuccess(done)); | ||
it('works with an anonymousID', function(done) { | ||
var p = gosquared.createPerson(); | ||
p.anonymousID = 'test'; | ||
var props = { | ||
custom: { | ||
test_node_gosquared: 'props' | ||
} | ||
}; | ||
p.setProperties(props, testSuccess(done)); | ||
}); | ||
it('doesn\'t work without an ID', function(done) { | ||
var p = gosquared.createPerson(); | ||
var props = { | ||
custom: { | ||
test_node_gosquared: 'props' | ||
} | ||
}; | ||
p.setProperties(props, testFail(done)); | ||
}); | ||
}); | ||
it('event', function(done) { | ||
var p = gosquared.createPerson('test-node-gosquared'); | ||
p.trackEvent('test_node_gosquared_event', testSuccess(done)); | ||
describe('#trackEvent', function() { | ||
it('works', function(done) { | ||
var p = gosquared.createPerson('test-node-gosquared'); | ||
p.trackEvent('test_node_gosquared_event', testSuccess(done)); | ||
}); | ||
it('doesn\'t work without an ID', function(done) { | ||
var p = gosquared.createPerson(); | ||
p.trackEvent('test_node_gosquared_event', testFail(done)); | ||
}); | ||
}); | ||
it('transaction', function(done) { | ||
var p = gosquared.createPerson('test-node-gosquared'); | ||
var t = p.createTransaction(utils.makeTransactionID()); | ||
t.track(testSuccess(done)); | ||
describe('#createTransaction', function(done) { | ||
it('works', function(done) { | ||
var p = gosquared.createPerson('test-node-gosquared'); | ||
var t = p.createTransaction(utils.makeTransactionID()); | ||
t.track(testSuccess(done)); | ||
}); | ||
}); | ||
@@ -180,8 +231,5 @@ }); | ||
it('can include additional customer attributes', function(done){ | ||
it('can include additional attributes', function(done){ | ||
var t = gosquared.createTransaction(utils.makeTransactionID(), { | ||
ua: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36', | ||
ip: '8.8.8.8', | ||
la: 'en-gb', | ||
ru: 'http://www.gosquared.com/ecommerce/' | ||
revenue: '$5.99' | ||
}); | ||
@@ -188,0 +236,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24890
24
748
72