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

@zaxiya/popularity-metric

v1.2.0

Published

Calculte the popularity of items using certain metrics like clicks, shares, etc.

Downloads

5

Readme

Popularity Metric

npm version License

Description

Calculte the popularity of items using certain metrics like clicks, shares, etc.

Installation

Install the package using npm:

npm install @zaxiya/popularity-metric

Usage

Registering Events and Weights

You can register events and their corresponding weights using the registerEventsAndWeights function. Here's an example:

import { registerEventsAndWeights } from "@zaxiya/popularity-metric";

// Register events and weights
registerEventsAndWeights([
  ["num_of_review", 0.2],
  ["average_rating", 0.25],
  // Add more events and weights
]);

Tracking Events

You can use the trackEvent function to track events and store their counts in localStorage. Here's an example:

import { trackEvent } from "@zaxiya/popularity-metric";

// Track an event
trackEvent("num_of_review", "providerA", 1);

Getting Store by Popularity

You can retrieve the stored event data and calculate the weighted averages using the getStoreByPopularity function. It returns an array sorted by popularity. Here's an example:

import { getStoreByPopularity } from "@zaxiya/popularity-metric";

// Get the store by popularity
const storeByPopularity = getStoreByPopularity();
console.log(storeByPopularity);

Tracking Time on Pages

You can track the time spent on specific pages using the trackTimeOnPages function. It allows you to define patterns for page URLs and tracks the time spent on matching pages. Here's an example:

import { trackTimeOnPages } from "@zaxiya/popularity-metric";

trackTimeOnPages({
  weight: 0.1,
  patterns: [
    {
      pattern: /\/provider\/\w+/,
      splitPosition: 2,
    },
  ],
});

The trackTimeOnPages function takes an object with configuration options as its parameter.

  • weight: This option allows you to assign a weight to the "time_on_page" metric. The weight determines the importance of the time spent on a page when calculating popularity metrics. You can adjust this value based on your specific requirements.

  • patterns: This option is an array of pattern objects. Each pattern object specifies a regular expression pattern (pattern) that matches the URLs of the pages you want to track. You can define multiple patterns to track different types of pages. In the provided code example, the pattern //provider/\w+/ is used to match URLs that contain "/provider/" followed by one or more word characters.

  • splitPosition: This option is used to extract a specific part of the matched URL as the ID for every provider or page. The value you provide here determines the position of the segment that will be used as the ID. In the given example, 2 is used, indicating that the ID should be extracted from the second segment of the URL after the "/provider/" part.

License

This package is open source and available under the MIT License.