npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

matomo-reporting-js

v1.2.0

Published

<h1 align="center"> matomo-reporting-js </h1>

Downloads

4

Readme

Easily access your Matomo's reporting API from within Node or a browser


This module lets you use the reporting API. If you wish to set up visit tracking, please see https://developer.matomo.org/guides/tracking-javascript-guide

Features:

  • Automatic query batching
  • Full async/await support
  • Full and easy access to the reporting API

Getting started

Install from NPM by running npm i -S matomo-reporting-js, or yarn add matomo-reporting-js Afterwards, you can pull the module in with import MatomoApi from 'matomo-reporting-js'

Options

Two types of options exist:
Options to pass when creating the instance with new:

new MatomoApi({
  // Mandatory
  // If you're using isomorphic-fetch, it's window.fetch in the browser 
  // and global.fetch in Node. Otherwise, please consult your implementation's
  // manual if any. Most browsers have window.fetch available by default.
  'fetch': global.fetch,
  
  // Optional, default: undefined
  // Defines the agent to be used for the fetch call.
  // Useful if you want to ignore SSL errors in server side applications.
  'agent': new https.Agent(),

  // Optional, default: undefined
  // If you specify a handler, network and API errors will not cause the promise
  // to reject and the Error instance will be passed to the function.
  // If the handler returns anything, it will be used for the promise response.
  // Otherwise, the promise will reject for any error.
  'handler': (error) => sendToLoggingService(error),
}, {
  // Mandatory
  // Set your Matomo instance's URL here
  // Depending on your server configuration, index.php may be required at the end.
  'endpoint': 'https://demo.matomo.org/index.php',
  
  // Mandatory
  // Your site ID you wish to track with this instance of the client
  'idSite':   3,
  
  // Optional, default: 100
  // This number (in miliseconds) dictates how much to wait for queries before
  // sending the bulk request to the API.
  'patience': 100,
  
  // Optional, default: undefined
  // This string defines your authentication token if your site is not set
  // to public.
  'token_auth': 'asdfghjkl1234567',
  
  // Optional, default: {}
  // Additional network headers to send in a key-value format
  'headers': {},
  
  // Optional, default: false
  // If true, the module will log its requests to the console before sending
  'debug': false,
})

Options to pass when running a query:

const api = new MatomoApi(options)

api.query({
  // Mandatory
  // The reporting API method you wish to use
  'method': 'ExampleAPI.getMatomoVersion',
  
  // Optional, default: 'month'
  // The timeframe you'd like to request data for
  'period': 'month',
  
  // Optional, default: 'today'
  // The start date the API should use to start from
  'date': 'today',
  
  // You must also put API specific options here as well, see the
  // examples below.
}),

Examples:

There are two main ways you can use this package. First, the standard Promise.all, which will net you an array of results that includes all responses.

import MatomoApi from 'matomo-reporting-js'
import fetch from 'node-fetch'

// Create and configure the interface
const api = new MatomoApi(options)

// Example implementation
const run = async () => {
  // Matomo provides some example APIs that return pre-defined strings.
  // Here, we run queries we want to receive.
  //
  // For a full list of what you can run, see https://developer.matomo.org/api-reference/reporting-api

  const results = await Promise.all([
    // Gets the running Matomo version
    api.query({
      'method': 'ExampleAPI.getMatomoVersion',
    }),
    // Adds two number arguments together
    // You can specify any argument that the reporting API accepts.
    api.query({
      'method': 'ExampleAPI.getSum',
      'a':      6,
      'b':      3,
    }),
    // Returns an object that has no data, but is otherwise successful
    api.query({
      'method': 'ExampleAPI.getNull',
    }),
    // Returns a string-key dictionary that consists of arrays with mixes types
    api.query({
      'method': 'ExampleAPI.getMultiArray',
    }),
  ])

  return JSON.stringify(results, null, 2)
}

run()
.then(console.log)
.catch(console.error)

More conveniently, you can also use it for singular requests and as traditional promises. Query batching will work automatically this way as well, even across functions and files.

const run = () => {
  api.query({
    'method': 'ExampleAPI.getMatomoVersion',
  })
  .then(console.log)
  .catch(console.error)

  api.query({
    'method': 'ExampleAPI.getSum',
    'a':      6,
    'b':      3,
  })
  .then(console.log)
  .catch(console.error)

  api.query({
    'method': 'ExampleAPI.getNull',
  })
  .then(console.log)
  .catch(console.error)

  api.query({
    'method': 'ExampleAPI.getMultiArray',
  })
  .then(console.log)
  .catch(console.error)
}

run()