PHPackages                             webiny/analytics-db - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. webiny/analytics-db

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

webiny/analytics-db
===================

Library to help you with storing different time series analytics data.

v1.0.0(8y ago)577312MITPHPPHP &gt;=7.0

Since Jan 2Pushed 8y ago9 watchersCompare

[ Source](https://github.com/Webiny/AnalyticsDb)[ Packagist](https://packagist.org/packages/webiny/analytics-db)[ Docs](http://www.webiny.com/)[ RSS](/packages/webiny-analytics-db/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (5)Dependencies (3)Versions (9)Used By (2)

AnalyticsDb
===========

[](#analyticsdb)

AnalyticsDb is a component that enables you to store and query different time-series (numerical) data. Simple use-case would be tracking the number of visitors for your website inside the given date/time range, or tracking ecommerce revenue for a given quarter.

```
// get analytics instance
$mongo = new \Webiny\Component\Mongo\Mongo('127.0.0.1:27017', 'webiny');
$analytics = new \Webiny\AnalyticsDb\AnalyticsDb($mongo);

// store some visitor data
$analytics->log('visitor')->addDimension('browser', 'chrome')->addDimension('country', 'UK');

// store some revenue data
$analytics->log('revenue', 0, 120.00)
    ->addAttribute('brand', 'Foo')
    ->addDimension('product', 'hdd', 79.50)
    ->addDimension('product', 'mouse', 20.50)
    ->addDimension('tax', 'in-country', 20);

// save the data
$analytics->save();

// query data
$query = $a->query('revenue', 0, DateHelper::rangeLast30Days());

// get total number of visitors for the last 30 days, and group them by day
$result = $query->stats()->getResult();

// get total number of visitors for the last year, and group them by month
$query = $a->query('revenue', 0, DateHelper::rangeYear());
$result = $query->stats()->monthly()->groupByTimestamp()->sortByTimestamp(1)->getResult();

// get revenue for last quarter and group it by revenue type
$query = $a->query('revenue', 0, DateHelper::rangeQ1());
$result = $query->groupByDimensionName()->sortByCount(-1)->getResult();
```

Dependencies
------------

[](#dependencies)

The component requires an instance of `\Webiny\Component\Mongo\Mongo` to access your Mongo database where it will create several collections to store the data.

\##Dimensions

Dimensions track different data which is still tied to your entity. For example say you have a product A, in 2 colors, red and blue. The product would be your entity, and colors would be your dimensions.

```
// track a view on the red product
$analytics->log('product', 'A')
  ->addDimension('color', 'red');

// track a view on the blue product
$analytics->log('product', 'A')
  ->addDimension('color', 'blue');
```

When tracking the dimensions, you can then get stats like "show me the views on all `red` version of my product".

```
$query = $a->query('product', 'A', DateHelper::rangeLast30Days());
$result = $query->dimension('color', 'red')->groupByDimensionValue()->sortByCount(-1)->getResult();
```

Attributes
----------

[](#attributes)

Attributes are much simpler than dimensions. Attributes are just additional tags you can attach to an entity so you can group, sort and filter by them. A typical use-case for attributes is say you have a product, which has a certain brand and you want to be able to get a list of top 10 products for a certain brand.

```
$analytics->log('product', 'A')->addAttribute('brand', '10');
```

You can add multiple attributes to a product.

Now you can do something like this:

```
// show me top 10 products for brand "10" for last 30 days
$query = $a->query('product', null, DateHelper::rangeLast30Days());
$result = $query->stats()->addAttributeFilter('brand', 10)->sortByCount('-1')->limit(10);
```

Storing data
------------

[](#storing-data)

The data is stored using the `log` method. Note that data is not actually saved until you call the `save` method.

To assign attributes to your data, for example you wish to increment the number of visitors on your site, but you also want
to store some attributes, like what browser the user used, and from which country he came from; for that you can use `dimensions`. Dimensions are also counters which can be queried.

For example, for this use case:

```
$analytics->log('visitor')->addDimension('browser', 'chrome')->addDimension('country', 'UK');
```

You can know how many visitors you had for a given date range, and you can group that result either by day, or by month. Since you stored some data in dimensions, you can also know, how many users used `chrome` vs, for example `firefox` or `ie`, and then you can cross reference that to the total number of your visitors.

You can also assign a referral value to the log, for example, you can track per-page analytics like so:

```
$analytics->log('page', 123)->addDimension('browser', 'chrome')->addDimension('country', 'UK');
```

This will track a visitor for page with the id of 123. And then later you can query the analytics data for that page.

Some best practice is not to query data with a large set of different referrals. For example if you want to know how many visitors in total you had on your website, don't query and then sum the number of visitors of all your pages. Instead store 2 different analytics data, one for pages, and one for visitors in general.

By default the `log` method will increment the value by 1, but in some cases, for example when you wish to track revenue, you want to specify the increment value, and this is done by using the 3rd parameter, like so:

```
$analytics->log('revenue', 0, 120.00);
```

This will increase the `revenue` counter by `120.00` (float value is supported).

Querying data
-------------

[](#querying-data)

To query the data, you need to get an instance of the `query`, like so:

```
$query = $a->query('revenue', 0, DateHelper::rangeLast30Days());
```

For the `query` you have to specify the entity name, referral, and the date range. There is a `DateHelper` class to help you in regards to some commonly used date ranges, but you can also specify your own custom range, it is just an array with two unix timestamps `[dateFromTimestamp, dateToTimestamp]`.

Once you have the `query` instance, you can get the results for the given range. By default the data is grouped by day, but you can also get it in a per-month format.

```
$query = $a->query('revenue', 0, DateHelper::rangeLast30Days());

// get data by day
$result = $query->stats()->getResult();

// get data by month
$result = $query->stats()->monthly()->getResult();
```

To query dimensions, use the `dimension` method, like so:

```
$query = $a->query('revenue', 0, DateHelper::rangeYear());

// show me the revenue breakdown by item type (eg, product, tax)
$result = $query->dimension()->groupByDimensionName()->sortByCount(-1)->getResult();

// show me total revenue just from products
$result = $query->dimension('product')->getResult();

// show me total revenue breakdown by product type
$result = $query->dimension('product')->groupByDimensionValue()->sortByCount(-1)->getResult();

// show me revenue for `HDD` product by days
$result = $query->dimension('package', 'PAYG')->getResult();

// show me total revenue for `HDD` product
$result = $query->dimension('package', 'PAYG')->groupByDimensionName()->getResult();
```

License and Contributions
-------------------------

[](#license-and-contributions)

Contributing &gt; Feel free to send PRs.

License &gt; [MIT](LICENSE)

Resources
---------

[](#resources)

To run unit tests, you need to use the following command:

```
$ cd path/to/AnalyticsDb/
$ composer install
$ phpunit

```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 77.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~90 days

Recently: every ~142 days

Total

8

Last Release

3191d ago

Major Versions

0.2.x-dev → 1.0.x-dev2017-09-29

PHP version history (2 changes)v0.1.0PHP &gt;=5.5.9

1.0.x-devPHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/4440afa738ed146b05c06073a90345e0464c4f4d042b039532d881ca24859d77?d=identicon)[SvenAlHamad](/maintainers/SvenAlHamad)

---

Top Contributors

[![SvenAlHamad](https://avatars.githubusercontent.com/u/3808420?v=4)](https://github.com/SvenAlHamad "SvenAlHamad (24 commits)")[![Pavel910](https://avatars.githubusercontent.com/u/3920893?v=4)](https://github.com/Pavel910 "Pavel910 (4 commits)")[![adrians5j](https://avatars.githubusercontent.com/u/5121148?v=4)](https://github.com/adrians5j "adrians5j (3 commits)")

---

Tags

analyticsdimensionsmetricsmongodbtime-serieswebinyanalyticstime series

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/webiny-analytics-db/health.svg)

```
[![Health](https://phpackages.com/badges/webiny-analytics-db/health.svg)](https://phpackages.com/packages/webiny-analytics-db)
```

###  Alternatives

[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

3.2k6.0M66](/packages/spatie-laravel-analytics)[rubix/ml

A high-level machine learning and deep learning library for the PHP language.

2.2k1.5M28](/packages/rubix-ml)[segmentio/analytics-php

Segment Analytics PHP Library

25922.3M30](/packages/segmentio-analytics-php)[panphp/pan

A simple, lightweight, and privacy-focused product analytics php package.

1.3k106.0k5](/packages/panphp-pan)[zumba/amplitude-php

PHP SDK for Amplitude

4010.1M5](/packages/zumba-amplitude-php)[bezhansalleh/filament-google-analytics

Google Analytics integration for FilamentPHP

209175.5k8](/packages/bezhansalleh-filament-google-analytics)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
