fvdm/nodejs-piwik


Access a Piwik API from node.js

https://www.npmjs.com/package/piwik

License: Unlicense

Language: JavaScript

Keywords: nodejs, piwik, piwik-api, unlicense


piwik

Track hits and access a Piwik API with Node.js

npm Build Status Coverage Status bitHound Dependencies bitHound Code Greenkeeper badge

Example

const piwik = require ('piwik').setup ('https://example.tld/piwik/', 'abc123');

// track a pageview
piwik.track (
  {
    idsite:      1,
    url:         'http://mysite.tld/some/page',
    action_name: 'Page Title',
    _cvar:       { '1': ['group', 'customer'] }
  },
  console.log
);

Installation

npm i piwik --save

.setup

( baseURL, [token], [timeout] )

In order to use this module you need to start with setup().

argument type required description
baseURL string yes The URL to your Piwik installation. Both HTTP and HTTPS are supported.
token string no Your API access token. Either set token or include token_auth in the baseURL.
timeout integer no Request wait time out in ms, default 5000 (5 seconds).
const piwik = require ('piwik') .setup ('https://example.tld/piwik/', 'abc123');

.api

( vars, callback )

Call an API method.

argument type required description
vars object yes see documentiation
callback function yes function (err, data)

Reporting API docs

// page urls for today
piwik.api (
  {
    method:   'Actions.getPageUrls',
    idSite:   1,
    period:   'day',
    date:     'today'
  },
  console.log
);

.track

( vars, callback )

Track a hit.

argument type required description
vars object or array yes see documentation
callback function no function (err, data)

Tracking API docs

// track a pageview
piwik.track (
  {
    idsite:      1,
    url:         'http://mysite.tld/some/page',
    action_name: 'Page Title',
    _cvar:       { '1': ['group', 'customer'] }
  },
  console.log
);

// track many at once (log import)
piwik.track (
  [
    {
      idsite:      1,
      url:         'http://mysite.tld/some/page',
      action_name: 'Page Title',
    },
    {
      idsite:      1,
      url:         'http://mysite.tld/blog/123-hello',
      action_name: 'Hello World',
    }
  ],
  console.log
);

.loadSpammers

( callback )

Retrieve referrer spammers blocklist maintained by Piwik as an array.

Open source list

Disclaimer: the example below is intended only for educational purposes. ;)

const Kira = require ('Kira');
const revenge = new Kira ();

piwik.loadSpammers (function (err, list) {
  if (err) { return console.log (err); }

  // Destroy them all
  list.forEach (function (target) {
    revenge.kill ('http://'+ target, 200, 10000);
  });
});

Callback and Errors

The callback function receives two parameters: err and data. When an error occurs err is an instance of Error. When all is good err is null and data is set.

message description additional
request failed Request cannot be made see err.error
http error HTTP error err.code and err.body
api error API error err.text
track failed Track method failed err.data
piwik.api (props, function (err, data) {
  if (err) {
    console.log (err);
    return;
  }

  console.log (data);
});

Unlicense

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org

Author

Franklin van de Meent

Buy me a coffee

Project Statistics

Sourcerank 11
Repository Size 151 KB
Stars 34
Forks 19
Watchers 1
Open issues 0
Dependencies 2
Contributors 4
Tags 14
Created
Last updated
Last pushed

Top Contributors See all

Franklin van de Meent Greenkeeper Julien Fontanet Josef Engelfrost

Packages Referencing this Repo

piwik
Track hits, access your Piwik API, or retrieve the referrer spammers list.
Latest release 1.0.9 - Updated - 34 stars

Recent Tags See all

v1.0.9 December 12, 2017
v1.0.8 December 19, 2016
v1.0.7 December 19, 2016
v1.0.6 May 25, 2016
v1.0.5 March 29, 2016
1.0.4 September 23, 2015
1.0.3 September 18, 2015
1.0.2 September 18, 2015
1.0.1 July 15, 2015
1.0.0 June 27, 2015
0.4.1 March 19, 2015
0.4.0 March 02, 2015
0.3.0 December 22, 2014
0.2.0 August 09, 2013

Interesting Forks See all

sandfox/nodejs-piwik
Access a Piwik API from node.js
CoffeeScript - Last pushed - 30 stars - 9 forks

Something wrong with this page? Make a suggestion

Last synced: 2017-12-12 15:06:15 UTC

Login to resync this repository