PHPackages                             revolution/laravel-google-searchconsole - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. revolution/laravel-google-searchconsole

ActiveLibrary[API Development](/categories/api)

revolution/laravel-google-searchconsole
=======================================

Google SearchConsole API for Laravel

4.2.0(2mo ago)184.8k↑46.7%MITPHPPHP ^8.3CI passing

Since Jun 2Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/invokable/laravel-google-searchconsole)[ Packagist](https://packagist.org/packages/revolution/laravel-google-searchconsole)[ GitHub Sponsors](https://github.com/invokable)[ RSS](/packages/revolution-laravel-google-searchconsole/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (31)Used By (0)

Google SearchConsole API for Laravel
====================================

[](#google-searchconsole-api-for-laravel)

[![Maintainability](https://camo.githubusercontent.com/b199fb3fa7b44c9225a4c4be6530f7500d091f1a45f0d40756f1f58a9aec99fc/68747470733a2f2f716c74792e73682f6261646765732f39666234333461342d316364332d343230332d616639322d6566636134636139386138312f6d61696e7461696e6162696c6974792e737667)](https://qlty.sh/gh/invokable/projects/laravel-google-searchconsole)[![Code Coverage](https://camo.githubusercontent.com/af6992804313b63c7787e5987a85ccf39e5430aab9acee29406b24bc76345c8c/68747470733a2f2f716c74792e73682f6261646765732f39666234333461342d316364332d343230332d616639322d6566636134636139386138312f746573745f636f7665726167652e737667)](https://qlty.sh/gh/invokable/projects/laravel-google-searchconsole)[![Ask DeepWiki](https://camo.githubusercontent.com/0f5ae213ac378635adeb5d7f13cef055ad2f7d9a47b36de7b1c67dbe09f609ca/68747470733a2f2f6465657077696b692e636f6d2f62616467652e737667)](https://deepwiki.com/invokable/laravel-google-searchconsole)

Overview
--------

[](#overview)

This Laravel package provides a comprehensive PHP wrapper for the Google Search Console API, enabling you to seamlessly integrate Search Console functionality into your Laravel applications. With this package, you can:

- **Query Search Analytics Data**: Retrieve search performance metrics including impressions, clicks, CTR, and average position
- **Analyze Website Performance**: Get detailed insights about your website's performance in Google Search results
- **Filter by Dimensions**: Query data by page, query, country, device, and more
- **Manage Site Properties**: List and manage your Search Console properties
- **Flexible Authentication**: Support for both OAuth 2.0 (user-based) and Service Account (server-to-server) authentication
- **Laravel Integration**: Built specifically for Laravel with facades, service providers, and Artisan commands

The package leverages the powerful `revolution/laravel-google-sheets` dependency for Google API client management and authentication, providing automatic token refresh and robust error handling.

**Perfect for**: SEO tools, analytics dashboards, automated reporting, website monitoring, and any application that needs to access Google Search Console data programmatically.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.3
- Laravel &gt;= 12.0

Installation
------------

[](#installation)

```
composer require revolution/laravel-google-searchconsole

php artisan vendor:publish --tag="google-config"
```

### Uninstall

[](#uninstall)

```
composer remove revolution/laravel-google-searchconsole

```

Sample project
--------------

[](#sample-project)

Configuration
-------------

[](#configuration)

### Authentication Methods

[](#authentication-methods)

This package supports two authentication methods for accessing the Google Search Console API:

- **OAuth 2.0**: Recommended for user-based access when you need to access data on behalf of individual Google users
- **Service Account**: Ideal for server-to-server applications with automated access to specific properties
- **Note**: API Key authentication is NOT supported by Google Search Console API

### Prerequisites

[](#prerequisites)

1. Go to the [Google Cloud Console](https://console.cloud.google.com/)
2. Create a new project or select an existing one
3. Enable the **Google Search Console API**
4. Create credentials based on your chosen authentication method

OAuth 2.0 Setup (using Socialite)
---------------------------------

[](#oauth-20-setup-using-socialite)

For OAuth authentication, this package works seamlessly with Laravel Socialite's official Google driver.

### 1. Install Laravel Socialite

[](#1-install-laravel-socialite)

```
composer require laravel/socialite
```

### 2. Create OAuth 2.0 Credentials

[](#2-create-oauth-20-credentials)

In Google Cloud Console:

1. Go to "Credentials" → "Create Credentials" → "OAuth 2.0 Client IDs"
2. Set application type to "Web application"
3. Add your authorized redirect URIs (e.g., `https://yourapp.com/auth/google/callback`)

### 3. Configure OAuth Settings

[](#3-configure-oauth-settings)

Add to `config/services.php`:

```
'google' => [
    'client_id'     => env('GOOGLE_CLIENT_ID'),
    'client_secret' => env('GOOGLE_CLIENT_SECRET'),
    'redirect'      => env('GOOGLE_REDIRECT_URI'),
],
```

Add to `config/google.php`:

```
'client_id'        => env('GOOGLE_CLIENT_ID'),
'client_secret'    => env('GOOGLE_CLIENT_SECRET'),
'redirect_uri'     => env('GOOGLE_REDIRECT_URI'),
'scopes'           => [\Google\Service\Webmasters::WEBMASTERS],
'access_type'      => 'offline',
'prompt'           => 'consent select_account',
```

### 4. Environment Variables for OAuth

[](#4-environment-variables-for-oauth)

```
GOOGLE_CLIENT_ID=your_oauth_client_id
GOOGLE_CLIENT_SECRET=your_oauth_client_secret
GOOGLE_REDIRECT_URI=https://yourapp.com/auth/google/callback
```

Service Account Setup
---------------------

[](#service-account-setup)

Service Accounts provide server-to-server authentication without user interaction. The Service Account email must be added as an owner or user in Search Console for each property you want to access.

### 1. Create Service Account Credentials

[](#1-create-service-account-credentials)

In Google Cloud Console:

1. Go to "Credentials" → "Create Credentials" → "Service Account"
2. Fill in the service account details
3. Create and download the JSON key file

### 2. Configure Service Account

[](#2-configure-service-account)

The package automatically uses Service Account authentication when configured through the `laravel-google-sheets` dependency. Place your service account JSON file in your Laravel storage directory.

Even for service accounts, scopes must be set in `config/google.php`:

```
'scopes' => [\Google\Service\Webmasters::WEBMASTERS],
```

### 3. Environment Variables for Service Account

[](#3-environment-variables-for-service-account)

#### Option 1: Using File Path

[](#option-1-using-file-path)

```
GOOGLE_SERVICE_ENABLED=true
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=/path/to/service-account.json
```

#### Option 2: Using JSON String

[](#option-2-using-json-string)

`GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION` can be an array other than a file path, so you can set it as a JSON string in `.env` and decode it to an array in `config/google.php`.

```
// config/google.php
'service' => [
    'file' => json_decode(env('GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION', ''), true),
],
```

```
// .env
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION='{"type": "service_account", "project_id": "your-project", "private_key_id": "...", "private_key": "...", "client_email": "...", "client_id": "...", "auth_uri": "...", "token_uri": "...", "auth_provider_x509_cert_url": "...", "client_x509_cert_url": "..."}'
```

This method is particularly well-suited for GitHub Actions deployment, as it allows the entire service account credentials to be stored as a single secret.

### 4. Add Service Account to Search Console

[](#4-add-service-account-to-search-console)

1. Go to [Google Search Console](https://search.google.com/search-console)
2. Select your property
3. Go to Settings → Users and permissions
4. Add your service account email as a user with "Full" permissions

Creating Custom Queries
-----------------------

[](#creating-custom-queries)

All query classes extend `Revolution\Google\SearchConsole\Query\AbstractQuery`, which is a subclass of `Google\Service\Webmasters\SearchAnalyticsQueryRequest`. This provides access to all Google Search Console API query parameters.

### Generate Query Classes

[](#generate-query-classes)

Use the Artisan command to create new query classes in `app/Search`:

```
php artisan make:search:query YourQueryName
```

### Query Structure

[](#query-structure)

Each query class must implement the `init()` method where you define your query parameters:

```
