0.2.8 • Published 2 years ago

content-recommender v0.2.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

npm version coverage-badge-green

content-recommender

A content recommendation system for Node.js based on Pearson correlation.

  • Creates unique UUIDs for each user visiting a website and stores in in localStorage
  • Keeps a log of each users page visits in a database. Currently supports fs and MongoDB
  • Calculates recommendations for paths to recommend new users based on similar users' visits

Table of Contents

Installation

npm install content-recommender

Importing

Server

const contentRecommender = require('content-recommender/server');

Client

import contentRecommender from 'content-recommender/client';

Setup examples

Server

Client (Coming soon..)

  • Pure JavaScript
  • React.js
  • Next.js

API

client.setId(localStorageKey)

Creates a random UUID for the current user, stores it to localStorage and returns it. If there already is a UUID, it is not changed.

Parameters

ParameterDescriptionisRequiredDefault value
localStorageKey: stringKey to be used to store user IDs in localStoragefalse'cr-id'

Return value

A string containing the user ID, e.g. '653d4b67-7aca-4ceb-ae1a-9f1d73573e01'

client.addPageVisit(options)

Sends an HTTP request to the server with all the required information to store the page visit. Meant to point to an endpoint utilizing server.addPageVisitFs(options) or server.addPageVisitMongo(options)

Parameters

ParameterDescriptionisRequiredDefault value
options: objecttrueundefined
options.baseUrl: stringBase of the server URL, e.g. 'http://myapi.com'trueundefined
options.currentPath: stringPathname of the current pagefalsewindow.location.pathname
options.localStorageKey: stringKey that was used to store used IDs in localStoragefalse'cr-id'
options.apiEndPoint: stringThe server endpoint that receives to HTTP requestfalse'/pageVisit'
options.userIdParameterName: stringKey to be used in query parameters for the user IDfalse'userId'
options.pathParameterName: stringKey to be used in query parameters for the current pathfalse'path'
options.fetchOptions: objectOptions that will be directly passed to fetch()*false{ method: 'GET' }

* = content-recommender internally uses the fetch API options.fetchOptions is passed directly to fetch function call: fetch(url, fetchOptions)

Return value

Response object directly from await fetch()

client.getRecommendation(options)

Gets a content recommendation for the current user from the server. Meant to point to an endpoint utilizing server.getRecommendationFs(options) or server.getRecommendationMongo(options)

Parameters

ParameterDescriptionisRequiredDefault value
options: objecttrueundefined
options.baseUrl: stringBase of the server URL, e.g. 'http://myapi.com'trueundefined
options.currentPath: stringPathname of the current page. Used to filter out current path from recommendationsfalsewindow.location.pathname
options.localStorageKey: stringKey was is used to store used IDs in localStoragefalse'cr-id'
options.apiEndPoint: stringThe server endpoint that receives to HTTP requestfalse'/getRecommendation'
options.userIdParameterName: stringKey to be used in query parameters for the user IDfalse'userId'
options.pathParameterName: stringKey to be used in query parameters for the current pathfalse'currentPath'
options.fetchOptions: objectOptions that will be directly passed to fetch()*false{ method: 'GET' }

* = content-recommender internally uses the fetch API options.fetchOptions is passed directly to fetch function call: fetch(url, fetchOptions)

Return value

An object containing pathnames and scores for each path:

{
  "/blog/somePost": 0.92212312,
  "/blog/otherPost": 0.12227173,
  "/blog/yetAnotherPost": -0.28388191
}

The score is a value between -1 and 1. The larger the score is, the more recommendation system thinks that path is a good recommendation for the user.

If the database is empty, has less than 3 users, or none of the users have visited paths that are not the user's current path, an empty object is returned

server.addPageVisitFs(options)

Reads the database, adds an entry for a page visit and writes it back to the database.

Parameters

ParameterDescriptionisRequiredDefault value
options: objecttrueundefined
options.userId: stringThe user ID of the current usertrueundefined
options.path: stringThe current path of the user to be stored to databasetrueundefined
options.readFromDatabase: functionA function that returns the entire database containing page visits for all userstrueundefined
options.writeToDatabase: functionA function that writes the entire to the databasetrueundefined

Return value

An object containing the updated database:

{
  "user1": {
    "/blog/somePost": 2,
    "/blog/otherPost": 21,
    "/blog/yetAnotherPost": 2
  },
  "user2": {
    "/blog/somePost": 12,
    "/blog/otherPost": 1,
    "/blog/yetAnotherPost": 212
  }
}

server.getRecommendationFs(options)

Calculates the confidence scores based on how much the system thinks a specific path should be recommended to the current user

Parameters

ParameterDescriptionisRequiredDefault value
options: objecttrueundefined
options.userId: stringThe user ID of the current usertrueundefined
options.readFromDatabase: functionA function that returns the entire database containing page visits for all userstrueundefined
options.currentPath: stringThe current path of the user. Is provided, gets filtered out from the recommendationfalseundefined
options.amount: numberAmount of paths the recommendation should containfalse10

Return value

An object containing pathnames and scores for each path:

{
  "/blog/somePost": 0.92212312,
  "/blog/otherPost": 0.12227173,
  "/blog/yetAnotherPost": -0.28388191
}

The score is a value between -1 and 1. The larger the score is, the more recommendation system thinks that path is a good recommendation for the user.

server.addPageVisitMongo(options)

Writes an entry for a page visit for a user to the database.

Parameters

ParameterDescriptionisRequiredDefault value
options: objecttrueundefined
options.userId: stringThe user ID of the current usertrueundefined
options.path: stringThe current path of the user to be stored to databasetrueundefined
options.UserModel: functionThe mongoose model for a User in databasetrueundefined

Return value

Newly updated user as an object

server.getRecommendationMongo(options)

Calculates the confidence scores based on how much the system thinks a specific path should be recommended to the current user

Parameters

ParameterDescriptionisRequiredDefault value
options: objecttrueundefined
options.userId: stringThe user ID of the current usertrueundefined
options.UserModel: functionThe mongoose model for a User in databasetrueundefined
options.currentPath: stringThe current path of the user. Is provided, gets filtered out from the recommendationfalseundefined
options.amount: numberAmount of paths the recommendation should containfalse10

Return value

An object containing pathnames and scores for each path:

{
  "/blog/somePost": 0.92212312,
  "/blog/otherPost": 0.12227173,
  "/blog/yetAnotherPost": -0.28388191
}

server.calculateRecommendationsMongo(options)

Calculates the recommendations for each user in the database at once and stores them in the database

Parameters

ParameterDescriptionisRequiredDefault value
options: objecttrueundefined
options.UserModel: functionThe mongoose model for a User in databasetrueundefined
options.amount: numberAmount of paths the recommendations should containfalse10

Return value

The entire database of users, including the newly calculated recommendations for each

0.2.8

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago