1.2.15 • Published 6 years ago

node-analytics v1.2.15

Weekly downloads
7
License
MIT
Repository
github
Last release
6 years ago

#node-analytics

node-analytics is an ExpressJS visitor analytic middleware.

Installation

$ npm install --save node-analytics

Prerequisites

Complete node-analytics functionality relies upon the following:

  • MongoDB: a mongod instance must be running on the server for data storage
  • a MaxMind GeoIP database for location data; the free GeoLite2 City database is available here
  • an open WebSocket for client behaviour data collection; ensure port is open (default is 8080) or pass in server object

Though it will function without one, some, or all of the above.

Basic usage

app.js

In its current iteration node-analytics will process every server request, including static files. To restrict it to page loads call the middleware after the express.static middleware.

var express = require('express')
,   analytics = require('node-analytics')

var app = express();

// app middleware //
app.use(express.static(path.join(__dirname, 'public')));
app.use(analytics());   // beneath express.static

Client

The client script node-analytics-client.js must be included on the served webpage beneath the socket.io client script. Specify the client JS directory client_dir as an option and the file will be automatically copied, or disable this behaviour by client_copy: false.

<script type='text/javascript' src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script type='text/javascript' src='/path/to/node-analytics-client.js'></script>

User behaviour

On the client side, node-analytics logs clicks, reaches, and reads of elements, each assigned their own class:

EventLogged when elementElement classNote
clickclicked (e.g. link)na_clickEmits for every click
reachscrolled tona_reachEmits once per page load
readpaused at and, presumptively, readna_readTallies time spent paused within element bounds

Events will be associated with an element's id property, or will otherwise be assigned an index.

<section id="contact" class="na_read">      //id: contact
  
  <a href="mailto:me" class="na_click">     //id: reach_point_0
    Contact us
  </a>
  
  <span="fine_print" class="na_reach">      //id: read_section_0
    in exchange for your marbles
  </span>
  
</section>

Options

node-analytics may be adapted by the following options:

app.js

KeyDescriptionDefault
db_hostMongoDB host'localhost'
db_portMongoDB port27017
db_nameMongoDB database name'node_analytics_db'
ws_portWebSocket port; disabled if ws_server or s_io is set8080
ws_serverExpress server object; disabled if s_io is setnull
s_iosocket.io server objectnull
geo_ipUse GeoIP booleantrue
mmdbMaxMind DB path'GeoLite2-City.mmdb'
logOutput log booleantrue
error_logError log booleantrue
secureCookies: HTTPS only booleantrue
secretCookies: AES encryption key'changeMe'

Example use with WebSocket server instead of port (see necessary client edit below):

var server = require('http').createServer(app);
server.listen(80);

app.use(analytics({
  ws_server:  server
}));

Example use with socket.io server:

var server = require('http').createServer(app);
server.listen(80);

var io = require('socket.io').listen(server);
// OR:
var io = require('socket.io').listen(8080);

app.use(analytics({
  s_io: io
}));

Client

KeyDescriptionDefault
ws_hostWebsocket hostlocation.hostname
ws_portWebsocket port; disable if using ws_server or s_io in app.js8080
click_classClick-log class'na_click'
reach_classReach-log class'na_reach'
read_classRead-log class'na_read'
force_protocolForce 'http' or 'https' socket connectionnull

Example use including server support (editing node-analytics-client.js):

var na_obj = {
    ws_port:        null          // must be disabled if server object is being used
  , click_class:    'clicked_me'
  , reach_class:    'reached_me'
  , read_class:     'read_me'
};
1.2.15

6 years ago

1.2.14

6 years ago

1.2.13

6 years ago

1.2.12

6 years ago

1.2.11

6 years ago

1.2.10

6 years ago

1.2.9

6 years ago

1.2.8

7 years ago

1.2.7

7 years ago

1.2.6

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.3.27

7 years ago

0.3.26

7 years ago

0.3.25

7 years ago

0.3.24

7 years ago

0.3.23

7 years ago

0.3.22

7 years ago

0.3.21

7 years ago

0.3.20

7 years ago

0.3.19

7 years ago

0.3.18

7 years ago

0.3.17

7 years ago

0.3.16

7 years ago

0.3.15

7 years ago

0.3.14

7 years ago

0.3.13

7 years ago

0.3.12

7 years ago

0.3.11

7 years ago

0.3.10

8 years ago

0.3.9

8 years ago

0.3.8

8 years ago

0.3.7

8 years ago

0.3.5

8 years ago

0.3.4

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.5

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.25

8 years ago

0.1.24

8 years ago

0.1.23

8 years ago

0.1.22

8 years ago

0.1.21

8 years ago

0.1.20

8 years ago

0.1.19

8 years ago

0.1.18

8 years ago

0.1.17

8 years ago

0.1.16

8 years ago

0.1.15

8 years ago

0.1.14

8 years ago

0.1.13

8 years ago

0.1.12

8 years ago

0.1.11

8 years ago

0.1.10

8 years ago

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago