{"id":18750714,"url":"https://github.com/webiny/analyticsdb","last_synced_at":"2025-04-12T23:32:24.120Z","repository":{"id":62547870,"uuid":"48246176","full_name":"webiny/AnalyticsDb","owner":"webiny","description":"[NOT MAINTAINED] A MongoDb powered time-series analytics library for PHP.","archived":false,"fork":false,"pushed_at":"2017-09-29T07:31:00.000Z","size":42,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-26T18:06:30.475Z","etag":null,"topics":["analytics","dimensions","metrics","mongodb","time-series","webiny"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webiny.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-18T16:52:33.000Z","updated_at":"2023-05-21T17:35:27.000Z","dependencies_parsed_at":"2022-11-02T22:16:18.055Z","dependency_job_id":null,"html_url":"https://github.com/webiny/AnalyticsDb","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FAnalyticsDb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FAnalyticsDb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FAnalyticsDb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FAnalyticsDb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webiny","download_url":"https://codeload.github.com/webiny/AnalyticsDb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647257,"owners_count":21139081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","dimensions","metrics","mongodb","time-series","webiny"],"created_at":"2024-11-07T17:12:54.166Z","updated_at":"2025-04-12T23:32:22.513Z","avatar_url":"https://github.com/webiny.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"AnalyticsDb\n=================\n\nAnalyticsDb is a component that enables you to store and query different time-series (numerical) data.\nSimple use-case would be tracking the number of visitors for your website inside the given date/time range, or tracking\necommerce revenue for a given quarter.\n\n```php\n// get analytics instance\n$mongo = new \\Webiny\\Component\\Mongo\\Mongo('127.0.0.1:27017', 'webiny');\n$analytics = new \\Webiny\\AnalyticsDb\\AnalyticsDb($mongo);\n\n// store some visitor data\n$analytics-\u003elog('visitor')-\u003eaddDimension('browser', 'chrome')-\u003eaddDimension('country', 'UK');\n\n// store some revenue data\n$analytics-\u003elog('revenue', 0, 120.00)\n    -\u003eaddAttribute('brand', 'Foo')\n    -\u003eaddDimension('product', 'hdd', 79.50)\n    -\u003eaddDimension('product', 'mouse', 20.50)\n    -\u003eaddDimension('tax', 'in-country', 20);\n\n// save the data\n$analytics-\u003esave();\n\n// query data\n$query = $a-\u003equery('revenue', 0, DateHelper::rangeLast30Days());\n\n// get total number of visitors for the last 30 days, and group them by day\n$result = $query-\u003estats()-\u003egetResult();\n\n// get total number of visitors for the last year, and group them by month\n$query = $a-\u003equery('revenue', 0, DateHelper::rangeYear());\n$result = $query-\u003estats()-\u003emonthly()-\u003egroupByTimestamp()-\u003esortByTimestamp(1)-\u003egetResult();\n\n// get revenue for last quarter and group it by revenue type\n$query = $a-\u003equery('revenue', 0, DateHelper::rangeQ1());\n$result = $query-\u003egroupByDimensionName()-\u003esortByCount(-1)-\u003egetResult();\n```\n\n## Dependencies\n\nThe component requires an instance of `\\Webiny\\Component\\Mongo\\Mongo` to access your Mongo database where it will create\nseveral collections to store the data.\n\n##Dimensions\n\nDimensions track different data which is still tied to your entity.\nFor 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.\n\n```php\n// track a view on the red product\n$analytics-\u003elog('product', 'A')\n  -\u003eaddDimension('color', 'red');\n\n// track a view on the blue product\n$analytics-\u003elog('product', 'A')\n  -\u003eaddDimension('color', 'blue');\n```\n\nWhen tracking the dimensions, you can then get stats like \"show me the views on all `red` version of my product\".\n\n```php\n$query = $a-\u003equery('product', 'A', DateHelper::rangeLast30Days());\n$result = $query-\u003edimension('color', 'red')-\u003egroupByDimensionValue()-\u003esortByCount(-1)-\u003egetResult();\n```\n\n\n## Attributes\n\nAttributes 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.\nA 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.\n\n```php\n$analytics-\u003elog('product', 'A')-\u003eaddAttribute('brand', '10');\n```\nYou can add multiple attributes to a product.\n\nNow you can do something like this:\n\n```php\n// show me top 10 products for brand \"10\" for last 30 days\n$query = $a-\u003equery('product', null, DateHelper::rangeLast30Days());\n$result = $query-\u003estats()-\u003eaddAttributeFilter('brand', 10)-\u003esortByCount('-1')-\u003elimit(10);\n```\n\n## Storing data\n\nThe data is stored using the `log` method. Note that data is not actually saved until you call the `save` method.\n\nTo assign attributes to your data, for example you wish to increment the number of visitors on your site, but you also want  \nto store some attributes, like what browser the user used, and from which country he came from; for that you can use `dimensions`.\nDimensions are also counters which can be queried. \n\nFor example, for this use case:\n```php\n$analytics-\u003elog('visitor')-\u003eaddDimension('browser', 'chrome')-\u003eaddDimension('country', 'UK');\n```\nYou can know how many visitors you had for a given date range, and you can group that result either by day, or by month.\nSince you stored some data in dimensions, you can also know, how many users used `chrome` vs, for example `firefox` or `ie`, \nand then you can cross reference that to the total number of your visitors.\n \nYou can also assign a referral value to the log, for example, you can track per-page analytics like so:\n\n```php\n$analytics-\u003elog('page', 123)-\u003eaddDimension('browser', 'chrome')-\u003eaddDimension('country', 'UK');\n```\nThis will track a visitor for page with the id of 123. And then later you can query the analytics data for that page.\n\nSome 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\nyou 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\npages, and one for visitors in general. \n\nBy default the `log` method will increment the value by 1, but in some cases, for example when you wish to track revenue, \n you want to specify the increment value, and this is done by using the 3rd parameter, like so:\n \n```php\n$analytics-\u003elog('revenue', 0, 120.00);\n```\nThis will increase the `revenue` counter by `120.00` (float value is supported). \n\n\n## Querying data\n\nTo query the data, you need to get an instance of the `query`, like so:\n```php\n$query = $a-\u003equery('revenue', 0, DateHelper::rangeLast30Days());\n```\nFor the `query` you have to specify the entity name, referral, and the date range.\nThere is a `DateHelper` class to help you in regards to some commonly used date ranges, but you can also specify your own custom range, \nit is just an array with two unix timestamps `[dateFromTimestamp, dateToTimestamp]`.\n\nOnce you have the `query` instance, you can get the results for the given range. By default the data is grouped by day, but you \ncan also get it in a per-month format.\n\n```php\n$query = $a-\u003equery('revenue', 0, DateHelper::rangeLast30Days());\n\n// get data by day\n$result = $query-\u003estats()-\u003egetResult();\n\n// get data by month\n$result = $query-\u003estats()-\u003emonthly()-\u003egetResult();\n```\n\nTo query dimensions, use the `dimension` method, like so:\n\n```php\n$query = $a-\u003equery('revenue', 0, DateHelper::rangeYear());\n\n// show me the revenue breakdown by item type (eg, product, tax)\n$result = $query-\u003edimension()-\u003egroupByDimensionName()-\u003esortByCount(-1)-\u003egetResult();\n\n// show me total revenue just from products\n$result = $query-\u003edimension('product')-\u003egetResult();\n\n// show me total revenue breakdown by product type\n$result = $query-\u003edimension('product')-\u003egroupByDimensionValue()-\u003esortByCount(-1)-\u003egetResult();\n\n// show me revenue for `HDD` product by days\n$result = $query-\u003edimension('package', 'PAYG')-\u003egetResult();\n\n// show me total revenue for `HDD` product\n$result = $query-\u003edimension('package', 'PAYG')-\u003egroupByDimensionName()-\u003egetResult();\n```\n\n## License and Contributions\n\nContributing \u003e Feel free to send PRs.\n\nLicense \u003e [MIT](LICENSE)\n\n## Resources\n\nTo run unit tests, you need to use the following command:\n```\n$ cd path/to/AnalyticsDb/\n$ composer install\n$ phpunit\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fanalyticsdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebiny%2Fanalyticsdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fanalyticsdb/lists"}