Google Search Console (GSC) is a powerful tool for website owners and digital marketers to monitor and optimize website performance on Google Search. Among its many features, the URL Inspection tool provides detailed insights into how Google views specific URLs, making it invaluable for diagnosing crawling and indexing issues. With the Google Search Console API, you can automate and scale URL inspections, saving time and uncovering insights more efficiently. This blog explores how to optimize URL inspection using the GSC API.
Why Automate URL Inspection?
Manually inspecting URLs via the Google Search Console web interface is time-consuming, especially for large websites. By leveraging the GSC API, you can:
- Automate repetitive tasks: Run bulk inspections for a list of URLs programmatically.
- Identify trends: Analyze patterns in indexing or crawling issues across your website.
- Integrate with workflows: Build custom tools or dashboards to monitor URL statuses.
- Save time: Focus on resolving issues rather than manually gathering data.
Key Features of the URL Inspection API
The URL Inspection API, introduced by Google, allows developers to:
- Retrieve the indexing status of a URL.
- Check for coverage issues, such as crawl errors or blocked resources.
- Determine if a URL is mobile-friendly or adheres to core web vitals.
- Identify canonicalization details, including user-declared and Google-selected canonicals.
- Fetch robots.txt status.
Analyze the Results
The API response contains valuable details, including:
- Indexing status: Whether the URL is indexed or not.
- Last crawl time: When Google last crawled the URL.
- Crawl issues: Errors or warnings encountered during crawling.
- Robots.txt status: Whether the URL is eligible for crawling
Export this data into a database and can visualize to identify patterns and prioritize fixes.
Sample PHP code :
<?php
function inspectUrl($url, $siteUrl) {
try {
// Initialize the Google API Client
$client = new Client();
$client->setAuthConfig('credentials.json'); // Path to your credentials.json file
$client->addScope('https://www.googleapis.com/auth/webmasters');
$client->setAccessType('offline');
// Initialize the Search Console API Service
$searchConsoleService = new SearchConsole($client);
// Create the Inspect URL Index Request
$inspectRequest = new SearchConsole\InspectUrlIndexRequest([
'inspectionUrl' => $url, // URL to inspect
'siteUrl' => $siteUrl, // Property URL (e.g., https://example.com)
'languageCode' => 'en', // Language (optional)
]);
// Call the API
$response = $searchConsoleService->urlInspection_index->inspect($inspectRequest);
// Display the response
echo "Inspection Results for URL: " . $url . PHP_EOL;
print_r($response);
} catch (Exception $e) {
// Handle exceptions
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
}
// Example Usage
$urlToInspect = 'https://example.com/page-to-inspect';
$propertyUrl = 'https://example.com';
inspectUrl($urlToInspect, $propertyUrl);