Comparing version
@@ -27,3 +27,3 @@ var http = require('http'); | ||
non200Code: 'The HTTP request completed with a non-200 status code.', | ||
errorEncountered: 'The GoSquared server did\'t like something, so it gave us an error.' | ||
errorEncountered: 'The GoSquared server didn\'t like something, so it gave us an error.' | ||
}; | ||
@@ -44,3 +44,4 @@ | ||
100: "Transaction items must have a name" | ||
100: "Transaction items must have a name", | ||
101: "Transactions must have an ID" | ||
}; | ||
@@ -172,5 +173,5 @@ | ||
GoSquared.prototype.event = require('./Event'); | ||
GoSquared.prototype.createTransaction = function(transactionID){ | ||
GoSquared.prototype.createTransaction = function(transactionID, opts){ | ||
var self = this; | ||
return new Transaction(self, transactionID); | ||
return new Transaction(self, transactionID, opts); | ||
}; | ||
@@ -177,0 +178,0 @@ |
@@ -29,1 +29,5 @@ var should = require('should'); | ||
}; | ||
module.exports.genTransactionID = function(){ | ||
return Math.floor(Math.random()*0xffffffff); | ||
}; |
var util = require('util'); | ||
var Transaction = module.exports = function(GS, transactionID){ | ||
var Transaction = module.exports = function(GS, transactionID, opts){ | ||
this.GS = GS; | ||
this.id = transactionID; | ||
this.items = []; | ||
if (typeof transactionID === 'object') { | ||
opts = transactionID; | ||
this.id = transactionID.id; | ||
} | ||
if (typeof this.id === 'undefined') this.GS._debug(101, 'WARNING'); | ||
else this.id = '' + this.id; | ||
if (typeof opts === 'object') this.opts = opts; | ||
else this.opts = {}; | ||
}; | ||
Transaction.prototype.addItem = function(item) { | ||
if (!item.name) return this.GS._debug(100, 'WARNING'); | ||
Transaction.prototype.addItem = function(itemName, itemOpts) { | ||
itemOpts = itemOpts || (typeof itemName === 'object' ? itemName : {}); | ||
if(typeof itemName !== 'object' && typeof itemName !== 'undefined') itemOpts.name = '' + itemName; | ||
this.items.push(item); | ||
if (!itemOpts.name) return this.GS._debug(100, 'WARNING'); | ||
this.items.push(itemOpts); | ||
}; | ||
@@ -17,6 +31,8 @@ | ||
items = items || []; | ||
items.map(this.addItem.bind(this)); | ||
for(var i = 0; i < items.length;){ | ||
this.addItem(items[i++]); | ||
} | ||
}; | ||
Transaction.prototype.record = function(done){ | ||
Transaction.prototype.track = function(done){ | ||
var GS = this.GS; | ||
@@ -35,6 +51,3 @@ | ||
i: this.items, | ||
d: { | ||
reveue: this.revenue, | ||
quantity: this.quantity | ||
} | ||
d: this.opts | ||
}) | ||
@@ -41,0 +54,0 @@ }; |
{ | ||
"name": "gosquared", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "GoSquared for your Node.JS application", | ||
@@ -5,0 +5,0 @@ "main": "lib/GoSquared.js", |
@@ -57,3 +57,3 @@ # node-gosquared | ||
### Recording data with this module | ||
### Recording data | ||
@@ -64,11 +64,10 @@ The module can also be used to send data to GoSquared. | ||
To record a transaction: | ||
To track a transaction: | ||
```javascript | ||
var transactionID = 123456789 | ||
var transactionID = 123456789; | ||
var t = gosquared.createTransaction(transationID); | ||
// Make sure each item has a name | ||
t.addItem({ | ||
name: 'Beer', | ||
t.addItem('Beer', { | ||
price: 3.50, | ||
@@ -81,18 +80,18 @@ quantity: 1, | ||
t.addItems([ | ||
{ | ||
name: 'More Beer', | ||
price: 4.99, | ||
quantity: 2, | ||
category: 'Alcoholic Drinks' | ||
}, | ||
{ | ||
name: 'Limoncello', | ||
price: 17.99, | ||
quantity: 1, | ||
category: 'Liquor' | ||
} | ||
{ | ||
name: 'More Beer', | ||
price: 4.99, | ||
quantity: 2, | ||
category: 'Alcoholic Drinks' | ||
}, | ||
{ | ||
name: 'Limoncello', | ||
price: 17.99, | ||
quantity: 1, | ||
category: 'Liquor' | ||
} | ||
]); | ||
// Send off to GoSquared | ||
t.record(function(){ | ||
t.track(function(){ | ||
// Done | ||
@@ -103,15 +102,45 @@ }); | ||
GoSquared automatically calculates the total revenue and quantity for each transaction by summing these values of each item. If you would like to override the total transaction revenue and quantity, you can do the following: | ||
GoSquared automatically calculates the total revenue and quantity for each transaction by summing these values of each item. If you would like to override the total transaction revenue and quantity, you can set them as transaction options: | ||
```javascript | ||
// Override revenue and quantity amounts before t.record() | ||
// Override revenue and quantity amounts | ||
var customRevenue = 10; | ||
var customQuantity = 5 | ||
t.revenue = customRevenue; | ||
t.quantity = customQuantity; | ||
var opts = { | ||
customRevenue: 10, | ||
customQuantity: 5 | ||
}; | ||
t.record(); | ||
var t = gosquared.createTransaction(transactionID, opts); | ||
t.track(); | ||
``` | ||
##### Including additional info | ||
One of the features of GoSquared Ecommerce is the ability to attribute revenue and quantity amounts to aspects of the customer, such as their country, language, browser or referring source. Although GoSquared is not able to automatically detect these for backend-triggered events, you can pass in the values GoSquared needs for this functionality. | ||
To do this, include any of the following in the transaction's options object: | ||
```javascript | ||
var userAgent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36'; // The browser's user agent | ||
var ip = '0.0.0.0'; // The cusomer's IP address. IPv4 or IPv6 | ||
var language = 'en-gb'; // The customer's ISO 639-1 language string | ||
var referringURL = 'http://www.gosquared.com/ecommerce/'; // The referring URL of the customer's visit | ||
var previousTransaction = { | ||
ts: Date.now() // The UNIX timestamp in ms of the customer's previous transaction, if any | ||
}; | ||
var opts = { | ||
ua: userAgent, | ||
ip: ip, | ||
la: language, | ||
ru: referringURL, | ||
pt: previousTransaction | ||
}; | ||
var t = gosquared.createTransaction(transactionID, opts); | ||
``` | ||
#### Events | ||
@@ -118,0 +147,0 @@ |
@@ -22,7 +22,9 @@ var GoSquared = require('../lib/GoSquared'); | ||
var t = GS.createTransaction(); | ||
t.record(th.testTrackError.bind(this, done)); | ||
t.track(th.testTrackError.bind(this, done)); | ||
}); | ||
it('items must have a name', function(){ | ||
var t = GS.createTransaction(12345678); | ||
var t = GS.createTransaction(th.genTransactionID()); | ||
t.addItem({ | ||
@@ -36,6 +38,43 @@ price: 1.99, | ||
it('records successfully', function(done){ | ||
var t = GS.createTransaction(12345678); | ||
it('tracks successfully with transaction ID as option', function(done){ | ||
var tid = '' + th.genTransactionID(); | ||
var t = GS.createTransaction({ id: tid }); | ||
t.addItem('node-gosquared-test', { | ||
price: 1.99, | ||
quantity: 1 | ||
}); | ||
t.id.should.equal(tid); | ||
t.track(done); | ||
}); | ||
it('coerces transaction ID to string', function(){ | ||
var tid = th.genTransactionID(); | ||
var t = GS.createTransaction(tid); | ||
t.id.should.equal('' + tid); | ||
}); | ||
it('tracks successfully with name as arg', function(done){ | ||
var name = 'node-gosquared-test'; | ||
var t = GS.createTransaction(th.genTransactionID()); | ||
t.addItem(name, { | ||
price: 1.99, | ||
quantity: 1 | ||
}); | ||
t.items[0].name.should.equal(name); | ||
t.track(done); | ||
}); | ||
it('tracks successfully with name as prop', function(done){ | ||
var name = 'node-gosquared-test'; | ||
var t = GS.createTransaction(th.genTransactionID()); | ||
t.addItem({ | ||
name: 'node-gosquared test', | ||
name: name, | ||
price: 1.99, | ||
@@ -45,9 +84,41 @@ quantity: 1 | ||
t.record(done); | ||
t.items[0].name.should.equal(name); | ||
t.track(done); | ||
}); | ||
it('tracks multiple items successfully', function(done){ | ||
var t = GS.createTransaction(th.genTransactionID()); | ||
t.addItems([ | ||
{ | ||
name: 'node-gosquared-test', | ||
price: 1.99, | ||
quantity: 1 | ||
}, | ||
{ | ||
name: 'node-gosquared-test', | ||
price: 1.99, | ||
quantity: 1 | ||
}, | ||
{ | ||
name: 'node-gosquared-test', | ||
price: 1.99, | ||
quantity: 1 | ||
} | ||
]); | ||
t.items.length.should.equal(3); | ||
t.track(done); | ||
}); | ||
it('can override revenue and quantity', function(done){ | ||
var t = GS.createTransaction(12345678); | ||
var t = GS.createTransaction(th.genTransactionID(), { | ||
revenue: 10, | ||
quantity: 5 | ||
}); | ||
t.addItem({ | ||
name: 'node-gosquared test', | ||
name: 'node-gosquared-test', | ||
price: 1.99, | ||
@@ -57,7 +128,21 @@ quantity: 1 | ||
t.revenue = 10; | ||
t.quantity = 5; | ||
t.track(done); | ||
}); | ||
t.record(done); | ||
it('can include additional customer attributes', function(done){ | ||
var t = GS.createTransaction(th.genTransactionID(), { | ||
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/' | ||
}); | ||
t.addItem({ | ||
name: 'node-gosquared-test', | ||
price: 1.99, | ||
quantity: 1 | ||
}); | ||
t.track(done); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
23296
18.24%582
15.25%175
19.86%