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

redux-analytics-manager

v2.1.5

Published

Redux middleware for registering actions to analytics events.

Downloads

28

Readme

Redux Analytics Manager

Build Status

This module works as redux middleware allowing you to listen for actions and send a specified analytics object. You can also register callbacks instead of sending vanilla objects. Action callbacks are passed the original action, the current state, and the next state.

Installation

npm install --save 'redux-analytics-manager'

Usage

  • Define a send method to be used with your registered actions. You can use any analytics library. The send method will be passed the the analytics object from the registered action along with the current state for further processing if necessary.
  • Register actions with either analytics objects, or callbacks. Callbacks are passed the original action, the current state, and the next state. If the callback doesn't return anything the send method won't be called.
  • Call the middleware create method

If you register the same action more than once, the calls to your send method will occur in the order they were defined. You can also use registerActions to register an array of actions to the same analytics object, callback, or a mixed array.

// Example using Google Analytics

import { applyMiddleware, createStore } from 'redux';
import { ReduxAnalyticsManager } from 'redux-analytics-manager';

const manager = new ReduxAnalyticsManager();

ga('create', 'UA-XXXXX-Y', 'auto');

function sendAnalytics(analyticObj, currState) {
    if (currState.analyticsEnabled) {
        ga('send', analyticsObj);
    }
}

manager.setSendMethod(sendAnalytics);

// Sending plain analytics object
manager.registerAction(
    'MY FANCY ACTION', 
    {
        eventCategory: 'fancy-actions',
        eventLabel: 'my fancy action'
    }
)

// Register callback and return analytics to be sent
manager.registerAction(
    'PURCHASE PRODUCT',
    (action, currState, nextState) => {
        const product = action.product;
        const region = currState.regionID;
        return {
            eventCategory: region,
            eventAction: 'product-purchase',
            eventLabel: product
        };
    }
);

// Register an array of listeners for a single action
manager.registerAction(
    'DOWNLOAD IMAGE',
    [
        {eventCategory: 'page-visits', eventAction: 'page-visited'},
        (action, currState, nextState) => {
            const imageName = action.image;
            const totalDownloads = nextState.images[imageName].downloads;
            return {
                eventCategory: 'Images',
                eventAction: 'image-downloaded',
                eventLabel: imageName,
                eventValue: totalDownloads
            };
        }
    ]
);

// Conditionally send analytics based on state
manager.registerAction(
    'LOGIN USER',
    (action, currState, nextState) => {
        const firstTime = (
            currState.loginCount = 0 && nextState.loginCount === 1;
        );
        if (firstTime) {
            return {
                eventCategory: 'First Time Events',
                eventAction: 'user-login',
                eventLabel: action.userName
            }
        }
    }
);

const analyticsMiddleware = manager.createMiddleware();

const store = createStore(rootReducer, applyMiddleware(analyticsMiddlware));

Methods

  • constructor: For typescript, pass an analytics object type and your appState type during instantiation
  • setSendMethod: User defined function that will be called with registered analytics object or the returned analytics object from registered callback
  • createMiddleware: Returns redux middleware to be used in the redux applyMiddlware method
  • registerAction: Register a single action to an analytics object, callback, or mixed array of either
  • registerActions: Register an array of actions to an analytics object, callback, or a mixed array of either
  • deRegisterAction: Removes action listener and stops calling send method / callbacks for that action
  • deRegisterActions: Removes action listeners for an array of actions
  • deRegisterAll: Removes all action listeners