Coder Social home page Coder Social logo

nodejs-piwik's Introduction

piwik (Matomo)

Track hits and access a Matomo API with Node.js

The package is named after the previous name of Matomo. I don't feel like changing the name, because people are using it in their apps.

npm Build Status Coverage Status

DEPRECATION NOTICE

This package is too old to modernize. Since there are much better modern alternatives available, the code with not be updated anymore. The GitHub repo will be archived.

Example

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

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

Installation

npm install piwik

.setup

( baseURL, [token], [timeout] )

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

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

.api

( vars, callback )

Call an API method.

argument type description
vars object see documentation
callback function (err, data)

Reporting API docs

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

.track

( vars, callback )

Track a hit.

argument type description
vars object or array see documentation
[callback] function (err, data)

Tracking API docs

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

// track many at once (log import)
matomo.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 Matomo as an array.

Open source list

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
matomo.api( props, ( 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 https://unlicense.org

Author

Franklin | Buy me a coffee

nodejs-piwik's People

Contributors

engelfrost avatar fvdm avatar greenkeeper[bot] avatar greenkeeperio-bot avatar julien-f avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nodejs-piwik's Issues

Cannot resolve module 'fs' in ...httpreq/lib

Adding

var piwik = require('piwik');

makes webpack go:

ERROR in .//piwik//httpreq/lib/httpreq.js
Module not found: Error: Cannot resolve module 'fs' in /home/user/workspace/project/node_modules/piwik/node_modules/httpreq/lib
@ .//piwik//httpreq/lib/httpreq.js 31:9-22 187:18-31

Any idea why?

Crash on no res

I'm hammering this with ab, piwik works on a small machine. Sometimes mysql goes offline, and piwik goes too. So no response returns without error and it crashes like this

/Users/xxxxx/Documents/workspace/xxxxx/node_modules/piwik/piwik.js:162
data = res.body;
^
TypeError: Cannot read property 'body' of undefined

I added a control to if there is a res or not.

if (res) {
    try {
        data = JSON.parse (res.body);
        if (data.result && data.result === 'error') {
          error = new Error ('api error');
          error.text = data.message || null;
        }
      }
      catch (e) {
        data = res.body;
      }

      if (res.statusCode >= 300) {
        error = new Error ('http error');
        error.code = res.statusCode;
        error.body = data;
      }
  }

so it stopped crashing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.