├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── search-console.php ├── phpunit.xml └── src ├── Exceptions ├── InvalidConfiguration.php └── InvalidPeriod.php ├── ExponentialBackoff.php ├── Period.php ├── SearchConsole.php ├── SearchConsoleClient.php ├── SearchConsoleClientFactory.php ├── SearchConsoleFacade.php └── SearchConsoleServiceProvider.php /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All Notable changes to `laravel-search-console` will be documented in this file. 4 | ## [1.8.0] - 2022-03-11 5 | - Add Compatibility With Laravel 9 6 | - Drop support for Laravel 8 7 | - Minimum PHP 8.0.2 8 | - Drop Support for Laravel Lumen 9 | 10 | ## [1.7.1] - 2021-04-11 11 | - Add 'AggregationType' request and response parameter 12 | 13 | ## [1.7.0] - 2021-04-11 14 | - Add 'dataState' request and response parameter 15 | 16 | ## [1.6.1] - 2021-03-09 17 | - Add Compatibility with PHP 8 18 | 19 | ## [1.6.0] - 2020-09-14 20 | - Add Compatibility With Laravel 8 21 | - Drop support for Laravel 7 22 | - Minimum PHP 7.3 23 | 24 | ## [1.5.0] - 2020-06-28 25 | - Use full path for json account config files 26 | - Removed files env vars 27 | 28 | ## [1.4.0] - 2020-04-12 29 | - Add Compatibility With Laravel 7 30 | - Drop support for Laravel 6 31 | - Minimum PHP 7.2.5 32 | 33 | ## [1.3.0] - 2019-09-15 34 | - Add Compatibility With Laravel 6 35 | - drop support for Laravel 5.8 and lower 36 | 37 | ## [1.2.0] - 2019-02-28 38 | - Add Compatibility With Laravel 5.8 39 | - drop support for Laravel 5.7 and lower 40 | - drop support for PHP 7.1 and lower 41 | 42 | ## [1.1.4] - 2018-10-30 43 | - Add Compatibility With Laravel 5.7 44 | 45 | ## [1.1.3] - 2018-07-28 46 | - Set row limit to 25000 47 | - Throw authentication exceptions immediately 48 | 49 | ## [1.1.2] - 2018-04-27 50 | - Accept JSON or Array as Access Token 51 | 52 | ## [1.1.1] - 2018-03-06 53 | - Add Compatibility With Laravel 5.6 54 | 55 | ## [1.1.0] - 2017-10-12 56 | - Clear Cache on setAccessToken() 57 | 58 | ## [1.0.0] - 2017-10-06 59 | - change default cache to null 60 | - First stable release 61 | 62 | ## [0.1.0] - 2017-09-07 63 | - add compatibility with Laravel 5.5 64 | - dropped support for anything lower than Laravel 5.4 65 | 66 | ## [0.0.1] - 2017-07-27 67 | - Initial Beta Release 68 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 46 | 47 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 48 | 49 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 50 | 51 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 52 | 53 | **Happy coding**! -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Felix Schulze 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Search Console 2 | 3 | [![Latest Version](https://img.shields.io/github/release/schulzefelix/laravel-search-console.svg?style=flat-square)](https://github.com/schulzefelix/laravel-search-console/releases) 4 | [![Software License][ico-license]](LICENSE.md) 5 | [![Build Status][ico-travis]][link-travis] 6 | [![Quality Score][ico-code-quality]][link-code-quality] 7 | [![StyleCI](https://styleci.io/repos/97710032/shield)](https://styleci.io/repos/97710032) 8 | [![Latest Version on Packagist][ico-version]][link-packagist] 9 | [![Total Downloads][ico-downloads]][link-downloads] 10 | 11 | Using this package you can easily retrieve data from Google Search Console API. 12 | 13 | Here are a few examples of the provided methods: 14 | 15 | ```php 16 | use SearchConsole; 17 | 18 | //list all available sites for that token 19 | SearchConsole::setAccessToken($token)->listSites(); 20 | 21 | //get site details (permissionLevel) for specific site 22 | SearchConsole::setAccessToken($token)->getSite('http://blog.example.com/'); 23 | ``` 24 | 25 | 26 | ## Install 27 | 28 | This package can be installed through Composer. 29 | 30 | ``` bash 31 | $ composer require schulzefelix/laravel-search-console 32 | ``` 33 | 34 | Optionally, you can publish the config file of this package with this command: 35 | 36 | ``` bash 37 | php artisan vendor:publish --provider="SchulzeFelix\SearchConsole\SearchConsoleServiceProvider" 38 | ``` 39 | 40 | The following config file will be published in `config/search-console.php` 41 | 42 | ```php 43 | return [ 44 | 45 | /* 46 | |-------------------------------------------------------------------------- 47 | | Authentication 48 | |-------------------------------------------------------------------------- 49 | | Google offers access via OAuth client IDs or service accounts. 50 | | For more information see: https://developers.google.com/identity/protocols/OAuth2 51 | | 52 | | Supported: "oauth", "oauth_json", "service_account", 53 | */ 54 | 55 | 'auth_type' => env('GOOGLE_AUTH_TYPE', 'oauth'), 56 | 57 | /* 58 | |-------------------------------------------------------------------------- 59 | | Application Credentials 60 | |-------------------------------------------------------------------------- 61 | | 62 | | https://developers.google.com/api-client-library/php/auth/service-accounts#creatinganaccount 63 | */ 64 | 65 | 'connections' => [ 66 | 67 | 'oauth' => [ 68 | 'client_id' => env('GOOGLE_CLIENT_ID'), 69 | 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 70 | ], 71 | 72 | 'oauth_json' => [ 73 | 'auth_config' => storage_path('app/searchconsole/oauth-account-credentials.json'), 74 | ], 75 | 76 | 'service_account' => [ 77 | 'application_credentials' => storage_path('app/searchconsole/service-account-credentials.json'), 78 | ], 79 | 80 | ], 81 | 82 | /* 83 | |-------------------------------------------------------------------------- 84 | | Cache Settings 85 | |-------------------------------------------------------------------------- 86 | | Here you may configure the "store" that the underlying Google_Client will 87 | | use to store it's data. You may also add extra parameters that will 88 | | be passed on setCacheConfig (see docs for google-api-php-client). 89 | | 90 | | Optional parameters: "lifetime", "prefix" 91 | */ 92 | 93 | 'cache' => [ 94 | 'store' => 'file', 95 | ], 96 | 97 | /* 98 | |-------------------------------------------------------------------------- 99 | | Application Name 100 | |-------------------------------------------------------------------------- 101 | */ 102 | 103 | 'application_name' => env('SEARCH_CONSOLE_APPLICATION_NAME', 'GSC Agent'), 104 | ]; 105 | ``` 106 | 107 | ## Usage 108 | 109 | Here are two basic example to retrieve all sites and an export for search analytics data. 110 | ### List Sites 111 | 112 | ```php 113 | $sites = SearchConsole::setAccessToken($token)->listSites(); 114 | ``` 115 | 116 | ### Search Analytics 117 | 118 | ```php 119 | use SearchConsole; 120 | use SchulzeFelix\SearchConsole\Period; 121 | 122 | $data = SearchConsole::setAccessToken($token)->setQuotaUser('uniqueQuotaUserString') 123 | ->searchAnalyticsQuery( 124 | 'https://www.example.com/', 125 | Period::create(Carbon::now()->subDays(30), Carbon::now()->subDays(2)), 126 | ['query', 'page', 'country', 'device', 'date'], 127 | [['dimension' => 'query', 'operator' => 'notContains', 'expression' => 'cheesecake']], 128 | 1000, 129 | 'web', 130 | 'all', 131 | 'auto' 132 | ); 133 | ``` 134 | 135 | ## Provided methos 136 | ### Retrieve One Site 137 | ```php 138 | public function public function getSite(string $siteUrl): array 139 | ``` 140 | 141 | ### Retrieve All Sites 142 | ```php 143 | public function public function listSites(): Collection 144 | ``` 145 | 146 | ### Retrieve Search Analytics Data 147 | ```php 148 | public function searchAnalyticsQuery(string $siteUrl, Period $period, array $dimensions = [], array $filters = [], int $rows = 1000, string $searchType = 'web', string $dataState = 'final', string $aggregationType = 'auto'): Collection 149 | ``` 150 | 151 | ### Check Access Token 152 | ```php 153 | public function public function isAccessTokenExpired(): Bool 154 | ``` 155 | 156 | ## Provided fluent configuration 157 | 158 | ### Set Access Token (Required) 159 | 160 | ```php 161 | $sites = SearchConsole::setAccessToken($token)->listSites(); 162 | ``` 163 | 164 | ### Set Quota User 165 | To avoid to the API limits, you can provide a unique string for the authenticated account. 166 | 167 | More information: https://developers.google.com/webmaster-tools/search-console-api-original/v3/limits 168 | ```php 169 | $sites = SearchConsole::setAccessToken($token)->setQuotaUser('uniqueQuotaUserString')->listSites(); 170 | ``` 171 | 172 | ## Get Underlying Service 173 | You can get access to the underlying `Google_Service_Webmasters` object: 174 | 175 | ```php 176 | SearchConsole::getWebmastersService(); 177 | ``` 178 | 179 | ## Change log 180 | 181 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 182 | 183 | ## Testing 184 | 185 | ``` bash 186 | $ vendor/bin/phpunit 187 | ``` 188 | 189 | ## Contributing 190 | 191 | Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. 192 | 193 | ## Security 194 | 195 | If you discover any security related issues, please email github@schulze.co instead of using the issue tracker. 196 | 197 | ## Credits 198 | 199 | - [Felix Schulze][link-author] 200 | - [All Contributors][link-contributors] 201 | 202 | ## License 203 | 204 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 205 | 206 | [ico-version]: https://img.shields.io/packagist/v/schulzefelix/laravel-search-console.svg?style=flat-square 207 | [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square 208 | [ico-travis]: https://img.shields.io/travis/schulzefelix/laravel-search-console/master.svg?style=flat-square 209 | [ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/schulzefelix/laravel-search-console.svg?style=flat-square 210 | [ico-code-quality]: https://scrutinizer-ci.com/g/schulzefelix/laravel-search-console/badges/quality-score.png?b=master 211 | [ico-downloads]: https://img.shields.io/packagist/dt/schulzefelix/laravel-search-console.svg?style=flat-square 212 | 213 | [link-packagist]: https://packagist.org/packages/schulzefelix/laravel-search-console 214 | [link-travis]: https://travis-ci.org/schulzefelix/laravel-search-console 215 | [link-scrutinizer]: https://scrutinizer-ci.com/g/schulzefelix/laravel-search-console/code-structure 216 | [link-code-quality]: https://scrutinizer-ci.com/g/schulzefelix/laravel-search-console 217 | [link-downloads]: https://packagist.org/packages/schulzefelix/laravel-search-console 218 | [link-author]: https://github.com/schulzefelix 219 | [link-contributors]: ../../contributors 220 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "schulzefelix/laravel-search-console", 3 | "type": "library", 4 | "description": "A Laravel package to retrieve data from Google Search Console", 5 | "keywords": [ 6 | "laravel-search-console", 7 | "laravel", 8 | "search console", 9 | "google", 10 | "webmaster tools" 11 | ], 12 | "homepage": "https://github.com/schulzefelix/laravel-search-console", 13 | "license": "MIT", 14 | "authors": [ 15 | { 16 | "name": "Felix Schulze", 17 | "email": "github@schulze.co", 18 | "homepage": "https://schulze.co", 19 | "role": "Developer" 20 | } 21 | ], 22 | "require": { 23 | "php": "^8.0.2", 24 | "google/apiclient": "^2.0", 25 | "illuminate/support": "^9.0", 26 | "symfony/cache": "^6.0" 27 | 28 | }, 29 | "require-dev": { 30 | "mockery/mockery": "^1.4", 31 | "orchestra/testbench": "^7.0", 32 | "phpunit/phpunit": "^9.4" 33 | }, 34 | "autoload": { 35 | "psr-4": { 36 | "SchulzeFelix\\SearchConsole\\": "src" 37 | } 38 | }, 39 | "extra":{ 40 | "laravel":{ 41 | "providers":[ 42 | "SchulzeFelix\\SearchConsole\\SearchConsoleServiceProvider" 43 | ], 44 | "aliases":{ 45 | "SearchConsole": "SchulzeFelix\\SearchConsole\\SearchConsoleFacade" 46 | } 47 | } 48 | }, 49 | "autoload-dev": { 50 | "psr-4": { 51 | "SchulzeFelix\\SearchConsole\\Tests\\": "tests" 52 | } 53 | }, 54 | "config": { 55 | "sort-packages": true 56 | }, 57 | "minimum-stability": "dev", 58 | "prefer-stable": true 59 | } 60 | -------------------------------------------------------------------------------- /config/search-console.php: -------------------------------------------------------------------------------- 1 | env('GOOGLE_AUTH_TYPE', 'oauth'), 16 | 17 | /* 18 | |-------------------------------------------------------------------------- 19 | | Application Credentials 20 | |-------------------------------------------------------------------------- 21 | | 22 | | https://developers.google.com/api-client-library/php/auth/service-accounts#creatinganaccount 23 | */ 24 | 25 | 'connections' => [ 26 | 27 | 'oauth' => [ 28 | 'client_id' => env('GOOGLE_CLIENT_ID'), 29 | 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 30 | ], 31 | 32 | 'oauth_json' => [ 33 | 'auth_config' => storage_path('app/searchconsole/oauth-account-credentials.json'), 34 | ], 35 | 'service_account' => [ 36 | 'application_credentials' => storage_path('app/searchconsole/service-account-credentials.json'), 37 | ], 38 | 39 | ], 40 | 41 | /* 42 | |-------------------------------------------------------------------------- 43 | | Cache Settings 44 | |-------------------------------------------------------------------------- 45 | | Here you may configure the "store" that the underlying Google_Client will 46 | | use to store it's data. You may also add extra parameters that will 47 | | be passed on setCacheConfig (see docs for google-api-php-client). 48 | | 49 | | Optional parameters: "lifetime", "prefix" 50 | */ 51 | 52 | 'cache' => [ 53 | 'store' => 'file', 54 | ], 55 | 56 | /* 57 | |-------------------------------------------------------------------------- 58 | | Application Name 59 | |-------------------------------------------------------------------------- 60 | */ 61 | 62 | 'application_name' => env('SEARCH_CONSOLE_APPLICATION_NAME', 'GSC Agent'), 63 | ]; 64 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidConfiguration.php: -------------------------------------------------------------------------------- 1 | format('Y-m-d')}` cannot be after end date `{$endDate->format('Y-m-d')}`."); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/ExponentialBackoff.php: -------------------------------------------------------------------------------- 1 | retries = $retries !== null ? (int) $retries : 3; 34 | $this->retryFunction = $retryFunction; 35 | $this->delayFunction = function ($delay) { 36 | usleep($delay); 37 | }; 38 | } 39 | 40 | /** 41 | * Executes the retry process. 42 | * 43 | * @param callable $function 44 | * @param array $arguments [optional] 45 | * @return mixed 46 | * 47 | * @throws \Exception The last exception caught while retrying. 48 | */ 49 | public function execute(callable $function, array $arguments = []) 50 | { 51 | $delayFunction = $this->delayFunction; 52 | $retryAttempt = 0; 53 | $exception = null; 54 | 55 | while (true) { 56 | try { 57 | return call_user_func_array($function, $arguments); 58 | } catch (\Exception $exception) { 59 | if ($this->retryFunction) { 60 | if (! call_user_func($this->retryFunction, $exception)) { 61 | throw $exception; 62 | } 63 | } 64 | 65 | if ($exception->getCode() >= 400 && $exception->getCode() <= 499) { 66 | break; 67 | } 68 | 69 | if ($retryAttempt >= $this->retries) { 70 | break; 71 | } 72 | 73 | $delayFunction($this->calculateDelay($retryAttempt)); 74 | $retryAttempt++; 75 | } 76 | } 77 | 78 | throw $exception; 79 | } 80 | 81 | /** 82 | * @param callable $delayFunction 83 | * @return void 84 | */ 85 | public function setDelayFunction(callable $delayFunction) 86 | { 87 | $this->delayFunction = $delayFunction; 88 | } 89 | 90 | /** 91 | * Calculates exponential delay. 92 | * 93 | * @param int $attempt 94 | * @return int 95 | */ 96 | private function calculateDelay($attempt) 97 | { 98 | return min( 99 | mt_rand(0, 1000000) + (pow(2, $attempt) * 1000000), 100 | self::MAX_DELAY_MICROSECONDS 101 | ); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/Period.php: -------------------------------------------------------------------------------- 1 | $endDate) { 24 | throw InvalidPeriod::startDateCannotBeAfterEndDate($startDate, $endDate); 25 | } 26 | 27 | $this->startDate = $startDate; 28 | 29 | $this->endDate = $endDate; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/SearchConsole.php: -------------------------------------------------------------------------------- 1 | client = $client; 21 | } 22 | 23 | /** 24 | * @param string $quotaUser 25 | * @return $this 26 | */ 27 | public function setQuotaUser(string $quotaUser) 28 | { 29 | $this->client->setQuotaUser($quotaUser); 30 | 31 | return $this; 32 | } 33 | 34 | /** 35 | * @param string|array $accessToken 36 | * @return $this 37 | */ 38 | public function setAccessToken($accessToken) 39 | { 40 | if (is_array($accessToken)) { 41 | $accessToken = trim(json_encode($accessToken)); 42 | } 43 | 44 | $this->client->setAccessToken($accessToken); 45 | 46 | $this->client->getGoogleClient()->getCache()->clear(); 47 | 48 | return $this; 49 | } 50 | 51 | /** 52 | * @param string $siteUrl 53 | * @return array 54 | */ 55 | public function getSite(string $siteUrl) 56 | { 57 | $sites = $this->client->getWebmastersService()->sites; 58 | 59 | $siteInfo = $sites->get($siteUrl); 60 | 61 | $response = [ 62 | 'siteUrl' => $siteInfo->getSiteUrl(), 63 | 'permissionLevel' => $siteInfo->getPermissionLevel(), 64 | ]; 65 | 66 | return $response; 67 | } 68 | 69 | public function listSites() 70 | { 71 | $sites = $this->client->getWebmastersService()->sites; 72 | $siteList = $sites->listSites(); 73 | 74 | $sitesCollection = new Collection(); 75 | foreach ($siteList->getSiteEntry() as $site) { 76 | $sitesCollection->push([ 77 | 'siteUrl' => $site->siteUrl, 78 | 'permissionLevel' => $site->permissionLevel, 79 | ]); 80 | } 81 | $sitesCollection = $sitesCollection->sortBy('siteUrl'); 82 | 83 | return $sitesCollection; 84 | } 85 | 86 | /** 87 | * Call the query method on the authenticated client. 88 | * 89 | * @param string $siteUrl 90 | * @param Period $period 91 | * @param array $dimensions 92 | * @param array $filters 93 | * @param int $rows 94 | * @param string $searchType 95 | * @param string $dataState 96 | * @param string $aggregationType 97 | * @return Collection 98 | * 99 | * @throws \Exception 100 | */ 101 | public function searchAnalyticsQuery(string $siteUrl, Period $period, array $dimensions = [], array $filters = [], int $rows = 1000, string $searchType = 'web', string $dataState = 'final', string $aggregationType = 'auto') 102 | { 103 | $request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest(); 104 | $request->setStartDate($period->startDate->toDateString()); 105 | $request->setEndDate($period->endDate->toDateString()); 106 | $request->setSearchType($searchType); 107 | $request->setDimensions($dimensions); 108 | $request->setDataState($dataState); 109 | $request->setAggregationType($aggregationType); 110 | $request = $this->applyFilters($request, $filters); 111 | 112 | return $this->client->performQuery($siteUrl, $rows, $request); 113 | } 114 | 115 | public function isAccessTokenExpired() 116 | { 117 | $googleClient = $this->client->getGoogleClient(); 118 | 119 | $newToken = $googleClient->fetchAccessTokenWithRefreshToken(); 120 | 121 | if (isset($newToken['error'])) { 122 | return true; 123 | } 124 | 125 | $googleClient->setAccessToken($newToken); 126 | 127 | return $googleClient->isAccessTokenExpired(); 128 | } 129 | 130 | /* 131 | * Get the underlying Google_Service_Webmasters object. You can use this 132 | * to basically call anything on the Google Search Console API. 133 | */ 134 | public function getWebmastersService(): \Google_Service_Webmasters 135 | { 136 | return $this->client->getWebmastersService(); 137 | } 138 | 139 | private function applyFilters(Google_Service_Webmasters_SearchAnalyticsQueryRequest $request, $filters) 140 | { 141 | $filterArray = []; 142 | foreach ($filters as $filterItem) { 143 | if (strlen($filterItem['expression']) === 0) { 144 | continue; 145 | } 146 | $filter = new Google_Service_Webmasters_ApiDimensionFilter(); 147 | $filter->setDimension($filterItem['dimension']); 148 | $filter->setOperator($filterItem['operator']); 149 | $filter->setExpression($filterItem['expression']); 150 | $filterArray[] = $filter; 151 | } 152 | 153 | if (count($filterArray)) { 154 | $filtergroup = new Google_Service_Webmasters_ApiDimensionFilterGroup(); 155 | $filtergroup->setFilters($filterArray); 156 | $request->setDimensionFilterGroups([$filtergroup]); 157 | } 158 | 159 | return $request; 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /src/SearchConsoleClient.php: -------------------------------------------------------------------------------- 1 | googleClient = $googleClient; 33 | } 34 | 35 | /** 36 | * @param string $siteUrl 37 | * @param int $rows 38 | * @param \Google_Service_Webmasters_SearchAnalyticsQueryRequest $request 39 | * @return Collection 40 | * 41 | * @throws \Exception 42 | */ 43 | public function performQuery($siteUrl, $rows, $request): Collection 44 | { 45 | $searchanalyticsResource = $this->getWebmastersService()->searchanalytics; 46 | 47 | $maxQueries = 2000; 48 | $currentRequest = 1; 49 | $dataRows = new Collection(); 50 | 51 | while ($currentRequest < $maxQueries) { 52 | $startRow = ($currentRequest - 1) * self::CHUNK_SIZE; 53 | 54 | $request->setRowLimit(self::CHUNK_SIZE); 55 | $request->setStartRow($startRow); 56 | 57 | $backoff = new ExponentialBackoff(10); 58 | $response = $backoff->execute(function () use ($searchanalyticsResource, $siteUrl, $request) { 59 | return $searchanalyticsResource->query($siteUrl, $request, $this->queryOptParams); 60 | }); 61 | 62 | // Stop if no more rows returned 63 | if (count($response->getRows()) == 0) { 64 | break; 65 | } 66 | 67 | foreach ($response->getRows() as $row) { 68 | /* 69 | * Use a unique hash as key to prevent duplicates caused by the query dimension problem with the google api 70 | * Google give less than 5000 rows back when two or more dimension with the query dimension are choosen, repeated calls give back more rows 71 | * https://productforums.google.com/forum/?hl=en#!topic/webmasters/wF_Rm9CGr4U 72 | */ 73 | 74 | if (is_array($row->getKeys()) && count($row->getKeys())) { 75 | $item = array_combine($request->getDimensions(), $row->getKeys()); 76 | $uniqueHash = $this->getUniqueItemHash($row, $request); 77 | } else { 78 | $uniqueHash = md5(Str::random(20)); 79 | $item = []; 80 | } 81 | 82 | $item['clicks'] = $row->getClicks(); 83 | $item['impressions'] = $row->getImpressions(); 84 | $item['ctr'] = $row->getCtr(); 85 | $item['position'] = $row->getPosition(); 86 | $item['searchType'] = $request->getSearchType(); 87 | $item['dataState'] = $request->getDataState(); 88 | $item['aggregationType'] = $request->getAggregationType(); 89 | 90 | $dataRows->put($uniqueHash, $item); 91 | } 92 | 93 | //Stop if the requested row count are reached 94 | if ($dataRows->count() >= $rows) { 95 | break; 96 | } 97 | 98 | $currentRequest++; 99 | } 100 | 101 | return $dataRows->take($rows); 102 | } 103 | 104 | /** 105 | * @param string $quotaUser 106 | */ 107 | public function setQuotaUser(string $quotaUser) 108 | { 109 | $quotaUser = md5($quotaUser); 110 | 111 | $this->queryOptParams['quotaUser'] = $quotaUser; 112 | 113 | $guzzleConfig = $this->googleClient->getHttpClient()->getConfig(); 114 | 115 | Arr::set($guzzleConfig, 'base_uri', Google_Client::API_BASE_PATH.'?quotaUser='.$quotaUser); 116 | 117 | $guzzleClient = new Client($guzzleConfig); 118 | 119 | $this->googleClient->setHttpClient($guzzleClient); 120 | } 121 | 122 | /** 123 | * @param string $accessToken 124 | */ 125 | public function setAccessToken(string $accessToken) 126 | { 127 | $this->googleClient->setAccessToken($accessToken); 128 | } 129 | 130 | /** 131 | * @return Google_Client 132 | */ 133 | public function getGoogleClient(): Google_Client 134 | { 135 | return $this->googleClient; 136 | } 137 | 138 | /** 139 | * @return Google_Service_Webmasters 140 | */ 141 | public function getWebmastersService(): Google_Service_Webmasters 142 | { 143 | return new Google_Service_Webmasters($this->googleClient); 144 | } 145 | 146 | /** 147 | * @param $row 148 | * @param $request 149 | * @return string 150 | */ 151 | private function getUniqueItemHash($row, $request) 152 | { 153 | $keys = implode('', $row->getKeys()); 154 | 155 | $filters = []; 156 | foreach ($request->getDimensionFilterGroups() as $dimensionFilterGroup) { 157 | foreach ($dimensionFilterGroup->filters as $filter) { 158 | $filters[] = $filter->dimension.$filter->expression.$filter->operator; 159 | } 160 | } 161 | $filters = implode('', $filters); 162 | 163 | return md5($keys.$filters.$request->getSearchType().$request->endDate.$request->startDate); 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/SearchConsoleClientFactory.php: -------------------------------------------------------------------------------- 1 | addScope(Google_Service_Webmasters::WEBMASTERS); 34 | $client->setAccessType('offline'); 35 | 36 | self::configureGzip($client, $config['application_name']); 37 | self::configureCache($client, $config['cache']); 38 | 39 | return $client; 40 | } 41 | 42 | /** 43 | * @param Google_Client $client 44 | * @param $config 45 | */ 46 | protected static function configureCache(Google_Client $client, $config) 47 | { 48 | $config = collect($config); 49 | 50 | $store = \Cache::store($config->get('store')); 51 | 52 | $cache = new Psr16Adapter($store); 53 | 54 | $client->setCache($cache); 55 | 56 | $client->setCacheConfig( 57 | $config->except('store')->toArray() 58 | ); 59 | } 60 | 61 | /** 62 | * @param Google_Client $client 63 | * @param $application_name 64 | */ 65 | private static function configureGzip(Google_Client $client, $application_name) 66 | { 67 | $client->setApplicationName($application_name.' (gzip)'); 68 | 69 | $options = []; 70 | $options['base_uri'] = Google_Client::API_BASE_PATH; 71 | $options['headers'] = [ 72 | 'User-Agent' => $application_name.' (gzip)', 73 | 'Accept-Encoding' => 'gzip', 74 | ]; 75 | 76 | $guzzleClient = new Client($options); 77 | 78 | $client->setHttpClient($guzzleClient); 79 | } 80 | 81 | /** 82 | * @param Google_Client $client 83 | * @param $config 84 | */ 85 | private static function configureAuthentication(Google_Client $client, $config) 86 | { 87 | switch ($config['auth_type']) { 88 | case 'oauth': 89 | $client->setClientId($config['connections']['oauth']['client_id']); 90 | $client->setClientSecret($config['connections']['oauth']['client_secret']); 91 | break; 92 | case 'oauth_json': 93 | $client->setAuthConfig($config['connections']['oauth_json']['auth_config']); 94 | break; 95 | case 'service_account': 96 | $client->useApplicationDefaultCredentials($config['connections']['service_account']['application_credentials']); 97 | break; 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/SearchConsoleFacade.php: -------------------------------------------------------------------------------- 1 | setupConfig(); 18 | } 19 | 20 | /** 21 | * Register any package services. 22 | * 23 | * @return void 24 | */ 25 | public function register() 26 | { 27 | $this->mergeConfigFrom(__DIR__.'/../config/search-console.php', 'search-console'); 28 | 29 | $this->app->bind(SearchConsoleClient::class, function () { 30 | $searchConsoleConfig = config('search-console'); 31 | 32 | return SearchConsoleClientFactory::createForConfig($searchConsoleConfig); 33 | }); 34 | 35 | $this->app->bind(SearchConsole::class, function () { 36 | $searchConsoleConfig = config('search-console'); 37 | 38 | $this->guardAgainstInvalidConfiguration($searchConsoleConfig); 39 | 40 | $client = app(SearchConsoleClient::class); 41 | 42 | return new SearchConsole($client); 43 | }); 44 | 45 | $this->app->alias(SearchConsole::class, 'laravel-searchconsole'); 46 | } 47 | 48 | /** 49 | * @param array|null $searchConsoleConfig 50 | * 51 | * @throws InvalidConfiguration 52 | */ 53 | protected function guardAgainstInvalidConfiguration(array $searchConsoleConfig = null) 54 | { 55 | if ($searchConsoleConfig['auth_type'] == 'service_account' && ! file_exists($searchConsoleConfig['connections']['service_account']['application_credentials'])) { 56 | throw InvalidConfiguration::credentialsJsonDoesNotExist($searchConsoleConfig['connections']['service_account']['application_credentials']); 57 | } 58 | 59 | if ($searchConsoleConfig['auth_type'] == 'oauth_json' && ! file_exists($searchConsoleConfig['connections']['oauth_json']['auth_config'])) { 60 | throw InvalidConfiguration::credentialsJsonDoesNotExist($searchConsoleConfig['connections']['oauth_json']['auth_config']); 61 | } 62 | } 63 | 64 | protected function setupConfig() 65 | { 66 | $source = realpath(__DIR__.'/../config/search-console.php'); 67 | 68 | $this->publishes([$source => config_path('search-console.php')]); 69 | 70 | $this->mergeConfigFrom($source, 'search-console'); 71 | } 72 | } 73 | --------------------------------------------------------------------------------