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

analytics-engine-js

v1.0.7

Published

`analytics-engine-js` is a robust Node.js package designed to interact with a Redis-backed service for logging events, retrieving detailed analytics, and monitoring system performance. This package provides methods to send event data, fetch analytics for

Downloads

160

Readme

Analytics Engine

analytics-engine-js is a robust Node.js package designed to interact with a Redis-backed service for logging events, retrieving detailed analytics, and monitoring system performance. This package provides methods to send event data, fetch analytics for specific commands, and obtain system statistics.

Table of Contents

Installation

Install the package via npm:

npm install analytics-engine-js

Usage

Creating an Instance

Instantiate the AnalyticsEngine by providing your authorization token and the instance URL of your analytics service.

import AnalyticsEngine from 'analytics-engine-js';

const analyticsClient = new AnalyticsEngine({
    authorization: 'your-authorization-token', // Replace with your actual authorization token
    instanceUrl: 'http://localhost:3000', // Your service URL
});

Sending Events

Use the event method to send event data to the analytics service. The method accepts an object with event details.

const eventData = {
    name: 'commandA', // The name of the command/event
    uniqueId: 'user1', // The ID of the user triggering the event
    createdAt: Date.now(), // The timestamp in milliseconds
    type: 'commands', // The type of event
};

await analyticsClient.event(eventData);
console.log('Event sent successfully!');

Retrieving Analytics

Retrieve analytics data for specific commands using the getStatistics method. You can specify options like lookback period and filters.

const statistics = await analyticsClient.getStatistics({
    lookback: 7, // Optional: Specify lookback period in days
    uniqueId: 'user1', // Optional: Filter by unique user ID
    type: 'commands', // Optional: Filter by event type
});
console.log('Analytics Data:', statistics);

Flushing Statistics

Use the flushStatistics method to delete analytics data based on specified criteria.

const flushResult = await analyticsClient.flushStatistics({
    type: 'commands', // Optional: Specify the type of data to flush
});
console.log('Flush successful:', flushResult);

Getting System Statistics

Monitor the overall performance of the analytics service by using the getStats method, which returns system and Redis statistics.

const stats = await analyticsClient.getStats();
console.log('System Statistics:', stats);

Data Structures

Request Data Example

The structure of the data sent when logging an event:

export type RequestData = {
    name: string; // The name of the event
    uniqueId?: string; // The ID of the user triggering the event
    createdAt?: number; // The timestamp of the event in milliseconds
    type?: string; // The type of event
};

// Example of RequestData
const eventData: RequestData = {
    name: 'commandA',
    uniqueId: 'user1',
    createdAt: Date.now(),
    type: 'commands',
};

Response Data Example

The structure of the response returned when retrieving analytics data:

export type ResponseType<T> = {
    status: HttpStatusCode.Ok;
    data: T; // The data returned on a successful request
} | {
    status: Omit<HttpStatusCode, HttpStatusCode.Ok>; // Any other status codes
    error: string; // Error message
};

// Example ResponseType for Analytics Data
const response: ResponseType<AnalyticsData<string>> = {
    status: HttpStatusCode.Ok,
    data: {
        global: {
            daily: {
                '2024-08-01': 10,
                '2024-08-02': 15,
            },
            weekly: {
                '2024-08-01': 40,
            },
            monthly: {
                '2024-08': 150,
            },
        },
        usages: {
            commandA: {
                daily: {
                    '2024-08-01': 5,
                    '2024-08-02': 8,
                },
                weekly: {
                    '2024-08-01': 30,
                },
                monthly: {
                    '2024-08': 100,
                },
            },
            commandB: {
                daily: {
                    '2024-08-01': 3,
                    '2024-08-02': 7,
                },
                weekly: {
                    '2024-08-01': 10,
                },
                monthly: {
                    '2024-08': 50,
                },
            },
        },
    },
};

Examples

Fetching Analytics Data

Here is an example of how to fetch and print analytics data:

type CommandList = 'commandA' | 'commandB';
const analyticsData = await analyticsClient.getStatistics<CommandList>({
    lookback: 7,
    uniqueId: 'user1',
    type: 'commands,
});
console.log('Analytics Data:', JSON.stringify(analyticsData, null, 2));

License

This project is licensed under the MIT License - see the LICENSE file for details.