All Projects → mlomb → countapi-js

mlomb / countapi-js

Licence: MIT license
Wrapper for CountAPI using promises.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to countapi-js

HurdleDMR.jl
Hurdle Distributed Multinomial Regression (HDMR) implemented in Julia
Stars: ✭ 19 (-48.65%)
Mutual labels:  count
subtlex-word-frequencies
A list of words from the SUBTLEX movie subtitles corpus, sorted by frequency.
Stars: ✭ 25 (-32.43%)
Mutual labels:  count
cloudpods
A cloud-native open-source unified multi-cloud and hybrid-cloud platform. 开源、云原生的多云管理及混合云融合平台
Stars: ✭ 1,469 (+3870.27%)
Mutual labels:  iaas
CircularCountdown
Android library to create a circular countdown, fully written in Kotlin
Stars: ✭ 24 (-35.14%)
Mutual labels:  count
count.macro
Babel macro for counting number of lines or words in files at compile time
Stars: ✭ 20 (-45.95%)
Mutual labels:  count
linec
🍬一个高颜值命令行统计代码行数的计数器。(counts lines of code)
Stars: ✭ 121 (+227.03%)
Mutual labels:  count
Vagrant Openstack Provider
Use Vagrant to manage OpenStack Cloud instances.
Stars: ✭ 229 (+518.92%)
Mutual labels:  iaas
MD REncoder
Rotary Encoder Library
Stars: ✭ 25 (-32.43%)
Mutual labels:  count
recount
R package for the recount2 project. Documentation website: http://leekgroup.github.io/recount/
Stars: ✭ 40 (+8.11%)
Mutual labels:  count
LinqBenchmarks
Benchmarking LINQ and alternative implementations
Stars: ✭ 138 (+272.97%)
Mutual labels:  count
Data-Structure-Algorithm-Programs
This Repo consists of Data structures and Algorithms
Stars: ✭ 464 (+1154.05%)
Mutual labels:  count
xxcloud
xxcloud,旨在整合数据中心异构虚拟化资源为统一的资源池,并在资源池上为用户提供各类IAAS、PAAS服务。
Stars: ✭ 64 (+72.97%)
Mutual labels:  iaas
vue-countable
✍️ Vue binding for countable.js. Provides real-time paragraph, sentence, word, and character counting.
Stars: ✭ 25 (-32.43%)
Mutual labels:  count
vue-countup
A plugin to count up to a figure using Vue.js
Stars: ✭ 42 (+13.51%)
Mutual labels:  count
CorBinian
CorBinian: A toolbox for modelling and simulating high-dimensional binary and count-data with correlations
Stars: ✭ 15 (-59.46%)
Mutual labels:  count
CounterView
一个数字变化效果的计数器视图控件
Stars: ✭ 38 (+2.7%)
Mutual labels:  count
sp who3
The sp_who3 stored procedure is a custom and open source alternative to the sp_who system stored procedures available in SQL Server.
Stars: ✭ 49 (+32.43%)
Mutual labels:  count
CADLab Loyalty
This is a end to end Loyalty business scenario
Stars: ✭ 21 (-43.24%)
Mutual labels:  iaas
verification-tests
Blackbox test suite for OpenShift.
Stars: ✭ 41 (+10.81%)
Mutual labels:  iaas
awesome-hosting
List of awesome hosting
Stars: ✭ 134 (+262.16%)
Mutual labels:  iaas

countapi-js

⚠️ This package is no longer mantained by me

npm Travis (.org)

Count API logo

This is the official promise-based wrapper for CountAPI. CountAPI is a free counting service, you can use it to track page hits, and custom events.

Installation

npm install countapi-js --save

Example

First, include the package

const countapi = require('countapi-js');

// or with ES6
import countapi from 'countapi-js';

Counting the number of visits per page

countapi.visits().then((result) => {
  console.log(result.value);
});

Refresh the page multiple times and check the console!

If you want to track the number of visits through the whole site instead

countapi.visits('global').then((result) => {
  console.log(result.value);
});

Or custom events

