Net::Google::WebmasterTools - Simple interface to the Google Webmaster Tools (Search Console) Core Reporting API
version 0.03
use Net::Google::WebmasterTools; use Net::Google::WebmasterTools::OAuth2; # Insert your website URL here. my $site_url = "http://www.example.com/"; # See GETTING STARTED for how to get a client id, client secret, and # refresh token my $client_id = "123456789012.apps.googleusercontent.com"; my $client_secret = "rAnDoMsEcReTrAnDoMsEcReT"; my $refresh_token = "RaNdOmSeCrEtRaNdOmSeCrEt"; my $wmt = Net::Google::WebmasterTools->new; # Authenticate my $oauth = Net::Google::WebmasterTools::OAuth2->new( client_id => $client_id, client_secret => $client_secret, ); my $token = $oauth->refresh_access_token($refresh_token); $wmt->token($token); # Build request my $req = $wmt->new_request( site_url => "$site_url", report_name => "searchAnalytics", method => "query", dimensions => ['country','device'], search_type => 'web', start_date => "2019-05-01", end_date => "2019-05-31", row_limit => 1000, ); # Send request my $res = $wmt->retrieve($req); die("GWMT error: " . $res->error_message) if !$res->is_success; # Print results for my $row (@{ $res->{'rows'} }) { print "Query: ", $row->{'keys'}->[0], "\n", Position: ", $row->{'position'}, " | ", Impressions: ", $row->{'impressions'}, " | ", Clicks: ", $row->{'clicks'}, " | ", CTR: ", $row->{'ctr'}, "\n---\n"; }
This module provides a simple, straight-forward interface to the Google Webmaster Tools (Search Console) API, Version 3.
See https://developers.google.com/webmaster-tools/v3/parameters for the complete API documentation.
This module is heavily based on Nick Wellnhofer's Net::Google::Analytics module
Net::Google::WebmasterTools - Simple interface to the Google Webmaster Tools (Search Console) API
Net::Google::WebmasterTools::OAuth2 provides for easy authentication and authorization using OAuth 2.0. First, you have to register your application through the Google APIs Console.
You will receive a client id and a client secret for your application in the APIs Console. For command line testing, you should use "Installed application" as application type. Then you can obtain a refresh token for your application by running the following script with your client id and secret:
use Net::Google::WebmasterTools::OAuth2; my $oauth = Net::Google::WebmasterTools::OAuth2->new( client_id => 'Your client id', client_secret => 'Your client secret', ); $oauth->interactive;
The script will display a URL and prompt for a code. Visit the URL in a browser and follow the directions to grant access to your application. You will be shown the code that you should enter in the Perl script. Then the script retrieves and prints a refresh token which can be used for non-interactive access.
my $wmt = Net::Google::WebmasterTools->new;
The constructor doesn't take any arguments.
$wmt->auth_params(@auth_params);
Set the raw authentication parameters as key/value pairs. These will we send as HTTP headers.
$wmt->token($token);
Authenticate using a token returned from Net::Google::WebmasterTools::OAuth2.
my $req = $wmt->new_request(param => $value, ...);
Creates and returns a new Net::Google::WebmasterTools::Request object.
Valid combinations of report_name and method:
Search traffic analytics data (documentation)
report_name => 'searchAnalytics', method => 'query'
XML Sitemaps reports (documentation)
report_name => 'sitemaps', method => 'list' # or delete, get, submit
report_name => '', method => 'add' # or delete, get, list
Counts of crawl errors (documentation)
report_name => 'urlCrawlErrorsCounts', method => 'query'
Information about specific crawl errors (documentation)
report_name => 'urlCrawlErrorsSamples', method => 'get' # or list, markAsFixed
my $res = $wmt->retrieve($req);
Sends the request. $req must be a Net::Google::WebmasterTools::Request object. This method returns a Net::Google::WebmasterTools::Response object.
my $res = $wmt->retrieve_http($req);
Sends the request and returns an HTTP::Response object. $req must be a Net::Google::WebmasterTools::Request object.
my $uri = $wmt->uri($req);
Returns the URI of the request. $req must be a Net::Google::WebmasterTools::Request object. This method returns a URI object.
$wmt->user_agent($ua);
Sets the LWP::UserAgent object to use for HTTP(S) requests. You only have to call this method if you want to provide your own user agent.
Rob Hammond <contact@rjh.am>, Nick Wellnhofer <wellnhofer@aevum.de>
This software is copyright (c) 2012 by Nick Wellnhofer.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
This software is copyright (c) 2015 by Nick Wellnhofer.
To install Net::Google::WebmasterTools, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::Google::WebmasterTools
CPAN shell
perl -MCPAN -e shell install Net::Google::WebmasterTools
For more information on module installation, please visit the detailed CPAN module installation guide.