This guide explains the basics of using the Perl client library for the Google Ads API.
Initialize the client
To start using the library, create an instance of Google::Ads::GoogleAds::Client
. The client can be configured in several ways,
as detailed in the Configuration guide
. A common approach is to
use a googleads.properties
file:
use
Google::Ads::GoogleAds::Client
;
my
$api_client
=
Google::Ads::GoogleAds::
Client
-
> new
();
Create a service
Once the client is initialized, you can create instances of the various API
services. For example, to interact with campaigns, you would create a CampaignService
instance:
my
$campaign_service
=
$api_client
-
> CampaignService
();
Make an API call
Here's an example of how to make an API call to get campaigns. This snippet demonstrates initializing the client, creating a service, and calling a method.
use
strict
;
use
warnings
;
use
Google::Ads::GoogleAds::Client
;
use
Google::Ads::GoogleAds::V22::Services::GoogleAdsService::SearchGoogleAdsRequest
;
# Initialize the Google Ads client. See the [Configuration guide](configuration) for more options.
my
$api_client
=
Google::Ads::GoogleAds::
Client
-
> new
();
# Get the GoogleAdsService.
my
$google_ads_service
=
$api_client
-
> GoogleAdsService
();
# Specify the customer ID and query.
my
$customer_id
=
" INSERT_CUSTOMER_ID_HERE
"
;
my
$query
=
"SELECT campaign.name, campaign.status FROM campaign"
;
# Create a search request.
my
$search_request
=
Google::Ads::GoogleAds::V22::Services::GoogleAdsService::
SearchGoogleAdsRequest
-
> new
({
customerId
=
>
$customer_id
,
query
=
>
$query
});
# Execute the search request.
my
$search_response
=
$google_ads_service
-
> search
(
$search_request
);
# Iterate over the results and print campaign names.
foreach
my
$google_ads_row
(
@
{
$search_response
-
> {
results
}})
{
printf
"Campaign with resource name '%s' and name '%s'.\n"
,
$google_ads_row
-
> {
campaign
}
-
> {
resourceName
},
$google_ads_row
-
> {
campaign
}
-
> {
name
};
}
Handle errors
API calls can result in exceptions. You should wrap your API calls in eval
blocks to catch and handle Google::Ads::GoogleAds::GoogleAdsException
.
use
strict
;
use
warnings
;
use
Google::Ads::GoogleAds::Client
;
use
Google::Ads::GoogleAds::GoogleAdsException
;
use
Google::Ads::GoogleAds::V22::Services::GoogleAdsService::SearchGoogleAdsRequest
;
my
$api_client
=
Google::Ads::GoogleAds::
Client
-
> new
();
my
$google_ads_service
=
$api_client
-
> GoogleAdsService
();
my
$customer_id
=
" INSERT_CUSTOMER_ID_HERE
"
;
my
$query
=
"SELECT campaign.name, campaign.status FROM campaign"
;
my
$search_request
=
Google::Ads::GoogleAds::V22::Services::GoogleAdsService::
SearchGoogleAdsRequest
-
> new
({
customerId
=
>
$customer_id
,
query
=
>
$query
});
eval
{
my
$search_response
=
$google_ads_service
-
> search
(
$search_request
);
# Process successful response.
foreach
my
$google_ads_row
(
@
{
$search_response
-
> {
results
}})
{
printf
"Campaign name: %s\n"
,
$google_ads_row
-
> {
campaign
}
-
> {
name
};
}
};
if
(
$@
)
{
if
(
blessed
(
$@
)
&&
$@
-
> isa
(
'Google::Ads::GoogleAds::GoogleAdsException'
))
{
my
$exception
=
$@
;
printf
"A Google Ads exception occurred:\n"
;
printf
" Request ID: %s\n"
,
$exception
-
> get_request_id
();
foreach
my
$error
(
@
{
$exception
-
> get_errors
()})
{
printf
" Error code: %s\n"
,
$error
-
> {
errorCode
};
printf
" Message: %s\n"
,
$error
-
> {
message
};
}
}
else
{
# Print other types of exceptions.
print
"An error occurred: $@\n"
;
}
}
Explore other examples
You can find several useful code samples in the examples
.
Most of the examples require parameters. You can either pass the parameters as
arguments (recommended) or edit the INSERT_XXXXX_HERE
values in the source
code. To see a usage statement for an example, pass -help
as a command-line
argument.
perl
examples/basic_operations/get_campaigns.pl
-help