document.getElementById("magic-button").addEventListener("click", () => {
  countapi.event('magic-button').then((result) => {
    alert(`The magic button has been pressed ${result.value} times.`);
  });
})

Keys and namespaces

Namespaces are meant to avoid name collisions. Its recommend use the domain of the application as namespace to avoid collision with other websites. By default, the visits and event functions will use the current domain as namespace and the provided arguments as key. If the namespace is not specified the key is assigned to the default namespace.

Keys and namespaces must match ^[A-Za-z0-9_-.]{3,64}$.

Available methods

ℹ️ Note: When requesting existing keys, if the key doesn't exists the status returned will be 404 (the promise will not fail).

get

countapi.get(namespace, key)
countapi.get(key)

Get the value of a key.

countapi.get('mysite.com', 'test').then((result) => { ... });

result may look like

{
    status: 200,
    path: "mysite.com/test",
    value: 136
}

set

countapi.set(namespace, key, value)
countapi.set(key, value)

Set the value of a key.

ℹ️ Note: To set a key, it must be created with enable_reset set to true.

countapi.set('mysite.com', 'test', 42).then((result) => { ... });

result may look like

{
    status: 200,
    path: "mysite.com/test",
    old_value: 136,
    value: 42
}

ℹ️ Note: If the key has enable_reset set to false, status will be 403 and the old_value will match value (the key stays the same), also the promise will NOT fail.

update

countapi.update(namespace, key, amount)
countapi.update(key, amount)

Updates a key with +/- amount.

ℹ️ Note: amount must be within update_lowerbound and update_upperbound specified during the creation of the key.

// subtract 3
countapi.update('mysite.com', 'test', -3).then((result) => { ... });

result may look like

{
    status: 200,
    path: "mysite.com/test",
    value: 42
}

ℹ️ Note: If the amount provided is invalid you will get status 403.

hit

countapi.hit(namespace, key)
countapi.hit(key)

A shorthand for update with amount=1. And useful if you don't want to create a key manually, since if you request a key that doesn't exists, a key with enable_reset=false, update_lowerbound=0 and update_upperbound=1 will be created automatically.

countapi.hit('mysite.com', 'visits').then((result) => { ... });

result may look like

{
    status: 200,
    path: "mysite.com/visits",
    value: 27
}

visits / event

countapi.visits()
countapi.visits(page) countapi.event(name)

Useful shorthands for hit using as namespace the current hostname. For .visits you may pass page to provide your own page identifier. If page is not provided it will be extracted from the current URL. For .event you have to pass a event name.

Examples for those are shown in the example section.

create

countapi.create()
countapi.create(options)

Create a key. All parameters are optional.

name default description
key New UUID Name of the key
namespace default Namespace to store the key
value 0 The initial value
enable_reset 0 Allows the key to be resetted with set
update_lowerbound -1 Restrict update to not subtract more than this number. This number must be negative or zero.
update_upperbound 1 Restrict update to not add more than this number. This number must be positive or zero.
countapi.create(options).then((result) => { ... });

result may look like

{
    status: 200,
    path: "default/6d5891ff-ebda-48fb-a760-8549d6a3bf3a",
    namespace: "default",
    key: "6d5891ff-ebda-48fb-a760-8549d6a3bf3a",
    value: 0
}

If there is a problem creating the key, the promise will be rejected with meaningful information.

info

countapi.info(namespace, key)
countapi.info(key)

Retrive information about a key.

countapi.info('test').then((result) => { ... });

result may look like

{
    status: 200,
    path: "default/test",
    namespace: "default",
    key: "test",
    ttl: 15769998014,
    created: 1553794487479,
    update_lowerbound: 0,
    update_upperbound: 1,
    enable_reset: false,
    value: 69
}

stats

countapi.stats()

Get some CountAPI stats.

countapi.stats().then((result) => { ... });

result may look like

{
    keys_created: 111111,
    keys_updated: 111111,
    requests: 111111,
    version: "xxxxxx"
}

Further documentation

Visit https://countapi.xyz for the full API documentation.

Testing

npm test

Dependencies

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].