{"id":18750650,"url":"https://github.com/webiny/websiteanalytics","last_synced_at":"2025-10-08T00:08:13.497Z","repository":{"id":41218511,"uuid":"48890525","full_name":"webiny/WebsiteAnalytics","owner":"webiny","description":"Library that gathers common analytics data about your website visitors","archived":false,"fork":false,"pushed_at":"2016-01-31T15:57:35.000Z","size":20,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-20T11:08:54.552Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"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":"2016-01-01T21:54:06.000Z","updated_at":"2024-06-26T09:41:55.000Z","dependencies_parsed_at":"2022-09-12T23:40:21.367Z","dependency_job_id":null,"html_url":"https://github.com/webiny/WebsiteAnalytics","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/webiny/WebsiteAnalytics","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FWebsiteAnalytics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FWebsiteAnalytics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FWebsiteAnalytics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FWebsiteAnalytics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webiny","download_url":"https://codeload.github.com/webiny/WebsiteAnalytics/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FWebsiteAnalytics/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278866931,"owners_count":26059671,"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","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-07T17:12:41.976Z","updated_at":"2025-10-08T00:08:13.482Z","avatar_url":"https://github.com/webiny.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Website Analytics\n=================\n\nThis component helps you track your website visitors and some common attributes around them.\n\nThe component tracks 3 different metrics:\n- `visitors` =\u003e unique website visitors\n- `page views` =\u003e non-unique website visitors\n- `url views` =\u003e page view metrics for a particular url (not unique)\n\nFor `visitors` and `url views` additional attributes are tracked:\n- `browser` =\u003e which browsers did the visitors use, eg, `chrome`, `firefox` .. etc\n- `country` =\u003e from which country did the visitors come\n- `device` =\u003e which device did they use, `mobile`, `tablet` or `desktop`\n- `os` =\u003e which operating system they have\n- `referrer domain` =\u003e from which domain did they come to your website\n- `referrer type` =\u003e did they come from a search engine, social network, some other referral or is it a direct visit\n\n## Setup\n\n### Dependencies\n\nThe component requires an instance of [`Webiny/AnalyticsDb`](https://github.com/Webiny/AnalyticsDb). Optionally, if you wish to track the `country` data, an instance\nof [`Webiny/GeoIp`](https://github.com/Webiny/GeoIp) is required.\n\n### Tracking\nTo track the visitor data, you have to create the instance of `WebsiteAnalytics` and call the `saveStats` method, like so:\n\n```php\n// get mongo instance for AnalyticsDb\n$mongo = new \\Webiny\\Component\\Mongo\\Mongo('127.0.0.1:27017', 'MyDatabase');\n\n// create AnalyticsDb instance\n$a = new \\Webiny\\AnalyticsDb\\AnalyticsDb($mongo);\n\n// create WebsiteAnalytics instance\n$wa = new \\Webiny\\WebsiteAnalytics\\WebsiteAnalytics($a);\n\n// save the stats\n$wa-\u003esaveStats();\n```\n\nIn order to track the `country` data, you need to also have the `GeoIp` instance:\n\n``` php\n// GeoIp provider\n$geo = new \\Webiny\\GeoIp\\GeoIp(new \\Webiny\\GeoIp\\Provider\\FreeGeoIp\\FreeGeoIp());\n\n$mongo = new \\Webiny\\Component\\Mongo\\Mongo('127.0.0.1:27017', 'MyDatabase');\n$a = new \\Webiny\\AnalyticsDb\\AnalyticsDb($mongo);\n\n\n$wa = new \\Webiny\\WebsiteAnalytics\\WebsiteAnalytics($a, $geo);\n$wa-\u003esaveStats();\n```\n\n### Query data\n\nIn order to query your data, you need to call the `query` method, which will return an instance of `Query` class.\nThe `query` method requires 1 parameter and that is the time range within which the data should be queried.\nThe date range is an array with 2 unix timestamps. Optionally you can use `Webiny\\AnalyticsDb\\DateHelper` class to get some commonly used date ranges, like `lastMonth`, `thisWeek` ... etc. \n\n```php\n$wa = new \\Webiny\\WebsiteAnalytics\\WebsiteAnalytics($a, $geoIp);\n// query between two dates\n$stats = $wa-\u003equery([1446336000, 1451606400]);\n\n// optionally: use the DateHelper class\n$stats = $wa-\u003equery(\\Webiny\\AnalyticsDb\\DateHelper::rangeLast90Days());\n```\n\nOnce you have the `Query` instance, which is returned by the `query` method, you can access the following methods to query your data:\n\n#### `visitorSum`\n\nReturn the total number of unique visitors for the given period.\n\n```php\n$stats-\u003evisitorsSum();\n```\n\nReturns a number with a total number of visitors.\n\n```text\n10342\n```\n\n#### `visitorsByDay`\n\nReturns the total number of visitors, per day for the given period in form of an array.\n\n```php\n$stats-\u003evisitorsByDay();\n```\n\nThe `_id` field represents the day timestamp, and the `totalCount` represents the number of visitors for that day.\n\n```text\nArray\n(\n    [0] =\u003e Array\n        (\n            [_id] =\u003e 1443916800\n            [totalCount] =\u003e 20\n        )\n\n    [1] =\u003e Array\n        (\n            [_id] =\u003e 1444003200\n            [totalCount] =\u003e 23\n        )\n\n    [2] =\u003e Array\n        (\n            [_id] =\u003e 1444089600\n            [totalCount] =\u003e 22\n        )\n    ...\n)\n```\n\n#### `visitorsByMonth`\n\nReturns the total number of visitors, per month for the given period in form of an array.\n\n\n```php\n$stats-\u003evisitorsByMonth();\n```\n\nThe `_id` field represents the day timestamp, and the `totalCount` represents the number of visitors for that day.\n\n```text\nArray\n(\n    [0] =\u003e Array\n        (\n            [_id] =\u003e 1446336000\n            [totalCount] =\u003e 797\n        )\n\n    [1] =\u003e Array\n        (\n            [_id] =\u003e 1448928000\n            [totalCount] =\u003e 839\n        )\n\n    [2] =\u003e Array\n        (\n            [_id] =\u003e 1451606400\n            [totalCount] =\u003e 42\n        )\n\n)\n```\n\n#### `visitorsDimensionSum`\n\nDimensions are attributes like `browser`, `country`, `device`, and other prior mentioned attributes on the top of the page.\n\nThe `visitorsDimensionSum` method returns a sum of a defined dimension value, of your visitors, for the given time period.\n\nThe method takes a dimension name, and optionally a dimension value. If you only provide a dimension name, the results will be grouped by different dimension values, like so:\n\n```php\n$stats-\u003evisitorsDimensionSum(Webiny\\WebsiteAnalytics\\StatHandlers\\Browser::NAME);\n```\n\nResult: \n\n```text\n(\n    [0] =\u003e Array\n        (\n            [_id] =\u003e Array\n                (\n                    [name] =\u003e browser\n                    [value] =\u003e safari\n                )\n\n            [totalCount] =\u003e 1284\n        )\n\n    [1] =\u003e Array\n        (\n            [_id] =\u003e Array\n                (\n                    [name] =\u003e browser\n                    [value] =\u003e chrome\n                )\n\n            [totalCount] =\u003e 468\n        )\n    ...\n)\n```\n\nAdditionally if you set a dimension value, the method will return a sum only for that given value:\n\n```php\n$stats-\u003evisitorsDimensionSum(Webiny\\WebsiteAnalytics\\StatHandlers\\Browser::NAME, 'safari');\n```\n\nResult:\n\n```text\n1284\n```\n\n#### `visitorsDimensionByDay`\n\nReturns a the number of visitors for the given dimension, grouped by day.\nNote that this method requires that you provide both dimension name and dimension value.\n\n```php\n$stats-\u003evisitorsDimensionByDay(Webiny\\WebsiteAnalytics\\StatHandlers\\Country::NAME, 'GB');\n```\n\nResult:\n\n```text\nArray\n(\n    [0] =\u003e Array\n        (\n            [_id] =\u003e 1443916800\n            [totalCount] =\u003e 3\n        )\n\n    [1] =\u003e Array\n        (\n            [_id] =\u003e 1444003200\n            [totalCount] =\u003e 1\n        )\n    ...\n)\n```\n\n#### `pageViewsSum`\n\nSame as `visitorSum` method, this one just returns the sum of page views which are not unique visitors.\n \n \n```php\n$stats-\u003epageViewsSum();\n```\n\n#### `pageViewsByDay`\n\nSame as `visitorsByDay` method, this one just returns the page views which are not unique visitors.\n\n```php\n$stats-\u003epageViewsByDay();\n```\n\n#### `pageViewsByMonth`\n\nSame as `visitorsByMonth` method, this one just returns the page views which are not unique visitors.\n\n```php\n$stats-\u003epageViewsByMonth();\n```\n\n#### `urlSum`\n\nReturns a sum of page views for a particular url. If you don't specify the url, you can get a list of top visited pages, for example:\n\n```php\n// get top 10 pages\n$stats-\u003eurlSum(null, 10);\n```\n\nThe `_id` field represents the page url.\n**NOTE:** Pages are tracked without the query parameters.\n\n```text\nArray\n(\n\n    [0] =\u003e Array\n        (\n            [_id] =\u003e /page-208/\n            [totalCount] =\u003e 46\n        )\n\n    [1] =\u003e Array\n        (\n            [_id] =\u003e /page-396/\n            [totalCount] =\u003e 45\n        )\n    ...\n)\n```\n\nIf you specify the page url, then the sum for that particular page is returned:\n\n```php\n$stats-\u003eurlSum('/page-208/');\n```\n\n```text\n28\n```\n\n#### `urlByDay`\n\nSame as `visitorsByDay` method, this one just returns the page views for a particular page, grouped by days.\n\n```php\n$stats-\u003eurlByDay('/page-101/');\n```\n\n#### `urlByMonth`\n\nSame as `visitorsByMonth` method, this one just returns the page views for a particular page, grouped by months.\n\n```php\n$stats-\u003eurlByMonth('/page-101/');\n```\n\n#### `urlDimensionSum`\n\nSame as `visitorsDimensionSum` method.\n\n```php\n$result = $stats-\u003eurlDimensionSum('/page-110/', Webiny\\WebsiteAnalytics\\StatHandlers\\Browser::NAME);\n\n// or\n$result = $stats-\u003eurlDimensionSum('/page-110/', Webiny\\WebsiteAnalytics\\StatHandlers\\Browser::NAME, 'safari');\n```\n\n\n#### `urlDimensionByDay`\n\nSame as `visitorsDimensionByDay` method, with the exception that you don't need to provide a dimension value. \n\n```php\n$stats-\u003eurlDimensionByDay('/page-110/', Webiny\\WebsiteAnalytics\\StatHandlers\\Browser::NAME);\n\n// or\n\n$stats-\u003eurlDimensionByDay('/page-110/', Webiny\\WebsiteAnalytics\\StatHandlers\\Browser::NAME, 'safari');\n```\n\n### Adding custom dimensions (attribute)\n\nTo add a custom dimension, create a class and implement `StatHandlerInterface` and then register your stat handler with the `WebsiteAnalytics` instance, like so:\n\n```php\n$waInstance = new \\Webiny\\WebsiteAnalytics\\WebsiteAnalytics($analyticDb, $geo);\n$myHandlerInstance = $waInstance-\u003eaddStatHandler('My\\Custom\\Handler\\Name');\n```\n\nNow your handler is registered and will track the registered attribute on each url visit and on each unique visitor. \nYou can also query your attribute analytics by using any of the provided `*Dimension*` methods, like `urlDimensionByDay`:\n\n```php\n$stats-\u003evisitorsDimensionSum('my-attribute-name');\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/WebsiteAnalytics/\n$ composer install\n$ phpunit\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fwebsiteanalytics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebiny%2Fwebsiteanalytics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fwebsiteanalytics/lists"}