michaelcrowcroft/google-search-console-laravel 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

michaelcrowcroft/google-search-console-laravel

最新稳定版本:v1.0.0

Composer 安装命令:

composer require michaelcrowcroft/google-search-console-laravel

包简介

A Laravel package for Google Search Console API integration

README 文档

README

A simple Laravel package for the Google Search Console API.

Installation

composer require michaelcrowcroft/google-search-console-laravel

php artisan vendor:publish --provider="MichaelCrowcroft\\GoogleSearchConsole\\GoogleSearchConsoleServiceProvider" --tag="google-search-console-config"

Usage

use GoogleSearchConsole;

// Set your access token and default site URL
GoogleSearchConsole::setAccessToken($token);
GoogleSearchConsole::setSiteUrl('https://example.com');

// Query search analytics
$data = GoogleSearchConsole::analytics()->query([
    'start_date' => '2024-01-01',
    'end_date' => '2024-01-31',
    'dimensions' => ['query', 'page'],
    'row_limit' => 100
]);

// Helper methods
$queries = GoogleSearchConsole::analytics()->getQueryData('2024-01-01', '2024-01-31');
$pages = GoogleSearchConsole::analytics()->getPageData('2024-01-01', '2024-01-31');
$countries = GoogleSearchConsole::analytics()->getCountryData('2024-01-01', '2024-01-31');

// Pass a different site if you want to overwrite the set URL, or haven't set one already.
$queries = GoogleSearchConsole::analytics()->getQueryData('https://othersite.com', '2024-01-01', '2024-01-31');

Analytics

The Query Method

The most powerful way to get search analytics data:

// Basic query (uses default site URL)
$data = GoogleSearchConsole::analytics()->query([
    'start_date' => '2024-01-01',
    'end_date' => '2024-01-31',
    'dimensions' => ['query'],
    'row_limit' => 100
]);

// Multiple dimensions
$data = GoogleSearchConsole::analytics()->query([
    'start_date' => '2024-01-01',
    'end_date' => '2024-01-31',
    'dimensions' => ['query', 'page', 'country'],
    'row_limit' => 1000
]);

// With filters
$data = GoogleSearchConsole::analytics()->query([
    'start_date' => '2024-01-01',
    'end_date' => '2024-01-31',
    'dimensions' => ['query'],
    'filters' => [
        // Dimension filter groups
    ],
    'row_limit' => 500
]);

// Or specify different site URL
$data = GoogleSearchConsole::analytics()->query('https://othersite.com', [
    'start_date' => '2024-01-01',
    'end_date' => '2024-01-31',
    'dimensions' => ['query']
]);

Helper Methods

For common use cases:

// Get top queries (uses default site URL)
$queries = GoogleSearchConsole::analytics()->getQueryData('2024-01-01', '2024-01-31', 100);

// Get top pages
$pages = GoogleSearchConsole::analytics()->getPageData('2024-01-01', '2024-01-31', 100);

// Get country performance
$countries = GoogleSearchConsole::analytics()->getCountryData('2024-01-01', '2024-01-31', 50);

// Get device performance
$devices = GoogleSearchConsole::analytics()->getDeviceData('2024-01-01', '2024-01-31');

// Get query and page combinations
$queryPages = GoogleSearchConsole::analytics()->getQueryPageData('2024-01-01', '2024-01-31');

// Or specify different site URL for specific calls
$queries = GoogleSearchConsole::analytics()->getQueryData('https://othersite.com', '2024-01-01', '2024-01-31');

Sitemaps

// List sitemaps (uses default site URL)
$sitemaps = GoogleSearchConsole::sitemaps()->list();

// Submit sitemap
GoogleSearchConsole::sitemaps()->submit('sitemap.xml');

// Get sitemap details
$sitemap = GoogleSearchConsole::sitemaps()->get('sitemap.xml');

// Delete sitemap
GoogleSearchConsole::sitemaps()->delete('old-sitemap.xml');

// Or specify different site URL
$sitemaps = GoogleSearchConsole::sitemaps()->list('https://othersite.com');

Sites

// List all verified sites
$sites = GoogleSearchConsole::sites()->list();

// Get site details (uses default site URL)
$site = GoogleSearchConsole::sites()->get();

// Check verification
$isVerified = GoogleSearchConsole::sites()->isVerified();

// Or specify different site URL
$site = GoogleSearchConsole::sites()->get('https://othersite.com');

URL Inspection

// Inspect URL (uses default site URL)
$result = GoogleSearchConsole::urlInspection()->inspect('https://example.com/page');

// Check if indexed
$isIndexed = GoogleSearchConsole::urlInspection()->isIndexed('https://example.com/page');

// Or specify different site URL
$result = GoogleSearchConsole::urlInspection()->inspect('https://othersite.com', 'https://othersite.com/page');

Token & Site Management

// Set access token
GoogleSearchConsole::setAccessToken($token);

// Set default site URL
GoogleSearchConsole::setSiteUrl('https://example.com');

// Check validity
if (GoogleSearchConsole::isAccessTokenValid()) {
    // Proceed
}

// Get current values
$token = GoogleSearchConsole::getAccessToken();
$siteUrl = GoogleSearchConsole::getSiteUrl();

Error Handling

The package throws \Google\Service\Exception for API errors:

try {
    $data = GoogleSearchConsole::analytics()->query('https://example.com', [
        'start_date' => '2024-01-01',
        'end_date' => '2024-01-31'
    ]);
} catch (\Google\Service\Exception $e) {
    Log::error('GSC Error: ' . $e->getMessage());
}
php artisan vendor:publish --provider="MichaelCrowcroft\\GoogleSearchConsole\\GoogleSearchConsoleServiceProvider" --tag="google-search-console-config"

License

MIT

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-25