tonypiper / Bitter

Bitter is a simple but powerful analytics library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bitter Documentation

https://secure.travis-ci.org/jeremyFreeAgent/Bitter.png?branch=master

Bitter is a simple but powerful analytics library

"Use Bitter and you have time to drink a bitter beer !"

-- Jérémy Romey

Bitter can answer following questions:

  • Has user X been online today? This week? This month?
  • Has user X performed action "Y"?
  • How many users have been active have this month? This hour?
  • How many unique users have performed action "Y" this week?
  • How many % of users that were active last week are still active?
  • How many % of users that were active last month are still active this month?

Bitter is very easy to use and enables you to create your own reports easily.

Note

Please look Bitter website for more info and documentation about this project.

Installation

Use Composer to install: free-agent/bitter.

In your composer.json you should have:

{
    "require": {
        "free-agent-bitter": "*"
    }
}

Bitter uses Redis (version >=2.6).

Note

Every keys created in Redis will be prefixed by bitter: ; temp keys by bitter_temp:.

Basic usage

Create a Bitter with a Redis client (Predis as example):

$redisClient = new \Predis\Client();
$bitter = new \Bitter($redisClient);

Mark user 404 as active and has been kicked by Chuck Norris:

$bitter->mark('active', 404);
$bitter->mark('kicked_by_chuck_norris', 404);

Note

Please don't use huge ids (e.g. 2^32 or bigger) cause this will require large amounts of memory.

Pass a DateTime as third argument:

$bitter->mark('damned_by_jack_bauer', 404, new \DateTime('yesterday'));

Test if user 404 as been kicked by Chuck Norris this week:

$currentWeek = new \Bitter\Event\Week('kicked_by_chuck_norris');

if ($bitter->in(404, $currentWeek) {
    echo 'User with id 404 has been kicked by Chuck Norris this week.';
} else {
    echo 'User with id 404 has not been kicked by Chuck Norris this week.';
}

How many users have been active yesterday:

$yesterday = new \Bitter\Event\Day('active', new \DateTime('yesterday'));

echo 'Yesterday: ' . $bitter->count($yesterday) . ' users has been active.';

Using BitOp

How many users that were active yesterday are active today:

$today     = new \Bitter\Event\Day('active', new \DateTime());
$yesterday = new \Bitter\Event\Day('active', new \DateTime('yesterday'));

$count = $bitter
    ->bitOpAnd('bit_op_example', $today, $yesterday)
    ->count('bit_op_example')
;
echo $count . ' were active yesterday are active today.';

Note

The bit_op_example key will expire after 60 seconds.

Test if user 13 was active yesterday and is active today:

$today     = new \Bitter\Event\Day('active', new \DateTime());
$yesterday = new \Bitter\Event\Day('active', new \DateTime('yesterday'));

$active = $bitter
    ->bitOpAnd('bit_op_example', $today, $yesterday)
    ->in(13, 'bit_op_example')
;
if ($active) {
    echo 'User 13 was active yesterday and today.';
} else {
    echo 'User 13 was not active yesterday and today.';
}

Note

Please look at Redis BITOP Command for performance considerations.

Unit Tests

You can run tests with:

bin/atoum -mcn 1 -d tests/units

Todo

Thanks

This library is a port of bitmapist (Python) by Amir Salihefendic.

About

Bitter is a simple but powerful analytics library

License:MIT License


Languages

Language:PHP 100.0%