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

session-event-logger

v1.3.0

Published

send event analytics from frontend

Downloads

13

Readme

Analytics Module README

Overview

This analytics module provides functionality to track user sessions and events and send data to a server for analysis. It offers flexibility in configuration, allowing customization of parameters such as server URLs, retry limits, heartbeat intervals, and more.

File Structure

  • analyticsMain.ts: Contains the main logic for managing sessions, tracking events, and sending data to the server.
  • analyticsInterface.ts: Defines interfaces for analytics options and the main analytics module.
  • analytics.ts: Implements the main analytics module based on the defined interfaces.
  • analyticsDefault.ts: Provides default options and helper functions for sending HTTP requests.

Usage

Configuration

Before using the analytics module, you need to configure it with the desired options. You can configure the following options:

  • baseUrl (string): The base URL of the analytics server. Default is an empty string.
  • retryLimit (number): The maximum number of retries for failed event submissions. Default is 5.
  • eventQueueTime (number): The interval (in milliseconds) at which queued events are sent to the server. Default is 0 (disabled).
  • heartBeatTime (number): The interval (in milliseconds) at which heartbeat events are sent to the server. Default is 10000 (10 seconds).
  • heartBeatPayload (any): Additional data to include in heartbeat events. Default is an empty object.
  • id (string or number): Identifier for the participant. Default is an empty string.
  • sendEventsInQueue (boolean): Whether to send events immediately or queue them. Default is false.
  • heartBeatUrl (string): The URL for sending heartbeat events. Default is an empty string.
  • sessionUrl (string): The URL for starting sessions. Default is an empty string.

To configure the analytics module, use the configure method:

import Analytics from './analytics';

const options = {
    baseUrl: 'https://example.com/analytics',
    retryLimit: 3,
    eventQueueTime: 5000,
    // Other options...
};

Analytics.configure(options)
    .then(() => {
        console.log('Analytics module configured successfully');
    })
    .catch((error) => {
        console.error('Error configuring analytics module:', error);
    });

Session Management

To start a session, use the startSession method: token and pubkey are for authorization

import Analytics from './analytics';

const sessionData = {
    // Session data object...
};

Analytics.startSession(sessionData,token,pubkey)
    .then(() => {
        console.log('Session started successfully');
    })
    .catch((error) => {
        console.error('Error starting session:', error);
    });
{
    "sessionId": "<generated session ID>",
    "clientTimestamp": "<current timestamp>",
    "uniqueId": "<ID>",
    "data":"sessionData"
}

To end a session, use the endSession method:

import Analytics from './analytics';

Analytics.endSession()
    .then(() => {
        console.log('Session ended successfully');
    })
    .catch((error) => {
        console.error('Error ending session:', error);
    });

Event Tracking

To track an event, use the trackEvent method:

import Analytics from './analytics';

const eventName = 'button_click';
const eventData = {
    // Event data object...
};

Analytics.trackEvent(eventName, eventData)
    .then(() => {
        console.log('Event tracked successfully');
    })
    .catch((error) => {
        console.error('Error tracking event:', error);
    });
{
    "eventType": "<event type>",
    "data": {
        // Event-specific data...
    },
    "sessionId": "<current session ID>",
    "clientTimestamp": "<current timestamp>",
    "uniqueId": "<ID>",
    // Other event-related data...
}

hearBeat

To check if session is active or not

{
    "sessionId": "<current session ID>",
    "clientTimestamp": "<current timestamp>",
    "uniqueId": "<ID>",
    "heartBeatPayload":"heartBeatPayload"
    // Other heartbeat data...
}

Default Values

The analytics module provides default values for configuration options. Here are the default values:

baseUrl: ""
retryLimit: 5
eventQueueTime: 0
heartBeatTime: 10000
heartBeatPayload: {}
id: ""
sendEventsInQueue: false
heartBeatUrl: ""
sessionUrl: ""