{"id":26940261,"url":"https://github.com/panakour/google-analytics","last_synced_at":"2025-09-03T12:44:36.404Z","repository":{"id":57035323,"uuid":"77175016","full_name":"panakour/google-analytics","owner":"panakour","description":"Get data from google analytics (API v4) using PHP","archived":false,"fork":false,"pushed_at":"2023-02-03T07:24:23.000Z","size":20,"stargazers_count":26,"open_issues_count":1,"forks_count":10,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-21T23:47:24.235Z","etag":null,"topics":["analytics-data","google-analytics","laravel","php"],"latest_commit_sha":null,"homepage":"","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/panakour.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-12-22T20:50:43.000Z","updated_at":"2024-08-14T21:40:03.000Z","dependencies_parsed_at":"2023-02-18T04:31:09.739Z","dependency_job_id":null,"html_url":"https://github.com/panakour/google-analytics","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/panakour%2Fgoogle-analytics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/panakour%2Fgoogle-analytics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/panakour%2Fgoogle-analytics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/panakour%2Fgoogle-analytics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/panakour","download_url":"https://codeload.github.com/panakour/google-analytics/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246837664,"owners_count":20841905,"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-data","google-analytics","laravel","php"],"created_at":"2025-04-02T15:17:25.121Z","updated_at":"2025-04-02T15:17:25.780Z","avatar_url":"https://github.com/panakour.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![StyleCI](https://styleci.io/repos/77175016/shield?branch=master)](https://styleci.io/repos/77175016)\n[![Latest Stable Version](https://poser.pugx.org/panakour/analytics/v/stable)](https://packagist.org/packages/panakour/analytics)\n[![Total Downloads](https://poser.pugx.org/panakour/analytics/downloads)](https://packagist.org/packages/panakour/analytics)\n[![License](https://poser.pugx.org/panakour/analytics/license)](https://packagist.org/packages/panakour/analytics)\n\n# Get easily whatever data you want from google analytics API V4 using laravel php framework.\n\nThis package helps php developers to use Google Analytics API V4 with convenient way. \nThe code is clean and was written with good OOP practices.\nAny help to improve this package would be appreciated.\n\nThe package is compatible with laravel framework.\n\n\n## Installation\n\nInstall the package via composer:\n``` shell\ncomposer require panakour/analytics\n```\nTo use it with laravel add the GoogleAnalyticsServiceProvider to the `config/app.php`:\n```php\n'providers' =\u003e [\n...\nPanakour\\Analytics\\GoogleAnalyticsServiceProvider::class\n]\n```\n\nIf you want to use the facade of the package add it to the `config/app.php`:\n```php\n'aliases' =\u003e [\n...\n'Analytics' =\u003e Panakour\\Analytics\\Facades\\Analytics::class\n]\n```\nCopy the analytics config file with the command:\n``` shell\nphp artisan vendor:publish --provider=\"Panakour\\Analytics\\GoogleAnalyticsServiceProvider\"\n```\nFor those who have a credential with google analytics api continue here otherwise look at [how to create credential with google analytics api](#create-credential-with-google-analytics-api-v4).\n\nBe sure that you have `service-account-credentials.json` file within `storage\\app\\google-analytics\\`.\n\nAdd the view id to `.env` file: \n```\nGOOGLE_ANALYTICS_VIEW_ID=324235464\n```\n\n## Usage\n\n#### You can get analytics data simply using the facade \n`Panakour\\Analytics\\Facades\\Analytics`\n\n##### To get all sessions of the last week\n`Analytics::get();` \n\n##### To get all sessions depends on the specific date range\n```php\nAnalytics::setDateRange('2016-10-01', '2016-11-25');\nAnalytics::get();\n```\n\n##### To get whatever data you want\n\n```php\nAnalytics::setDateRange('2016-10-01', '2016-11-25');\nAnalytics::setMaxResults(20);\nAnalytics::setMetrics(['ga:entrances', 'ga:pageviews', 'ga:bounceRate']);\nAnalytics::setDimension(['ga:pagePath', 'ga:pageTitle']);\nAnalytics::setDimensionFilter('ga:pagePath', 'REGEXP', '/i-want-to-get-all-data-that-has-this-page-path');\nAnalytics::setOrder('ga:pageviews', 'VALUE', 'DESCENDING');\nreturn Analytics::get();\n```\n\n`setMetrics` and `setDimension` methods accept an array containing the wanted metrics and dimension. Available [metrics and dimensions](https://developers.google.com/analytics/devguides/reporting/core/dimsmets)\n\n`setDimensionFilter` accept 3 parameters. First parameter get the dimension name in which you want to filter analytics data. Second parameter get the [operator](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#operator) (REGEXP, BEGINS_WITH, ENDS_WITH and more) you want. Third parameter get the expression. For example if you want to get all analytics data in which the page path include `play` or `simple` words you can use: `Analytics::setDimensionFilter('ga:pagePath', 'REGEXP', '(\\/play\\/|\\/simple\\/)');`\n\nIf you want to get analytics data for multiple pages in a single request by their exact paths, you can use the 'IN_LIST' operator and pass an array of paths as the third parameter. E.g. \n`Analytics::setDimensionFilter('ga:pagePath', 'IN_LIST', ['/i-want-data-for-this-path', '/and/this-path-too']);`\n\n`setOrder` method accept 3 parameters. First parameter get the name in which you want to order the data. Second get [OrderType](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#ordertype) usually `VALUE`. Third get the [SortOrder](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#sortorder) usually `ASCENDING` or `DESCENDING`.\n\n#### Get data using Analytics contract \n`Panakour\\Analytics\\Contracts\\Analytics`\n\nInstead of facade you can get analytics data using the Analytics interface:\n```php\nuse Panakour\\Analytics\\Contracts\\Analytics;\n\nclass GoogleAnalyticsController\n{\n    //inject analytics interface\n    public function get(Analytics $analytics)\n    {\n        $analytics-\u003esetDateRange('2016-12-01', '2016-12-20');\n        $analytics-\u003esetMaxResults(11);\n        $analytics-\u003esetMetrics(['ga:pageviews', 'ga:uniquePageviews', 'ga:avgTimeOnPage', 'ga:entrances', 'ga:bounceRate']);\n        $analytics-\u003esetDimension(['ga:pagePath', 'ga:pageTitle']);\n        $analytics-\u003esetDimensionFilter('ga:pagePath', 'REGEXP', '(\\/this-value-in-path\\/|\\/or-this-value-in-path\\/)');\n        $analytics-\u003esetOrder('ga:pageviews', 'VALUE', 'DESCENDING');\n        return $analytics-\u003eget();\n    }\n}    \n```\n\n## Create credential with google analytics api v4\nGuide from google analytics api v4: https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php\n - Create a project using [Google API Console](https://console.developers.google.com/start/api?id=analyticsreporting.googleapis.com\u0026credential=client_key):\n  - Open the [Service accounts page](https://console.developers.google.com/permissions/serviceaccounts). If prompted, select your project that you have created and click open.\n - Click `Create service account`.\n - In the Create service account window, type a name for the service account, and select Furnish a new private key. In the key type select JSON and then click create. If you want to grant G Suite domain-wide authority to the service account, also select Enable G Suite Domain-wide Delegation.\n - Copy Service account ID you have created and then go to [google analytics admin panel](https://analytics.google.com/analytics/web/#management/Settings) select `User Management` and add the account you have copied with the permissions that you want.\n - Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely.\n - Rename the **json** file to `service-account-credentials.json`.\n - To get the **view id** you can use the [account explorer](https://ga-dev-tools.appspot.com/account-explorer/) or from google analytics `view settings` from [admin panel](https://analytics.google.com/analytics/web/#management/Settings).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpanakour%2Fgoogle-analytics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpanakour%2Fgoogle-analytics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpanakour%2Fgoogle-analytics/lists"}