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

apostrophe-docs-popularity

v2.0.2

Published

Track and sort the popularity of pieces and pages on an Apostrophe site according to metrics of your choice: social network votes, views, votes, etc.

Downloads

21

Readme

apostrophe-docs-popularity

Tracks the popularity of your documents according to various metrics of your choice, resulting in a popularity property for each document. You get to choose which doc types participate.

Currently the metrics available are Facebook social reactions (including likes), Facebook comments and Facebook shares.

Facebook "dislikes" cannot be distinguished from likes.

Facebook data is obtained via the sharedcount API. You will need to obtain an API key from sharedcount to use this module.

Installation

npm install apostrophe-docs-popularity

Configuration

// in app.js
const apos = require('apostrophe')({
  modules: {
    'apostrophe-docs-popularity': {
      sharedcountApiKey: 'GOGETYOUROWN'
    },
    // configure it for one piece type
    'profiles': {
      extend: 'apostrophe-pieces',
      name: 'profile',
      // etc, then...
      popularity: {
        metrics: {
          facebook: {
            reactions: {
              // Give each metric a score so you can
              // weight some as more important than others.
              // Higher scores contribute more points to the
              // popularity score per reaction, etc.
              // It's just simple addition
              score: 1
            },
            comments: {
              score: 2
            },
            shares: {
              score: 3
            }
          }
        }
      }
    },
    // configure it for all pages (not pieces)
    'apostrophe-custom-pages': {
      popularity: // see above
    },
    // configure it for all docs with URLs,
    // both pages and pieces
    'apostrophe-doc-type-manager': {
      popularity: // see above
    }
  }
});

Updating popularity

Use the provided command line task:

node app apostrophe-docs-popularity:update-metrics

Use cron to schedule this to run at a time of your choosing, probably no more than once a day. See the sharedcount website for API quotas and pricing.

Sorting by popularity

The simplest way is to just change the default sort for one of your piece types. Let's continue the profiles module example from before:

    'profiles': {
      extend: 'apostrophe-pieces',
      name: 'profile',
      // Sort by popularity (descending order), then by title
      // as a tiebreaker
      sort: { popularity: -1, title: 1 },
      popularity: { ... see above ... }
    }

You can also sort any Apostrophe find() query by popularity:

// Most popular profile first
const profiles = self.apos.getManager('profile')
  .find(req)
  .sort({ popularity: -1 })
  .limit(10)
  .toArray();

For instance, you might do that in an apostrophe-pages:beforeSend promise event handler and attach it to req.data.profiles.

popularity is just a mongodb document property, so this works with direct MongoDB queries too.

Displaying popularity

If you have a piece, you could display its popularity score simply as piece.popularity. But you're probably also interested in displaying individual metrics like the number of Facebook reactions.

You can access that number as piece.popularityMetrics.facebook.reactions.

Note that likes, dislikes, "wow" reactions, etc. cannot be distinguished from one another. This is a limitation of the data available from the API.