במדריך הזה מוסבר על השימוש בספריית הלקוח של Perl עבור Google Ads API.
אתחול הלקוח
כדי להתחיל להשתמש בספרייה, יוצרים מופע של Google::Ads::GoogleAds::Client. אפשר להגדיר את הלקוח בכמה דרכים, כמו שמפורט במדריך ההגדרה. גישה נפוצה היא שימוש בקובץ googleads.properties:
use strict;
use warnings;
use Google::Ads::GoogleAds::Client;
my $api_client = Google::Ads::GoogleAds::Client->new();
יצירת שירות
אחרי שהלקוח מאותחל, אפשר ליצור מופעים של שירותי API שונים. לדוגמה, כדי ליצור אינטראקציה עם קמפיינים, יוצרים מופע של CampaignService:
use strict;
use warnings;
my $campaign_service = $api_client->CampaignService();
כברירת מחדל, $api_client טוען שירותים עבור DEFAULT_API_VERSION של גרסת הספרייה המותקנת (V25 בגרסה האחרונה; גרסאות משניות כמו v25.1 משתמשות במודול של הגרסה הראשית, V25). כדי לטרגט גרסה ספציפית נתמכת של API, מעבירים את version => "V25" אל Client->new({...}) או קוראים ל-$api_client->set_version("V25").
ביצוע קריאה ל-API
דוגמה לשליחת קריאה ל-API כדי לאחזר קמפיינים. בקטע הקוד הזה מוצגת דוגמה לאתחול הלקוח, ליצירת שירות ולקריאה לשיטה.
use strict;
use warnings;
use Google::Ads::GoogleAds::Client;
use Google::Ads::GoogleAds::V25::Services::GoogleAdsService::SearchGoogleAdsRequest;
# Initialize the Google Ads client. See the Configuration guide for 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::V25::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};
}
טיפול בשגיאות
קריאות ל-API עלולות להוביל לשגיאות. ערך ברירת המחדל של die_on_faults הוא 0 (false),
שגורם לשיטות של שירותים להפיק אזהרה ולהחזיר אובייקט Google::Ads::GoogleAds::GoogleAdsException כשמתרחשת שגיאת API. כדי לבדוק אם התשובה שחוזרת היא GoogleAdsException, אפשר לקרוא ל-get_google_ads_failure() ולבדוק את הרשומות של GoogleAdsError.
לחלופין, אם מגדירים את die_on_faults => 1 ב-Client->new({...}) (או שולחים קריאה ל-$api_client->set_die_on_faults(1)), קוד ה-method של השירות שולח קריאה ל-die() עם תגובת השגיאה בפורמט JSON הגולמי כשמתרחשת שגיאה ב-API, ואפשר לטפל בה באמצעות בלוק eval (if ($@)).
use strict;
use warnings;
use Scalar::Util qw(blessed);
use Google::Ads::GoogleAds::Client;
use Google::Ads::GoogleAds::V25::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::V25::Services::GoogleAdsService::SearchGoogleAdsRequest
->new({
customerId => $customer_id,
query => $query,
});
my $search_response = $google_ads_service->search($search_request);
if (blessed($search_response)
&& $search_response->isa("Google::Ads::GoogleAds::GoogleAdsException"))
{
printf "An API exception occurred (HTTP %s, status '%s'): %s\n",
$search_response->get_code(),
$search_response->get_status(),
$search_response->get_message();
my $google_ads_failure = $search_response->get_google_ads_failure();
if ($google_ads_failure) {
foreach my $error (@{$google_ads_failure->{errors} || []}) {
my ($error_type, $error_value) = %{$error->{errorCode} || {}};
printf " Error code (%s): %s\n",
$error_type || "unknown",
$error_value || "UNKNOWN";
printf " Message: %s\n", $error->{message};
}
}
} else {
foreach my $google_ads_row (@{$search_response->{results} || []}) {
printf "Campaign name: %s\n", $google_ads_row->{campaign}->{name};
}
}
דוגמאות נוספות
בספריית הדוגמאות יש כמה דוגמאות קוד שימושיות. ברוב הדוגמאות נדרשים פרמטרים. אפשר להעביר את הפרמטרים כארגומנטים (מומלץ) או לערוך את הערכים של INSERT_XXXXX_HERE בקוד המקור. כדי לראות את הצהרת השימוש לדוגמה, מעבירים את -help כארגומנט בשורת הפקודה.
perl examples/basic_operations/get_campaigns.pl -help