From fa89377df758d0953891113ebb4f7388b588394f Mon Sep 17 00:00:00 2001 From: "Thanet (Knack) Praneenararat" Date: Tue, 25 Dec 2018 21:49:07 +0900 Subject: [PATCH 1/2] Added CreateCustomer example. --- examples/AccountManagement/CreateCustomer.php | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 examples/AccountManagement/CreateCustomer.php diff --git a/examples/AccountManagement/CreateCustomer.php b/examples/AccountManagement/CreateCustomer.php new file mode 100644 index 000000000..637058f0f --- /dev/null +++ b/examples/AccountManagement/CreateCustomer.php @@ -0,0 +1,134 @@ +parseCommandArguments([ + ArgumentNames::CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT + ]); + + // Generate a refreshable OAuth2 credential for authentication. + $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); + + // Construct a Google Ads client configured from a properties file and the + // OAuth2 credentials above. + $googleAdsClient = (new GoogleAdsClientBuilder()) + ->fromFile() + ->withOAuth2Credential($oAuth2Credential) + ->build(); + + try { + self::runExample( + $googleAdsClient, + $options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID + ); + } catch (GoogleAdsException $googleAdsException) { + printf( + "Request with ID '%s' has failed.%sGoogle Ads failure details:%s", + $googleAdsException->getRequestId(), + PHP_EOL, + PHP_EOL + ); + foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) { + /** @var GoogleAdsError $error */ + printf( + "\t%s: %s%s", + $error->getErrorCode()->getErrorCode(), + $error->getMessage(), + PHP_EOL + ); + } + } catch (ApiException $apiException) { + printf( + "ApiException was thrown with message '%s'.%s", + $apiException->getMessage(), + PHP_EOL + ); + } + } + + /** + * Runs the example. + * + * @param GoogleAdsClient $googleAdsClient the Google Ads API client + * @param int $customerId the client customer ID without hyphens + */ + public static function runExample(GoogleAdsClient $googleAdsClient, $customerId) + { + $customer = new Customer([ + 'resource_name' => ResourceNames::forCustomer($customerId), + 'descriptive_name' => new StringValue( + ['value' => 'Account created with CustomerService on ' . date('Ymd h:i:s')] + ), + // For a list of valid currency codes and time zones see this documentation: + // https://developers.google.com/adwords/api/docs/appendix/codes-formats. + 'currency_code' => new StringValue(['value' => 'USD']), + 'time_zone' => new StringValue(['value' => 'America/New_York']), + // The below values are optional. For more information about URL + // options see: https://support.google.com/google-ads/answer/6305348. + 'tracking_url_template' => new StringValue(['value' => '{lpurl}?device={device}']), + 'final_url_suffix' => new StringValue([ + 'value' => 'keyword={keyword}&matchtype={matchtype}&adgroupid={adgroupid}' + ]), + 'has_partners_badge' => new BoolValue(['value' => false]) + ]); + + // Issues a mutate request to create an account + $customerServiceClient = $googleAdsClient->getCustomerServiceClient(); + $response = $customerServiceClient->createCustomerClient($customerId, $customer); + + printf( + 'Created a customer with resource name "%s" under the manager account with ' + . 'customer ID %d.%s', + $response->getResourceName(), + $customerId, + PHP_EOL + ); + } +} + +CreateCustomer::main(); From 54c82145f89a478bbc80a16e85fda2d0b92fad5f Mon Sep 17 00:00:00 2001 From: "Thanet (Knack) Praneenararat" Date: Wed, 26 Dec 2018 17:04:09 +0900 Subject: [PATCH 2/2] Use the word managerCustomerId for better clarification --- examples/AccountManagement/CreateCustomer.php | 16 +++++++--------- examples/Utils/ArgumentNames.php | 2 ++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/AccountManagement/CreateCustomer.php b/examples/AccountManagement/CreateCustomer.php index 637058f0f..f39423aaa 100644 --- a/examples/AccountManagement/CreateCustomer.php +++ b/examples/AccountManagement/CreateCustomer.php @@ -26,7 +26,6 @@ use Google\Ads\GoogleAds\Lib\GoogleAdsClientBuilder; use Google\Ads\GoogleAds\Lib\GoogleAdsException; use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; -use Google\Ads\GoogleAds\Util\ResourceNames; use Google\Ads\GoogleAds\V0\Errors\GoogleAdsError; use Google\Ads\GoogleAds\V0\Resources\Customer; use Google\ApiCore\ApiException; @@ -41,14 +40,14 @@ */ class CreateCustomer { - const CUSTOMER_ID = 'INSERT_CUSTOMER_ID_HERE'; + const MANAGER_CUSTOMER_ID = 'INSERT_MANAGER_CUSTOMER_ID_HERE'; public static function main() { // Either pass the required parameters for this example on the command line, or insert them // into the constants above. $options = (new ArgumentParser())->parseCommandArguments([ - ArgumentNames::CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT + ArgumentNames::MANAGER_CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT ]); // Generate a refreshable OAuth2 credential for authentication. @@ -64,7 +63,7 @@ public static function main() try { self::runExample( $googleAdsClient, - $options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID + $options[ArgumentNames::MANAGER_CUSTOMER_ID] ?: self::MANAGER_CUSTOMER_ID ); } catch (GoogleAdsException $googleAdsException) { printf( @@ -95,12 +94,11 @@ public static function main() * Runs the example. * * @param GoogleAdsClient $googleAdsClient the Google Ads API client - * @param int $customerId the client customer ID without hyphens + * @param int $managerCustomerId the manager customer ID without hyphens */ - public static function runExample(GoogleAdsClient $googleAdsClient, $customerId) + public static function runExample(GoogleAdsClient $googleAdsClient, $managerCustomerId) { $customer = new Customer([ - 'resource_name' => ResourceNames::forCustomer($customerId), 'descriptive_name' => new StringValue( ['value' => 'Account created with CustomerService on ' . date('Ymd h:i:s')] ), @@ -119,13 +117,13 @@ public static function runExample(GoogleAdsClient $googleAdsClient, $customerId) // Issues a mutate request to create an account $customerServiceClient = $googleAdsClient->getCustomerServiceClient(); - $response = $customerServiceClient->createCustomerClient($customerId, $customer); + $response = $customerServiceClient->createCustomerClient($managerCustomerId, $customer); printf( 'Created a customer with resource name "%s" under the manager account with ' . 'customer ID %d.%s', $response->getResourceName(), - $customerId, + $managerCustomerId, PHP_EOL ); } diff --git a/examples/Utils/ArgumentNames.php b/examples/Utils/ArgumentNames.php index c632e693b..6a60debe9 100644 --- a/examples/Utils/ArgumentNames.php +++ b/examples/Utils/ArgumentNames.php @@ -41,6 +41,7 @@ final class ArgumentNames const LOCALE = 'locale'; const LOCATION_ID = 'locationId'; const LOCATION_NAMES = 'locationNames'; + const MANAGER_CUSTOMER_ID = 'managerCustomerId'; const MERCHANT_CENTER_ACCOUNT_ID = 'merchantCenterAccountId'; const PERCENT_CPC_BID_MICRO_AMOUNT = 'percentCpcBidMicroAmount'; const RECOMMENDATION_ID = 'recommendationId'; @@ -67,6 +68,7 @@ final class ArgumentNames self::LOCALE => 'The locale', self::LOCATION_ID => 'The location ID', self::LOCATION_NAMES => 'The list of location names', + self::MANAGER_CUSTOMER_ID => 'The manager customer ID', self::MERCHANT_CENTER_ACCOUNT_ID => 'The Merchant center account ID', self::PERCENT_CPC_BID_MICRO_AMOUNT => 'The CPC bid micro amount for the Percent CPC bidding strategy',