במדריך הזה מוסברים העקרונות הבסיסיים של השימוש בספריית הלקוח 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 של גרסת הספרייה המותקנת (V26 בגרסה האחרונה; גרסאות משניות כמו v25.1 משתמשות במודול הגרסה הראשית שלהן, V25). כדי לטרגט גרסת API נתמכת ספציפית, מעבירים את version => "V26" אל Client->new({...}) או קוראים ל-$api_client->set_version("V26").
שליחת קריאה ל-API
דוגמה לקריאה ל-API כדי לקבל קמפיינים. קטע הקוד הזה מדגים איך להפעיל את הלקוח, ליצור שירות ולקרוא ל-method.
use strict;
use warnings;
use Google::Ads::GoogleAds::Client;
use Google::Ads::GoogleAds::V26::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::V26::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), מה שגורם ל-methods של השירות להציג אזהרה ולהחזיר אובייקט 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::V26::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::V26::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