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

modern-analytics

v0.0.13

Published

modern analytics app

Downloads

225

Readme

modern-analytics

An Modern Analytics!

What is this repository for?

A lightweight analytics abstraction library for tracking page views & custom events. This library has support of custom analytics plugin and google analytics plugin.

Install

This module is distributed via npm, which is bundled with node and should be installed as one of your project's dependencies.

npm install modern-analytics --save

How to use

Step 1:

Wrap App component with AnalyticsProvider which is imported from modern-analytics package

import React from 'react'
import { BrowserRouter as Router } from 'react-router-dom'
import { AnalyticsProvider } from 'modern-analytics'

function App() {
  return (
    <Router basename='/'>
      <AnalyticsProvider>
        <ModernPages /> //Application Pages
      </AnalyticsProvider>
    </Router>
  )
}

export default App

Step 2:

We have to setDetails that are required for custom analytics using setAnalyiticDetails function provided by useAnalyticsContext hook inside your ModernPages component

import { useAnalyticsContext } from 'modern-analytics'

const ModernPages = () => {
  const { analytics, setAnalyiticDetails } = useAnalyticsContext()
  useEffect(() => {
    setAnalyiticDetails({
      appName: 'your appName',
      appVersion: 'your appVersion',
      analyticsUrl: 'your analytics Url where data will store',
      userId: 'your userId',
    })
  }, [])

  return <div>ModernPages</div>
}

setAnalyticDetails

This callback function is used to set required details for analytics.

Arguments

  • appVersion (required) string - your app version
  • analyticsUrl (required) string - your analytics api url
  • userId (optional) string - user id
  • [disableCustomPlugin] (optional) boolean - option to disable custom plugin
  • [onAnalyticsError] (optional) function - request callback on api error
  • [externalPluginsConfig] (optional) Object - config object to provide details for google analytics plugin use
  • [extraPlugins] (optional) Record<string,any>[] - option to provide custom plugins from outside

analyticsUrl

Here analyticsUrl is api endpoint which is used to save data using post request. This request will contain some payload which will expect following attributes:

  • eid (required) string The unique identifier of the event.
  • et (required) string The type or category of the event.
  • en (required) string The name of the event.
  • etm (required) integer The date and time when the event occurred.
  • uid (required) string The identifier for the user who triggered the event.
  • sid (optional) string The identifier for the user's session.
  • pn (optional) string The name of the page where the event occurred.
  • pu (optional) string The URL of the page where the event occurred.
  • ep (optional) object Additional event-specific properties as key-value pairs.
  • an (required) string The name of the app where the event occurred.
  • av (required) string The version of the app where the event occurred.
  • ua (required) string The user agent string, which provides additional device and browser information.
  • rf (optional) string The source URL or referrer, if the event resulted from a referral.
  • eo (optional) string The outcome or result of the event, if applicable.

Step 3:

To collect the data we have to call functions provided by anlytics instance which are following

analytics.track

Track an analytics event. This will trigger track calls in any installed plugins

Arguments

  • eventName String - Event name
  • [properties] (optional) Object - Additional event-specific properties as key-value pairs.

Example

// Basic event tracking
analytics.track('buttonClicked')

// Event tracking with properties
analytics.track('buttonClicked', {
  price: 11,
  sku: '1234',
})

analytics.page

Trigger page view. This will trigger page calls in any installed plugins

Arguments

  • [properties] (optional) Object - Additional event-specific properties as key-value pairs.

Example

// Basic page tracking
analytics.page()

// Page tracking with page data
analytics.page({
  pageData: 'xyz page',
})

Other plugin support

For now this library supports two plugin custom plugin and google analytics plugin. To use google analytics, just pass config required for google analytics in externalPluginsConfig key.

For using google analytics v4 please provide measurement ids array in googleAnalyticsV4MeasurementIds and for using google analytics v3 please provide tracking id string in googleAnalyticsV3TrackingId.

Here is a quick example of a plugin:

import { useAnalyticsContext } from 'modern-analytics'

const ModernPages = () => {
  const { analytics, setAnalyiticDetails } = useAnalyticsContext()
  useEffect(() => {
    setAnalyiticDetails({
      appName: 'appName',
      appVersion: 'appVersion',
      analyticsUrl: 'Url',
      userId: 'userId',
      externalPluginsConfig: {
        googleAnalyticsV4MeasurementIds: ['id1', 'id2'],
        googleAnalyticsV3TrackingId: 'id3',
      },
      disableCustomPlugin: true,
    })
  }, [])

  return <div>ModernPages</div>
}

If you want to enable both plugins, set disableCustomPlugin to false and add externalPluginsConfig in details