piwik-tracker
Advanced tools
Comparing version 0.1.2 to 1.0.0
41
index.js
@@ -11,8 +11,10 @@ /** | ||
var events = require('events'), | ||
util = require('util'), | ||
qs = require('querystring'), | ||
agent; | ||
const assert = require('assert'); | ||
const events = require('events'); | ||
const util = require('util'); | ||
const qs = require('querystring'); | ||
let agent; | ||
/** | ||
@@ -23,18 +25,10 @@ * @constructor | ||
*/ | ||
function PiwikTracker(siteId, trackerUrl) { | ||
function PiwikTracker (siteId, trackerUrl) { | ||
if (!(this instanceof PiwikTracker)) { return new PiwikTracker(siteId, trackerUrl); } | ||
events.EventEmitter.call(this); | ||
if (!siteId || isNaN(+siteId)) { | ||
throw new Error('siteId must be provided.'); | ||
} | ||
assert.ok(siteId && !isNaN(siteId), 'Piwik siteId required.'); | ||
assert.ok(trackerUrl && typeof trackerUrl == 'string', 'Piwik tracker URL required, e.g. http://example.com/piwik.php') | ||
assert.ok(trackerUrl.endsWith('piwik.php'), 'A tracker URL must end with "piwik.php"') | ||
if (!trackerUrl || typeof trackerUrl !== 'string') { | ||
throw new Error('A tracker URL must be provided, e.g. http://example.com/piwik.php'); | ||
} | ||
if (trackerUrl.toString().indexOf('piwik.php') === -1) { | ||
throw new Error('A tracker URL must contain piwik.php in the URL, e.g. http://example.com/piwik.php'); | ||
} | ||
this.siteId = siteId; | ||
@@ -44,3 +38,3 @@ this.trackerUrl = trackerUrl; | ||
// Use either HTTPS or HTTP agent according to Piwik tracker URL | ||
agent = require( /^https:/.test(trackerUrl) ? 'https' : 'http' ); | ||
agent = require( trackerUrl.startsWith('https') ? 'https' : 'http' ); | ||
} | ||
@@ -58,4 +52,3 @@ util.inherits(PiwikTracker, events.EventEmitter); | ||
*/ | ||
PiwikTracker.prototype.track = function track(options) { | ||
var self = this; | ||
PiwikTracker.prototype.track = function track (options) { | ||
var hasErrorListeners = this.listeners('error').length; | ||
@@ -72,15 +65,13 @@ | ||
if (!options.url) { throw new Error('URL to be tracked must be specified.'); } | ||
assert.ok(options.url, 'URL to be tracked must be specified.'); | ||
var requestUrl = this.trackerUrl + '?' + qs.stringify(options); | ||
var req = agent.get(requestUrl, function(res) { | ||
var req = agent.get(requestUrl, (res) => { | ||
// Check HTTP statuscode for 200 and 30x | ||
if ( !/^(200|30[12478])$/.test(res.statusCode) ) { | ||
if (hasErrorListeners) { self.emit('error', res.statusCode); } | ||
if (hasErrorListeners) { this.emit('error', res.statusCode); } | ||
} | ||
}); | ||
req.on('error', function(e) { | ||
if (hasErrorListeners) { self.emit('error', e.message); } | ||
}); | ||
req.on('error', (err) => hasErrorListeners && this.emit('error', err.message)); | ||
@@ -87,0 +78,0 @@ req.end(); |
{ | ||
"name": "piwik-tracker", | ||
"version": "0.1.2", | ||
"version": "1.0.0", | ||
"description": "A wrapper for the Piwik tracking HTTP API", | ||
@@ -16,3 +16,5 @@ "keywords": [ | ||
}, | ||
"main": "./index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"repository": { | ||
@@ -26,12 +28,12 @@ "type": "git", | ||
"devDependencies": { | ||
"mocha": "~1.16.2", | ||
"sinon-chai": "~2.4.0", | ||
"chai": "~1.8.1", | ||
"sinon": "~1.7.3", | ||
"nock": "~0.27.1" | ||
"chai": "^3.5.0", | ||
"mocha": "^2.5.3", | ||
"nock": "^8.0.0", | ||
"sinon": "^1.17.5", | ||
"sinon-chai": "^2.8.0" | ||
}, | ||
"engines": { | ||
"node": ">=0.8.0" | ||
"node": ">=4.2.0" | ||
}, | ||
"license": "MIT" | ||
} |
Sorry, the diff of this file is not supported yet
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
0
5497
4
59