webiny/analytics-db
Composer 安装命令:
composer require webiny/analytics-db
包简介
Library to help you with storing different time series analytics data.
关键字:
README 文档
README
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
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 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
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
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
Contributing > Feel free to send PRs.
License > MIT
Resources
To run unit tests, you need to use the following command:
$ cd path/to/AnalyticsDb/
$ composer install
$ phpunit
webiny/analytics-db 适用场景与选型建议
webiny/analytics-db 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 773 次下载、GitHub Stars 达 5, 最近一次更新时间为 2015 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「analytics」 「time series」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webiny/analytics-db 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webiny/analytics-db 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webiny/analytics-db 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tools to convert between .NET and PHP date formats
Adds the EDTF data type to Wikibase
Date component is a set of methods to help with the manipulation of dates.
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Delayed Process For Laravel.
Analytics chooser extensions for site settings.
统计信息
- 总下载量: 773
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 10
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-28