Strapi

Strapi is a headless CMS (content management system) based on Node.js. You can add Moesif API analytics and monitoring to your Strapi application using the Strapi Moesif plugin.

Supported Versions

Strapi Moesif plugin versions 2.0.0 and later only support Strapi 4 and above. For Strapi 3, use the 1.0.5 and later versions of the plugin.

Prerequisites

Before using Strapi Moesif plugin, make sure you meet the following requirements:

Installation

  1. Install the plugin with npm:

     npm install --save strapi-plugin-moesif
    
  2. Then enable the plugin n the plugins configuration file ./config/plugins.js:

     module.exports = {
       'strapi-plugin-moesif': {
         enabled: true,
         config: {
           moesif: {
           //custom config passed to moesif middleware goes here
           }
         },
       },
     }
    
  3. Add Moesif to your middleware list in the middlewares configuration file ./config/middleware.js:

     module.exports = [
       'strapi::errors',
       'strapi::security',
       'strapi::cors',
       'strapi::poweredBy',
       'strapi::logger',
       'strapi::query',
       'strapi::body',
       'strapi::favicon',
       'strapi::public',
       'plugin::strapi-plugin-moesif.moesif'
     ];
    
  4. Add a MOESIF_APPLICATION_ID environment variable. To get your Application ID, follow these steps: a. Log into Moesif Portal.

    b. Select the account icon to bring up the settings menu.

    c. Select Installation or API Keys.

    d. Copy your Moesif Application ID from the Collector Application ID field.

  5. Run Strapi:

    With npm:

     npm run develop
    

    With yarn:

     yarn develop
    

To test, make a few API calls to your endpoint like this:

curl http://localhost:1337

Heroku

For Heroku, install Moesif as an add-on. Heroku automatically creates and manages the MOESIF_APPLICATION_ID environment variable.

Configuration Options

This plugin uses moesif-nodejs under the hood. Therefore, you can use the configuration options for moesif-nodejs in Strapi Moesif plugin.

identifyUser

To track Strapi users, define the identifyUser function:

identifyUser: function (req, res) {
  if (req.state && req.state.user) {
    return String(req.state.user.id);
  }
  return undefined;
}

skip

You can set up Moesif to track all traffic in and out of your application, but you may be interested in API metrics specifically. The default configuration of Strapi Moesif plugin skips all non-JSON communication to avoid having excessive amounts of file requests in your Moesif dashboard.

To override the skip function, include one in your configuration, or set to send all communications:

// return true if the data should be skipped
skip: function (req, res) {
  // don't log non JSON types
  return (
    res.headers && !res.headers["Content-Type"].includes("application/json")
  );
}

//send all data regardless of type
skip: null

Updated: