Low-code Integrations

Integrating Scoby Analytics with Express

Prerequisites

Ensure you have your License Key and Salt ready.

Setting up Environment Variables in Express

  1. Open the .env file:
  • Located in the root directory of your Express project.
  1. Add the following lines:

    SCOBY_API_KEY=your_api_key_here
    SCOBY_SALT=your_generated_salt_here
    
  • Replace your_api_key_here with your License Key.
  • Replace your_generated_salt_here with the generated salt.
  1. Access these values in your Express application:

    const apiKey = process.env.SCOBY_API_KEY;
    const salt = process.env.SCOBY_SALT;
    

Creating the Express Middleware

  1. Create a new middleware:

    touch middleware/scobyAnalyticsMiddleware.js
    
  2. Edit scobyAnalyticsMiddleware.js:

  • Install the necessary modules:

    npm i @scoby/analytics-ts request-ip
    
  • Import necessary modules at the top:

    const { Client } = require("@scoby/analytics-ts");
    const { getClientIp } = require("request-ip");
    
  • Define the middleware function:

    const scobyAnalyticsMiddleware = (req, res, next) => {
        const apiKey = process.env.SCOBY_API_KEY;
        const salt = process.env.SCOBY_SALT;
    
        const scoby = new Client(apiKey, salt);
    
        const ipAddress = getClientIp(request);
        const userAgent = request.headers["user-agent"];
        
        const requestedUrl = request.query["url"];
        const referringUrl = request.query["ref"];
        
        client.logPageView({
            ipAddress,
            userAgent,
            requestedUrl,
            referringUrl,
        });
    
        next();
    };
    
    module.exports = scobyAnalyticsMiddleware;
    
  1. Register the middleware:
  • In your main Express application file (e.g., app.js or server.js), import and use the middleware:

    const express = require('express');
    const scobyAnalyticsMiddleware = require('./middleware/scobyAnalyticsMiddleware');
    
    const app = express();
    
    // Register the middleware
    app.use(scobyAnalyticsMiddleware);
    
    // Other middleware and routes
    // ...
    
    app.listen(3000, () => {
        console.log('Server is running on port 3000');
    });
    

Final Steps

  1. Clear the configuration cache:
  • Not applicable in Express, but ensure your environment variables are properly loaded by restarting your server if necessary.

With the middleware set up and environment variables configured, Scoby Analytics SDK's features are now available throughout your Express application using the License Key and salt securely stored in the .env file.

Previous
Looker Studio