Count events

This is for:

Developer

The Tally endpoints are wrapped by a simple API, which allows you to count any event and retrieve that count. You can also store metadata against the count.

Use

const getTally = require('@qubit/http-api-tally')

const tally = getTally('opType', 'trackingId', 'counter', period)

tally.increment('ABC1234').then(() => {
  console.log('Purchases of ABC1234 increased!')
})

Where:

  • opType is the opType to use (for example, ecount)

  • trackingId is the tracking Id for the property (for example, demo_property_uk)

  • counter is the type of interaction we want to count (for example, purchases)

  • period is the time window, in minutes, over which results will be aggregated (for example, 1440). Range is between one minute and 43200 minutes (30 days)

Warning

The parameters opType, trackingId, counter, period can’t contain forward or back slashes / \. Use underscores instead.

opType

ecount

  • increment(item) - increases the counter of item by 1

  • get(item) - retrieves the value of the counter for the specified item

topk

  • increment(item, data) - increases the counter of item by 1. The data is optional. When provided they will be stored alongside the counter:

tally.increment('item1')

OR

tally.increment('item3', {
  metadata: JSON.stringify({
    description: '...'
  })
})
Note

All metadata should be stored in the format above, with values stringified and stored against a metadata property in an object.

  • get(k) - retrieves the k top counters. When data was sent in the call of the increment function, they will be returned in the response set:

// Get the top 3 items of the counterName list
tally.get(3).then((data) => {
// data is a list of the top 3 items, for example, (item1, item2, item3)
console.log(data)
})
Warning

We limit the response to return only the top 1,000 items.

Sample request

// Get the tally package
const getTally = require('@qubit/http-api-tally')
// Define the variables of the tally endpoint
const strategy = 'opType'
const trackingId = options.meta.trackingId // your Qubit trackingId
const counterName = 't001-most-viewed'
const period = 48 // social proof period time in hours
const tally = getTally(strategy, trackingId, counterName, 60 * period)
// Increment the endpoint for different item Ids
tally.increment('item1')
tally.increment('item2')
tally.increment('item3')
// Get the top 3 items of the counterName list
tally.get(3).then((data) => {
// data is a list of the top 3 items, for example, (item1, item2, item3)
console.log(data)
})

Sample response

[
  { key: 'item2', count: 5, data: { description: '...' } }
  { key: 'item1', count: 4 }
  { key: 'item3', count: 1, data: { description: '...' } }
]