M O O N I T O

Integrate Analytics API with your Node.js Application (e.g., Express.js)

Here is the documentation guide on integrating Analytics API into Node.js applications (e.g., Express.js), we'll use the Fetch API for making HTTP requests in the browser.

  • First, sign up for the domain you want to protect and learn about its visitors on the Analytics page.

Add New Domain

  • Then, install the node-fetch library.
npm install node-fetch
  • Now, you can use the following Node.js code.
const express = require('express');
const fetch = require('node-fetch');

const app = express();

/* Configuration start */
const apiPublicKey = 'Your API Public Key'; // Put your API Public Key here
const apiSecretKey = 'Your API Secret Key'; // Put your API Secret Key here
/* Configuration end */

// Assuming you are using Express.js, if not, adjust the request object accordingly
app.use(async (req, res, next) => {
  // Client IP Address Retrieval
  const clientIp =
    req.headers['cf-connecting-ip'] ||
    req.headers['x-forwarded-for'] ||
    req.connection.remoteAddress;

  try {
    // Construct URL for API request
    const apiUrl =
      'https://moonito.net/api/v1/analytics?' +
      new URLSearchParams({
        ip: clientIp,
        ua: encodeURIComponent(req.headers['user-agent']),
        events: encodeURIComponent(req.url),
        domain: req.headers.host.toLowerCase(),
      });

    // Set up Fetch options
    const requestOptions = {
      method: 'GET',
      headers: {
        'User-Agent':
          'Mozilla/5.0 (Linux; Android 13; SM-G991U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36',
        'X-Public-Key': apiPublicKey,
        'X-Secret-Key': apiSecretKey,
      },
    };

    // Execute Fetch request
    const response = await fetch(apiUrl, requestOptions);
    const result = await response.json();

    // Process data as needed
    if (result.data.status.need_to_block) {
      // Do some action when a visitor is detected as "need_to_block"
      // For example, return 403 Forbidden
      res.status(403).send('Forbidden');
    } else {
      next();
    }
  } catch (error) {
    console.error('Error:', error);
    next();
  }
});

// Rest of your Express.js application setup goes here
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});
  • Input your Secret and Public API Key, then save.
const apiPublicKey = 'Your API Public Key'; // Put your API Public Key here
const apiSecretKey = 'Your API Secret Key'; // Put your API Secret Key here
  • Edit response handling, the code processes the API response, and if the status indicates the need to block the visitor, it takes action (e.g., return 403 Forbidden).
// Process data as needed
if (result.data.status.need_to_block) {
  // Do some action when a visitor is detected as "need_to_block"
  // For example, return 403 Forbidden
  res.status(403).send('Forbidden');
} else {
  next();
}
  • This example assumes you're using Express.js. If you're using another framework, adjust the request object accordingly. Also, ensure that your application has the necessary permissions to make outbound requests.
  • Do some testing. Open your Node.js application on a web browser. This Node.js code will run when the page loads, requesting the analytics endpoint.
  • For information regarding visitor statistics, you can go to the Analytics page for details.