Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

piwik-tracker

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

piwik-tracker - npm Package Compare versions

Comparing version
0.1.2
to
1.0.0
+16
-25
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();

@@ -1,2 +0,2 @@

Copyright 2014-2015 Frederic Hemberger
Copyright 2014-2016 Frederic Hemberger

@@ -3,0 +3,0 @@ Permission is hereby granted, free of charge, to any person obtaining

{
"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"
}
# http://editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[test/fixtures/*]
insert_final_newline = false
trim_trailing_whitespace = false
{
"node": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"expr": true,
"eqeqeq": true,
"immed": true,
"indent": 2,
"latedef": true,
"newcap": true,
"noarg": true,
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
"globals": {
"describe": false,
"it": false,
"before": false,
"after": false,
"beforeEach": false,
"afterEach": false
}
}
language: node_js
node_js:
- iojs
- '0.12'
- '0.11'
- '0.10'
/*jshint -W068 */
'use strict';
var chai = require('chai'),
sinon = require('sinon'),
sinonChai = require('sinon-chai'),
http = require('http'),
https = require('https'),
nock = require('nock');
chai.should();
chai.use(sinonChai);
var PiwikTracker = require('../index.js');
describe('PiwikTracker()', function() {
it('should thow if no parameters provided', function() {
(function(){ new PiwikTracker(); }).should.throw(/siteId/);
});
it('should thow if no siteId is provided', function() {
(function(){ new PiwikTracker(null); }).should.throw(/siteId/);
});
it('should thow if no trackerUrl is provided', function() {
(function(){ new PiwikTracker(1); }).should.throw(/tracker/);
});
it('should thow if no trackerUrl is not valid (no piwik.php endpoint)', function() {
(function(){ new PiwikTracker(1,'http://example.com/index.php'); }).should.throw(/tracker/);
});
it('should have properties siteId/trackerUrl', function() {
var piwik = new PiwikTracker(1, 'http://example.com/piwik.php');
piwik.siteId.should.equal(1);
piwik.trackerUrl.should.equal('http://example.com/piwik.php');
});
});
describe('#track()', function() {
var httpMock, httpSpy, piwik;
beforeEach(function() {
piwik = new PiwikTracker(1, 'http://example.com/piwik.php');
httpMock = nock('http://example.com')
.filteringPath(function() { return '/piwik.php'; })
.get('/piwik.php');
httpSpy = sinon.spy(http, 'get');
});
afterEach(function() {
piwik = null;
nock.restore();
httpSpy.restore();
});
it('should throw without parameter', function() {
(function(){ piwik.track(); }).should.throw(/URL/);
});
it('should accept a url as string', function() {
httpMock.reply(200);
piwik.track('http://mywebsite.com/');
httpSpy.should.have.been.calledWith('http://example.com/piwik.php?url=http%3A%2F%2Fmywebsite.com%2F&idsite=1&rec=1');
});
it('should accept an parameter object', function() {
httpMock.reply(200);
piwik.track({ url: 'http://mywebsite.com/' });
httpSpy.should.have.been.calledWith('http://example.com/piwik.php?url=http%3A%2F%2Fmywebsite.com%2F&idsite=1&rec=1');
});
it('should throw without options.url', function() {
(function(){ piwik.track({}); }).should.throw(/URL/);
});
it('should emit an error if HTTP response status is not 200/30x', function(done) {
httpMock.reply(404);
piwik.on('error', function(param) {
param.should.match(/^(404|getaddrinfo ENOTFOUND)/);
done();
});
piwik.track({ url: 'http://mywebsite.com/' });
});
});
describe('#track() - HTTPS support', function() {
var httpsMock, httpsSpy, piwik;
before(function() {
piwik = new PiwikTracker(1, 'https://example.com/piwik.php');
httpsMock = nock('https://example.com')
.filteringPath(function() { return '/piwik.php'; })
.get('/piwik.php');
httpsSpy = sinon.spy(https, 'get');
});
after(function() {
piwik = null;
nock.restore();
httpsSpy.restore();
});
it('should use HTTPS to access Piwik, when stated in the URL', function() {
httpsMock.reply(200);
piwik.track('http://mywebsite.com/');
httpsSpy.should.have.been.calledWith('https://example.com/piwik.php?url=http%3A%2F%2Fmywebsite.com%2F&idsite=1&rec=1');
});
});