diff --git a/ChangeLog.md b/ChangeLog.md index 62549f7c5..96b846b26 100755 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,9 @@ +### 18.1.0 + +AdWords: + - Added support and examples for v201705. + - Removed support for AdWords Express (AWX) API. + ### 18.0.0 DFP: diff --git a/examples/AdWords/v201607/Express/AddPromotion.php b/examples/AdWords/v201607/Express/AddPromotion.php deleted file mode 100755 index dfcd34779..000000000 --- a/examples/AdWords/v201607/Express/AddPromotion.php +++ /dev/null @@ -1,122 +0,0 @@ -SetExpressBusinessId($businessId); - - // Get the promotion service. - $promotionService = $user->GetService('PromotionService', - ADWORDS_VERSION); - - // Set up the new Promotion. - $marsTourPromotion = new Promotion(); - $budget = new Money(); - $budget->microAmount = 1000000; - $marsTourPromotion->name = 'Mars Tour Promotion ' . uniqid(); - $marsTourPromotion->status = 'PAUSED'; - $marsTourPromotion->destinationUrl = 'http://www.example.com'; - $marsTourPromotion->budget = $budget; - $marsTourPromotion->callTrackingEnabled = true; - - // Criteria - $criteria = array(); - - // Criterion - Travel Agency product service - $productService = new ProductService(); - $productService->text = 'Travel Agency'; - $criteria[] = $productService; - - // Criterion - English language - // The ID can be found in the documentation: - // https://developers.google.com/adwords/api/docs/appendix/languagecodes - $language = new Language(); - $language->id = 1000; - $criteria[] = $language; - - // Criterion - City of California - $location = new Location(); - $location->id = 21137; - $criteria[] = $location; - - $marsTourPromotion->criteria = $criteria; - - // Creatives - $creatives = array(); - - $creative1 = new Creative('Standard Mars Trip', 'Fly coach to Mars', - 'Free in-flight pretzels'); - $creatives[] = $creative1; - - $creative2 = new Creative('Deluxe Mars Trip', 'Fly first class to Mars', - 'Unlimited powdered orange drink'); - $creatives[] = $creative2; - - $marsTourPromotion->creatives = $creatives; - - $operations = array(); - - $operation = new PromotionOperation(); - $operation->operand = $marsTourPromotion; - $operation->operator = 'ADD'; - $operations[] = $operation; - - $result = $promotionService->mutate($operations); - $addedPromotion = $result[0]; - - printf("Added promotion ID %d with name '%s' to business ID %d\n", - $addedPromotion->id, $addedPromotion->name, $businessId); -} - -// Don't run the example if the file is being included. -if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { - return; -} - -try { - // Get AdWordsUser from credentials in "../auth.ini" - // relative to the AdWordsUser.php file's directory. - $user = new AdWordsUser(); - - // Log every SOAP XML request and response. - $user->LogAll(); - - // Run the example. - AddPromotionExample($user, $businessId); -} catch (Exception $e) { - printf("An error has occurred: %s\n", $e->getMessage()); -} diff --git a/examples/AdWords/v201607/Express/GetBudgetSuggestion.php b/examples/AdWords/v201607/Express/GetBudgetSuggestion.php deleted file mode 100755 index c84610f62..000000000 --- a/examples/AdWords/v201607/Express/GetBudgetSuggestion.php +++ /dev/null @@ -1,112 +0,0 @@ -GetService('BudgetSuggestionService', - ADWORDS_VERSION); - - $criteria = array(); - - // Create selector. - $selector = new BudgetSuggestionSelector(); - - // Criterion - Travel Agency product/service. - // See GetProductServicesExample.php for an example of how to get valid - // product/service settings. - $productService = new ProductService(); - $productService->text = "Travel Agency"; - $productService->locale = "en_US"; - $criteria[] = $productService; - - // Criterion - English language. - // The ID can be found in the documentation: - // https://developers.google.com/adwords/api/docs/appendix/languagecodes - $language = new Language(); - $language->id = 1000; - $criteria[] = $language; - - // Criterion - Mountain View, California location. - // The ID can be found in the documentation: - // https://developers.google.com/adwords/api/docs/appendix/geotargeting - // https://developers.google.com/adwords/api/docs/appendix/cities-DMAregions - $location = new Location(); - $location->id = 1014044; - $criteria[] = $location; - - $selector->criteria = $criteria; - - $budgetSuggestion = $budgetSuggestionService->get($selector); - - printf( - "Budget suggestion for criteria is:\n" . - " SuggestedBudget=%s\n" . - " Min/MaxBudget=%s/%s\n" . - " Min/MaxCpc=%s/%s\n" . - " CPM=%s\n" . - " CPC=%s\n" . - " Impressions=%d\n", - toString($budgetSuggestion->suggestedBudget), - toString($budgetSuggestion->minBudget), - toString($budgetSuggestion->maxBudget), - toString($budgetSuggestion->minCpc), - toString($budgetSuggestion->maxCpc), - toString($budgetSuggestion->cpm), - toString($budgetSuggestion->cpc), - $budgetSuggestion->impressions - ); -} - -function toString($money) { - if (!$money) return ''; - return strval($money->microAmount); -} - -// Don't run the example if the file is being included. -if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { - return; -} - -try { - // Get AdWordsUser from credentials in "../auth.ini" - // relative to the AdWordsUser.php file's directory. - $user = new AdWordsUser(); - - // Log every SOAP XML request and response. - $user->LogAll(); - - // Run the example. - GetBudgetSuggestionExample($user); -} catch (Exception $e) { - printf("An error has occurred: %s\n", $e->getMessage()); -} diff --git a/examples/AdWords/v201609/Express/AddExpressBusinesses.php b/examples/AdWords/v201609/Express/AddExpressBusinesses.php deleted file mode 100755 index cbc2d12fc..000000000 --- a/examples/AdWords/v201609/Express/AddExpressBusinesses.php +++ /dev/null @@ -1,87 +0,0 @@ -GetService('ExpressBusinessService', - ADWORDS_VERSION); - - $business1 = new ExpressBusiness(); - $business1->status = 'ENABLED'; - $business1->name = 'Express Interplanetary Cruise #' . uniqid(); - $business1->website = 'http://www.example.com/cruise1'; - - $business2 = new ExpressBusiness(); - $business2->status = 'ENABLED'; - $business2->name = 'Express Interplanetary Cruise #' . uniqid(); - $business2->website = 'http://www.example.com/cruise2'; - - $operations = array(); - - $operation1 = new ExpressBusinessOperation(); - $operation1->operand = $business1; - $operation1->operator = 'ADD'; - $operations[] = $operation1; - - $operation2 = new ExpressBusinessOperation(); - $operation2->operand = $business2; - $operation2->operator = 'ADD'; - $operations[] = $operation2; - - $addedBusinesses = $businessService->mutate($operations); - - foreach($addedBusinesses as $addedBusiness) { - printf("Added express business with ID %d and name '%s'\n", - $addedBusiness->id, $addedBusiness->name); - } -} - -// Don't run the example if the file is being included. -if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { - return; -} - -try { - // Get AdWordsUser from credentials in "../auth.ini" - // relative to the AdWordsUser.php file's directory. - $user = new AdWordsUser(); - - // Log every SOAP XML request and response. - $user->LogAll(); - - // Run the example. - AddExpressBusinessesExample($user); -} catch (Exception $e) { - printf("An error has occurred: %s\n", $e->getMessage()); -} diff --git a/examples/AdWords/v201609/Express/AddPromotion.php b/examples/AdWords/v201609/Express/AddPromotion.php deleted file mode 100755 index 17896aabb..000000000 --- a/examples/AdWords/v201609/Express/AddPromotion.php +++ /dev/null @@ -1,115 +0,0 @@ -SetExpressBusinessId($businessId); - - // Get the promotion service. - $promotionService = $user->GetService('PromotionService', - ADWORDS_VERSION); - - // Set up the new Promotion. - $marsTourPromotion = new Promotion(); - $budget = new Money(); - $budget->microAmount = 1000000; - $marsTourPromotion->name = 'Mars Tour Promotion ' . uniqid(); - $marsTourPromotion->status = 'PAUSED'; - $marsTourPromotion->destinationUrl = 'http://www.example.com'; - $marsTourPromotion->budget = $budget; - $marsTourPromotion->callTrackingEnabled = true; - - // Criteria - $criteria = array(); - - // Criterion - Travel Agency product service - $productService = new ProductService(); - $productService->text = 'Travel Agency'; - $criteria[] = $productService; - - // Criterion - English language - // The ID can be found in the documentation: - // https://developers.google.com/adwords/api/docs/appendix/languagecodes - $language = new Language(); - $language->id = 1000; - $criteria[] = $language; - - // Criterion - City of California - $location = new Location(); - $location->id = 21137; - $criteria[] = $location; - - $marsTourPromotion->criteria = $criteria; - - // Expanded creative - $expandedCreative = new ExpandedCreative( - 'Standard Mars Trip', 'Fly coach to Mars', 'Free in-flight pretzels'); - - $marsTourPromotion->expandedCreative = $expandedCreative; - - $operations = array(); - - $operation = new PromotionOperation(); - $operation->operand = $marsTourPromotion; - $operation->operator = 'ADD'; - $operations[] = $operation; - - $result = $promotionService->mutate($operations); - $addedPromotion = $result[0]; - - printf("Added promotion ID %d with name '%s' to business ID %d\n", - $addedPromotion->id, $addedPromotion->name, $businessId); -} - -// Don't run the example if the file is being included. -if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { - return; -} - -try { - // Get AdWordsUser from credentials in "../auth.ini" - // relative to the AdWordsUser.php file's directory. - $user = new AdWordsUser(); - - // Log every SOAP XML request and response. - $user->LogAll(); - - // Run the example. - AddPromotionExample($user, $businessId); -} catch (Exception $e) { - printf("An error has occurred: %s\n", $e->getMessage()); -} diff --git a/examples/AdWords/v201609/Express/GetBudgetSuggestion.php b/examples/AdWords/v201609/Express/GetBudgetSuggestion.php deleted file mode 100755 index 1c3ae86a2..000000000 --- a/examples/AdWords/v201609/Express/GetBudgetSuggestion.php +++ /dev/null @@ -1,112 +0,0 @@ -GetService('BudgetSuggestionService', - ADWORDS_VERSION); - - $criteria = array(); - - // Create selector. - $selector = new BudgetSuggestionSelector(); - - // Criterion - Travel Agency product/service. - // See GetProductServicesExample.php for an example of how to get valid - // product/service settings. - $productService = new ProductService(); - $productService->text = "Travel Agency"; - $productService->locale = "en_US"; - $criteria[] = $productService; - - // Criterion - English language. - // The ID can be found in the documentation: - // https://developers.google.com/adwords/api/docs/appendix/languagecodes - $language = new Language(); - $language->id = 1000; - $criteria[] = $language; - - // Criterion - Mountain View, California location. - // The ID can be found in the documentation: - // https://developers.google.com/adwords/api/docs/appendix/geotargeting - // https://developers.google.com/adwords/api/docs/appendix/cities-DMAregions - $location = new Location(); - $location->id = 1014044; - $criteria[] = $location; - - $selector->criteria = $criteria; - - $budgetSuggestion = $budgetSuggestionService->get($selector); - - printf( - "Budget suggestion for criteria is:\n" . - " SuggestedBudget=%s\n" . - " Min/MaxBudget=%s/%s\n" . - " Min/MaxCpc=%s/%s\n" . - " CPM=%s\n" . - " CPC=%s\n" . - " Impressions=%d\n", - toString($budgetSuggestion->suggestedBudget), - toString($budgetSuggestion->minBudget), - toString($budgetSuggestion->maxBudget), - toString($budgetSuggestion->minCpc), - toString($budgetSuggestion->maxCpc), - toString($budgetSuggestion->cpm), - toString($budgetSuggestion->cpc), - $budgetSuggestion->impressions - ); -} - -function toString($money) { - if (!$money) return ''; - return strval($money->microAmount); -} - -// Don't run the example if the file is being included. -if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { - return; -} - -try { - // Get AdWordsUser from credentials in "../auth.ini" - // relative to the AdWordsUser.php file's directory. - $user = new AdWordsUser(); - - // Log every SOAP XML request and response. - $user->LogAll(); - - // Run the example. - GetBudgetSuggestionExample($user); -} catch (Exception $e) { - printf("An error has occurred: %s\n", $e->getMessage()); -} diff --git a/examples/AdWords/v201702/Express/AddExpressBusinesses.php b/examples/AdWords/v201702/Express/AddExpressBusinesses.php deleted file mode 100755 index bbd0432a7..000000000 --- a/examples/AdWords/v201702/Express/AddExpressBusinesses.php +++ /dev/null @@ -1,87 +0,0 @@ -GetService('ExpressBusinessService', - ADWORDS_VERSION); - - $business1 = new ExpressBusiness(); - $business1->status = 'ENABLED'; - $business1->name = 'Express Interplanetary Cruise #' . uniqid(); - $business1->website = 'http://www.example.com/cruise1'; - - $business2 = new ExpressBusiness(); - $business2->status = 'ENABLED'; - $business2->name = 'Express Interplanetary Cruise #' . uniqid(); - $business2->website = 'http://www.example.com/cruise2'; - - $operations = array(); - - $operation1 = new ExpressBusinessOperation(); - $operation1->operand = $business1; - $operation1->operator = 'ADD'; - $operations[] = $operation1; - - $operation2 = new ExpressBusinessOperation(); - $operation2->operand = $business2; - $operation2->operator = 'ADD'; - $operations[] = $operation2; - - $addedBusinesses = $businessService->mutate($operations); - - foreach($addedBusinesses as $addedBusiness) { - printf("Added express business with ID %d and name '%s'\n", - $addedBusiness->id, $addedBusiness->name); - } -} - -// Don't run the example if the file is being included. -if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { - return; -} - -try { - // Get AdWordsUser from credentials in "../auth.ini" - // relative to the AdWordsUser.php file's directory. - $user = new AdWordsUser(); - - // Log every SOAP XML request and response. - $user->LogAll(); - - // Run the example. - AddExpressBusinessesExample($user); -} catch (Exception $e) { - printf("An error has occurred: %s\n", $e->getMessage()); -} diff --git a/examples/AdWords/v201702/Express/AddPromotion.php b/examples/AdWords/v201702/Express/AddPromotion.php deleted file mode 100755 index 002414cb0..000000000 --- a/examples/AdWords/v201702/Express/AddPromotion.php +++ /dev/null @@ -1,115 +0,0 @@ -SetExpressBusinessId($businessId); - - // Get the promotion service. - $promotionService = $user->GetService('PromotionService', - ADWORDS_VERSION); - - // Set up the new Promotion. - $marsTourPromotion = new Promotion(); - $budget = new Money(); - $budget->microAmount = 1000000; - $marsTourPromotion->name = 'Mars Tour Promotion ' . uniqid(); - $marsTourPromotion->status = 'PAUSED'; - $marsTourPromotion->destinationUrl = 'http://www.example.com'; - $marsTourPromotion->budget = $budget; - $marsTourPromotion->callTrackingEnabled = true; - - // Criteria - $criteria = array(); - - // Criterion - Travel Agency product service - $productService = new ProductService(); - $productService->text = 'Travel Agency'; - $criteria[] = $productService; - - // Criterion - English language - // The ID can be found in the documentation: - // https://developers.google.com/adwords/api/docs/appendix/languagecodes - $language = new Language(); - $language->id = 1000; - $criteria[] = $language; - - // Criterion - City of California - $location = new Location(); - $location->id = 21137; - $criteria[] = $location; - - $marsTourPromotion->criteria = $criteria; - - // Expanded creative - $expandedCreative = new ExpandedCreative( - 'Standard Mars Trip', 'Fly coach to Mars', 'Free in-flight pretzels'); - - $marsTourPromotion->expandedCreative = $expandedCreative; - - $operations = array(); - - $operation = new PromotionOperation(); - $operation->operand = $marsTourPromotion; - $operation->operator = 'ADD'; - $operations[] = $operation; - - $result = $promotionService->mutate($operations); - $addedPromotion = $result[0]; - - printf("Added promotion ID %d with name '%s' to business ID %d\n", - $addedPromotion->id, $addedPromotion->name, $businessId); -} - -// Don't run the example if the file is being included. -if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { - return; -} - -try { - // Get AdWordsUser from credentials in "../auth.ini" - // relative to the AdWordsUser.php file's directory. - $user = new AdWordsUser(); - - // Log every SOAP XML request and response. - $user->LogAll(); - - // Run the example. - AddPromotionExample($user, $businessId); -} catch (Exception $e) { - printf("An error has occurred: %s\n", $e->getMessage()); -} diff --git a/examples/AdWords/v201702/Express/GetBudgetSuggestion.php b/examples/AdWords/v201702/Express/GetBudgetSuggestion.php deleted file mode 100755 index fb888336b..000000000 --- a/examples/AdWords/v201702/Express/GetBudgetSuggestion.php +++ /dev/null @@ -1,112 +0,0 @@ -GetService('BudgetSuggestionService', - ADWORDS_VERSION); - - $criteria = array(); - - // Create selector. - $selector = new BudgetSuggestionSelector(); - - // Criterion - Travel Agency product/service. - // See GetProductServicesExample.php for an example of how to get valid - // product/service settings. - $productService = new ProductService(); - $productService->text = "Travel Agency"; - $productService->locale = "en_US"; - $criteria[] = $productService; - - // Criterion - English language. - // The ID can be found in the documentation: - // https://developers.google.com/adwords/api/docs/appendix/languagecodes - $language = new Language(); - $language->id = 1000; - $criteria[] = $language; - - // Criterion - Mountain View, California location. - // The ID can be found in the documentation: - // https://developers.google.com/adwords/api/docs/appendix/geotargeting - // https://developers.google.com/adwords/api/docs/appendix/cities-DMAregions - $location = new Location(); - $location->id = 1014044; - $criteria[] = $location; - - $selector->criteria = $criteria; - - $budgetSuggestion = $budgetSuggestionService->get($selector); - - printf( - "Budget suggestion for criteria is:\n" . - " SuggestedBudget=%s\n" . - " Min/MaxBudget=%s/%s\n" . - " Min/MaxCpc=%s/%s\n" . - " CPM=%s\n" . - " CPC=%s\n" . - " Impressions=%d\n", - toString($budgetSuggestion->suggestedBudget), - toString($budgetSuggestion->minBudget), - toString($budgetSuggestion->maxBudget), - toString($budgetSuggestion->minCpc), - toString($budgetSuggestion->maxCpc), - toString($budgetSuggestion->cpm), - toString($budgetSuggestion->cpc), - $budgetSuggestion->impressions - ); -} - -function toString($money) { - if (!$money) return ''; - return strval($money->microAmount); -} - -// Don't run the example if the file is being included. -if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { - return; -} - -try { - // Get AdWordsUser from credentials in "../auth.ini" - // relative to the AdWordsUser.php file's directory. - $user = new AdWordsUser(); - - // Log every SOAP XML request and response. - $user->LogAll(); - - // Run the example. - GetBudgetSuggestionExample($user); -} catch (Exception $e) { - printf("An error has occurred: %s\n", $e->getMessage()); -} diff --git a/examples/AdWords/v201702/Express/UpdatePromotion.php b/examples/AdWords/v201702/Express/UpdatePromotion.php deleted file mode 100755 index fbf146be0..000000000 --- a/examples/AdWords/v201702/Express/UpdatePromotion.php +++ /dev/null @@ -1,86 +0,0 @@ -SetExpressBusinessId($businessId); - - // Get the service, which loads the required classes. - $promotionService = $user->GetService('PromotionService', - ADWORDS_VERSION); - - // Update the budget for the promotion. - $promotion = new Promotion(); - $promotion->id = $promotionId; - $newBudget = new Money(); - $newBudget->microAmount = 2000000; - $promotion->budget = $newBudget; - - $operations = array(); - - $operation = new PromotionOperation(); - $operation->operand = $promotion; - $operation->operator = 'SET'; - $operations[] = $operation; - - $result = $promotionService->mutate($operations); - $mutatedPromotion = $result[0]; - - printf("Promotion ID %d for business ID %d now has budget micro amount %d\n", - $mutatedPromotion->id, $businessId, - $mutatedPromotion->budget->microAmount); -} - -// Don't run the example if the file is being included. -if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { - return; -} - -try { - // Get AdWordsUser from credentials in "../auth.ini" - // relative to the AdWordsUser.php file's directory. - $user = new AdWordsUser(); - - // Log every SOAP XML request and response. - $user->LogAll(); - - // Run the example. - UpdatePromotionExample($user, $businessId, $promotionId); -} catch (Exception $e) { - printf("An error has occurred: %s\n", $e->getMessage()); -} diff --git a/examples/AdWords/v201607/Express/UpdatePromotion.php b/examples/AdWords/v201705/AccountManagement/AcceptServiceLink.php similarity index 57% rename from examples/AdWords/v201607/Express/UpdatePromotion.php rename to examples/AdWords/v201705/AccountManagement/AcceptServiceLink.php index 4361a0ba5..5aa34ed07 100755 --- a/examples/AdWords/v201607/Express/UpdatePromotion.php +++ b/examples/AdWords/v201705/AccountManagement/AcceptServiceLink.php @@ -1,7 +1,7 @@ SetExpressBusinessId($businessId); - +function AcceptServiceLinkExample(AdWordsUser $user, $serviceLinkId) { // Get the service, which loads the required classes. - $promotionService = $user->GetService('PromotionService', - ADWORDS_VERSION); - - // Update the budget for the promotion. - $promotion = new Promotion(); - $promotion->id = $promotionId; - $newBudget = new Money(); - $newBudget->microAmount = 2000000; - $promotion->budget = $newBudget; + $customerService = $user->GetService('CustomerService', ADWORDS_VERSION); - $operations = array(); + // Create service link and set the status to ACTIVE. + $serviceLink = new ServiceLink(); + $serviceLink->serviceLinkId = $serviceLinkId; + $serviceLink->serviceType = 'MERCHANT_CENTER'; + $serviceLink->linkStatus = 'ACTIVE'; - $operation = new PromotionOperation(); - $operation->operand = $promotion; + // Create operation. + $operation = new ServiceLinkOperation(); $operation->operator = 'SET'; - $operations[] = $operation; + $operation->operand = $serviceLink; + + $operations = array($operation); - $result = $promotionService->mutate($operations); - $mutatedPromotion = $result[0]; + // Make the mutate request. + $serviceLinks = $customerService->mutateServiceLinks($operations); - printf("Promotion ID %d for business ID %d now has budget micro amount %d\n", - $mutatedPromotion->id, $businessId, - $mutatedPromotion->budget->microAmount); + // Display the results. + foreach ($serviceLinks as $serviceLink) { + printf( + "Service link with service link ID %d, type '%s' updated to status: %s." + . "\n", + $serviceLink->serviceLinkId, + $serviceLink->serviceType, + $serviceLink->linkStatus + ); + } } // Don't run the example if the file is being included. @@ -80,7 +82,7 @@ function UpdatePromotionExample(AdWordsUser $user, $businessId, $promotionId) { $user->LogAll(); // Run the example. - UpdatePromotionExample($user, $businessId, $promotionId); + AcceptServiceLinkExample($user, $serviceLinkId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201705/AccountManagement/CreateAccount.php b/examples/AdWords/v201705/AccountManagement/CreateAccount.php new file mode 100755 index 000000000..74eaa9732 --- /dev/null +++ b/examples/AdWords/v201705/AccountManagement/CreateAccount.php @@ -0,0 +1,83 @@ +GetService('ManagedCustomerService', ADWORDS_VERSION); + + // Create customer. + $customer = new ManagedCustomer(); + $customer->name = 'Account #' . uniqid(); + $customer->currencyCode = 'EUR'; + $customer->dateTimeZone = 'Europe/London'; + + // Create operation. + $operation = new ManagedCustomerOperation(); + $operation->operator = 'ADD'; + $operation->operand = $customer; + + $operations = array($operation); + + // Make the mutate request. + $result = $managedCustomerService->mutate($operations); + + // Display result. + $customer = $result->value[0]; + printf("Account with customer ID '%s' was created.\n", + $customer->customerId); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + CreateAccountExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/AccountManagement/GetAccountChanges.php b/examples/AdWords/v201705/AccountManagement/GetAccountChanges.php new file mode 100755 index 000000000..b5071f3f2 --- /dev/null +++ b/examples/AdWords/v201705/AccountManagement/GetAccountChanges.php @@ -0,0 +1,138 @@ +GetService('CampaignService', ADWORDS_VERSION); + $customerSyncService = $user->GetService('CustomerSyncService', ADWORDS_VERSION); + + // Get an array of all campaign ids. + $campaignIds = array(); + $selector = new Selector(); + $selector->fields = array('Id'); + $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); + do { + $page = $campaignService->get($selector); + if (isset($page->entries)) { + foreach ($page->entries as $campaign) { + $campaignIds[] = $campaign->id; + } + } + $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE; + } while ($page->totalNumEntries > $selector->paging->startIndex); + + // Set the date time range, from 24 hours ago until now. + $dateTimeRange = new DateTimeRange(); + $dateTimeRange->min = date('Ymd his', strtotime('-1 day')); + $dateTimeRange->max = date('Ymd his'); + + // Create selector. + $selector = new CustomerSyncSelector(); + $selector->dateTimeRange = $dateTimeRange; + $selector->campaignIds = $campaignIds; + + // Make the get request. + $accountChanges = $customerSyncService->get($selector); + + // Display results. + if (isset($accountChanges)) { + printf("Most recent change: %s\n", $accountChanges->lastChangeTimestamp); + if (isset($accountChanges->changedCampaigns)) { + foreach ($accountChanges->changedCampaigns as $campaignChangeData) { + printf("Campaign with id '%.0f' has change status '%s'.\n", + $campaignChangeData->campaignId, + $campaignChangeData->campaignChangeStatus); + if ($campaignChangeData->campaignChangeStatus != 'NEW') { + printf("\tAdded campaign criteria: %s\n", + ArrayToString($campaignChangeData->addedCampaignCriteria)); + printf("\tRemoved campaign criteria: %s\n", + ArrayToString($campaignChangeData->removedCampaignCriteria)); + if (isset($campaignChangeData->changedAdGroups)) { + foreach($campaignChangeData->changedAdGroups as + $adGroupChangeData) { + printf("\tAd Group with id '%.0f' has change status '%s'.\n", + $adGroupChangeData->adGroupId, + $adGroupChangeData->adGroupChangeStatus); + if ($adGroupChangeData->adGroupChangeStatus != 'NEW') { + printf("\t\tChanged ads: %s\n", + ArrayToString($adGroupChangeData->changedAds)); + printf("\t\tChanged criteria: %s\n", + ArrayToString($adGroupChangeData->changedCriteria)); + printf("\t\tRemoved criteria: %s\n", + ArrayToString($adGroupChangeData->removedCriteria)); + } + } + } + } + } + } + } else { + print "No changes were found.\n"; + } +} + +/** + * Converts an array of values to a comma-separated string. + * @param array $array an array of values that can be converted to a string + * @return string a comma-separated string of the values + */ +function ArrayToString($array) { + if (!isset($array)) { + return ''; + } else { + return implode(', ', $array); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetAccountChangesExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/AccountManagement/GetAccountHierarchy.php b/examples/AdWords/v201705/AccountManagement/GetAccountHierarchy.php new file mode 100755 index 000000000..938f2d3de --- /dev/null +++ b/examples/AdWords/v201705/AccountManagement/GetAccountHierarchy.php @@ -0,0 +1,125 @@ +GetService('ManagedCustomerService', ADWORDS_VERSION); + + // Create selector. + $selector = new Selector(); + // Specify the fields to retrieve. + $selector->fields = array('CustomerId', 'Name'); + $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); + + // Create map from customerID to account. + $accounts = array(); + // Create map from customerId to parent and child links. + $childLinks = array(); + $parentLinks = array(); + do { + // Make the get request. + $graph = $managedCustomerService->get($selector); + + // Create links between manager and clients. + if (isset($graph->entries)) { + if (isset($graph->links)) { + foreach ($graph->links as $link) { + $childLinks[$link->managerCustomerId][] = $link; + $parentLinks[$link->clientCustomerId] = $link; + } + } + foreach ($graph->entries as $account) { + $accounts[$account->customerId] = $account; + } + } + $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE; + } while ($selector->paging->startIndex < $graph->totalNumEntries); + + $rootAccount = null; + foreach ($accounts as $account) { + if (!array_key_exists($account->customerId, $parentLinks)) { + $rootAccount = $account; + break; + } + } + + if ($rootAccount !== null) { + // Display account tree. + print "(Customer Id, Account Name)\n"; + DisplayAccountTree($rootAccount, $accounts, $childLinks, 0); + } else { + printf("No accounts were found.\n"); + } +} + +/** + * Displays an account tree, starting at the account provided, and recursing to + * all child accounts. + * @param ManagedCustomer $account the account to display + * @param array $accounts a map from customerId to account + * @param array $links a map from customerId to child links + * @param int $depth the depth of the current account in the tree + */ +function DisplayAccountTree($account, $accounts, $links, $depth) { + print str_repeat('-', $depth * 2); + printf("%s, %s\n", $account->customerId, $account->name); + if (array_key_exists($account->customerId, $links)) { + foreach ($links[$account->customerId] as $childLink) { + $childAccount = $accounts[$childLink->clientCustomerId]; + DisplayAccountTree($childAccount, $accounts, $links, $depth + 1); + } + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetAccountHierarchyExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/AdvancedOperations/AddAdCustomizer.php b/examples/AdWords/v201705/AdvancedOperations/AddAdCustomizer.php new file mode 100755 index 000000000..e1f6fa433 --- /dev/null +++ b/examples/AdWords/v201705/AdvancedOperations/AddAdCustomizer.php @@ -0,0 +1,238 @@ +GetService('AdCustomizerFeedService', + ADWORDS_VERSION); + + $nameAttribute = new AdCustomizerFeedAttribute(); + $nameAttribute->name = 'Name'; + $nameAttribute->type = 'STRING'; + + $priceAttribute = new AdCustomizerFeedAttribute(); + $priceAttribute->name = 'Price'; + $priceAttribute->type = 'STRING'; + + $dateAttribute = new AdCustomizerFeedAttribute(); + $dateAttribute->name = 'Date'; + $dateAttribute->type = 'DATE_TIME'; + + $customizerFeed = new AdCustomizerFeed(); + $customizerFeed->feedName = $feedName; + $customizerFeed->feedAttributes = array($nameAttribute, $priceAttribute, + $dateAttribute); + + $feedOperation = new AdCustomizerFeedOperation(); + $feedOperation->operand = $customizerFeed; + $feedOperation->operator = 'ADD'; + + $operations = array($feedOperation); + + // Add the feed. + $result = $adCustomizerFeedService->mutate($operations); + $addedFeed = $result->value[0]; + + printf("Created ad customizer feed with ID %d and name '%s'.\n", + $addedFeed->feedId, $addedFeed->feedName); + + return $addedFeed; +} + +/** + * Creates FeedItems with the values to use in ad customizations for each ad + * group in adGroupIds + * + * @param AdWordsUser $user the user to run the example with + * @param array $adGroupIds the IDs of the ad groups to target with the FeedItem + * @param AdCustomizerFeed $adCustomizerFeed the customizer feed + */ +function CreateCustomizerFeedItems(AdWordsUser $user, $adGroupIds, + $adCustomizerFeed) { + // Get the FeedItemService, which loads the required classes. + $feedItemService = $user->GetService('FeedItemService', ADWORDS_VERSION); + + $operations = array(); + + $marsDate = mktime(0, 0, 0, date('m'), 1, date('Y')); + $venusDate = mktime(0, 0, 0, date('m'), 15, date('Y')); + // Create operations to add FeedItems. + $operations[] = CreateFeedItemAddOperation('Mars', '$1234.56', + date('Ymd His', $marsDate), $adGroupIds[0], $adCustomizerFeed); + $operations[] = CreateFeedItemAddOperation('Venus', '$1450.00', + date('Ymd His', $venusDate), $adGroupIds[1], $adCustomizerFeed); + + $result = $feedItemService->mutate($operations); + + foreach ($result->value as $feedItem) { + printf("FeedItem with feedItemId %d was added.\n", $feedItem->feedItemId); + } + + return $adCustomizerFeed; +} + +/** + * Creates a FeedItemOperation that will create a FeedItem with the specified + * values and ad group target when sent to FeedItemService.mutate. + * + * @param string $name the value for the name attribute of the FeedItem + * @param string $price the value for the price attribute of the FeedItem + * @param string $date the value for the date attribute of the FeedItem + * @param string $adGroupId the ID of the ad group to target with the FeedItem + * @param AdCustomizerFeed $adCustomizerFeed the customizer feed + */ +function CreateFeedItemAddOperation($name, $price, $date, $adGroupId, + $adCustomizerFeed) { + // Create the FeedItemAttributeValues for our text values. + $nameAttributeValue = new FeedItemAttributeValue(); + $nameAttributeValue->feedAttributeId = + $adCustomizerFeed->feedAttributes[0]->id; + $nameAttributeValue->stringValue = $name; + $priceAttributeValue = new FeedItemAttributeValue(); + $priceAttributeValue->feedAttributeId = + $adCustomizerFeed->feedAttributes[1]->id; + $priceAttributeValue->stringValue = $price; + $dateAttributeValue = new FeedItemAttributeValue(); + $dateAttributeValue->feedAttributeId = + $adCustomizerFeed->feedAttributes[2]->id; + $dateAttributeValue->stringValue = $date; + + // Create the feed item and operation. + $item = new FeedItem(); + $item->feedId = $adCustomizerFeed->feedId; + $item->attributeValues = + array($nameAttributeValue, $priceAttributeValue, $dateAttributeValue); + + $adGroupTargeting = new FeedItemAdGroupTargeting(); + $adGroupTargeting->TargetingAdGroupId = $adGroupId; + $item->adGroupTargeting = $adGroupTargeting; + + $operation = new FeedItemOperation(); + $operation->operand = $item; + $operation->operator = 'ADD'; + return $operation; +} + +/** + * Creates text ads that use ad customizations for the specified ad group IDs. + * + * @param AdWordsUser $user the user to run the example with + * @param array $adGroupIds the IDs of the ad groups to target with the FeedItem + * @param string $feedName the name of the new AdCustomizerFeed + */ +function CreateAdsWithCustomizations(AdWordsUser $user, $adGroupIds, + $feedName) { + // Get the service, which loads the required classes. + $adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION); + + $expandedTextAd = new ExpandedTextAd(); + $expandedTextAd->headlinePart1 = + sprintf('Luxury Cruise to {=%s.Name}', $feedName); + $expandedTextAd->headlinePart2 = sprintf('Only {=%s.Price}', $feedName); + $expandedTextAd->description = sprintf('Offer ends in {=countdown(%s.Date)}!', + $feedName); + $expandedTextAd->finalUrls = array('http://www.example.com'); + + // We add the same ad to both ad groups. When they serve, they will show + // different values, since they match different feed items. + $operations = array(); + + foreach ($adGroupIds as $adGroupId) { + // Create ad group ad. + $adGroupAd = new AdGroupAd(); + $adGroupAd->adGroupId = $adGroupId; + $adGroupAd->ad = $expandedTextAd; + + // Create operation. + $operation = new AdGroupAdOperation(); + $operation->operand = $adGroupAd; + $operation->operator = 'ADD'; + $operations[] = $operation; + } + + // Make the mutate request. + $result = $adGroupAdService->mutate($operations); + + // Display results. + foreach ($result->value as $adGroupAd) { + printf("Text ad with ID %d and status '%s' was added.\n", + $adGroupAd->ad->id, $adGroupAd->status); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddAdCustomizerExample($user, $adGroupIds, $feedName); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/AdvancedOperations/AddAdGroupBidModifier.php b/examples/AdWords/v201705/AdvancedOperations/AddAdGroupBidModifier.php new file mode 100755 index 000000000..59106c2fa --- /dev/null +++ b/examples/AdWords/v201705/AdvancedOperations/AddAdGroupBidModifier.php @@ -0,0 +1,98 @@ +GetService('AdGroupBidModifierService', + ADWORDS_VERSION); + + // Mobile criterion ID. + $criterionId = 30001; + + // Prepare to add an ad group level override. + $agBidModifier = new AdGroupBidModifier(); + $agBidModifier->adGroupId = $adGroupId; + $agBidModifier->criterion = new Platform(); + $agBidModifier->criterion->id = $criterionId; + $agBidModifier->bidModifier = $bidModifier; + + $operation = new AdGroupBidModifierOperation(); + + // Use 'ADD' to add a new modifier and 'SET' to update an existing one. A + // modifier can be removed with the 'REMOVE' operator. + $operation->operator = 'ADD'; + $operation->operand = $agBidModifier; + + $response = $bidModifierService->mutate(array($operation)); + + foreach ($response->value as $modifier) { + $value = 'none'; + if (is_numeric($modifier->bidModifier)) { + $value = $modifier->bidModifier; + } + printf( + 'AdGroup ID %d, Criterion ID %d was updated with ' . + "ad group level modifier: %s\n", + $modifier->adGroupId, + $modifier->criterion->id, + $value + ); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddAdGroupBidModifierExample($user, $adGroupId, $bidModifier); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/AdvancedOperations/AddClickToDownloadAd.php b/examples/AdWords/v201705/AdvancedOperations/AddClickToDownloadAd.php new file mode 100755 index 000000000..f7bee6492 --- /dev/null +++ b/examples/AdWords/v201705/AdvancedOperations/AddClickToDownloadAd.php @@ -0,0 +1,139 @@ +GetService('AdGroupAdService', ADWORDS_VERSION); + + // Optionally specify a landscape image. The image needs to be in a BASE64 + // encoded form. Here we download a demo image and encode it for this ad. + $imageData = MediaUtils::GetBase64Data('http://goo.gl/9JmyKk'); + + // Create the template ad. + $clickToDownloadAppAd = new TemplateAd(); + + $clickToDownloadAppAd->name = 'Ad for demo game'; + $clickToDownloadAppAd->templateId = 353; + $clickToDownloadAppAd->finalUrls = array( + 'http://play.google.com/store/apps/details?id=com.example.demogame'); + $clickToDownloadAppAd->displayUrl = 'play.google.com'; + + // Create the template elements for the ad. You can refer to + // https://developers.google.com/adwords/api/docs/appendix/templateads + // for the list of avaliable template fields. + $headline = new TemplateElementField(); + $headline->name = 'headline'; + $headline->fieldText = 'Enjoy your drive in Mars'; + $headline->type = 'TEXT'; + + $description1 = new TemplateElementField(); + $description1->name = 'description1'; + $description1->fieldText = 'Realistic physics simulation'; + $description1->type = 'TEXT'; + + $description2 = new TemplateElementField(); + $description2->name = 'description2'; + $description2->fieldText = 'Race against players online'; + $description2->type = 'TEXT'; + + $appId = new TemplateElementField(); + $appId->name = 'appId'; + $appId->fieldText = 'com.example.demogame'; + $appId->type = 'TEXT'; + + $appStore = new TemplateElementField(); + $appStore->name = 'appStore'; + $appStore->fieldText = '2'; + $appStore->type = 'ENUM'; + + $landscapeImage = new TemplateElementField(); + $landscapeImage->name = 'landscapeImage'; + $landscapeImage->fieldMedia = new Image($imageData); + $landscapeImage->type = 'IMAGE'; + + $adData = new TemplateElement(); + $adData->uniqueName = 'adData'; + $adData->fields = array($headline, $description1, $description2, $appId, + $appStore, $landscapeImage); + + $clickToDownloadAppAd->templateElements = array($adData); + + // Create the adgroupad. + $clickToDownloadAppAdGroupAd = new AdGroupAd(); + $clickToDownloadAppAdGroupAd->adGroupId = $adGroupId; + $clickToDownloadAppAdGroupAd->ad = $clickToDownloadAppAd; + + // Optional: Set the status. + $clickToDownloadAppAdGroupAd->status = 'PAUSED'; + + // Create the operation. + $operation = new AdGroupAdOperation(); + $operation->operator = 'ADD'; + $operation->operand = $clickToDownloadAppAdGroupAd; + + $operations = array($operation); + + // Create the ads. + $result = $adGroupAdService->mutate($operations); + + foreach ($result->value as $adGroupAd) { + printf('New click-to-download ad with ID = %d and URL = "%s" ' . + "was created.\n", $adGroupAd->ad->id, $adGroupAd->ad->finalUrls[0]); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddClickToDownloadAd($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/AdvancedOperations/AddExpandedTextAdWithUpgradedUrls.php b/examples/AdWords/v201705/AdvancedOperations/AddExpandedTextAdWithUpgradedUrls.php new file mode 100755 index 000000000..661517b1c --- /dev/null +++ b/examples/AdWords/v201705/AdvancedOperations/AddExpandedTextAdWithUpgradedUrls.php @@ -0,0 +1,141 @@ +GetService('AdGroupAdService', ADWORDS_VERSION); + + $operations = array(); + + // Create expanded text ad with a tracking template and custom parameters. + $expandedTextAd = new ExpandedTextAd(); + $expandedTextAd->headlinePart1 = 'Luxury Cruise to Mars'; + $expandedTextAd->headlinePart2 = 'Visit the Red Planet in style.'; + $expandedTextAd->description = 'Low-gravity fun for everyone!'; + + // Specify a tracking url for 3rd party tracking provider. You may + // specify one at customer, campaign, ad group, ad, criterion or + // feed item levels. + $expandedTextAd->trackingUrlTemplate = + 'http://tracker.example.com/?season={_season}&promocode={_promocode}' . + '&u={lpurl}'; + + // Since your tracking url has two custom parameters, provide their + // values too. This can be provided at campaign, ad group, ad, criterion + // or feed item levels. + $seasonParameter = new CustomParameter(); + $seasonParameter->key = 'season'; + $seasonParameter->value = 'christmas'; + + $promoCodeParameter = new CustomParameter(); + $promoCodeParameter->key = 'promocode'; + $promoCodeParameter->value = 'NYC123'; + + $expandedTextAd->urlCustomParameters = new CustomParameters(); + $expandedTextAd->urlCustomParameters->parameters = array($seasonParameter, + $promoCodeParameter); + + // Specify a list of final urls. This field cannot be set if url field is + // set. This may be specified at ad, criterion and feed item levels. + $expandedTextAd->finalUrls = array('http://www.example.com/cruise/space/', + 'http://www.example.com/locations/mars/'); + + // Specify a list of final mobile urls. This field cannot be set if url + // field is set, or finalUrls is unset. This may be specified at ad, + // criterion and feed item levels. + $expandedTextAd->finalMobileUrls = array( + 'http://mobile.example.com/cruise/space/', + 'http://mobile.example.com/locations/mars/' + ); + + // Create ad group ad. + $adGroupAd = new AdGroupAd(); + $adGroupAd->adGroupId = $adGroupId; + $adGroupAd->ad = $expandedTextAd; + + // Set additional settings (optional). + $adGroupAd->status = 'PAUSED'; + + // Create operation. + $operation = new AdGroupAdOperation(); + $operation->operand = $adGroupAd; + $operation->operator = 'ADD'; + $operations[] = $operation; + + // Make the mutate request. + $result = $adGroupAdService->mutate($operations); + + // Display results. + foreach ($result->value as $adGroupAd) { + $ad = $adGroupAd->ad; + printf("Ad with ID %d was added.\n", $ad->id); + print("Upgraded URL properties:\n"); + printf(" Final URLs: %s\n", implode(', ', $ad->finalUrls)); + printf(" Final Mobile URLs: %s\n", implode(', ', $ad->finalMobileUrls)); + printf(" Tracking URL template: %s\n", $ad->trackingUrlTemplate); + printf(" Custom parameters: %s\n", + implode(', ', + array_map(function($param) { + return sprintf('%s=%s', $param->key, $param->value); + }, + $ad->urlCustomParameters->parameters))); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddExpandedTextAdWithUpgradedUrlsExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/AdvancedOperations/AddHtml5Ad.php b/examples/AdWords/v201705/AdvancedOperations/AddHtml5Ad.php new file mode 100755 index 000000000..6c50d070f --- /dev/null +++ b/examples/AdWords/v201705/AdvancedOperations/AddHtml5Ad.php @@ -0,0 +1,132 @@ +GetService('AdGroupAdService', ADWORDS_VERSION); + + // Create the template ad. + $html5Ad = new TemplateAd(); + + $html5Ad->name = 'Ad for HTML5'; + $html5Ad->templateId = 419; + $html5Ad->finalUrls = array('http://example.com/html5'); + $html5Ad->displayUrl = 'example.com/html5'; + + $dimensions = new Dimensions(); + $dimensions->width = 300; + $dimensions->height = 250; + $html5Ad->dimensions = $dimensions; + + // The HTML5 zip file contains all the HTML, CSS, and images needed for the + // HTML5 ad. For help on creating an HTML5 zip file, check out Google Web + // Designer (https://www.google.com/webdesigner/). + $html5Zip = MediaUtils::GetBase64Data('https://goo.gl/9Y7qI2'); + + // Create a media bundle containing the zip file with all the HTML5 + // components. + // NOTE: You may also upload an HTML5 zip using MediaService.upload() + // and simply set the mediaId field below. See UploadMediaBundle.php for an + // example. + $mediaBundle = new MediaBundle(); + $mediaBundle->data = $html5Zip; + $mediaBundle->entryPoint = 'carousel/index.html'; + $mediaBundle->type = 'MEDIA_BUNDLE'; + + // Create the template elements for the ad. You can refer to + // https://developers.google.com/adwords/api/docs/appendix/templateads + // for the list of avaliable template fields. + $media = new TemplateElementField(); + $media->name = 'Custom_layout'; + $media->fieldMedia = $mediaBundle; + $media->type = 'MEDIA_BUNDLE'; + + $layout = new TemplateElementField(); + $layout->name = 'layout'; + $layout->fieldText = 'Custom'; + $layout->type = 'ENUM'; + + $adData = new TemplateElement(); + $adData->uniqueName = 'adData'; + $adData->fields = array($media, $layout); + + $html5Ad->templateElements = array($adData); + + // Create the ad group ad. + $html5AdGroupAd = new AdGroupAd(); + $html5AdGroupAd->adGroupId = $adGroupId; + $html5AdGroupAd->ad = $html5Ad; + + // Optional: Set the status. + $html5AdGroupAd->status = 'PAUSED'; + + // Create the operation. + $operation = new AdGroupAdOperation(); + $operation->operator = 'ADD'; + $operation->operand = $html5AdGroupAd; + + $operations = array($operation); + + // Create the ads. + $result = $adGroupAdService->mutate($operations); + + foreach ($result->value as $adGroupAd) { + printf("New HTML5 ad with ID %d and display URL '%s' was created.\n", + $adGroupAd->ad->id, $adGroupAd->ad->displayUrl); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddHtml5Ad($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/AdvancedOperations/AddResponsiveDisplayAd.php b/examples/AdWords/v201705/AdvancedOperations/AddResponsiveDisplayAd.php new file mode 100755 index 000000000..5bbf6758a --- /dev/null +++ b/examples/AdWords/v201705/AdvancedOperations/AddResponsiveDisplayAd.php @@ -0,0 +1,115 @@ +GetService('MediaService', ADWORDS_VERSION); + + // Creates image. + $image = new Image(); + $image->data = MediaUtils::GetBase64Data('https://goo.gl/3b9Wfh'); + $image->type = 'IMAGE'; + + // Make the upload request. + $result = $mediaService->upload(array($image)); + $image = $result[0]; + + // Get the service, which loads the required classes. + $adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION); + + // Create a responsive display ad. + $responsiveDisplayAd = new ResponsiveDisplayAd(); + + // This ad format does not allow the creation of an image using the + // Image.data field. An image must first be created using the MediaService, + // and Image.mediaId must be populated when creating the ad. + $marketingImage = new Image(); + $marketingImage->mediaId = $image->mediaId; + + $responsiveDisplayAd->marketingImage = $marketingImage; + $responsiveDisplayAd->shortHeadline = 'Travel'; + $responsiveDisplayAd->longHeadline = 'Travel the World'; + $responsiveDisplayAd->description = 'Take to the air!'; + $responsiveDisplayAd->businessName = 'Google'; + $responsiveDisplayAd->finalUrls = array('http://www.example.com'); + + // Create ad group ad. + $adGroupAd = new AdGroupAd(); + $adGroupAd->adGroupId = $adGroupId; + $adGroupAd->ad = $responsiveDisplayAd; + + // Set additional settings (optional). + $adGroupAd->status = 'PAUSED'; + + // Create operation. + $operation = new AdGroupAdOperation(); + $operation->operand = $adGroupAd; + $operation->operator = 'ADD'; + $operations[] = $operation; + + // Make the mutate request. + $result = $adGroupAdService->mutate($operations); + + // Display results. + foreach ($result->value as $adGroupAd) { + printf("Responsive display ad with ID '%d' and short headline '%s'" + . " was added.\n", $adGroupAd->ad->id, $adGroupAd->ad->shortHeadline); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddResponsiveDisplayAd($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/AdvancedOperations/AddTextAdWithUpgradedUrls.php b/examples/AdWords/v201705/AdvancedOperations/AddTextAdWithUpgradedUrls.php new file mode 100755 index 000000000..8af2d5b53 --- /dev/null +++ b/examples/AdWords/v201705/AdvancedOperations/AddTextAdWithUpgradedUrls.php @@ -0,0 +1,147 @@ +GetService('AdGroupAdService', ADWORDS_VERSION); + + $numAds = 5; + $operations = array(); + for ($i = 0; $i < $numAds; $i++) { + // Create text ad. + $textAd = new TextAd(); + $textAd->headline = 'Cruise #' . uniqid(); + $textAd->description1 = 'Visit the Red Planet in style.'; + $textAd->description2 = 'Low-gravity fun for everyone!'; + $textAd->displayUrl = 'www.example.com'; + + // Specify a tracking url for 3rd party tracking provider. You may + // specify one at customer, campaign, ad group, ad, criterion or + // feed item levels. + $textAd->trackingUrlTemplate = + 'http://tracker.example.com/?season={_season}&promocode={_promocode}' . + '&u={lpurl}'; + + // Since your tracking url has two custom parameters, provide their + // values too. This can be provided at campaign, ad group, ad, criterion + // or feed item levels. + $seasonParameter = new CustomParameter(); + $seasonParameter->key = 'season'; + $seasonParameter->value = 'christmas'; + + $promoCodeParameter = new CustomParameter(); + $promoCodeParameter->key = 'promocode'; + $promoCodeParameter->value = 'NYC123'; + + $textAd->urlCustomParameters = new CustomParameters(); + $textAd->urlCustomParameters->parameters = array($seasonParameter, + $promoCodeParameter); + + // Specify a list of final urls. This field cannot be set if url field is + // set. This may be specified at ad, criterion and feed item levels. + $textAd->finalUrls = array('http://www.example.com/cruise/space/', + 'http://www.example.com/locations/mars/'); + + // Specify a list of final mobile urls. This field cannot be set if url + // field is set, or finalUrls is unset. This may be specified at ad, + // criterion and feed item levels. + $textAd->finalMobileUrls = array('http://mobile.example.com/cruise/space/', + 'http://mobile.example.com/locations/mars/'); + + // Create ad group ad. + $adGroupAd = new AdGroupAd(); + $adGroupAd->adGroupId = $adGroupId; + $adGroupAd->ad = $textAd; + + // Set additional settings (optional). + $adGroupAd->status = 'PAUSED'; + + // Create operation. + $operation = new AdGroupAdOperation(); + $operation->operand = $adGroupAd; + $operation->operator = 'ADD'; + $operations[] = $operation; + } + + // Make the mutate request. + $result = $adGroupAdService->mutate($operations); + + // Display results. + foreach ($result->value as $adGroupAd) { + $ad = $adGroupAd->ad; + printf("Text ad with headline '%s' and ID '%d' was added.\n", + $ad->headline, $ad->id); + printf(" displayUrl is '%s'\n", + $ad->displayUrl); + print("Upgraded URL properties:\n"); + printf(" Final URLs: %s\n", + implode(', ', $ad->finalUrls)); + printf(" Final Mobile URLs: %s\n", + implode(', ', $ad->finalMobileUrls)); + printf(" Tracking URL template: %s\n", + $ad->trackingUrlTemplate); + printf(" Custom parameters: %s\n", + implode(', ', + array_map(function($param) { + return sprintf('%s=%s', $param->key, $param->value); + }, + $ad->urlCustomParameters->parameters))); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddTextAdWithUpgradedUrlsExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/AdvancedOperations/AddUniversalAppCampaign.php b/examples/AdWords/v201705/AdvancedOperations/AddUniversalAppCampaign.php new file mode 100755 index 000000000..18a7a2053 --- /dev/null +++ b/examples/AdWords/v201705/AdvancedOperations/AddUniversalAppCampaign.php @@ -0,0 +1,231 @@ +GetService('CampaignService', ADWORDS_VERSION); + + // Create campaign with some properties set. + $campaign = new Campaign(); + $campaign->name = 'Interplanetary Cruise #' . uniqid(); + // Recommendation: Set the campaign to PAUSED when creating it to stop + // the ads from immediately serving. Set to ENABLED once you've added + // targeting and the ads are ready to serve. + $campaign->status = 'PAUSED'; + + // Set the advertising channel and subchannel types for Universal app + // campaigns. + $campaign->advertisingChannelType = 'MULTI_CHANNEL'; + $campaign->advertisingChannelSubType = 'UNIVERSAL_APP_CAMPAIGN'; + + // Set the campaign's bidding strategy. Universal App campaigns + // only support TARGET_CPA bidding strategy. + $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); + $biddingStrategyConfiguration->biddingStrategyType = 'TARGET_CPA'; + + // Set the target CPA to $1 / app install. + $biddingScheme = new TargetCpaBiddingScheme(); + $biddingScheme->targetCpa = new Money(1000000); + + $biddingStrategyConfiguration->biddingScheme = $biddingScheme; + $campaign->biddingStrategyConfiguration = $biddingStrategyConfiguration; + + // Set the campaign's budget. + $campaign->budget = new Budget(); + $campaign->budget->budgetId = CreateBudget($user); + + // Optional: Set the start date. + $campaign->startDate = date('Ymd', strtotime('+1 day')); + + // Optional: Set the end date. + $campaign->endDate = date('Ymd', strtotime('+1 year')); + + // Set the campaign's assets and ad text ideas. These values will be used to + // generate ads. + $universalAppSetting = new UniversalAppCampaignSetting(); + $universalAppSetting->appId = 'com.labpixies.colordrips'; + $universalAppSetting->description1 = 'A cool puzzle game'; + $universalAppSetting->description2 = 'Remove connected blocks'; + $universalAppSetting->description3 = '3 difficulty levels'; + $universalAppSetting->description4 = '4 colorful fun skins'; + + // Optional: You can set up to 10 image assets for your campaign. + // See UploadImage.php for an example on how to upload images. + // + // $universalAppSetting->imageMediaIds = array(INSERT_IMAGE_MEDIA_ID_HERE); + + // Optimize this campaign for getting new users for your app. + $universalAppSetting->universalAppBiddingStrategyGoalType = + 'OPTIMIZE_FOR_INSTALL_CONVERSION_VOLUME'; + + // If you select bidding strategy goal type as + // OPTIMIZE_FOR_IN_APP_CONVERSION_VOLUME, then you may specify a set of + // conversion types for in-app actions to optimize the campaign towards. + // Conversion type IDs can be retrieved using ConversionTrackerService.get. + // + // $campaign->selectiveOptimization = new SelectiveOptimization(); + // $campaign->selectiveOptimization->conversionTypeIds = array( + // INSERT_CONVERSION_TYPE_ID_1_HERE, + // INSERT_CONVERSION_TYPE_ID_2_HERE + // ); + + // Optional: Set the campaign settings for Advanced location options. + $geoTargetTypeSetting = new GeoTargetTypeSetting(); + $geoTargetTypeSetting->negativeGeoTargetType = 'LOCATION_OF_PRESENCE'; + $campaign->settings = array($universalAppSetting, $geoTargetTypeSetting); + + // Create the campaign operation. + $operations = array(); + $operation = new CampaignOperation(); + $operation->operand = $campaign; + $operation->operator = 'ADD'; + $operations[] = $operation; + + // Add campaigns on the server. + $result = $campaignService->mutate($operations); + + // Print information for each campaign. + foreach ($result->value as $campaign) { + printf("Universal App Campaign with name '%s' and ID %d was added.\n", + $campaign->name, $campaign->id); + // Optional: Set the campaign's location and language targeting. No other + // targeting criteria can be used for Universal App campaigns. + SetCampaignTargetingCriteria($campaign->id, $user); + } +} + +/** + * Creates the budget for the campaign. + * + * @return the new budget ID + */ +function CreateBudget(AdWordsUser $user) { + // Get the BudgetService, which loads the required classes. + $budgetService = $user->GetService('BudgetService', ADWORDS_VERSION); + + // Create the shared budget (required). + $budget = new Budget(); + $budget->name = 'Interplanetary Cruise Budget #' . uniqid(); + $budget->amount = new Money(50000000); + $budget->deliveryMethod = 'STANDARD'; + + // Universal App campaigns don't support shared budgets. + $budget->isExplicitlyShared = false; + $operations = array(); + + // Create operation. + $operation = new BudgetOperation(); + $operation->operand = $budget; + $operation->operator = 'ADD'; + $operations[] = $operation; + + // Make the mutate request. + $result = $budgetService->mutate($operations); + $budget = $result->value[0]; + + printf("Budget with name '%s' and ID %d was created.\n", + $budget->name, $budget->budgetId); + + return $budget->budgetId; +} + +/** + * Sets the campaign's targeting criteria. + * + * @param AdWordsUser $user the AdWords user object + */ +function SetCampaignTargetingCriteria($campaignId, AdWordsUser $user) { + // Get the service, which loads the required classes. + $campaignCriterionService = + $user->GetService('CampaignCriterionService', ADWORDS_VERSION); + + $campaignCriteria = array(); + // Create locations. The IDs can be found in the documentation or retrieved + // with the LocationCriterionService. + $california = new Location(); + $california->id = 21137; + $campaignCriteria[] = new CampaignCriterion($campaignId, null, $california); + + $mexico = new Location(); + $mexico->id = 2484; + $campaignCriteria[] = new CampaignCriterion($campaignId, null, $mexico); + + // Create languages. The IDs can be found in the documentation or retrieved + // with the ConstantDataService. + $english = new Language(); + $english->id = 1000; + $campaignCriteria[] = new CampaignCriterion($campaignId, null, $english); + + $spanish = new Language(); + $spanish->id = 1003; + $campaignCriteria[] = new CampaignCriterion($campaignId, null, $spanish); + + // Create operations to add each of the criteria above. + $operations = array(); + foreach ($campaignCriteria as $campaignCriterion) { + $operations[] = new CampaignCriterionOperation($campaignCriterion, 'ADD'); + } + + // Set the campaign targets. + $result = $campaignCriterionService->mutate($operations); + + // Display added campaign targets. + foreach ($result->value as $campaignCriterion) { + printf("Campaign criterion of type '%s' and ID %d was added.\n", + $campaignCriterion->criterion->CriterionType, + $campaignCriterion->criterion->id + ); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddUniversalAppCampaignExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201607/Express/GetExpressBusinesses.php b/examples/AdWords/v201705/AdvancedOperations/GetAdGroupBidModifiers.php similarity index 67% rename from examples/AdWords/v201607/Express/GetExpressBusinesses.php rename to examples/AdWords/v201705/AdvancedOperations/GetAdGroupBidModifiers.php index 1010eb8f0..1d1ebac2c 100755 --- a/examples/AdWords/v201607/Express/GetExpressBusinesses.php +++ b/examples/AdWords/v201705/AdvancedOperations/GetAdGroupBidModifiers.php @@ -1,7 +1,7 @@ GetService('ExpressBusinessService', - ADWORDS_VERSION); + $bidModifierService = $user->GetService('AdGroupBidModifierService', + ADWORDS_VERSION); // Create selector. $selector = new Selector(); - $selector->fields = array('Id', 'Name', 'Website', 'Status'); - $selector->ordering[] = new OrderBy('Name', 'ASCENDING'); + $selector->fields = array('CampaignId', 'AdGroupId', 'BidModifier', 'Id'); + $selector->ordering[] = new OrderBy('CampaignId', 'ASCENDING'); // Create paging controls. $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); do { // Make the get request. - $page = $businessService->get($selector); + $page = $bidModifierService->get($selector); // Display results. if (isset($page->entries)) { - foreach ($page->entries as $business) { - printf( - "Express business found with name '%s' id %s website: %s " - . "status: %s\n", - $business->name, - $business->id, - $business->website, - $business->status + foreach ($page->entries as $modifier) { + $value = 'none'; + if (is_numeric($modifier->bidModifier)) { + $value = $modifier->bidModifier; + } + printf("Campaign ID %d, AdGroup ID %d, Criterion ID %d has ad group " . + "level modifier: %s\n", + $modifier->campaignId, + $modifier->adGroupId, + $modifier->criterion->id, + $value ); } } else { - print "No express businesses were found.\n"; + print "No bid modifiers were found.\n"; } // Advance the paging index. @@ -84,7 +87,7 @@ function GetExpressBusinessesExample(AdWordsUser $user) { $user->LogAll(); // Run the example. - GetExpressBusinessesExample($user); + GetAdGroupBidModifiersExample($user); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201705/AdvancedOperations/UsePortfolioBiddingStrategy.php b/examples/AdWords/v201705/AdvancedOperations/UsePortfolioBiddingStrategy.php new file mode 100755 index 000000000..09888ff1e --- /dev/null +++ b/examples/AdWords/v201705/AdvancedOperations/UsePortfolioBiddingStrategy.php @@ -0,0 +1,191 @@ +budgetId; + } + CreateCampaignWithBiddingStrategy($user, $biddingStrategy->id, + $sharedBudgetId); +} + +/** + * Creates the bidding strategy object. + * @param AdWordsUser $user the user to run the example with + */ +function CreateBiddingStrategy(AdWordsUser $user) { + // Get the BiddingStrategyService, which loads the required classes. + $biddingStrategyService = + $user->GetService('BiddingStrategyService', ADWORDS_VERSION); + + // Create a portfolio bidding strategy. + $biddingStrategy = new SharedBiddingStrategy(); + $biddingStrategy->name = "Maximize Clicks " . uniqid(); + + $biddingScheme = new TargetSpendBiddingScheme(); + // Optionally set additional bidding scheme parameters. + $biddingScheme->bidCeiling = new Money('2000000'); + $biddingScheme->spendTarget = new Money('20000000'); + + $biddingStrategy->biddingScheme = $biddingScheme; + + // Create operation. + $operation = new BiddingStrategyOperation(); + $operation->operator = 'ADD'; + $operation->operand = $biddingStrategy; + + $result = $biddingStrategyService->mutate(array($operation)); + + $newBiddingStrategy = $result->value[0]; + + printf( + "Portfolio bidding strategy with name '%s' and ID %d of type %s was " + . "created.\n", + $newBiddingStrategy->name, + $newBiddingStrategy->id, + $newBiddingStrategy->biddingScheme->biddingSchemeType + ); + + return $newBiddingStrategy; +} + +/** + * Creates an explicit budget to be used only to create the Campaign. + * @param AdWordsUser $user the user to run the example with + */ +function CreateSharedBudget($user) { + // Get the BudgetService, which loads the required classes. + $budgetService = $user->GetService('BudgetService', ADWORDS_VERSION); + + // Create a shared budget + $budget = new Budget(); + $budget->name = "Shared Interplanetary Budget #" . uniqid(); + $budget->period = 'DAILY'; + $budget->amount = new Money(50000000); + $budget->deliveryMethod = 'STANDARD'; + $budget->isExplicitlyShared = true; + + $operations = array(); + + // Create operation. + $operation = new BudgetOperation(); + $operation->operand = $budget; + $operation->operator = 'ADD'; + $operations[] = $operation; + + // Make the mutate request. + $result = $budgetService->mutate($operations); + return $result->value[0]; +} + +/** + * Create a Campaign with a Portfolio Bidding Strategy. + * @param AdWordsUser $user the user to run the example with + * @param string $biddingStrategyId the bidding strategy id to use + * @param string $sharedBudgetId the shared budget id to use + */ +function CreateCampaignWithBiddingStrategy(AdWordsUser $user, + $biddingStrategyId, $sharedBudgetId) { + // Get the CampaignService, which loads the required classes. + $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION); + + // Create campaign. + $campaign = new Campaign(); + $campaign->name = 'Interplanetary Cruise #' . uniqid(); + + // Set the budget. + $campaign->budget = new Budget(); + $campaign->budget->budgetId = $sharedBudgetId; + $campaign->advertisingChannelType = 'SEARCH'; + + // Set bidding strategy (required). + $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); + $biddingStrategyConfiguration->biddingStrategyId = $biddingStrategyId; + + $campaign->biddingStrategyConfiguration = $biddingStrategyConfiguration; + + // Set network targeting (recommended). + $networkSetting = new NetworkSetting(); + $networkSetting->targetGoogleSearch = true; + $networkSetting->targetSearchNetwork = true; + $networkSetting->targetContentNetwork = true; + $campaign->networkSetting = $networkSetting; + // Recommendation: Set the campaign to PAUSED when creating it to stop + // the ads from immediately serving. Set to ENABLED once you've added + // targeting and the ads are ready to serve. + $campaign->status = 'PAUSED'; + + // Create operation. + $operation = new CampaignOperation(); + $operation->operand = $campaign; + $operation->operator = 'ADD'; + + $result = $campaignService->mutate(array($operation)); + + $newCampaign = $result->value[0]; + + printf("Campaign with name '%s', ID %d and bidding scheme ID %d was " + . "created.\n", $newCampaign->name, $newCampaign->id, + $newCampaign->biddingStrategyConfiguration->biddingStrategyId); + + return $newCampaign; +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + UsePortfolioBiddingStrategyExample($user, $budgetId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/BasicOperations/AddAdGroupDemographicCriteria.php b/examples/AdWords/v201705/BasicOperations/AddAdGroupDemographicCriteria.php new file mode 100755 index 000000000..eef4f28b1 --- /dev/null +++ b/examples/AdWords/v201705/BasicOperations/AddAdGroupDemographicCriteria.php @@ -0,0 +1,107 @@ +GetService('AdGroupCriterionService', + ADWORDS_VERSION); + + // Create biddable ad group criterion for gender + $genderTarget = new Gender(); + // Criterion Id for male. The IDs can be found here + // https://developers.google.com/adwords/api/docs/appendix/genders + $genderTarget->id = 10; + + $genderBiddableAdGroupCriterion = new BiddableAdGroupCriterion(); + $genderBiddableAdGroupCriterion->adGroupId = $adGroupId; + $genderBiddableAdGroupCriterion->criterion = $genderTarget; + + // Create negative ad group criterion for age range + $ageRangeNegative = new AgeRange(); + // Criterion Id for age 18 to 24. The IDs can be found here + // https://developers.google.com/adwords/api/docs/appendix/ages + + $ageRangeNegative->id = 503001; + $ageRangeNegativeAdGroupCriterion = new NegativeAdGroupCriterion(); + $ageRangeNegativeAdGroupCriterion->adGroupId = $adGroupId; + $ageRangeNegativeAdGroupCriterion->criterion = $ageRangeNegative; + + $operations = array(); + + // Create operations. + $genderBiddableAdGroupCriterionOperation = new AdGroupCriterionOperation(); + $genderBiddableAdGroupCriterionOperation->operand = + $genderBiddableAdGroupCriterion; + $genderBiddableAdGroupCriterionOperation->operator = 'ADD'; + $operations[] = $genderBiddableAdGroupCriterionOperation; + + $ageRangeNegativeAdGroupCriterionOperation = new AdGroupCriterionOperation(); + $ageRangeNegativeAdGroupCriterionOperation->operand = + $ageRangeNegativeAdGroupCriterion; + $ageRangeNegativeAdGroupCriterionOperation->operator = 'ADD'; + $operations[] = $ageRangeNegativeAdGroupCriterionOperation; + + // Make the mutate request. + $result = $adGroupCriterionService->mutate($operations); + + // Display results. + foreach ($result->value as $adGroupCriterion) { + printf("Ad group criterion with ad group ID '%s', criterion ID '%s' " . + "and type '%s' was added.\n", $adGroupCriterion->adGroupId, + $adGroupCriterion->criterion->id, + $adGroupCriterion->criterion->CriterionType); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddAdGroupDemographicCriteriaExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/BasicOperations/AddAdGroups.php b/examples/AdWords/v201705/BasicOperations/AddAdGroups.php new file mode 100755 index 000000000..3076af19e --- /dev/null +++ b/examples/AdWords/v201705/BasicOperations/AddAdGroups.php @@ -0,0 +1,111 @@ +GetService('AdGroupService', ADWORDS_VERSION); + + $numAdGroups = 2; + $operations = array(); + for ($i = 0; $i < $numAdGroups; $i++) { + // Create ad group. + $adGroup = new AdGroup(); + $adGroup->campaignId = $campaignId; + $adGroup->name = 'Earth to Mars Cruise #' . uniqid(); + + // Set bids (required). + $bid = new CpcBid(); + $bid->bid = new Money(1000000); + $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); + $biddingStrategyConfiguration->bids[] = $bid; + $adGroup->biddingStrategyConfiguration = $biddingStrategyConfiguration; + + // Set additional settings (optional). + $adGroup->status = 'ENABLED'; + + // Targeting restriction settings. Depending on the criterionTypeGroup + // value, most TargetingSettingDetail only affect Display campaigns. + // However, the USER_INTEREST_AND_LIST value works for RLSA campaigns - + // Search campaigns targeting using a remarketing list. + $targetingSetting = new TargetingSetting(); + // Restricting to serve ads that match your ad group placements. + // This is equivalent to choosing "Target and bid" in the UI. + $targetingSetting->details[] = + new TargetingSettingDetail('PLACEMENT', false); + // Using your ad group verticals only for bidding. This is equivalent + // to choosing "Bid only" in the UI. + $targetingSetting->details[] = + new TargetingSettingDetail('VERTICAL', true); + $adGroup->settings[] = $targetingSetting; + + // Create operation. + $operation = new AdGroupOperation(); + $operation->operand = $adGroup; + $operation->operator = 'ADD'; + $operations[] = $operation; + } + + // Make the mutate request. + $result = $adGroupService->mutate($operations); + + // Display result. + $adGroups = $result->value; + foreach ($adGroups as $adGroup) { + printf("Ad group with name '%s' and ID '%s' was added.\n", $adGroup->name, + $adGroup->id); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddAdGroupsExample($user, $campaignId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/BasicOperations/AddCampaigns.php b/examples/AdWords/v201705/BasicOperations/AddCampaigns.php new file mode 100755 index 000000000..61f5d0954 --- /dev/null +++ b/examples/AdWords/v201705/BasicOperations/AddCampaigns.php @@ -0,0 +1,145 @@ +GetService('BudgetService', ADWORDS_VERSION); + + // Create the shared budget (required). + $budget = new Budget(); + $budget->name = 'Interplanetary Cruise Budget #' . uniqid(); + $budget->amount = new Money(50000000); + $budget->deliveryMethod = 'STANDARD'; + + $operations = array(); + + // Create operation. + $operation = new BudgetOperation(); + $operation->operand = $budget; + $operation->operator = 'ADD'; + $operations[] = $operation; + + // Make the mutate request. + $result = $budgetService->mutate($operations); + $budget = $result->value[0]; + + // Get the CampaignService, which loads the required classes. + $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION); + + $numCampaigns = 2; + $operations = array(); + for ($i = 0; $i < $numCampaigns; $i++) { + // Create campaign. + $campaign = new Campaign(); + $campaign->name = 'Interplanetary Cruise #' . uniqid(); + $campaign->advertisingChannelType = 'SEARCH'; + + // Set shared budget (required). + $campaign->budget = new Budget(); + $campaign->budget->budgetId = $budget->budgetId; + + // Set bidding strategy (required). + $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); + $biddingStrategyConfiguration->biddingStrategyType = 'MANUAL_CPC'; + + // You can optionally provide a bidding scheme in place of the type. + $biddingScheme = new ManualCpcBiddingScheme(); + $biddingScheme->enhancedCpcEnabled = false; + $biddingStrategyConfiguration->biddingScheme = $biddingScheme; + + $campaign->biddingStrategyConfiguration = $biddingStrategyConfiguration; + + // Set network targeting (optional). + $networkSetting = new NetworkSetting(); + $networkSetting->targetGoogleSearch = true; + $networkSetting->targetSearchNetwork = true; + $networkSetting->targetContentNetwork = true; + $campaign->networkSetting = $networkSetting; + + // Set additional settings (optional). + // Recommendation: Set the campaign to PAUSED when creating it to stop + // the ads from immediately serving. Set to ENABLED once you've added + // targeting and the ads are ready to serve. + $campaign->status = 'PAUSED'; + $campaign->startDate = date('Ymd', strtotime('+1 day')); + $campaign->endDate = date('Ymd', strtotime('+1 month')); + $campaign->adServingOptimizationStatus = 'ROTATE'; + + // Set frequency cap (optional). + $frequencyCap = new FrequencyCap(); + $frequencyCap->impressions = 5; + $frequencyCap->timeUnit = 'DAY'; + $frequencyCap->level = 'ADGROUP'; + $campaign->frequencyCap = $frequencyCap; + + // Set advanced location targeting settings (optional). + $geoTargetTypeSetting = new GeoTargetTypeSetting(); + $geoTargetTypeSetting->positiveGeoTargetType = 'DONT_CARE'; + $geoTargetTypeSetting->negativeGeoTargetType = 'DONT_CARE'; + $campaign->settings[] = $geoTargetTypeSetting; + + // Create operation. + $operation = new CampaignOperation(); + $operation->operand = $campaign; + $operation->operator = 'ADD'; + $operations[] = $operation; + } + + // Make the mutate request. + $result = $campaignService->mutate($operations); + + // Display results. + foreach ($result->value as $campaign) { + printf("Campaign with name '%s' and ID '%s' was added.\n", $campaign->name, + $campaign->id); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddCampaignsExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201607/Express/GetPromotions.php b/examples/AdWords/v201705/BasicOperations/AddExpandedTextAds.php similarity index 50% rename from examples/AdWords/v201607/Express/GetPromotions.php rename to examples/AdWords/v201705/BasicOperations/AddExpandedTextAds.php index c78277f88..232fe90d8 100755 --- a/examples/AdWords/v201607/Express/GetPromotions.php +++ b/examples/AdWords/v201705/BasicOperations/AddExpandedTextAds.php @@ -1,7 +1,7 @@ SetExpressBusinessId($businessId); - +function AddExpandedTextAdsExample(AdWordsUser $user, $adGroupId) { // Get the service, which loads the required classes. - $promotionService = $user->GetService('PromotionService', ADWORDS_VERSION); + $adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION); + + // Create an expanded text ad. + $expandedTextAd = new ExpandedTextAd(); + $expandedTextAd->headlinePart1 = 'Cruise to Mars #' . uniqid(); + $expandedTextAd->headlinePart2 = 'Best Space Cruise Line'; + $expandedTextAd->description = 'Buy your tickets now!'; + $expandedTextAd->finalUrls = array('http://www.example.com'); + $expandedTextAd->path1 = 'all-inclusive'; + $expandedTextAd->path2 = 'deals'; + + // Create ad group ad. + $adGroupAd = new AdGroupAd(); + $adGroupAd->adGroupId = $adGroupId; + $adGroupAd->ad = $expandedTextAd; - // Create selector. - $selector = new Selector(); - $selector->fields = array('PromotionId', 'Name', 'Status', 'DestinationUrl', - 'CallTrackingEnabled', 'Budget', 'PromotionCriteria', 'RemainingBudget', - 'Creatives', 'CampaignIds'); - $selector->ordering[] = new OrderBy('Name', 'ASCENDING'); + // Set additional settings (optional). + $adGroupAd->status = 'PAUSED'; - // Create paging controls. - $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); + // Create operation. + $operation = new AdGroupAdOperation(); + $operation->operand = $adGroupAd; + $operation->operator = 'ADD'; - do { - // Make the get request. - $page = $promotionService->get($selector); + $operations = array($operation); - // Display results. - if (isset($page->entries)) { - foreach ($page->entries as $promotion) { - printf("Express promotion found with name '%s' " . - "id %s destinationUrl: %s\n", $promotion->name, $promotion->id, - $promotion->destinationUrl); - } - } else { - print "No express promotions were found.\n"; - } + // Make the mutate request. + $result = $adGroupAdService->mutate($operations); - // Advance the paging index. - $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE; - } while ($page->totalNumEntries > $selector->paging->startIndex); + // Display results. + foreach ($result->value as $adGroupAd) { + printf( + "Expanded text ad with ID '%d' and headline '%s - %s' was added.\n", + $adGroupAd->ad->id, + $adGroupAd->ad->headlinePart1, + $adGroupAd->ad->headlinePart2 + ); + } } // Don't run the example if the file is being included. @@ -85,7 +92,7 @@ function GetPromotionsExample(AdWordsUser $user, $businessId) { $user->LogAll(); // Run the example. - GetPromotionsExample($user, $businessId); + AddExpandedTextAdsExample($user, $adGroupId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201705/BasicOperations/AddKeywords.php b/examples/AdWords/v201705/BasicOperations/AddKeywords.php new file mode 100755 index 000000000..4c0c2685b --- /dev/null +++ b/examples/AdWords/v201705/BasicOperations/AddKeywords.php @@ -0,0 +1,107 @@ +GetService('AdGroupCriterionService', ADWORDS_VERSION); + + $numKeywords = 5; + $operations = array(); + for ($i = 0; $i < $numKeywords; $i++) { + // Create keyword criterion. + $keyword = new Keyword(); + $keyword->text = 'mars cruise ' . uniqid(); + $keyword->matchType = 'BROAD'; + + // Create biddable ad group criterion. + $adGroupCriterion = new BiddableAdGroupCriterion(); + $adGroupCriterion->adGroupId = $adGroupId; + $adGroupCriterion->criterion = $keyword; + + // Set additional settings (optional). + $adGroupCriterion->userStatus = 'PAUSED'; + $adGroupCriterion->finalUrls = array('http://www.example.com/mars'); + + // Set bids (optional). + $bid = new CpcBid(); + $bid->bid = new Money(500000); + $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); + $biddingStrategyConfiguration->bids[] = $bid; + $adGroupCriterion->biddingStrategyConfiguration = + $biddingStrategyConfiguration; + + $adGroupCriteria[] = $adGroupCriterion; + + // Create operation. + $operation = new AdGroupCriterionOperation(); + $operation->operand = $adGroupCriterion; + $operation->operator = 'ADD'; + $operations[] = $operation; + } + + // Make the mutate request. + $result = $adGroupCriterionService->mutate($operations); + + // Display results. + foreach ($result->value as $adGroupCriterion) { + printf("Keyword with text '%s', match type '%s', and ID '%s' was added.\n", + $adGroupCriterion->criterion->text, + $adGroupCriterion->criterion->matchType, + $adGroupCriterion->criterion->id); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddKeywordsExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201702/Express/GetPromotions.php b/examples/AdWords/v201705/BasicOperations/GetAdGroups.php similarity index 70% rename from examples/AdWords/v201702/Express/GetPromotions.php rename to examples/AdWords/v201705/BasicOperations/GetAdGroups.php index 73937eec8..2d2c4a5c9 100755 --- a/examples/AdWords/v201702/Express/GetPromotions.php +++ b/examples/AdWords/v201705/BasicOperations/GetAdGroups.php @@ -1,7 +1,7 @@ SetExpressBusinessId($businessId); - +function GetAdGroupsExample(AdWordsUser $user, $campaignId) { // Get the service, which loads the required classes. - $promotionService = $user->GetService('PromotionService', ADWORDS_VERSION); + $adGroupService = $user->GetService('AdGroupService', ADWORDS_VERSION); // Create selector. $selector = new Selector(); - $selector->fields = array('PromotionId', 'Name', 'Status', 'DestinationUrl', - 'CallTrackingEnabled', 'Budget', 'PromotionCriteria', 'ExpandedCreative', - 'CampaignIds'); + $selector->fields = array('Id', 'Name'); $selector->ordering[] = new OrderBy('Name', 'ASCENDING'); + // Create predicates. + $selector->predicates[] = + new Predicate('CampaignId', 'IN', array($campaignId)); + // Create paging controls. $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); do { // Make the get request. - $page = $promotionService->get($selector); + $page = $adGroupService->get($selector); // Display results. if (isset($page->entries)) { - foreach ($page->entries as $promotion) { - printf("Express promotion found with name '%s' " . - "id %s destinationUrl: %s\n", $promotion->name, $promotion->id, - $promotion->destinationUrl); + foreach ($page->entries as $adGroup) { + printf("Ad group with name '%s' and ID '%s' was found.\n", + $adGroup->name, $adGroup->id); } } else { - print "No express promotions were found.\n"; + print "No ad groups were found.\n"; } // Advance the paging index. @@ -85,7 +85,7 @@ function GetPromotionsExample(AdWordsUser $user, $businessId) { $user->LogAll(); // Run the example. - GetPromotionsExample($user, $businessId); + GetAdGroupsExample($user, $campaignId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201609/Express/GetExpressBusinesses.php b/examples/AdWords/v201705/BasicOperations/GetCampaigns.php similarity index 72% rename from examples/AdWords/v201609/Express/GetExpressBusinesses.php rename to examples/AdWords/v201705/BasicOperations/GetCampaigns.php index d25b78790..806575262 100755 --- a/examples/AdWords/v201609/Express/GetExpressBusinesses.php +++ b/examples/AdWords/v201705/BasicOperations/GetCampaigns.php @@ -1,7 +1,7 @@ GetService('ExpressBusinessService', - ADWORDS_VERSION); + $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION); // Create selector. $selector = new Selector(); - $selector->fields = array('Id', 'Name', 'Website', 'Status'); + $selector->fields = array('Id', 'Name'); $selector->ordering[] = new OrderBy('Name', 'ASCENDING'); // Create paging controls. @@ -47,22 +46,16 @@ function GetExpressBusinessesExample(AdWordsUser $user) { do { // Make the get request. - $page = $businessService->get($selector); + $page = $campaignService->get($selector); // Display results. if (isset($page->entries)) { - foreach ($page->entries as $business) { - printf( - "Express business found with name '%s' id %s website: %s " - . "status: %s\n", - $business->name, - $business->id, - $business->website, - $business->status - ); + foreach ($page->entries as $campaign) { + printf("Campaign with name '%s' and ID '%s' was found.\n", + $campaign->name, $campaign->id); } } else { - print "No express businesses were found.\n"; + print "No campaigns were found.\n"; } // Advance the paging index. @@ -84,7 +77,7 @@ function GetExpressBusinessesExample(AdWordsUser $user) { $user->LogAll(); // Run the example. - GetExpressBusinessesExample($user); + GetCampaignsExample($user); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201705/BasicOperations/GetCampaignsWithAwql.php b/examples/AdWords/v201705/BasicOperations/GetCampaignsWithAwql.php new file mode 100755 index 000000000..4bb617966 --- /dev/null +++ b/examples/AdWords/v201705/BasicOperations/GetCampaignsWithAwql.php @@ -0,0 +1,83 @@ +GetService('CampaignService', ADWORDS_VERSION); + + // Create AWQL query. + $query = 'SELECT Id, Name, Status ORDER BY Name'; + + // Create paging controls. + $offset = 0; + + do { + $pageQuery = sprintf('%s LIMIT %d,%d', $query, $offset, + AdWordsConstants::RECOMMENDED_PAGE_SIZE); + // Make the query request. + $page = $campaignService->query($pageQuery); + + // Display results. + if (isset($page->entries)) { + foreach ($page->entries as $campaign) { + printf("Campaign with name '%s' and ID '%s' was found.\n", + $campaign->name, $campaign->id); + } + } else { + print "No campaigns were found.\n"; + } + + // Advance the paging offset. + $offset += AdWordsConstants::RECOMMENDED_PAGE_SIZE; + } while ($page->totalNumEntries > $offset); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetCampaignsWithAwqlExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/BasicOperations/GetExpandedTextAds.php b/examples/AdWords/v201705/BasicOperations/GetExpandedTextAds.php new file mode 100755 index 000000000..792460341 --- /dev/null +++ b/examples/AdWords/v201705/BasicOperations/GetExpandedTextAds.php @@ -0,0 +1,101 @@ +GetService('AdGroupAdService', ADWORDS_VERSION); + + // Create selector. + $selector = new Selector(); + $selector->fields = + array('Id', 'Status', 'HeadlinePart1', 'HeadlinePart2', 'Description'); + $selector->ordering[] = new OrderBy('Id', 'ASCENDING'); + + // Create predicates. + $selector->predicates[] = new Predicate('AdGroupId', 'IN', array($adGroupId)); + $selector->predicates[] = + new Predicate('AdType', 'IN', array('EXPANDED_TEXT_AD')); + $selector->predicates[] = + new Predicate('Status', 'IN', array('ENABLED', 'PAUSED')); + + // Create paging controls. + $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); + + do { + // Make the get request. + $page = $adGroupAdService->get($selector); + + // Display results. + if (isset($page->entries)) { + foreach ($page->entries as $adGroupAd) { + printf( + "Expanded text ad with ID '%d' status '%s', and headline '%s - %s' " + . "was found.\n", + $adGroupAd->ad->id, + $adGroupAd->status, + $adGroupAd->ad->headlinePart1, + $adGroupAd->ad->headlinePart2 + ); + } + } else { + print "No expanded text ads were found.\n"; + } + + // Advance the paging index. + $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE; + } while ($page->totalNumEntries > $selector->paging->startIndex); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetExpandedTextAdsExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201702/Express/GetProductServices.php b/examples/AdWords/v201705/BasicOperations/GetKeywords.php similarity index 62% rename from examples/AdWords/v201702/Express/GetProductServices.php rename to examples/AdWords/v201705/BasicOperations/GetKeywords.php index 3d081c8f6..eae7d672f 100755 --- a/examples/AdWords/v201702/Express/GetProductServices.php +++ b/examples/AdWords/v201705/BasicOperations/GetKeywords.php @@ -1,7 +1,7 @@ GetService('ProductServiceService', - ADWORDS_VERSION); + $adGroupCriterionService = + $user->GetService('AdGroupCriterionService', ADWORDS_VERSION); // Create selector. $selector = new Selector(); - $selector->fields = array('ProductServiceText'); + $selector->fields = array('Id', 'CriteriaType', 'KeywordMatchType', + 'KeywordText'); + $selector->ordering[] = new OrderBy('KeywordText', 'ASCENDING'); // Create predicates. - $selector->predicates[] = new Predicate('ProductServiceText', 'EQUALS', - array($productServiceSuggestion)); - $selector->predicates[] = new Predicate('Locale', 'EQUALS', - array($localeText)); + $selector->predicates[] = new Predicate('AdGroupId', 'IN', array($adGroupId)); + $selector->predicates[] = + new Predicate('CriteriaType', 'IN', array('KEYWORD')); // Create paging controls. $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); do { // Make the get request. - $page = $productServiceService->get($selector); + $page = $adGroupCriterionService->get($selector); // Display results. if (isset($page->entries)) { - foreach ($page->entries as $productService) { - printf("Product/service with text '%s' found\n", $productService->text); + foreach ($page->entries as $adGroupCriterion) { + printf("Keyword with text '%s', match type '%s', criteria type '%s', " + . "and ID '%s' was found.\n", + $adGroupCriterion->criterion->text, + $adGroupCriterion->criterion->matchType, + $adGroupCriterion->criterion->type, + $adGroupCriterion->criterion->id); } } else { - print "No products/services were found.\n"; + print "No keywords were found.\n"; } // Advance the paging index. @@ -87,7 +92,7 @@ function GetProductServicesExample(AdWordsUser $user, $productServiceSuggestion, $user->LogAll(); // Run the example. - GetProductServicesExample($user, $productServiceSuggestion, $localeText); + GetKeywordsExample($user, $adGroupId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201702/Express/GetExpressBusinesses.php b/examples/AdWords/v201705/BasicOperations/GetLabels.php similarity index 70% rename from examples/AdWords/v201702/Express/GetExpressBusinesses.php rename to examples/AdWords/v201705/BasicOperations/GetLabels.php index f7a554f7d..5ad1c33ee 100755 --- a/examples/AdWords/v201702/Express/GetExpressBusinesses.php +++ b/examples/AdWords/v201705/BasicOperations/GetLabels.php @@ -1,7 +1,6 @@ GetService('ExpressBusinessService', - ADWORDS_VERSION); + $labelService = $user->GetService('LabelService', ADWORDS_VERSION); // Create selector. $selector = new Selector(); - $selector->fields = array('Id', 'Name', 'Website', 'Status'); - $selector->ordering[] = new OrderBy('Name', 'ASCENDING'); + $selector->fields = array('LabelId', 'LabelName'); + $selector->ordering[] = new OrderBy('LabelName', 'ASCENDING'); // Create paging controls. $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); do { // Make the get request. - $page = $businessService->get($selector); + $page = $labelService->get($selector); // Display results. if (isset($page->entries)) { - foreach ($page->entries as $business) { - printf( - "Express business found with name '%s' id %s website: %s " - . "status: %s\n", - $business->name, - $business->id, - $business->website, - $business->status - ); + foreach ($page->entries as $label) { + printf("Label with name '%s' and ID '%s' was found.\n", + $label->name, $label->id); } } else { - print "No express businesses were found.\n"; + print "No labels were found.\n"; } // Advance the paging index. @@ -84,7 +76,7 @@ function GetExpressBusinessesExample(AdWordsUser $user) { $user->LogAll(); // Run the example. - GetExpressBusinessesExample($user); + GetLabelsExample($user); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201609/Express/GetProductServices.php b/examples/AdWords/v201705/BasicOperations/GetTextAds.php similarity index 64% rename from examples/AdWords/v201609/Express/GetProductServices.php rename to examples/AdWords/v201705/BasicOperations/GetTextAds.php index 579c921c7..0ef70a747 100755 --- a/examples/AdWords/v201609/Express/GetProductServices.php +++ b/examples/AdWords/v201705/BasicOperations/GetTextAds.php @@ -1,7 +1,6 @@ GetService('ProductServiceService', - ADWORDS_VERSION); + $adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION); // Create selector. $selector = new Selector(); - $selector->fields = array('ProductServiceText'); + $selector->fields = array('Headline', 'Id'); + $selector->ordering[] = new OrderBy('Headline', 'ASCENDING'); // Create predicates. - $selector->predicates[] = new Predicate('ProductServiceText', 'EQUALS', - array($productServiceSuggestion)); - $selector->predicates[] = new Predicate('Locale', 'EQUALS', - array($localeText)); + $selector->predicates[] = new Predicate('AdGroupId', 'IN', array($adGroupId)); + $selector->predicates[] = new Predicate('AdType', 'IN', array('TEXT_AD')); + // By default disabled ads aren't returned by the selector. To return them + // include the DISABLED status in a predicate. + $selector->predicates[] = + new Predicate('Status', 'IN', array('ENABLED', 'PAUSED', 'DISABLED')); // Create paging controls. $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); do { // Make the get request. - $page = $productServiceService->get($selector); + $page = $adGroupAdService->get($selector); // Display results. if (isset($page->entries)) { - foreach ($page->entries as $productService) { - printf("Product/service with text '%s' found\n", $productService->text); + foreach ($page->entries as $adGroupAd) { + printf("Text ad with headline '%s' and ID '%s' was found.\n", + $adGroupAd->ad->headline, $adGroupAd->ad->id); } } else { - print "No products/services were found.\n"; + print "No text ads were found.\n"; } // Advance the paging index. @@ -87,7 +88,7 @@ function GetProductServicesExample(AdWordsUser $user, $productServiceSuggestion, $user->LogAll(); // Run the example. - GetProductServicesExample($user, $productServiceSuggestion, $localeText); + GetTextAdsExample($user, $adGroupId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201705/BasicOperations/PauseAd.php b/examples/AdWords/v201705/BasicOperations/PauseAd.php new file mode 100755 index 000000000..2a443cfdb --- /dev/null +++ b/examples/AdWords/v201705/BasicOperations/PauseAd.php @@ -0,0 +1,91 @@ +GetService('AdGroupAdService', ADWORDS_VERSION); + + // Create ad using an existing ID. Use the base class Ad instead of + // ExpandedTextAd to avoid having to set ad-specific fields. + $ad = new Ad(); + $ad->id = $adId; + + // Create ad group ad. + $adGroupAd = new AdGroupAd(); + $adGroupAd->adGroupId = $adGroupId; + $adGroupAd->ad = $ad; + + // Update the status. + $adGroupAd->status = 'PAUSED'; + + // Create operation. + $operation = new AdGroupAdOperation(); + $operation->operand = $adGroupAd; + $operation->operator = 'SET'; + + $operations = array($operation); + + // Make the mutate request. + $result = $adGroupAdService->mutate($operations); + + // Display result. + $adGroupAd = $result->value[0]; + printf("Ad of type '%s' with ID '%s' has updated status '%s'.\n", + $adGroupAd->ad->AdType, $adGroupAd->ad->id, $adGroupAd->status); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + PauseAdExample($user, $adGroupId, $adId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201609/Express/UpdatePromotion.php b/examples/AdWords/v201705/BasicOperations/RemoveAd.php similarity index 57% rename from examples/AdWords/v201609/Express/UpdatePromotion.php rename to examples/AdWords/v201705/BasicOperations/RemoveAd.php index 9a5cdcd68..d8de00e1f 100755 --- a/examples/AdWords/v201609/Express/UpdatePromotion.php +++ b/examples/AdWords/v201705/BasicOperations/RemoveAd.php @@ -1,7 +1,6 @@ SetExpressBusinessId($businessId); - +function RemoveAdExample(AdWordsUser $user, $adGroupId, $adId) { // Get the service, which loads the required classes. - $promotionService = $user->GetService('PromotionService', - ADWORDS_VERSION); + $adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION); + + // Create base class ad to avoid setting type specific fields. + $ad = new Ad(); + $ad->id = $adId; - // Update the budget for the promotion. - $promotion = new Promotion(); - $promotion->id = $promotionId; - $newBudget = new Money(); - $newBudget->microAmount = 2000000; - $promotion->budget = $newBudget; + // Create ad group ad. + $adGroupAd = new AdGroupAd(); + $adGroupAd->adGroupId = $adGroupId; + $adGroupAd->ad = $ad; - $operations = array(); + // Create operation. + $operation = new AdGroupAdOperation(); + $operation->operand = $adGroupAd; + $operation->operator = 'REMOVE'; - $operation = new PromotionOperation(); - $operation->operand = $promotion; - $operation->operator = 'SET'; - $operations[] = $operation; + $operations = array($operation); - $result = $promotionService->mutate($operations); - $mutatedPromotion = $result[0]; + // Make the mutate request. + $result = $adGroupAdService->mutate($operations); - printf("Promotion ID %d for business ID %d now has budget micro amount %d\n", - $mutatedPromotion->id, $businessId, - $mutatedPromotion->budget->microAmount); + // Display result. + $adGroupAd = $result->value[0]; + printf("Ad with ID '%d' was removed.\n", $adGroupAd->ad->id); } // Don't run the example if the file is being included. @@ -80,7 +79,7 @@ function UpdatePromotionExample(AdWordsUser $user, $businessId, $promotionId) { $user->LogAll(); // Run the example. - UpdatePromotionExample($user, $businessId, $promotionId); + RemoveAdExample($user, $adGroupId, $adId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201702/Express/UpdateExpressBusiness.php b/examples/AdWords/v201705/BasicOperations/RemoveAdGroup.php similarity index 62% rename from examples/AdWords/v201702/Express/UpdateExpressBusiness.php rename to examples/AdWords/v201705/BasicOperations/RemoveAdGroup.php index a31f60ea1..d76a76601 100755 --- a/examples/AdWords/v201702/Express/UpdateExpressBusiness.php +++ b/examples/AdWords/v201705/BasicOperations/RemoveAdGroup.php @@ -1,7 +1,6 @@ GetService('ExpressBusinessService', - ADWORDS_VERSION); + $adGroupService = $user->GetService('AdGroupService', ADWORDS_VERSION); - // Update the name and website for the business. - $business = new ExpressBusiness(); - $business->id = $businessId; - $business->name = 'Express Interplanetary Cruise #' . uniqid(); - $business->website = 'http://www.example.com/?myParam=' . uniqid(); + // Create ad group with REMOVED status. + $adGroup = new AdGroup(); + $adGroup->id = $adGroupId; + $adGroup->status = 'REMOVED'; - $operations = array(); - - $operation = new ExpressBusinessOperation(); - $operation->operand = $business; + // Create operations. + $operation = new AdGroupOperation(); + $operation->operand = $adGroup; $operation->operator = 'SET'; - $operations[] = $operation; - $result = $businessService->mutate($operations); - $mutatedBusiness = $result[0]; + $operations = array($operation); + + // Make the mutate request. + $result = $adGroupService->mutate($operations); - printf("Express business with ID %d and name '%s' was updated\n", - $mutatedBusiness->id, $mutatedBusiness->name); + // Display result. + $adGroup = $result->value[0]; + printf("Ad group with ID '%d' was removed.\n", $adGroup->id); } // Don't run the example if the file is being included. @@ -74,7 +73,7 @@ function UpdateExpressBusinessExample(AdWordsUser $user, $businessId) { $user->LogAll(); // Run the example. - UpdateExpressBusinessExample($user, $businessId); + RemoveAdGroupExample($user, $adGroupId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201609/Express/UpdateExpressBusiness.php b/examples/AdWords/v201705/BasicOperations/RemoveCampaign.php similarity index 62% rename from examples/AdWords/v201609/Express/UpdateExpressBusiness.php rename to examples/AdWords/v201705/BasicOperations/RemoveCampaign.php index a69adacbe..c2f11008f 100755 --- a/examples/AdWords/v201609/Express/UpdateExpressBusiness.php +++ b/examples/AdWords/v201705/BasicOperations/RemoveCampaign.php @@ -1,7 +1,6 @@ GetService('ExpressBusinessService', - ADWORDS_VERSION); + $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION); - // Update the name and website for the business. - $business = new ExpressBusiness(); - $business->id = $businessId; - $business->name = 'Express Interplanetary Cruise #' . uniqid(); - $business->website = 'http://www.example.com/?myParam=' . uniqid(); + // Create campaign with REMOVED status. + $campaign = new Campaign(); + $campaign->id = $campaignId; + $campaign->status = 'REMOVED'; - $operations = array(); - - $operation = new ExpressBusinessOperation(); - $operation->operand = $business; + // Create operations. + $operation = new CampaignOperation(); + $operation->operand = $campaign; $operation->operator = 'SET'; - $operations[] = $operation; - $result = $businessService->mutate($operations); - $mutatedBusiness = $result[0]; + $operations = array($operation); + + // Make the mutate request. + $result = $campaignService->mutate($operations); - printf("Express business with ID %d and name '%s' was updated\n", - $mutatedBusiness->id, $mutatedBusiness->name); + // Display result. + $campaign = $result->value[0]; + printf("Campaign with ID '%d' was removed.\n", $campaign->id); } // Don't run the example if the file is being included. @@ -74,7 +73,7 @@ function UpdateExpressBusinessExample(AdWordsUser $user, $businessId) { $user->LogAll(); // Run the example. - UpdateExpressBusinessExample($user, $businessId); + RemoveCampaignExample($user, $campaignId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201705/BasicOperations/RemoveKeyword.php b/examples/AdWords/v201705/BasicOperations/RemoveKeyword.php new file mode 100755 index 000000000..47781256c --- /dev/null +++ b/examples/AdWords/v201705/BasicOperations/RemoveKeyword.php @@ -0,0 +1,88 @@ +GetService('AdGroupCriterionService', ADWORDS_VERSION); + + // Create criterion using an existing ID. Use the base class Criterion + // instead of Keyword to avoid having to set keyword-specific fields. + $criterion = new Criterion(); + $criterion->id = $criterionId; + + // Create ad group criterion. + $adGroupCriterion = new AdGroupCriterion(); + $adGroupCriterion->adGroupId = $adGroupId; + $adGroupCriterion->criterion = new Criterion($criterionId); + + // Create operation. + $operation = new AdGroupCriterionOperation(); + $operation->operand = $adGroupCriterion; + $operation->operator = 'REMOVE'; + + $operations = array($operation); + + // Make the mutate request. + $result = $adGroupCriterionService->mutate($operations); + + // Display result. + $adGroupCriterion = $result->value[0]; + printf("Keyword with ID '%d' was removed.\n", + $adGroupCriterion->criterion->id); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + RemoveKeywordExample($user, $adGroupId, $criterionId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/BasicOperations/UpdateAdGroup.php b/examples/AdWords/v201705/BasicOperations/UpdateAdGroup.php new file mode 100755 index 000000000..6962bc126 --- /dev/null +++ b/examples/AdWords/v201705/BasicOperations/UpdateAdGroup.php @@ -0,0 +1,89 @@ +GetService('AdGroupService', ADWORDS_VERSION); + + // Create ad group using an existing ID. + $adGroup = new AdGroup(); + $adGroup->id = $adGroupId; + + // Update the bid. + $bid = new CpcBid(); + $bid->bid = new Money(0.75 * AdWordsConstants::MICROS_PER_DOLLAR); + $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); + $biddingStrategyConfiguration->bids[] = $bid; + $adGroup->biddingStrategyConfiguration = $biddingStrategyConfiguration; + + // Create operation. + $operation = new AdGroupOperation(); + $operation->operand = $adGroup; + $operation->operator = 'SET'; + + $operations = array($operation); + + // Make the mutate request. + $result = $adGroupService->mutate($operations); + + // Display result. + $adGroup = $result->value[0]; + printf("Ad group with ID '%s' has updated default bid '$%s'.\n", $adGroup->id, + $adGroup->biddingStrategyConfiguration->bids[0]->bid->microAmount / + AdWordsConstants::MICROS_PER_DOLLAR); + +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + UpdateAdGroupExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201607/Express/UpdateExpressBusiness.php b/examples/AdWords/v201705/BasicOperations/UpdateCampaign.php similarity index 62% rename from examples/AdWords/v201607/Express/UpdateExpressBusiness.php rename to examples/AdWords/v201705/BasicOperations/UpdateCampaign.php index 468c6f746..bd53473b8 100755 --- a/examples/AdWords/v201607/Express/UpdateExpressBusiness.php +++ b/examples/AdWords/v201705/BasicOperations/UpdateCampaign.php @@ -1,7 +1,7 @@ GetService('ExpressBusinessService', - ADWORDS_VERSION); + $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION); - // Update the name and website for the business. - $business = new ExpressBusiness(); - $business->id = $businessId; - $business->name = 'Express Interplanetary Cruise #' . uniqid(); - $business->website = 'http://www.example.com/?myParam=' . uniqid(); + // Create campaign using an existing ID. + $campaign = new Campaign(); + $campaign->id = $campaignId; + $campaign->status = 'PAUSED'; - $operations = array(); - - $operation = new ExpressBusinessOperation(); - $operation->operand = $business; + // Create operation. + $operation = new CampaignOperation(); + $operation->operand = $campaign; $operation->operator = 'SET'; - $operations[] = $operation; - $result = $businessService->mutate($operations); - $mutatedBusiness = $result[0]; + $operations = array($operation); + + // Make the mutate request. + $result = $campaignService->mutate($operations); - printf("Express business with ID %d and name '%s' was updated\n", - $mutatedBusiness->id, $mutatedBusiness->name); + // Display result. + $campaign = $result->value[0]; + printf("Campaign with ID '%s' was paused.\n", $campaign->id); } // Don't run the example if the file is being included. @@ -74,7 +74,7 @@ function UpdateExpressBusinessExample(AdWordsUser $user, $businessId) { $user->LogAll(); // Run the example. - UpdateExpressBusinessExample($user, $businessId); + UpdateCampaignExample($user, $campaignId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201705/BasicOperations/UpdateKeyword.php b/examples/AdWords/v201705/BasicOperations/UpdateKeyword.php new file mode 100755 index 000000000..b289ba8d2 --- /dev/null +++ b/examples/AdWords/v201705/BasicOperations/UpdateKeyword.php @@ -0,0 +1,89 @@ +GetService('AdGroupCriterionService', ADWORDS_VERSION); + + // Create ad group criterion. + $adGroupCriterion = new BiddableAdGroupCriterion(); + $adGroupCriterion->adGroupId = $adGroupId; + // Create criterion using an existing ID. Use the base class Criterion + // instead of Keyword to avoid having to set keyword-specific fields. + $adGroupCriterion->criterion = new Criterion($criterionId); + + // Update final URL. + $adGroupCriterion->finalUrls = array('http://www.example.com/new'); + + // Create operation. + $operation = new AdGroupCriterionOperation(); + $operation->operand = $adGroupCriterion; + $operation->operator = 'SET'; + + $operations = array($operation); + + // Make the mutate request. + $result = $adGroupCriterionService->mutate($operations); + + // Display result. + $adGroupCriterion = $result->value[0]; + printf("Keyword with ID '%s' has updated final URL '%s'.\n", + $adGroupCriterion->criterion->id, $adGroupCriterion->finalUrls->urls[0]); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + UpdateKeywordExample($user, $adGroupId, $criterionId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/CampaignManagement/AddCampaignLabels.php b/examples/AdWords/v201705/CampaignManagement/AddCampaignLabels.php new file mode 100755 index 000000000..145104bc8 --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/AddCampaignLabels.php @@ -0,0 +1,84 @@ +GetService('CampaignService', ADWORDS_VERSION); + + $operations = array(); + foreach ($campaignIds as $campaignId) { + $campaignLabel = new CampaignLabel(); + $campaignLabel->campaignId = $campaignId; + $campaignLabel->labelId = $labelId; + + $operation = new CampaignLabelOperation(); + $operation->operand = $campaignLabel; + $operation->operator = 'ADD'; + + $operations[] = $operation; + } + + // Make the mutate request. + $result = $campaignService->mutateLabel($operations); + + // Display results. + foreach ($result->value as $campaignLabel) { + printf("Campaign label for campaign ID '%s' and label ID '%d' was added.\n", + $campaignLabel->campaignId, $campaignLabel->labelId); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddCampaignLabelsExample($user, $campaignIds, $labelId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/CampaignManagement/AddCompleteCampaignsUsingBatchJob.php b/examples/AdWords/v201705/CampaignManagement/AddCompleteCampaignsUsingBatchJob.php new file mode 100755 index 000000000..cba2f4170 --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/AddCompleteCampaignsUsingBatchJob.php @@ -0,0 +1,386 @@ +GetService('BatchJobService', ADWORDS_VERSION); + + // Create a BatchJob. + $addOp = new BatchJobOperation(); + $addOp->operator = 'ADD'; + $addOp->operand = new BatchJob(); + $addOps[] = $addOp; + + $result = $batchJobService->mutate($addOps); + $batchJob = $result->value[0]; + + // Get the upload URL from the new job. + $uploadUrl = $batchJob->uploadUrl->url; + printf("Created BatchJob with ID %d, status '%s' and upload 'URL' %s.\n", + $batchJob->id, $batchJob->status, $uploadUrl); + + $namePrefix = uniqid(); + // Create and add an operation to create a new budget. + $budgetOperation = buildBudgetOperation($namePrefix); + $operations = array($budgetOperation); + + // Create and add an operation to create new campaigns. + $campaignOperations = buildCampaignOperations($namePrefix, $budgetOperation); + $operations = array_merge($operations, $campaignOperations); + + // Create and add operations to create new negative keyword criteria for + // each campaign. + $campaignCriterionOperations = + buildCampaignCriterionOperations($campaignOperations); + $operations = array_merge($operations, $campaignCriterionOperations); + + // Create and add operations to create new ad groups. + $adGroupOperations = buildAdGroupOperations($namePrefix, $campaignOperations); + $operations = array_merge($operations, $adGroupOperations); + + // Create and add operations to create new ad group criteria (keywords). + $adGroupCriterionOperations = + buildAdGroupCriterionOperations($adGroupOperations); + $operations = array_merge($operations, $adGroupCriterionOperations); + + // Create and add operations to create new ad group ads (text ads). + $adGroupAdOperations = buildAdGroupAdOperations($adGroupOperations); + $operations = array_merge($operations, $adGroupAdOperations); + + // Use BatchJobUtils to upload all operations. + $batchJobUtils = new BatchJobUtils($batchJob->uploadUrl->url); + $batchJobUtils->UploadBatchJobOperations($operations); + + printf("Uploaded %d operations for batch job with ID %d.\n", + count($operations), $batchJob->id); + + // Poll for completion of the batch job using an exponential back off. + $pollAttempts = 0; + $isPending = true; + do { + $sleepSeconds = POLL_FREQUENCY_SECONDS * pow(2, $pollAttempts); + printf("Sleeping %d seconds...\n", $sleepSeconds); + sleep($sleepSeconds); + + $selector = new Selector(); + $selector->fields = array('Id', 'Status', 'DownloadUrl', 'ProcessingErrors', + 'ProgressStats'); + $selector->predicates[] = new Predicate('Id', 'EQUALS', $batchJob->id); + $batchJob = $batchJobService->get($selector)->entries[0]; + printf("Batch job ID %d has status '%s'.\n", $batchJob->id, + $batchJob->status); + + $pollAttempts++; + if ($batchJob->status !== 'ACTIVE' && + $batchJob->status !== 'AWAITING_FILE' && + $batchJob->status !== 'CANCELING') { + $isPending = false; + } + } while ($isPending && $pollAttempts <= MAX_POLL_ATTEMPTS); + + if ($isPending) { + throw new BatchJobException( + sprintf("Job is still pending state after polling %d times.", + MAX_POLL_ATTEMPTS)); + } + + if ($batchJob->processingErrors !== null) { + $i = 0; + foreach ($batchJob->processingErrors as $processingError) { + printf( + " Processing error [%d]: errorType=%s, trigger=%s, errorString=%s," + . " fieldPath=%s, reason=%s\n", + $i++, + $processingError->ApiErrorType, + $processingError->trigger, + $processingError->errorString, + $processingError->fieldPath, + $processingError->reason + ); + } + } else { + printf("No processing errors found.\n"); + } + + if ($batchJob->downloadUrl !== null && $batchJob->downloadUrl->url !== null) { + $xmlResponse = + $batchJobUtils->DownloadBatchJobResults($batchJob->downloadUrl->url); + printf("Downloaded results from %s:\n", $batchJob->downloadUrl->url); + $deserializer = new XmlDeserializer(BatchJobUtils::$CLASS_MAP); + $mutateResponse = $deserializer->ConvertXmlToObject($xmlResponse); + if (empty($mutateResponse)) { + printf(" No results available.\n"); + } else { + foreach ($mutateResponse->rval as $mutateResult) { + $outcome = $mutateResult->errorList === null ? 'SUCCESS' : 'FAILURE'; + printf(" Operation [%d] - %s\n", $mutateResult->index, $outcome); + } + } + } else { + printf("No results available for download.\n"); + } +} + +/** + * Builds objects of AdGroupAdOperation for creating an ad group ad for + * ad groups in the specified ad group operations. + * + * @param array $adGroupOperations an array of AdGroupOperation + * @return array an array of AdGroupAdOperation + */ +function buildAdGroupAdOperations(array $adGroupOperations) { + $operations = array(); + foreach ($adGroupOperations as $adGroupOperation) { + $adGroupId = $adGroupOperation->operand->id; + $adGroupAd = new AdGroupAd(); + $adGroupAd->adGroupId = $adGroupId; + + $expandedTextAd = new ExpandedTextAd(); + $expandedTextAd->headlinePart1 = 'Luxury Cruise to Mars'; + $expandedTextAd->headlinePart2 = 'Visit the Red Planet in style.'; + $expandedTextAd->description = 'Low-gravity fun for everyone!'; + $expandedTextAd->finalUrls[] = 'http://www.example.com/1'; + + $adGroupAd->ad = $expandedTextAd; + + $operation = new AdGroupAdOperation(); + $operation->operator = 'ADD'; + $operation->operand = $adGroupAd; + + $operations[] = $operation; + } + return $operations; +} + +/** + * Builds objects of AdGroupCriterionOperation for creating biddable criteria + * (as keywords) for ad groups in the specified ad group operations. 50% of + * keywords are created with some invalid characters to demonstrate how + * BatchJobService returns information about such errors. + * + * @param array $adGroupOperations an array of AdGroupOperation + * @return array an array of AdGroupCriterionOperation + */ +function buildAdGroupCriterionOperations(array $adGroupOperations) { + $adGroupCriteriaOperations = array(); + + // Create AdGroupCriterionOperations to add keywords. + foreach ($adGroupOperations as $adGroupOperation) { + $newAdGroupId = $adGroupOperation->operand->id; + for ($i = 0; $i < NUMBER_OF_KEYWORDS_TO_ADD; $i++) { + // Create Keyword. + $text = sprintf("mars%d", $i); + + // Make 50% of keywords invalid to demonstrate error handling. + if ($i % 2 == 0) { + $text = $text . '!!!'; + } + $keyword = new Keyword(); + $keyword->text = $text; + $keyword->matchType = 'BROAD'; + + // Create BiddableAdGroupCriterion. + $biddableAdGroupCriterion = new BiddableAdGroupCriterion(); + $biddableAdGroupCriterion->adGroupId = $newAdGroupId; + $biddableAdGroupCriterion->criterion = $keyword; + + // Create AdGroupCriterionOperation. + $operation = new AdGroupCriterionOperation(); + $operation->operand = $biddableAdGroupCriterion; + $operation->operator = 'ADD'; + + // Add to list. + $adGroupCriteriaOperations[] = $operation; + } + } + return $adGroupCriteriaOperations; +} + +/** + * Builds objects of AdGroupOperation for creating ad groups for campaigns in + * the specified campaign operations. + * + * @param string $namePrefix a prefix string used to name ad groups + * @param array $campaignOperations an array of CampaignOperation + * @return array an array of AdGroupOperation + */ +function buildAdGroupOperations($namePrefix, array $campaignOperations) { + $operations = array(); + foreach ($campaignOperations as $campaignOperation) { + for ($i = 0; $i < NUMBER_OF_ADGROUPS_TO_ADD; $i++) { + $adGroup = new AdGroup(); + $adGroup->campaignId = $campaignOperation->operand->id; + $adGroup->id = TempIdGenerator::Generate(); + $adGroup->name = sprintf("Batch Ad Group %s.%s", $namePrefix, + strval($adGroup->id)); + + $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); + $bid = new CpcBid(); + $bid->bid = new Money(10000000); + $biddingStrategyConfiguration->bids[] = $bid; + + $adGroup->biddingStrategyConfiguration = $biddingStrategyConfiguration; + + $operation = new AdGroupOperation(); + $operation->operand = $adGroup; + $operation->operator = 'ADD'; + + $operations[] = $operation; + } + } + return $operations; +} + +/** + * Builds objects of CampaignCriterionOperation for creating a negative campaign + * criterion (as keyword) for campaigns in the specified campaign operations. + * + * @param array $campaignOperations an array of CampaignOperation + * @return array an array of CampaignCriterionOperation + */ +function buildCampaignCriterionOperations(array $campaignOperations) { + $operations = array(); + foreach ($campaignOperations as $campaignOperation) { + $keyword = new Keyword(); + $keyword->matchType = 'BROAD'; + $keyword->text = 'venus'; + + $negativeCriterion = new NegativeCampaignCriterion(); + $negativeCriterion->campaignId = $campaignOperation->operand->id; + $negativeCriterion->criterion = $keyword; + + $operation = new CampaignCriterionOperation(); + $operation->operand = $negativeCriterion; + $operation->operator = 'ADD'; + + $operations[] = $operation; + } + return $operations; +} + +/** + * Builds objects of CampaignOperation for creating a campaign using the ID of + * budget in the specified budget operation. + * + * @param string $namePrefix a prefix string used to name campaigns + * @param BudgetOperation $budgetOperation an object of BudgetOperation + * @return array an array of CampaignOperation + */ +function buildCampaignOperations($namePrefix, + BudgetOperation $budgetOperation) { + $budgetId = $budgetOperation->operand->budgetId; + + $operations = array(); + for ($i = 0; $i < NUMBER_OF_CAMPAIGNS_TO_ADD; $i++) { + $campaign = new Campaign(); + $campaign->id = TempIdGenerator::Generate(); + $campaign->name = sprintf("Batch Campaign %s.%s", $namePrefix, + strval($campaign->id)); + + // Recommendation: Set the campaign to PAUSED when creating it to stop + // the ads from immediately serving. Set to ENABLED once you've added + // targeting and the ads are ready to serve. + $campaign->status = 'PAUSED'; + $campaign->advertisingChannelType = 'SEARCH'; + + $budget = new Budget(); + $budget->budgetId = $budgetId; + $campaign->budget = $budget; + $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); + $biddingStrategyConfiguration->biddingStrategyType = 'MANUAL_CPC'; + + // You can optionally provide a bidding scheme in place of the type. + $cpcBiddingScheme = new ManualCpcBiddingScheme(); + $cpcBiddingScheme->enhancedCpcEnabled = false; + $biddingStrategyConfiguration->biddingScheme = $cpcBiddingScheme; + + $campaign->biddingStrategyConfiguration = $biddingStrategyConfiguration; + + $operation = new CampaignOperation(); + $operation->operand = $campaign; + $operation->operator = 'ADD'; + $operations[] = $operation; + } + return $operations; +} + +/** + * Builds BudgetOperation for creating a budget. + * + * @param string $namePrefix a prefix string used to name a budget + * @return BudgetOperation an object of BudgetOperation + */ +function buildBudgetOperation($namePrefix) { + $budget = new Budget(); + $budget->budgetId = TempIdGenerator::Generate(); + $budget->name = 'Interplanetary Cruise #' . $namePrefix; + $budgetAmount = new Money(); + $budgetAmount->microAmount = 50000000; + $budget->amount = $budgetAmount; + $budget->deliveryMethod = 'STANDARD'; + + $budgetOperation = new BudgetOperation(); + $budgetOperation->operand = $budget; + $budgetOperation->operator = 'ADD'; + return $budgetOperation; +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddCompleteCampaignUsingBatchJobExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/CampaignManagement/AddDraft.php b/examples/AdWords/v201705/CampaignManagement/AddDraft.php new file mode 100755 index 000000000..9689cf3eb --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/AddDraft.php @@ -0,0 +1,114 @@ +GetService('DraftService', ADWORDS_VERSION); + + // Create a draft. + $draft = new Draft(); + $draft->baseCampaignId = $baseCampaignId; + $draft->draftName = 'Test Draft #' . uniqid(); + + // Create an operation. + $operation = new DraftOperation(); + $operation->operand = $draft; + $operation->operator = 'ADD'; + $operations[] = $operation; + + // Make the mutate request. + $result = $draftService->mutate($operations); + $draft = $result->value[0]; + printf( + "Draft with ID %d, base campaign ID %d, and draft campaign ID" + . " %d was added.\n", + $draft->draftId, + $draft->baseCampaignId, + $draft->draftCampaignId + ); + + // Once the draft is created, you can modify the draft campaign as if it were + // a real campaign. For example, you may add criteria, adjust bids, or even + // include additional ads. Adding a criterion is shown here. + $campaignCriterionService = + $user->GetService('CampaignCriterionService', ADWORDS_VERSION); + + // Create a criterion. + $language = new Language(); + $language->id = 1003; // Spanish + $campaignCriterion = new CampaignCriterion(); + $campaignCriterion->campaignId = $draft->draftCampaignId; + $campaignCriterion->criterion = $language; + + // Create an operation. + $operations = array(); + $operation = new CampaignCriterionOperation(); + $operation->operand = $campaignCriterion; + $operation->operator = 'ADD'; + $operations[] = $operation; + + // Make the mutate request. + $result = $campaignCriterionService->mutate($operations); + $campaignCriterion = $result->value[0]; + + printf("Draft updated to include criteria in the campaign with ID %d.\n", + $draft->draftCampaignId); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddDraftExample($user, $baseCampaignId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/CampaignManagement/AddKeywordsUsingIncrementalBatchJob.php b/examples/AdWords/v201705/CampaignManagement/AddKeywordsUsingIncrementalBatchJob.php new file mode 100755 index 000000000..916917702 --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/AddKeywordsUsingIncrementalBatchJob.php @@ -0,0 +1,253 @@ +GetService('BatchJobService', ADWORDS_VERSION); + + // Create a BatchJob. + $addOp = new BatchJobOperation(); + $addOp->operator = 'ADD'; + $addOp->operand = new BatchJob(); + $addOps[] = $addOp; + + $result = $batchJobService->mutate($addOps); + $batchJob = $result->value[0]; + + // Get the upload URL from the new job. + $uploadUrl = $batchJob->uploadUrl->url; + printf("Created BatchJob with ID %d, status '%s' and upload URL '%s'.\n", + $batchJob->id, $batchJob->status, $uploadUrl); + + // Use BatchJobUtils to upload all operations. + $batchJobUtils = new BatchJobUtils($uploadUrl); + + // Generate and upload the first set of operations. + $adGroupCriterionOperations = + buildAdGroupCriterionOperations($adGroupId); + $batchJobUtils->UploadIncrementalBatchJobOperations( + $adGroupCriterionOperations); + printf("Uploaded %d operations for batch job with ID %d.\n", + count($adGroupCriterionOperations), $batchJob->id); + + // Generate and upload the second set of operations. + $adGroupCriterionOperations = + buildAdGroupCriterionOperations($adGroupId); + $batchJobUtils->UploadIncrementalBatchJobOperations( + $adGroupCriterionOperations); + printf("Uploaded %d operations for batch job with ID %d.\n", + count($adGroupCriterionOperations), $batchJob->id); + + // Generate and upload the third and final set of operations. + $adGroupCriterionOperations = + buildAdGroupCriterionOperations($adGroupId); + $batchJobUtils->UploadIncrementalBatchJobOperations( + $adGroupCriterionOperations, true); + printf("Uploaded %d operations for batch job with ID %d.\n", + count($adGroupCriterionOperations), $batchJob->id); + + // Poll for completion of the batch job using an exponential back off. + $pollAttempts = 0; + $isPending = true; + $wasCancelRequested = false; + + $selector = new Selector(); + $selector->fields = array('Id', 'Status', 'DownloadUrl', 'ProcessingErrors', + 'ProgressStats'); + $selector->predicates[] = new Predicate('Id', 'EQUALS', $batchJob->id); + do { + $sleepSeconds = POLL_FREQUENCY_SECONDS * pow(2, $pollAttempts); + printf("Sleeping %d seconds...\n", $sleepSeconds); + sleep($sleepSeconds); + + $batchJob = $batchJobService->get($selector)->entries[0]; + printf("Batch job ID %d has status '%s'.\n", $batchJob->id, + $batchJob->status); + + $pollAttempts++; + if ($batchJob->status !== 'ACTIVE' && + $batchJob->status !== 'AWAITING_FILE' && + $batchJob->status !== 'CANCELING') { + $isPending = false; + } + + // Optional: Cancel the job if it has not completed after polling + // MAX_POLL_ATTEMPTS times. + if ($isPending && !$wasCancelRequested + && $pollAttempts == MAX_POLL_ATTEMPTS) { + $batchJob->status = 'CANCELING'; + $batchJobSetOperation = new BatchJobOperation(); + $batchJobSetOperation->operand = $batchJob; + $batchJobSetOperation->operator = 'SET'; + + // Only request cancellation once per job. + $wasCancelRequested = true; + try { + $operations[] = $batchJobSetOperation; + $batchJob = $batchJobService->mutate($operations)->value[0]; + printf("Requested cancellation of batch job with ID %d.\n", + $batchJob->id); + // Reset the poll attempt counter to wait for cancellation. + $pollAttempts = 0; + } catch (Exception $e) { + $errors = $e->detail->ApiExceptionFault->errors; + if ($errors !== null + && $errors->enc_value instanceof BatchJobError) { + if ($errors->enc_value->reason === 'INVALID_STATE_CHANGE') { + printf("Attempt to cancel batch job with ID %d was rejected because" + . " the job already completed or was canceled.\n", + $batchJob->id); + // Reset the poll attempt counter to wait for cancellation. + $pollAttempts = 0; + continue; + } + } + throw $e; + } + } + } while ($isPending && $pollAttempts <= MAX_POLL_ATTEMPTS); + + if ($isPending) { + throw new BatchJobException( + sprintf("Job is still pending state after polling %d times.", + MAX_POLL_ATTEMPTS)); + } + + if ($batchJob->processingErrors !== null) { + $i = 0; + foreach ($batchJob->processingErrors as $processingError) { + printf( + " Processing error [%d]: errorType=%s, trigger=%s, errorString=%s," + . " fieldPath=%s, reason=%s\n", + $i++, + $processingError->ApiErrorType, + $processingError->trigger, + $processingError->errorString, + $processingError->fieldPath, + $processingError->reason + ); + } + } else { + printf("No processing errors found.\n"); + } + + if ($batchJob->downloadUrl !== null && $batchJob->downloadUrl->url !== null) { + $xmlResponse = + $batchJobUtils->DownloadBatchJobResults($batchJob->downloadUrl->url); + printf("Downloaded results from %s:\n", $batchJob->downloadUrl->url); + $deserializer = new XmlDeserializer(BatchJobUtils::$CLASS_MAP); + $mutateResponse = $deserializer->ConvertXmlToObject($xmlResponse); + if (empty($mutateResponse)) { + printf(" No results available.\n"); + } else { + foreach ($mutateResponse->rval as $mutateResult) { + $outcome = $mutateResult->errorList === null ? 'SUCCESS' : 'FAILURE'; + printf(" Operation [%d] - %s\n", $mutateResult->index, $outcome); + } + } + } else { + printf("No results available for download.\n"); + } +} + +/** + * Builds objects of AdGroupCriterionOperation for creating biddable criteria + * (as keywords) for an ad group with the specified ID. 10% of + * keywords are created with some invalid characters to demonstrate how + * BatchJobService returns information about such errors. + * + * @param string $adGroupId the ID of the ad group to add the keywords to + * @return array an array of AdGroupCriterionOperation + */ +function buildAdGroupCriterionOperations($adGroupId) { + $adGroupCriterionOperations = array(); + + $suffix = uniqid(); + // Create AdGroupCriterionOperations to add keywords. + for ($i = 0; $i < NUMBER_OF_KEYWORDS_TO_ADD; $i++) { + // Create Keyword. + $text = sprintf("mars%s-%d", $suffix, $i); + + // Make 10% of keywords invalid to demonstrate error handling. + if ($i % 10 == 0) { + $text = $text . '!!!'; + } + $keyword = new Keyword(); + $keyword->text = $text; + $keyword->matchType = 'BROAD'; + + // Create BiddableAdGroupCriterion. + $biddableAdGroupCriterion = new BiddableAdGroupCriterion(); + $biddableAdGroupCriterion->adGroupId = $adGroupId; + $biddableAdGroupCriterion->criterion = $keyword; + + // Create AdGroupCriterionOperation. + $operation = new AdGroupCriterionOperation(); + $operation->operand = $biddableAdGroupCriterion; + $operation->operator = 'ADD'; + + // Add to list. + $adGroupCriterionOperations[] = $operation; + } + return $adGroupCriterionOperations; +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddKeywordsUsingIncrementalBatchJob($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/CampaignManagement/AddTrial.php b/examples/AdWords/v201705/CampaignManagement/AddTrial.php new file mode 100755 index 000000000..6c5d130b0 --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/AddTrial.php @@ -0,0 +1,139 @@ +GetService('TrialService', ADWORDS_VERSION); + $trialAsynErrorService = + $user->GetService('TrialAsyncErrorService', ADWORDS_VERSION); + + // Create a trial. + $trial = new Trial(); + $trial->draftId = $draftId; + $trial->baseCampaignId = $baseCampaignId; + $trial->name = 'Test Trial #' . uniqid(); + $trial->trafficSplitPercent = 50; + + // Create an operation. + $operation = new TrialOperation(); + $operation->operand = $trial; + $operation->operator = 'ADD'; + $operations[] = $operation; + + // Make the mutate request. + $result = $trialService->mutate($operations); + $trial = $result->value[0]; + + $selector = new Selector(); + $selector->fields = + array('Id', 'Status', 'BaseCampaignId', 'TrialCampaignId'); + $selector->predicates = new Predicate('Id', 'IN', array($trial->id)); + + // Since creating a trial is asynchronous, we have to poll it to wait for it + // to finish. + $pollAttempts = 0; + $isPending = true; + $trial = null; + do { + $sleepSeconds = POLL_FREQUENCY_SECONDS * pow(2, $pollAttempts); + printf("Sleeping %d seconds...\n", $sleepSeconds); + sleep($sleepSeconds); + + $trial = $trialService->get($selector)->entries[0]; + printf("Trial ID %d has status '%s'.\n", $trial->id, $trial->status); + + $pollAttempts++; + $isPending = ($trial->status === 'CREATING') ? true : false; + } while ($isPending && $pollAttempts <= MAX_POLL_ATTEMPTS); + + if ($trial->status === 'ACTIVE') { + // The trial creation was successful. + printf("Trial created with ID %d and trial campaign ID %d\n", $trial->id, + $trial->trialCampaignId); + } else if ($trial->status === 'CREATION_FAILED') { + // The trial creation failed, and errors can be fetched from the + // TrialAsyncErrorService. + $selector = new Selector(); + $selector->fields = array('TrialId', 'AsyncError'); + $selector->predicates = new Predicate('TrialId', 'IN', array($trial->id)); + + $errors = $trialAsynErrorService->get($selector)->entries; + + if (count($errors) === 0) { + printf("Could not retrieve errors for the trial with ID %d\n", + $trial->id); + } else { + printf("Could not create trial due to the following errors:\n"); + $i = 0; + foreach ($errors as $error) { + printf("Error #%d: %s\n", $i++, $error->asyncError); + } + } + } else { + // Most likely, the trial is still being created. You can continue polling, + // but we have limited the number of attempts in the example. + printf("Timed out waiting to create trial from draft with ID %d with base " + . "campaign with ID %d\n", $draftId, $baseCampaignId); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddTrialExample($user, $draftId, $baseCampaignId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/CampaignManagement/GetAdGroupsForDraft.php b/examples/AdWords/v201705/CampaignManagement/GetAdGroupsForDraft.php new file mode 100755 index 000000000..c871816a8 --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/GetAdGroupsForDraft.php @@ -0,0 +1,84 @@ +GetService('AdGroupService', ADWORDS_VERSION); + + // Create selector. + $selector = new Selector(); + $selector->fields = array('Id'); + + // Create predicates. + $selector->predicates[] = + new Predicate('CampaignId', 'EQUALS', $draftCampaignId); + + // Create paging controls. + $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); + + // Make the get request. + $page = $adGroupService->get($selector); + + // Display results. + if ($page->totalNumEntries !== null && $page->totalNumEntries > 0) { + printf("Found %d ad groups.\n", $page->totalNumEntries); + } else { + print "No ad groups were found.\n"; + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetAdGroupsForDraftExample($user, $draftCampaignId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/CampaignManagement/GetAllDisapprovedAds.php b/examples/AdWords/v201705/CampaignManagement/GetAllDisapprovedAds.php new file mode 100755 index 000000000..61966b747 --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/GetAllDisapprovedAds.php @@ -0,0 +1,109 @@ +GetService('AdGroupAdService', ADWORDS_VERSION); + + // Create selector. + $selector = new Selector(); + $selector->fields = array('Id', 'PolicySummary'); + $selector->ordering = array(new OrderBy('Id', 'ASCENDING')); + + // Create predicates. + $selector->predicates[] = new Predicate('AdGroupId', 'IN', array($adGroupId)); + + // Create paging controls. + $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); + + $disapprovedAdsCount = 0; + do { + // Make the get request. + $page = $adGroupAdService->get($selector); + + // Display results. + if (isset($page->entries)) { + foreach ($page->entries as $adGroupAd) { + if ($adGroupAd->policySummary->combinedApprovalStatus + !== 'DISAPPROVED') { + // Skip ad group ads that are not disapproved. + continue; + } + + $disapprovedAdsCount++; + printf( + "Ad with ID %d, and type '%s' was disapproved with the " + . "following policy topic entries:\n", + $adGroupAd->ad->id, + $adGroupAd->ad->AdType + ); + foreach ($adGroupAd->policySummary->policyTopicEntries + as $policyTopicEntry) { + printf( + " topic id: %s, topic name: '%s'\n", + $policyTopicEntry->policyTopicId, + $policyTopicEntry->policyTopicName + ); + } + } + } + + // Advance the paging index. + $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE; + } while ($page->totalNumEntries > $selector->paging->startIndex); + printf("%d disapproved ads were found.\n", $disapprovedAdsCount); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetAllDisapprovedAdsExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/CampaignManagement/GetAllDisapprovedAdsWithAwql.php b/examples/AdWords/v201705/CampaignManagement/GetAllDisapprovedAdsWithAwql.php new file mode 100755 index 000000000..e91a717bd --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/GetAllDisapprovedAdsWithAwql.php @@ -0,0 +1,107 @@ +GetService('AdGroupAdService', ADWORDS_VERSION); + + // Create a query. + $query = sprintf('SELECT Id, PolicySummary WHERE AdGroupId = %d ORDER BY Id', + $adGroupId); + + // Create paging controls. + $offset = 0; + $disapprovedAdsCount = 0; + do { + $pageQuery = sprintf('%s LIMIT %d,%d', $query, $offset, + AdWordsConstants::RECOMMENDED_PAGE_SIZE); + + // Make the query request. + $page = $adGroupAdService->query($pageQuery); + + // Display results. + if (isset($page->entries)) { + foreach ($page->entries as $adGroupAd) { + if ($adGroupAd->policySummary->combinedApprovalStatus + !== 'DISAPPROVED') { + // Skip ad group ads that are not disapproved. + continue; + } + + $disapprovedAdsCount++; + printf( + "Ad with ID %d, and type '%s' was disapproved with the " + . "following policy topic entries:\n", + $adGroupAd->ad->id, + $adGroupAd->ad->AdType + ); + foreach ($adGroupAd->policySummary->policyTopicEntries + as $policyTopicEntry) { + printf( + " topic id: %s, topic name: '%s'\n", + $policyTopicEntry->policyTopicId, + $policyTopicEntry->policyTopicName + ); + } + } + } + + // Advance the paging offset. + $offset += AdWordsConstants::RECOMMENDED_PAGE_SIZE; + } while ($page->totalNumEntries > $offset); + printf("%d disapproved ads were found.\n", $disapprovedAdsCount); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetAllDisapprovedAdsWithAwqlExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201609/Express/GetPromotions.php b/examples/AdWords/v201705/CampaignManagement/GetCampaignsByLabel.php similarity index 61% rename from examples/AdWords/v201609/Express/GetPromotions.php rename to examples/AdWords/v201705/CampaignManagement/GetCampaignsByLabel.php index 8e8869b0b..d9a27e0f2 100755 --- a/examples/AdWords/v201609/Express/GetPromotions.php +++ b/examples/AdWords/v201705/CampaignManagement/GetCampaignsByLabel.php @@ -1,7 +1,7 @@ SetExpressBusinessId($businessId); - +function GetCampaignsByLabelExample(AdWordsUser $user, $labelId) { // Get the service, which loads the required classes. - $promotionService = $user->GetService('PromotionService', ADWORDS_VERSION); + $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION); // Create selector. $selector = new Selector(); - $selector->fields = array('PromotionId', 'Name', 'Status', 'DestinationUrl', - 'CallTrackingEnabled', 'Budget', 'PromotionCriteria', 'ExpandedCreative', - 'CampaignIds'); + $selector->fields = array('Id', 'Name', 'Labels'); + // Labels filtering is performed by ID. You can use containsAny to select + // campaigns with any of the label IDs, containsAll to select campaigns with + // all of the label IDs, or containsNone to select campaigns with none of the + // label IDs. + $selector->predicates[] = new Predicate('Labels', 'CONTAINS_ANY', + array($labelId)); $selector->ordering[] = new OrderBy('Name', 'ASCENDING'); // Create paging controls. @@ -53,17 +55,20 @@ function GetPromotionsExample(AdWordsUser $user, $businessId) { do { // Make the get request. - $page = $promotionService->get($selector); + $page = $campaignService->get($selector); // Display results. if (isset($page->entries)) { - foreach ($page->entries as $promotion) { - printf("Express promotion found with name '%s' " . - "id %s destinationUrl: %s\n", $promotion->name, $promotion->id, - $promotion->destinationUrl); + foreach ($page->entries as $campaign) { + printf("Campaign with name '%s' and ID '%d' and labels '%s'" . + " was found.\n", $campaign->name, $campaign->id, + implode(', ', + array_map(function($label) { + return sprintf('%d/%s', $label->id, $label->name); + }, $campaign->labels))); } } else { - print "No express promotions were found.\n"; + print "No campaigns were found.\n"; } // Advance the paging index. @@ -85,7 +90,7 @@ function GetPromotionsExample(AdWordsUser $user, $businessId) { $user->LogAll(); // Run the example. - GetPromotionsExample($user, $businessId); + GetCampaignsByLabelExample($user, $labelId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201705/CampaignManagement/GraduateTrial.php b/examples/AdWords/v201705/CampaignManagement/GraduateTrial.php new file mode 100755 index 000000000..bb96e7aaa --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/GraduateTrial.php @@ -0,0 +1,107 @@ +GetService('TrialService', ADWORDS_VERSION); + $budgetService = $user->GetService('BudgetService', ADWORDS_VERSION); + + // To graduate a trial, you must specify a different budget from the base + // campaign. The base campaign (in order to have had a trial based on it) + // must have a non-shared budget, so it cannot be shared with the new + // independent campaign created by graduation. + $budget = new Budget(); + $budget->name = 'Trial Budget #' . uniqid(); + $budget->amount = new Money(50000000); + $budget->deliveryMethod = 'STANDARD'; + + $operations = array(); + $operation = new BudgetOperation(); + $operation->operand = $budget; + $operation->operator = 'ADD'; + $operations[] = $operation; + + $budget = $budgetService->mutate($operations)->value[0]; + + // Create a trial. + $trial = new Trial(); + $trial->id = $trialId; + $trial->budgetId = $budget->budgetId; + $trial->status = 'GRADUATED'; + + // Create an operation. + $operations = array(); + $operation = new TrialOperation(); + $operation->operand = $trial; + $operation->operator = 'SET'; + $operations[] = $operation; + + // Make the mutate request. + $trial = $trialService->mutate($operations)->value[0]; + + // Graduation is a synchronous operation, so the campaign is already ready. + // If you promote instead, make sure to see the polling scheme demonstrated + // in AddTrial.php to wait for the asynchronous operation to finish. + printf( + "Trial with ID %d graduated. Campaign with ID %d was given a new budget " + . "ID %d and is no longer dependent on this trial.\n", + $trial->id, + $trial->trialCampaignId, + $budget->budgetId + ); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GraduateTrialExample($user, $trialId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/CampaignManagement/SetAdParameters.php b/examples/AdWords/v201705/CampaignManagement/SetAdParameters.php new file mode 100755 index 000000000..df74e7a97 --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/SetAdParameters.php @@ -0,0 +1,89 @@ +GetService('AdParamService', ADWORDS_VERSION); + + // Create ad parameters. + $adParam1 = new AdParam($adGroupId, $keywordId, '100', 1); + $adParam2 = new AdParam($adGroupId, $keywordId, '$40', 2); + + // Create operations. + $operations = array(); + + $adParamOperation1 = new AdParamOperation(); + $adParamOperation1->operand = $adParam1; + $adParamOperation1->operator = 'SET'; + $operations[] = $adParamOperation1; + + $adParamOperation2 = new AdParamOperation(); + $adParamOperation2->operand = $adParam2; + $adParamOperation2->operator = 'SET'; + $operations[] = $adParamOperation2; + + // Make the mutate request. + $adParams = $adParamService->mutate($operations); + + // Display results. + foreach ($adParams as $adParam) { + printf("Ad parameter with insertion text '%s' and parameter index '%s' " + . "was set.\n", $adParam->insertionText, $adParam->paramIndex); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + SetAdParametersExample($user, $adGroupId, $keywordId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/CampaignManagement/SetBidModifier.php b/examples/AdWords/v201705/CampaignManagement/SetBidModifier.php new file mode 100755 index 000000000..e55587161 --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/SetBidModifier.php @@ -0,0 +1,98 @@ +GetService('CampaignCriterionService', ADWORDS_VERSION); + + // Create Mobile Platform. The ID can be found in the documentation. + // https://developers.google.com/adwords/api/docs/appendix/platforms + $mobile = new Platform(); + $mobile->id = 30001; // HighEndMobile = 30001 + + // Create criterion with modified bid. + $criterion = new CampaignCriterion(); + $criterion->campaignId = $campaignId; + $criterion->criterion = $mobile; + $criterion->bidModifier = $bidModifier; + + // Create SET operation. + $operation = new CampaignCriterionOperation(); + $operation->operator = 'SET'; + $operation->operand = $criterion; + + // Update campaign criteria. + $results = $campaignCriterionService->mutate(array($operation)); + + // Display campaign criteria. + if (count($results->value)) { + foreach ($results->value as $campaignCriterion) { + printf( + "Campaign criterion with campaign ID '%s', criterion ID '%s', " + . "and type '%s' was modified with bid %.2f.\n", + $campaignCriterion->campaignId, + $campaignCriterion->criterion->id, + $campaignCriterion->criterion->type, + $campaignCriterion->bidModifier); + } + + return true; + } + print 'No campaign criterias were modified.'; +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + SetBidModifierExample($user, $campaignId, $bidModifier); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/CampaignManagement/ValidateTextAd.php b/examples/AdWords/v201705/CampaignManagement/ValidateTextAd.php new file mode 100755 index 000000000..4575f8c6f --- /dev/null +++ b/examples/AdWords/v201705/CampaignManagement/ValidateTextAd.php @@ -0,0 +1,101 @@ +GetService('AdGroupAdService', ADWORDS_VERSION, null, null, true); + + // Create invalid expanded text ad. + $expandedTextAd = new ExpandedTextAd(); + $expandedTextAd->headlinePart1 = 'Luxury Cruise to Mars'; + $expandedTextAd->headlinePart2 = 'Visit the Red Planet in style.'; + $expandedTextAd->description = 'Low-gravity fun for all astronauts in orbit.'; + $expandedTextAd->finalUrls = array('http://www.example.com'); + + // Create ad group ad. + $adGroupAd = new AdGroupAd(); + $adGroupAd->adGroupId = $adGroupId; + $adGroupAd->ad = $expandedTextAd; + + // Create operation. + $operation = new AdGroupAdOperation(); + $operation->operand = $adGroupAd; + $operation->operator = 'ADD'; + + $operations = array($operation); + + // Make the mutate request. + try { + $result = $adGroupAdValidationService->mutate($operations); + printf("The expanded text ad is valid.\n"); + } catch (SoapFault $e) { + $errors = ErrorUtils::GetApiErrors($e); + if (sizeof($errors) > 0) { + printf("The expanded text ad is invalid for the following reasons:\n"); + foreach ($errors as $error) { + printf(" %s @ %s\n", $error->errorString, $error->fieldPath); + } + } else { + // Not an API error, so throw it up a level. + throw $e; + } + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + ValidateTextAdExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/ErrorHandling/HandlePartialFailures.php b/examples/AdWords/v201705/ErrorHandling/HandlePartialFailures.php new file mode 100755 index 000000000..407248b71 --- /dev/null +++ b/examples/AdWords/v201705/ErrorHandling/HandlePartialFailures.php @@ -0,0 +1,113 @@ +GetService('AdGroupCriterionService', + ADWORDS_VERSION, null, null, null, true); + + // Create keywords. + $keywords = array(); + $keywords[] = new Keyword('mars cruise', 'BROAD'); + $keywords[] = new Keyword('inv@lid cruise', 'BROAD'); + $keywords[] = new Keyword('venus cruise', 'BROAD'); + $keywords[] = new Keyword('b(a)d keyword cruise', 'BROAD'); + + // Create ad group criteria and operations. + $operations = array(); + foreach ($keywords as $keyword) { + $adGroupCriterion = new BiddableAdGroupCriterion(); + $adGroupCriterion->adGroupId = $adGroupId; + $adGroupCriterion->criterion = $keyword; + + $operation = new AdGroupCriterionOperation(); + $operation->operand = $adGroupCriterion; + $operation->operator = 'ADD'; + + $operations[] = $operation; + } + + // Make the mutate request. + $result = $adGroupCriterionService->mutate($operations); + + // Display results. + foreach ($result->value as $adGroupCriterion) { + if ($adGroupCriterion->AdGroupCriterionType == 'BiddableAdGroupCriterion') { + printf("Keyword with text '%s', match type '%s', and ID '%s' was " + . "added.\n", $adGroupCriterion->criterion->text, + $adGroupCriterion->criterion->matchType, + $adGroupCriterion->criterion->id); + } + } + + // Check for partial failures. + if (isset($result->partialFailureErrors)) { + foreach ($result->partialFailureErrors as $error) { + $index = ErrorUtils::GetSourceOperationIndex($error); + if (isset($index)) { + $adGroupCriterion = $operations[$index]->operand; + printf("Keyword with text '%s' and match type '%s' failed with error " + . "'%s'.\n", $adGroupCriterion->criterion->text, + $adGroupCriterion->criterion->matchType, $error->errorString); + } else { + printf("Operations failed with error '%s'.\n", $error->errorString); + } + } + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + HandlePartialFailuresExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/ErrorHandling/HandlePolicyViolationError.php b/examples/AdWords/v201705/ErrorHandling/HandlePolicyViolationError.php new file mode 100755 index 000000000..c023a8c71 --- /dev/null +++ b/examples/AdWords/v201705/ErrorHandling/HandlePolicyViolationError.php @@ -0,0 +1,141 @@ +GetService('AdGroupAdService', ADWORDS_VERSION); + + // Get validateOnly version of the AdGroupAdService. + $adGroupAdValidationService = + $user->GetService('AdGroupAdService', ADWORDS_VERSION, null, null, true); + + // Create text ad that violates an exemptable policy. This ad will only + // trigger an error in the production environment. + $textAd = new TextAd(); + $textAd->headline = 'Mars Cruise !!!'; + $textAd->description1 = 'Visit the Red Planet in style.'; + $textAd->description2 = 'Low-gravity fun for everyone!'; + $textAd->displayUrl = 'www.example.com'; + $textAd->finalUrls = array('http://www.example.com/'); + + // Create ad group ad. + $adGroupAd = new AdGroupAd(); + $adGroupAd->adGroupId = $adGroupId; + $adGroupAd->ad = $textAd; + + // Create operation. + $operation = new AdGroupAdOperation(); + $operation->operand = $adGroupAd; + $operation->operator = 'ADD'; + + $operations = array($operation); + + try { + // Make the mutate request. + $result = $adGroupAdValidationService->mutate($operations); + } catch (SoapFault $fault) { + $errors = ErrorUtils::GetApiErrors($fault); + if (sizeof($errors) == 0) { + // Not an API error, so throw fault. + throw $fault; + } + $operationIndicesToRemove = array(); + foreach ($errors as $error) { + if ($error->ApiErrorType == 'PolicyViolationError') { + $operationIndex = ErrorUtils::GetSourceOperationIndex($error); + $operation = $operations[$operationIndex]; + printf("Ad with headline '%s' violated %s policy '%s'.\n", + $operation->operand->ad->headline, + $error->isExemptable ? 'exemptable' : 'non-exemptable', + $error->externalPolicyName); + if ($error->isExemptable) { + // Add exemption request to the operation. + printf("Adding exemption request for policy name '%s' on text " + ."'%s'.\n", $error->key->policyName, $error->key->violatingText); + $operation->exemptionRequests[] = new ExemptionRequest($error->key); + } else { + // Remove non-exemptable operation. + print "Removing the operation from the request.\n"; + $operationIndicesToRemove[] = $operationIndex; + } + } else { + // Non-policy error returned, throw fault. + throw $fault; + } + } + $operationIndicesToRemove = array_unique($operationIndicesToRemove); + rsort($operationIndicesToRemove, SORT_NUMERIC); + foreach ($operationIndicesToRemove as $operationIndex) { + unset($operations[$operationIndex]); + } + } + + if (sizeof($operations) > 0) { + // Retry the mutate request. + $result = $adGroupAdService->mutate($operations); + + // Display results. + foreach ($result->value as $adGroupAd) { + printf("Text ad with headline '%s' and ID '%s' was added.\n", + $adGroupAd->ad->headline, $adGroupAd->ad->id); + } + } else { + print "All the operations were invalid with non-exemptable errors.\n"; + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + HandlePolicyViolationErrorExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Extensions/AddGoogleMyBusinessLocationExtensions.php b/examples/AdWords/v201705/Extensions/AddGoogleMyBusinessLocationExtensions.php new file mode 100755 index 000000000..0f5a06f14 --- /dev/null +++ b/examples/AdWords/v201705/Extensions/AddGoogleMyBusinessLocationExtensions.php @@ -0,0 +1,206 @@ +GetService('FeedService'); + + // Create a feed that will sync to the Google My Business account specified + // by gmbEmailAddress. Do not add FeedAttributes to this object, as AdWords + // will add them automatically because this will be a system generated feed. + $gmbFeed = new Feed(); + $gmbFeed->name = 'Google My Business feed #' . uniqid(); + + $feedData = new PlacesLocationFeedData(); + $feedData->emailAddress = $gmbEmailAddress; + $feedData->businessAccountIdentifier = $gmbBusinessAccount; + + // Optional: specify labels to filter Google My Business listings. If + // specified, only listings that have any of the labels set are + // synchronized into FeedItems. + $feedData->labelFilters = array('Stores in New York City'); + + $oAuthInfo = new OAuthInfo(); + $oAuthInfo->httpMethod = 'GET'; + $oAuthInfo->httpRequestUrl = 'https://www.googleapis.com/auth/adwords'; + $oAuthInfo->httpAuthorizationHeader = sprintf('Bearer %s', $gmbAccessToken); + $feedData->oAuthInfo = $oAuthInfo; + + $gmbFeed->systemFeedGenerationData = $feedData; + + // Since this feed's feed items will be managed by AdWords, + // you must set its origin to ADWORDS. + $gmbFeed->origin = 'ADWORDS'; + + // Create an operation to add the feed. + $feedOperation = new FeedOperation(); + $feedOperation->operand = $gmbFeed; + $feedOperation->operator = 'ADD'; + + // Add the feed. Since it is a system generated feed, AdWords will + // automatically: + // 1. Set up the FeedAttributes on the feed. + // 2. Set up a FeedMapping that associates the FeedAttributes of the feed + // with the placeholder fields of the LOCATION placeholder type. + $addFeedResult = $feedService->mutate(array($feedOperation)); + $addedFeed = $addFeedResult->value[0]; + printf("Added GMB feed with ID %d\n", $addedFeed->id); + + $customerFeedService = $user->GetService('CustomerFeedService'); + + // Add a CustomerFeed that associates the feed with this customer for + // the LOCATION placeholder type. + $customerFeed = new CustomerFeed(); + $customerFeed->feedId = $addedFeed->id; + $customerFeed->placeholderTypes = array(PLACEHOLDER_LOCATION); + + // Create a matching function that will always evaluate to true. + $customerMatchingFunction = new FeedFunction(); + $constOperand = new ConstantOperand(); + $constOperand->type = 'BOOLEAN'; + $constOperand->booleanValue = true; + $customerMatchingFunction->lhsOperand = array($constOperand); + $customerMatchingFunction->operator = 'IDENTITY'; + $customerFeed->matchingFunction = $customerMatchingFunction; + + // Create an operation to add the customer feed. + $customerFeedOperation = new CustomerFeedOperation(); + $customerFeedOperation->operand = $customerFeed; + $customerFeedOperation->operator = 'ADD'; + + // After the completion of the Feed ADD operation above the added feed will + // not be available for usage in a CustomerFeed until the sync between the + // AdWords and GMB accounts completes. The loop below will retry adding + // the CustomerFeed up to ten times with an exponential back-off policy. + $addedCustomerFeed = null; + $numberOfAttempts = 0; + do { + $numberOfAttempts++; + try { + $customerFeedResult = + $customerFeedService->mutate(array($customerFeedOperation)); + $addedCustomerFeed = $customerFeedResult->value[0]; + printf("Attempt #%d to add the CustomerFeed was successful\n", + $numberOfAttempts); + } catch (Exception $e) { + // Wait using exponential backoff policy + $sleepSeconds = 5 * pow(2, $numberOfAttempts); + printf("Attempt #%d to add the CustomerFeed was not successful. " + . "Waiting %d seconds before trying again.\n", $numberOfAttempts, + $sleepSeconds); + sleep($sleepSeconds); + } + } while ($numberOfAttempts < MAX_CUSTOMER_FEED_ADD_ATTEMPTS + && $addedCustomerFeed == null); + + if($addedCustomerFeed == null) { + throw new Exception('Could not create the CustomerFeed after ' + . MAX_CUSTOMER_FEED_ADD_ATTEMPTS . ' attempts. Please retry ' + . 'the CustomerFeed ADD operation later.'); + } + + printf("Added CustomerFeed for feed ID %d and placeholder type %d\n", + $addedCustomerFeed->feedId, $addedCustomerFeed->placeholderTypes[0]); + + // OPTIONAL: Create a CampaignFeed to specify which FeedItems to use at the + // Campaign level. This will be similar to the CampaignFeed in the + // AddSitelinks example, except you can also filter based on the business + // name and category of each FeedItem by using a FeedAttributeOperand in + // your matching function. + + // OPTIONAL: Create an AdGroupFeed for even more fine grained control over + // which feed items are used at the AdGroup level. + +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + $oAuth2Info = $user->GetOAuth2Info(); + $oAuth2Handler = $user->GetOAuth2Handler(); + if (!isset($oAuth2Info['access_token'])) { + $oAuth2Info = $oAuth2Handler->GetOrRefreshAccessToken($oAuth2Info); + $user->SetOAuth2Info($oAuth2Info); + } + + // Log every SOAP XML request and response. + $user->LogAll(); + + // If the GMB_EMAIL_ADDRESS is the same user you used to generate your + // AdWords API refresh token, leave the assignment below unchanged. + // Otherwise, to obtain an access token for your GMB account, run the + // Auth/GetRefreshToken example. Make sure you are logged in as the same user + // as GMB_EMAIL_ADDRESS above when you follow the link provided by the example + // then call GetOAuth2Info on the generated AdWordsUser object and copy and + // paste the value into the assignment below. + define('GMB_ACCESS_TOKEN', $oAuth2Info['access_token']); + + // Run the example. + AddGoogleMyBusinessLocationExtensions( + $user, GMB_EMAIL_ADDRESS, GMB_ACCESS_TOKEN, BUSINESS_ACCOUNT_IDENTIFIER); + +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Extensions/AddPrices.php b/examples/AdWords/v201705/Extensions/AddPrices.php new file mode 100755 index 000000000..8ae02fec0 --- /dev/null +++ b/examples/AdWords/v201705/Extensions/AddPrices.php @@ -0,0 +1,152 @@ +GetService('CustomerExtensionSettingService', ADWORDS_VERSION); + + // Create the price extension feed item. + $priceFeedItem = new PriceFeedItem(); + $priceFeedItem->priceExtensionType = 'SERVICES'; + // Price qualifer is optional. + $priceFeedItem->priceQualifier = 'FROM'; + $priceFeedItem->trackingUrlTemplate = 'http://tracker.example.com/?u={lpurl}'; + $priceFeedItem->language = 'en'; + $priceFeedItem->campaignTargeting = + new FeedItemCampaignTargeting($campaignId); + $priceFeedItem->scheduling = new FeedItemScheduling(array( + new FeedItemSchedule('SUNDAY', 10, 'ZERO', 18, 'ZERO'), + new FeedItemSchedule('SATURDAY', 10, 'ZERO', 22, 'ZERO') + )); + + // To create a price extension, at least three table rows are needed. + $tableRows = array(); + $tableRows[] = createPriceTableRow( + 'Scrubs', + 'Body Scrub, Salt Scrub', + 'http://www.example.com/scrubs', + 60 * AdWordsConstants::MICROS_PER_DOLLAR, + 'USD', + 'PER_HOUR' + ); + $tableRows[] = createPriceTableRow( + 'Hair Cuts', + 'Once a month', + 'http://www.example.com/haircuts', + 75 * AdWordsConstants::MICROS_PER_DOLLAR, + 'USD', + 'PER_MONTH' + ); + $tableRows[] = createPriceTableRow( + 'Skin Care Package', + 'Four times a month', + 'http://www.example.com/skincarepackage', + 250 * AdWordsConstants::MICROS_PER_DOLLAR, + 'USD', + 'PER_MONTH' + ); + + $priceFeedItem->tableRows = $tableRows; + + // Create your customer extension settings. This associates the price + // extension to your account. + $customerExtensionSetting = new CustomerExtensionSetting(); + $customerExtensionSetting->extensionType = 'PRICE'; + $customerExtensionSetting->extensionSetting = new ExtensionSetting(); + $customerExtensionSetting->extensionSetting->extensions = + array($priceFeedItem); + + // Create operation. + $operation = new CustomerExtensionSettingOperation(); + $operation->operator = 'ADD'; + $operation->operand = $customerExtensionSetting; + + $operations = array($operation); + + // Add the price extension. + $result = $customerExtensionSettingService->mutate($operations); + + // Print the results. + $newExtensionSetting = $result->value[0]; + printf("Extension setting with type '%s' was added to your account.\n", + $newExtensionSetting->extensionType); +} + +/** + * Creates a new price table row with the specified attributes. + * + * @param string $header the header of price table row + * @param string $description the description of price table row + * @param string $finalUrl the final url of price table row + * @param integer $priceInMicros the price in micro amount + * @param string $currencyCode the 3-character currency code + * @param string $priceUnit the unit of shown price + */ +function createPriceTableRow($header, $description, $finalUrl, $priceInMicros, + $currencyCode, $priceUnit) { + $priceTableRow = new PriceTableRow(); + $priceTableRow->header = $header; + $priceTableRow->description = $description; + $priceTableRow->finalUrls = new UrlList(array($finalUrl)); + $priceTableRow->price = + new MoneyWithCurrency(new Money($priceInMicros), $currencyCode); + $priceTableRow->priceUnit = $priceUnit; + return $priceTableRow; +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddPricesExample($user, $campaignId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Extensions/AddSitelinks.php b/examples/AdWords/v201705/Extensions/AddSitelinks.php new file mode 100755 index 000000000..3f5d9e84f --- /dev/null +++ b/examples/AdWords/v201705/Extensions/AddSitelinks.php @@ -0,0 +1,154 @@ +GetService('CampaignExtensionSettingService', ADWORDS_VERSION); + $customerService = $user->GetService('CustomerService', ADWORDS_VERSION); + + // Find the matching customer and its time zone. The getCustomers method will + // return a single Customer object corresponding to the user's + // clientCustomerId. + $customers = $customerService->getCustomers(); + $customer = $customers[0]; + printf("Found customer ID %d with time zone %s.\n", $customer->customerId, + $customer->dateTimeZone); + + // Create the sitelinks: + + // A simple one. + $sitelink1 = new SitelinkFeedItem(); + $sitelink1->sitelinkText = 'Store Hours'; + $sitelink1->sitelinkFinalUrls = + new UrlList(array('http://www.example.com/storehours')); + + // This one to show the Thanksgiving specials link only from 20 - 27 Nov. + $sitelink2 = new SitelinkFeedItem(); + $sitelink2->sitelinkText = 'Thanksgiving Specials'; + $sitelink2->sitelinkFinalUrls = + new UrlList(array('http://www.example.com/thanksgiving')); + $sitelink2->startTime = date('Y') . '1120 000000 ' . $customer->dateTimeZone; + $sitelink2->endTime = date('Y') . '1127 235959 ' . $customer->dateTimeZone; + // Target this sitelink for United States only. See + // https://developers.google.com/adwords/api/docs/appendix/geotargeting + // for valid geolocation codes. + $location = new Location(); + $location->id = 2840; + $sitelink2->geoTargeting = $location; + + // Restrict targeting only to people physically within the United States. + // Otherwise, this could also show to people interested in the United States + // but not physically located there. + $sitelink2->geoTargetingRestriction = + new FeedItemGeoRestriction('LOCATION_OF_PRESENCE'); + + // Sitelink targetted on high end mobile. + $sitelink3 = new SitelinkFeedItem(); + $sitelink3->sitelinkText = 'Wifi available'; + $sitelink3->sitelinkFinalUrls = + new UrlList(array('http://www.example.com/mobile/wifi')); + $sitelink3->devicePreference = 30001; + // Target this sitelink only when the ad is triggered by the keyword + // "free wifi". + $keyword = new Keyword(); + $keyword->text = 'free wifi'; + $keyword->matchType = 'BROAD'; + $sitelink3->keywordTargeting = $keyword; + + // Show the happy hours link only during Mon - Fri 6PM to 9PM. + $sitelink4 = new SitelinkFeedItem(); + $sitelink4->sitelinkText = 'Happy Hours Now!'; + $sitelink4->sitelinkFinalUrls = + new UrlList(array('http://www.example.com/happyhours')); + $sitelink4->scheduling = new FeedItemScheduling(array( + new FeedItemSchedule('MONDAY', 18, 'ZERO', 21, 'ZERO'), + new FeedItemSchedule('TUESDAY', 18, 'ZERO', 21, 'ZERO'), + new FeedItemSchedule('WEDNESDAY', 18, 'ZERO', 21, 'ZERO'), + new FeedItemSchedule('THURSDAY', 18, 'ZERO', 21, 'ZERO'), + new FeedItemSchedule('FRIDAY', 18, 'ZERO', 21, 'ZERO') + )); + + // Create your campaign extension settings. This associates the sitelinks + // to your campaign. + $campaignExtensionSetting = new CampaignExtensionSetting(); + $campaignExtensionSetting->campaignId = $campaignId; + $campaignExtensionSetting->extensionType = 'SITELINK'; + $campaignExtensionSetting->extensionSetting = new ExtensionSetting(); + $campaignExtensionSetting->extensionSetting->extensions = array(); + $campaignExtensionSetting->extensionSetting->extensions[] = $sitelink1; + $campaignExtensionSetting->extensionSetting->extensions[] = $sitelink2; + $campaignExtensionSetting->extensionSetting->extensions[] = $sitelink3; + $campaignExtensionSetting->extensionSetting->extensions[] = $sitelink4; + + // Create operation. + $operation = new CampaignExtensionSettingOperation(); + $operation->operator = 'ADD'; + $operation->operand = $campaignExtensionSetting; + + $operations = array($operation); + + // Add the sitelinks. + $result = $campaignExtensionSettingService->mutate($operations); + + // Print the results. + $newExtensionSetting = $result->value[0]; + printf("Extension setting with type '%s' was added to campaign ID %d\n", + $newExtensionSetting->extensionType, $newExtensionSetting->campaignId); + + return $newExtensionSetting; +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddSitelinksExample($user, $campaignId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Extensions/AddSitelinksUsingFeeds.php b/examples/AdWords/v201705/Extensions/AddSitelinksUsingFeeds.php new file mode 100755 index 000000000..f75907104 --- /dev/null +++ b/examples/AdWords/v201705/Extensions/AddSitelinksUsingFeeds.php @@ -0,0 +1,315 @@ +GetService('FeedService', ADWORDS_VERSION); + + // Create attributes. + $textAttribute = new FeedAttribute(); + $textAttribute->type = 'STRING'; + $textAttribute->name = 'Link Text'; + $finalUrlAttribute = new FeedAttribute(); + $finalUrlAttribute->type = 'URL_LIST'; + $finalUrlAttribute->name = 'Link URL'; + $line1Attribute = new FeedAttribute(); + $line1Attribute->type = 'STRING'; + $line1Attribute->name = 'Line 1 Description'; + $line2Attribute = new FeedAttribute(); + $line2Attribute->type = 'STRING'; + $line2Attribute->name = 'Line 2 Description'; + + // Create the feed. + $sitelinksFeed = new Feed(); + $sitelinksFeed->name = 'Feed For Sitelinks'; + $sitelinksFeed->attributes = array($textAttribute, $finalUrlAttribute, + $line1Attribute, $line2Attribute); + $sitelinksFeed->origin = 'USER'; + + // Create operation. + $operation = new FeedOperation(); + $operation->operator = 'ADD'; + $operation->operand = $sitelinksFeed; + + $operations = array($operation); + + // Add the feed. + $result = $feedService->mutate($operations); + + $savedFeed = $result->value[0]; + $sitelinksData['sitelinksFeedId'] = $savedFeed->id; + $savedAttributes = $savedFeed->attributes; + $sitelinksData['linkTextFeedAttributeId'] = $savedAttributes[0]->id; + $sitelinksData['linkFinalUrlFeedAttributeId'] = $savedAttributes[1]->id; + $sitelinksData['line1FeedAttribute'] = $savedAttributes[2]->id; + $sitelinksData['line2FeedAttribute'] = $savedAttributes[3]->id; + + printf('Feed with name "%s" and ID %d with linkTextAttributeId %d' + . ", linkFinalUrlAttributeId %d, line1Attribute %d and line2Attribute %d " + . "were created.\n", + $savedFeed->name, + $savedFeed->id, + $savedAttributes[0]->id, + $savedAttributes[1]->id, + $savedAttributes[2]->id, + $savedAttributes[3]->id); + + return $sitelinksData; +} + +/** + * Adds sitelinks items to the feed. + * @param AdWordsUser $user the user to run the example with + * @param array $sitelinksData IDs associated to created sitelinks feed metadata + */ +function CreateSitelinksFeedItems(AdWordsUser $user, $sitelinksData) { + // Get the FeedItemService, which loads the required classes. + $feedItemService = $user->GetService('FeedItemService', ADWORDS_VERSION); + + // Create operations to add FeedItems. + $home = NewSitelinkFeedItemAddOperation($sitelinksData, 'Home', + 'http://www.example.com', 'Home line 1', 'Home line 2'); + $stores = NewSitelinkFeedItemAddOperation($sitelinksData, 'Stores', + 'http://www.example.com/stores', 'Stores line 1', 'Stores line 2'); + $onSale = NewSitelinkFeedItemAddOperation($sitelinksData, 'On Sale', + 'http://www.example.com/sale', 'On Sale line 1', 'On Sale line 2'); + $support = NewSitelinkFeedItemAddOperation($sitelinksData, 'Support', + 'http://www.example.com/support', 'Support line 1', 'Support line 2'); + $products = NewSitelinkFeedItemAddOperation($sitelinksData, 'Products', + 'http://www.example.com/products', 'Products line 1', 'Products line 2'); + // This site link is using geographical targeting by specifying the criterion + // ID for California. + $aboutUs = NewSitelinkFeedItemAddOperation($sitelinksData, 'About Us', + 'http://www.example.com/about', 'About Us line 1', 'About Us line 2', + 21137); + + $operations = array($home, $stores, $onSale, $support, $products, $aboutUs); + + $result = $feedItemService->mutate($operations); + $sitelinksData['sitelinkFeedItemIds'] = array(); + + foreach ($result->value as $feedItem) { + printf("FeedItem with feedItemId %d was added.\n", $feedItem->feedItemId); + $sitelinksData['sitelinkFeedItemIds'][] = $feedItem->feedItemId; + } + + return $sitelinksData; +} + +// See the Placeholder reference page for a list of all the placeholder types +// and fields. +// https://developers.google.com/adwords/api/docs/appendix/placeholders.html +define('PLACEHOLDER_SITELINKS', 1); +define('PLACEHOLDER_FIELD_SITELINK_LINK_TEXT', 1); +define('PLACEHOLDER_FIELD_SITELINK_FINAL_URL', 5); +define('PLACEHOLDER_FIELD_LINE_1_TEXT', 3); +define('PLACEHOLDER_FIELD_LINE_2_TEXT', 4); + +/** + * Maps the feed attributes to the sitelink placeholders. + * @param AdWordsUser $user the user to run the example with + * @param array $sitelinksData IDs associated to created sitelinks feed metadata + */ +function CreateSitelinksFeedMapping(AdWordsUser $user, $sitelinksData) { + // Get the FeedMappingService, which loads the required classes. + $feedMappingService = $user->GetService('FeedMappingService', + ADWORDS_VERSION); + + // Map the FeedAttributeIds to the fieldId constants. + $linkTextFieldMapping = new AttributeFieldMapping(); + $linkTextFieldMapping->feedAttributeId = + $sitelinksData['linkTextFeedAttributeId']; + $linkTextFieldMapping->fieldId = PLACEHOLDER_FIELD_SITELINK_LINK_TEXT; + $linkFinalUrlFieldMapping = new AttributeFieldMapping(); + $linkFinalUrlFieldMapping->feedAttributeId = + $sitelinksData['linkFinalUrlFeedAttributeId']; + $linkFinalUrlFieldMapping->fieldId = PLACEHOLDER_FIELD_SITELINK_FINAL_URL; + $line1FieldMapping = new AttributeFieldMapping(); + $line1FieldMapping->feedAttributeId = $sitelinksData['line1FeedAttribute']; + $line1FieldMapping->fieldId = PLACEHOLDER_FIELD_LINE_1_TEXT; + $line2FieldMapping = new AttributeFieldMapping(); + $line2FieldMapping->feedAttributeId = $sitelinksData['line2FeedAttribute']; + $line2FieldMapping->fieldId = PLACEHOLDER_FIELD_LINE_2_TEXT; + + // Create the FieldMapping and operation. + $feedMapping = new FeedMapping(); + $feedMapping->placeholderType = PLACEHOLDER_SITELINKS; + $feedMapping->feedId = $sitelinksData['sitelinksFeedId']; + $feedMapping->attributeFieldMappings = + array($linkTextFieldMapping, $linkFinalUrlFieldMapping, + $line1FieldMapping, $line2FieldMapping); + $operation = new FeedMappingOperation(); + $operation->operand = $feedMapping; + $operation->operator = 'ADD'; + + $operations = array($operation); + + // Save the field mapping. + $result = $feedMappingService->mutate($operations); + foreach ($result->value as $feedMapping) { + printf('Feed mapping with ID %d and placeholderType %d was saved for ' . + "feed with ID %d.\n", + $feedMapping->feedMappingId, + $feedMapping->placeholderType, + $feedMapping->feedId); + } +} + +/** + * Creates the CampaignFeed associated to the feed data already populated. + * @param AdWordsUser $user the user to run the example with + * @param array $sitelinksData IDs associated to created sitelinks feed metadata + * @param string $campaignId the ID of the campaign to add the sitelinks to + */ +function CreateSitelinksCampaignFeed(AdWordsUser $user, $sitelinksData, + $campaignId) { + // Get the CampaignFeedService, which loads the required classes. + $campaignFeedService = $user->GetService('CampaignFeedService', + ADWORDS_VERSION); + $matchingFunctionString = sprintf( + 'AND( IN(FEED_ITEM_ID, {%s}), EQUALS(CONTEXT.DEVICE, "Mobile") )', + implode(',', $sitelinksData['sitelinkFeedItemIds'])); + + $campaignFeed = new CampaignFeed(); + $campaignFeed->feedId = $sitelinksData['sitelinksFeedId']; + $campaignFeed->campaignId = $campaignId; + + $matchingFunction = new FeedFunction(); + $matchingFunction->functionString = $matchingFunctionString; + $campaignFeed->matchingFunction = $matchingFunction; + // Specifying placeholder types on the CampaignFeed allows the same feed + // to be used for different placeholders in different Campaigns. + $campaignFeed->placeholderTypes = array(PLACEHOLDER_SITELINKS); + + $operation = new CampaignFeedOperation(); + $operation->operand = $campaignFeed; + $operation->operator = 'ADD'; + + $operations = array($operation); + + $result = $campaignFeedService->mutate($operations); + foreach ($result->value as $savedCampaignFeed) { + printf("Campaign with ID %d was associated with feed with ID %d.\n", + $savedCampaignFeed->campaignId, + $savedCampaignFeed->feedId); + } +} + +/** + * Creates a SitelinkFeedItem and wraps it in an ADD operation. + * @param array $sitelinksData IDs associated to created sitelinks feed metadata + * @param string $text text of the sitelink + * @param string $finalUrl URL of the sitelink + * @param string $line1 first line of the sitelink description + * @param string $line2 second line of the sitelink description + * @param int $locationId the criterion ID of location to be targeted + */ +function NewSitelinkFeedItemAddOperation($sitelinksData, $text, $finalUrl, + $line1, $line2, $locationId = null) { + // Create the FeedItemAttributeValues for our text values. + $linkTextAttributeValue = new FeedItemAttributeValue(); + $linkTextAttributeValue->feedAttributeId = + $sitelinksData['linkTextFeedAttributeId']; + $linkTextAttributeValue->stringValue = $text; + $linkFinalUrlAttributeValue = new FeedItemAttributeValue(); + $linkFinalUrlAttributeValue->feedAttributeId = + $sitelinksData['linkFinalUrlFeedAttributeId']; + $linkFinalUrlAttributeValue->stringValues = array($finalUrl); + $line1AttributeValue = new FeedItemAttributeValue(); + $line1AttributeValue->feedAttributeId = $sitelinksData['line1FeedAttribute']; + $line1AttributeValue->stringValue = $line1; + $line2AttributeValue = new FeedItemAttributeValue(); + $line2AttributeValue->feedAttributeId = $sitelinksData['line2FeedAttribute']; + $line2AttributeValue->stringValue = $line2; + + // Create the feed item and operation. + $item = new FeedItem(); + $item->feedId = $sitelinksData['sitelinksFeedId']; + $item->attributeValues = + array($linkTextAttributeValue, $linkFinalUrlAttributeValue, + $line1AttributeValue, $line2AttributeValue); + + // OPTIONAL: Use geographical targeting on a feed. + // The IDs can be found in the documentation or retrieved with the + // LocationCriterionService. + if ($locationId !== null) { + $location = new Location(); + $location->id = $locationId; + $item->geoTargeting = $location; + $item->geoTargetingRestriction = + new FeedItemGeoRestriction('LOCATION_OF_PRESENCE'); + } + + $operation = new FeedItemOperation(); + $operation->operand = $item; + $operation->operator = 'ADD'; + return $operation; +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddSitelinksUsingFeedsExample($user, $campaignId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Migration/MigrateToExtensionSettings.php b/examples/AdWords/v201705/Migration/MigrateToExtensionSettings.php new file mode 100755 index 000000000..bb687f229 --- /dev/null +++ b/examples/AdWords/v201705/Migration/MigrateToExtensionSettings.php @@ -0,0 +1,374 @@ +feedId = $feedId; + $this->feedItemId = $feedItemId; + } +} + +/** + * Runs the example. + * @param AdWordsUser $user the user to run the example with + */ +function MigrateToExtensionSettingsExample(AdWordsUser $user) { + $feeds = GetFeeds($user); + foreach ($feeds as $feed) { + // Retrieve all the sitelinks from the current feed. + $feedItems = GetSitelinksFromFeed($user, $feed->id); + printf("Loaded %d sitelinks for feed ID %d.\n", + count($feedItems), $feed->id); + + // Get all the instances where a sitelink from this feed has been added + // to a campaign. + $campaignFeeds = + GetCampaignFeeds($user, $feed->id, PLACEHOLDER_TYPE_SITELINKS); + printf("Loaded %d sitelink to campaign mappings for feed ID %d.\n", + count($campaignFeeds), $feed->id); + + if (!empty($campaignFeeds)) { + $allFeedItemsToDelete = array(); + foreach ($campaignFeeds as $campaignFeed) { + // Retrieve the sitelinks that have been associated with this campaign. + $feedItemIds = GetFeedItemsForCampaign($campaignFeed); + $platformRestrictions = + GetPlatformRestrictionsForCampaign($campaignFeed); + + if(empty($feedItemIds)) { + printf("Skipping feed ID %d for campaign %d -- matching function is " + . "missing or too complex for this script.\n", + $campaignFeed->feedId, $campaignFeed->campaignId); + continue; + } + + // Delete the campaign feed that associates the sitelinks from the feed + // to the campaign. + DeleteCampaignFeed($user, $campaignFeed); + + // Mark the sitelinks from the feed for deletion. + $allFeedItemsToDelete = + array_merge($allFeedItemsToDelete, $feedItemIds); + + // Create extension settings instead of sitelinks. + CreateExtensionSetting( + $user, $feedItems, $campaignFeed->campaignId, $feedItemIds, + $platformRestrictions); + } + // Delete all the sitelinks from the feed. + $allFeedItemsToDelete = array_unique($allFeedItemsToDelete); + DeleteOldFeedItems($user, $allFeedItemsToDelete, $feed->id); + + } + } +} + +function GetFeeds($user) { + $feedService = $user->GetService('FeedService', ADWORDS_VERSION); + $page = $feedService->query('SELECT Id, Name, Attributes WHERE ' + . 'Origin="USER" AND FeedStatus="ENABLED"'); + return $page->entries; +} + +function GetFeedItems($user, $feedId) { + $feedItemService = $user->GetService('FeedItemService', ADWORDS_VERSION); + $page = $feedItemService->query( + sprintf('SELECT FeedItemId, AttributeValues, Scheduling WHERE ' + . 'Status = "ENABLED" AND FeedId = %d', $feedId)); + return $page->entries; +} + +function GetCampaignFeeds($user, $feedId, $placeholderId) { + $campaignFeedService = + $user->GetService('CampaignFeedService', ADWORDS_VERSION); + + $page = $campaignFeedService->query( + sprintf('SELECT CampaignId, MatchingFunction, PlaceholderTypes WHERE ' + . 'Status="ENABLED" AND FeedId = %d AND PlaceholderTypes ' + . 'CONTAINS_ANY[%d]', $feedId, $placeholderId)); + return $page->entries; +} + +function GetFeedMapping($user, $feedId, $placeholderTypeId) { + $feedMappingService = + $user->GetService('FeedMappingService', ADWORDS_VERSION); + $page = $feedMappingService->query( + sprintf('SELECT FeedMappingId, AttributeFieldMappings WHERE FeedId="%d" ' + . 'AND PlaceholderType="%d" AND Status="ENABLED"', + $feedId, $placeholderTypeId)); + $attributeMappings = array(); + if (!empty($page->entries)) { + // Normally, a feed attribute is mapped only to one field. However, + // you may map it to more than one field if needed. + foreach ($page->entries as $feedMapping) { + foreach ($feedMapping->attributeFieldMappings as $attributeMapping) { + if (!isset($attributeMappings[$attributeMapping->feedAttributeId])) { + $attributeMappings[$attributeMapping->feedAttributeId] = array(); + } + $attributeMappings[$attributeMapping->feedAttributeId][] = + $attributeMapping->fieldId; + } + } + } + return $attributeMappings; +} + +function GetSitelinksFromFeed($user, $feedId) { + printf("Processing feed ID %d...\n", $feedId); + $sitelinks = array(); + + // Retrieve all the feed items from the feed. + $feedItems = GetFeedItems($user, $feedId); + + if (!empty($feedItems)) { + // Retrieve the feed's attribute mapping. + $feedMappings = GetFeedMapping($user, $feedId, PLACEHOLDER_TYPE_SITELINKS); + + foreach ($feedItems as $feedItem) { + $sitelinkFromFeed = + new SitelinkFromFeed($feedItem->feedId, $feedItem->feedItemId); + + foreach ($feedItem->attributeValues as $attributeValue) { + // This attribute hasn't been mapped to a field. + if (!isset($feedMappings[$attributeValue->feedAttributeId])) { + continue; + } + // Get the list of all the fields to which this attribute has been + // mapped. + foreach ($feedMappings[$attributeValue->feedAttributeId] as $fieldId) { + // Read the appropriate value depending on the ID of the mapped field. + switch ($fieldId) { + case PLACEHOLDER_FIELD_TEXT: + $sitelinkFromFeed->text = $attributeValue->stringValue; + break; + case PLACEHOLDER_FIELD_URL: + $sitelinkFromFeed->url = $attributeValue->stringValue; + break; + case PLACEHOLDER_FIELD_FINAL_URLS: + $sitelinkFromFeed->finalUrls = $attributeValue->stringValues; + break; + case PLACEHOLDER_FIELD_FINAL_MOBILE_URLS: + $sitelinkFromFeed->finalMobileUrls = + $attributeValue->stringValues; + break; + case PLACEHOLDER_FIELD_TRACKING_URL_TEMPLATE: + $sitelinkFromFeed->trackingUrlTemplate = + $attributeValue->stringValue; + break; + case PLACEHOLDER_FIELD_LINE2: + $sitelinkFromFeed->line2 = $attributeValue->stringValue; + break; + case PLACEHOLDER_FIELD_LINE3: + $sitelinkFromFeed->line3 = $attributeValue->stringValue; + break; + } + } + } + $sitelinkFromFeed->scheduling = $feedItem->scheduling; + $sitelinks[$feedItem->feedItemId] = $sitelinkFromFeed; + } + } + return $sitelinks; +} + +function GetPlatformRestrictionsForCampaign($campaignFeed) { + $platformRestrictions = 'NONE'; + if ($campaignFeed->matchingFunction->operator == 'AND') { + foreach ($campaignFeed->matchingFunction->lhsOperand as $argument) { + if (get_class($argument) == 'FunctionOperand') { + if ($argument->value->operator == 'EQUALS' && + get_class($argument->value->lhsOperand[0]) == + 'RequestContextOperand') { + $requestContextOperand = $argument->value->lhsOperand[0]; + if ($requestContextOperand->contextType == 'DEVICE_PLATFORM') { + $platformRestrictions = + strtoupper($argument->value->rhsOperand[0]->stringValue); + } + } + } + } + } + return $platformRestrictions; +} + +function GetFeedItemsForCampaign($campaignFeed) { + $feedItems = array(); + + if ($campaignFeed->matchingFunction->operator == 'IN') { + // Check if matchingFunction is of the form IN(FEED_ITEM_ID,{xxx,xxx}). + // Extract feed items if applicable. + $feedItems = array_merge($feedItems, + GetFeedItemsFromArgument($campaignFeed->matchingFunction)); + } else if ($campaignFeed->matchingFunction->operator == 'AND') { + foreach ($campaignFeed->matchingFunction->lhsOperand as $argument) { + // Check if matchingFunction is of the form IN(FEED_ITEM_ID,{xxx,xxx}). + // Extract feed items if applicable. + if (get_class($argument) == 'FunctionOperand') { + if ($argument->value->operator == 'IN') { + $feedItems = array_merge($feedItems, + GetFeedItemsFromArgument($argument->value)); + } + } + } + } + // There are no other matching functions involving feed item IDs. + return $feedItems; +} + +function GetFeedItemsFromArgument($function) { + $feedItems = array(); + + if (count($function->lhsOperand) == 1 && + get_class($function->lhsOperand[0]) == + 'RequestContextOperand' && + $function->lhsOperand[0]->contextType == + 'FEED_ITEM_ID' && + $function->operator == 'IN') { + foreach ($function->rhsOperand as $argument) { + $feedItems[] = $argument->longValue; + } + } + return $feedItems; +} + +function CreateExtensionSetting($user, $feedItems, $campaignId, $feedItemIds, + $platformRestrictions) { + $campaignExtensionSettingService = + $user->GetService('CampaignExtensionSettingService', ADWORDS_VERSION); + + $extensionSetting = new CampaignExtensionSetting(); + $extensionSetting->campaignId = $campaignId; + $extensionSetting->extensionType = 'SITELINK'; + $extensionSetting->extensionSetting = new ExtensionSetting(); + + $extensionFeedItems = array(); + foreach ($feedItemIds as $feedItemId) { + $feedItem = $feedItems[$feedItemId]; + $newFeedItem = new SitelinkFeedItem($feedItem->text, $feedItem->url, + $feedItem->line2, $feedItem->line3, $feedItem->finalUrls, + $feedItem->finalMobileUrls, $feedItem->trackingUrlTemplate, null, null, + null, null, null, null, null, $feedItem->scheduling); + $extensionFeedItems[] = $newFeedItem; + } + $extensionSetting->extensionSetting->extensions = $extensionFeedItems; + $extensionSetting->extensionSetting->platformRestrictions = + $platformRestrictions; + $extensionSetting->extensionType = 'SITELINK'; + + $operation = new CampaignExtensionSettingOperation($extensionSetting, 'ADD'); + + printf("Adding %d sitelinks for campaign ID %d...\n", + count($feedItemIds), $campaignId); + + return $campaignExtensionSettingService->mutate(array($operation)); +} + +function DeleteCampaignFeed($user, $campaignFeed) { + $campaignFeedService = + $user->GetService('CampaignFeedService', ADWORDS_VERSION); + + printf("Deleting association of feed ID %d and and campaign ID %d...\n", + $campaignFeed->feedId, $campaignFeed->campaignId); + + $operation = new CampaignFeedOperation($campaignFeed, 'REMOVE'); + return $campaignFeedService->mutate(array($operation)); +} + +function DeleteOldFeedItems($user, $feedItemIds, $feedId) { + if (empty($feedItemIds)) { + return; + } + + $feedItemService = $user->GetService('FeedItemService', ADWORDS_VERSION); + + $operations = array(); + foreach ($feedItemIds as $feedItemId) { + $feedItem = new FeedItem(); + $feedItem->feedId = $feedId; + $feedItem->feedItemId = $feedItemId; + $operation = new FeedItemOperation($feedItem, 'REMOVE'); + $operations[] = $operation; + } + + printf("Deleting %d old feed items from feed ID %d...\n", + count($feedItemIds), $feedId); + + return $feedItemService->mutate($operations); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in '../auth.ini' + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + MigrateToExtensionSettingsExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Misc/GetAllImagesAndVideos.php b/examples/AdWords/v201705/Misc/GetAllImagesAndVideos.php new file mode 100755 index 000000000..8da450651 --- /dev/null +++ b/examples/AdWords/v201705/Misc/GetAllImagesAndVideos.php @@ -0,0 +1,97 @@ +GetService('MediaService', ADWORDS_VERSION); + + // Create selector. + $selector = new Selector(); + $selector->fields = array('MediaId', 'Width', 'Height', 'MimeType', 'Name'); + $selector->ordering = array(new OrderBy('MediaId', 'ASCENDING')); + + // Create predicates. + $selector->predicates[] = + new Predicate('Type', 'IN', array('IMAGE', 'VIDEO')); + + // Create paging controls. + $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); + + do { + // Make the get request. + $page = $mediaService->get($selector); + + // Display images. + if (isset($page->entries)) { + foreach ($page->entries as $media) { + if ($media->MediaType == 'Image') { + $dimensions = MapUtils::GetMap($media->dimensions); + printf("Image with dimensions '%dx%d', MIME type '%s', and id '%s' " + . "was found.\n", $dimensions['FULL']->width, + $dimensions['FULL']->height, $media->mimeType, $media->mediaId); + } else if ($media->MediaType == 'Video') { + printf("Video with name '%s' and id '%s' was found.\n", $media->name, + $media->mediaId); + } + } + } else { + print "No images or videos were found.\n"; + } + + // Advance the paging index. + $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE; + } while ($page->totalNumEntries > $selector->paging->startIndex); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetAllImagesAndVideosExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Misc/UploadImage.php b/examples/AdWords/v201705/Misc/UploadImage.php new file mode 100755 index 000000000..3879fe897 --- /dev/null +++ b/examples/AdWords/v201705/Misc/UploadImage.php @@ -0,0 +1,74 @@ +GetService('MediaService', ADWORDS_VERSION); + + // Create image. + $image = new Image(); + $image->data = MediaUtils::GetBase64Data('http://goo.gl/HJM3L'); + $image->type = 'IMAGE'; + + // Make the upload request. + $result = $mediaService->upload(array($image)); + + // Display result. + $image = $result[0]; + $dimensions = MapUtils::GetMap($image->dimensions); + printf("Image with dimensions '%dx%d', MIME type '%s', and id '%s' was " + . "uploaded.\n", $dimensions['FULL']->width, + $dimensions['FULL']->height, $image->mimeType, $image->mediaId); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + UploadImageExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Misc/UploadMediaBundle.php b/examples/AdWords/v201705/Misc/UploadMediaBundle.php new file mode 100755 index 000000000..c4b21f7ad --- /dev/null +++ b/examples/AdWords/v201705/Misc/UploadMediaBundle.php @@ -0,0 +1,79 @@ +GetService('MediaService', ADWORDS_VERSION); + + // Create HTML5 media. + $html5Zip = new MediaBundle(); + $html5Zip->data = MediaUtils::GetBase64Data('https://goo.gl/9Y7qI2'); + $html5Zip->type = 'MEDIA_BUNDLE'; + + // Make the upload request. + $result = $mediaService->upload(array($html5Zip)); + + // Display result. + $mediaBundle = $result[0]; + $dimensions = MapUtils::GetMap($mediaBundle->dimensions); + printf( + "HTML5 media with ID %d, dimensions '%dx%d', MIME type '%s' was " + . "uploaded.\n", + $mediaBundle->mediaId, + $dimensions['FULL']->width, + $dimensions['FULL']->height, + $mediaBundle->mimeType + ); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + UploadMediaBundleExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Optimization/EstimateKeywordTraffic.php b/examples/AdWords/v201705/Optimization/EstimateKeywordTraffic.php new file mode 100755 index 000000000..90622408e --- /dev/null +++ b/examples/AdWords/v201705/Optimization/EstimateKeywordTraffic.php @@ -0,0 +1,195 @@ +GetService('TrafficEstimatorService', ADWORDS_VERSION); + + // Create keywords. Up to 2000 keywords can be passed in a single request. + $keywords = array(); + $keywords[] = new Keyword('mars cruise', 'BROAD'); + $keywords[] = new Keyword('cheap cruise', 'PHRASE'); + $keywords[] = new Keyword('cruise', 'EXACT'); + + // Create a keyword estimate request for each keyword. + $keywordEstimateRequests = array(); + foreach ($keywords as $keyword) { + $keywordEstimateRequest = new KeywordEstimateRequest(); + $keywordEstimateRequest->keyword = $keyword; + $keywordEstimateRequests[] = $keywordEstimateRequest; + } + + // Negative keywords don't return estimates, but adjust the estimates of the + // other keywords in the hypothetical ad group. + $negativeKeywords = array(); + $negativeKeywords[] = new Keyword('moon walk', 'BROAD'); + + // Create a keyword estimate request for each negative keyword. + foreach ($negativeKeywords as $negativeKeyword) { + $keywordEstimateRequest = new KeywordEstimateRequest(); + $keywordEstimateRequest->keyword = $negativeKeyword; + $keywordEstimateRequest->isNegative = true; + $keywordEstimateRequests[] = $keywordEstimateRequest; + } + + // Create ad group estimate requests. + $adGroupEstimateRequest = new AdGroupEstimateRequest(); + $adGroupEstimateRequest->keywordEstimateRequests = $keywordEstimateRequests; + $adGroupEstimateRequest->maxCpc = new Money(1000000); + + // Create campaign estimate requests. + $campaignEstimateRequest = new CampaignEstimateRequest(); + $campaignEstimateRequest->adGroupEstimateRequests[] = $adGroupEstimateRequest; + + // Optional: Set additional criteria for filtering estimates. + // See http://code.google.com/apis/adwords/docs/appendix/countrycodes.html + // for a detailed list of country codes. + // Set targeting criteria. Only locations and languages are supported. + $unitedStates = new Location(); + $unitedStates->id = 2840; + $campaignEstimateRequest->criteria[] = $unitedStates; + + // See http://code.google.com/apis/adwords/docs/appendix/languagecodes.html + // for a detailed list of language codes. + $english = new Language(); + $english->id = 1000; + $campaignEstimateRequest->criteria[] = $english; + + // Create selector. + $selector = new TrafficEstimatorSelector(); + $selector->campaignEstimateRequests[] = $campaignEstimateRequest; + + // Optional: Request a list of campaign level estimates segmented by platform. + $selector->platformEstimateRequested = true; + + // Make the get request. + $result = $trafficEstimatorService->get($selector); + + // Display results. + $platformEstimates = $result->campaignEstimates[0]->platformEstimates; + if ($platformEstimates !== null) { + foreach ($platformEstimates as $platformEstimate) { + if ($platformEstimate->minEstimate !== null + && $platformEstimate->maxEstimate !== null) { + printf( + "Results for the platform with ID %d and name '%s':\n", + $platformEstimate->platform->id, + $platformEstimate->platform->platformName + ); + printMeanEstimate($platformEstimate->minEstimate, + $platformEstimate->maxEstimate); + } + } + } + + $keywordEstimates = + $result->campaignEstimates[0]->adGroupEstimates[0]->keywordEstimates; + for ($i = 0; $i < sizeof($keywordEstimates); $i++) { + $keywordEstimateRequest = $keywordEstimateRequests[$i]; + // Skip negative keywords, since they don't return estimates. + if (!$keywordEstimateRequest->isNegative) { + $keyword = $keywordEstimateRequest->keyword; + $keywordEstimate = $keywordEstimates[$i]; + + if ($keywordEstimate->min !== null && $keywordEstimate->max !== null) { + // Print the mean of the min and max values. + printf("Results for the keyword with text '%s' and match type '%s':\n", + $keyword->text, $keyword->matchType); + printMeanEstimate($keywordEstimate->min, $keywordEstimate->max); + } + } + } +} + +/** + * Prints estimated average CPC, ad position, daily clicks, and daily costs + * between the provided lower bound and upper bound of estimated stats. + * + * @param StatsEstimate $minEstimate the lower bound on the estimated stats + * @param StatsEstimate $maxEstimate the upper bound on the estimated stats + */ +function printMeanEstimate(StatsEstimate $minEstimate, + StatsEstimate $maxEstimate) { + $minEstimateAverageCpcMicroAmount = ($minEstimate->averageCpc === null) + ? 0 : $minEstimate->averageCpc->microAmount; + $maxEstimateAverageCpcMicroAmount = ($maxEstimate->averageCpc === null) + ? 0 : $maxEstimate->averageCpc->microAmount; + $meanAverageCpc = + ($minEstimateAverageCpcMicroAmount + $maxEstimateAverageCpcMicroAmount) + / 2; + + $minEstimateAveragePosition = ($minEstimate->averagePosition === null) + ? 0 : $minEstimate->averagePosition; + $maxEstimateAveragePosition = ($maxEstimate->averagePosition === null) + ? 0 : $maxEstimate->averagePosition; + $meanAveragePosition = + ($minEstimateAveragePosition + $maxEstimateAveragePosition) / 2; + + $minEstimateClicksPerDay = ($minEstimate->clicksPerDay === null) + ? 0 : $minEstimate->clicksPerDay; + $maxEstimateClicksPerDay = ($maxEstimate->clicksPerDay === null) + ? 0 : $maxEstimate->clicksPerDay; + $meanClicks = ($minEstimateClicksPerDay + $maxEstimateClicksPerDay) / 2; + + $minEstimateTotalCostMicroAmount = ($minEstimate->totalCost === null) + ? 0 : $minEstimate->totalCost->microAmount; + $maxEstimateTotalCostMicroAmount = ($maxEstimate->totalCost === null) + ? 0 : $maxEstimate->totalCost->microAmount; + $meanTotalCost = + ($minEstimateTotalCostMicroAmount + $maxEstimateTotalCostMicroAmount) / 2; + + printf(" Estimated avaerage CPC: %.0f\n", $meanAverageCpc); + printf(" Estimated ad position: %.2f\n", $meanAveragePosition); + printf(" Estimated daily clicks: %d\n", $meanClicks); + printf(" Estimated daily cost: %.0f\n\n", $meanTotalCost); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + EstimateKeywordTrafficExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Optimization/GetCampaignCriterionBidModifierSimulations.php b/examples/AdWords/v201705/Optimization/GetCampaignCriterionBidModifierSimulations.php new file mode 100755 index 000000000..20bb5cfd7 --- /dev/null +++ b/examples/AdWords/v201705/Optimization/GetCampaignCriterionBidModifierSimulations.php @@ -0,0 +1,127 @@ +GetService('DataService', ADWORDS_VERSION); + + // Create selector. + $selector = new Selector(); + $selector->fields = array( + 'BidModifier', + 'CampaignId', + 'CriterionId', + 'StartDate', + 'EndDate', + 'LocalClicks', + 'LocalCost', + 'LocalImpressions', + 'TotalLocalClicks', + 'TotalLocalCost', + 'TotalLocalImpressions', + 'RequiredBudget' + ); + + // Create predicates. + $selector->predicates[] = + new Predicate('CampaignId', 'IN', array($campaignId)); + + // Create paging controls. + $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); + + do { + // Make the getCampaignCriterionBidLandscape request. + $page = $dataService->getCampaignCriterionBidLandscape($selector); + + // Display results. + if (isset($page->entries)) { + foreach ($page->entries as $bidModifierLandscape) { + printf( + "Found campaign-level criterion bid modifier landscapes for" + . " criterion with ID %d, start date '%s', end date '%s', and" + . " landscape points:\n", + $bidModifierLandscape->criterionId, + $bidModifierLandscape->startDate, + $bidModifierLandscape->endDate + ); + foreach ($bidModifierLandscape->landscapePoints as $landscapePoint) { + printf( + " bid modifier: %.2f => clicks: %d, cost: %.0f, " + . "impressions: %d, total clicks: %d, total cost: %.0f, " + . "total impressions: %d, and required budget: %.0f\n", + $landscapePoint->bidModifier, + $landscapePoint->clicks, + $landscapePoint->cost->microAmount, + $landscapePoint->impressions, + $landscapePoint->totalLocalClicks, + $landscapePoint->totalLocalCost->microAmount, + $landscapePoint->totalLocalImpressions, + $landscapePoint->requiredBudget->microAmount + ); + } + print "\n"; + } + } else if ($selector->paging->startIndex === 0) { + printf("No campaign criterion bid modifier landscapes were found.\n"); + } + // Advance the paging index. + $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE; + } while (isset($page->entries) && count($page->entries) > 0); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetCampaignCriterionBidModifierSimulationsExample($user, $campaignId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Optimization/GetKeywordBidSimulations.php b/examples/AdWords/v201705/Optimization/GetKeywordBidSimulations.php new file mode 100755 index 000000000..85eb513c2 --- /dev/null +++ b/examples/AdWords/v201705/Optimization/GetKeywordBidSimulations.php @@ -0,0 +1,101 @@ +GetService('DataService', ADWORDS_VERSION); + + // Create selector. + $selector = new Selector(); + $selector->fields = array('AdGroupId', 'CriterionId', 'StartDate', 'EndDate', + 'Bid', 'LocalClicks', 'LocalCost', 'LocalImpressions'); + + // Create predicates. + $selector->predicates[] = new Predicate('AdGroupId', 'IN', array($adGroupId)); + + // Create paging controls. + $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); + + do{ + // Make the getCriterionBidLandscape request. + $page = $dataService->getCriterionBidLandscape($selector); + + // Display results. + if (isset($page->entries)) { + foreach ($page->entries as $bidLandscape) { + printf("Found criterion bid landscape for keyword with id '%s', start " + . "date '%s', end date '%s', and landscape points:\n", + $bidLandscape->criterionId, $bidLandscape->startDate, + $bidLandscape->endDate); + foreach ($bidLandscape->landscapePoints as $bidLandscapePoint) { + printf(" bid: %.0f => clicks: %d, cost: %.0f, impressions: %d\n", + $bidLandscapePoint->bid->microAmount, + $bidLandscapePoint->clicks, + $bidLandscapePoint->cost->microAmount, + $bidLandscapePoint->impressions + ); + } + print "\n"; + } + } else if ($selector->paging->startIndex === 0) { + printf("No criterion bid landscapes were found.\n"); + } + // Advance the paging index. + $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE; + } while (isset($page->entries) && count($page->entries) > 0); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetKeywordBidSimulationsExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Optimization/GetKeywordIdeas.php b/examples/AdWords/v201705/Optimization/GetKeywordIdeas.php new file mode 100755 index 000000000..630ea08e4 --- /dev/null +++ b/examples/AdWords/v201705/Optimization/GetKeywordIdeas.php @@ -0,0 +1,126 @@ +GetService('TargetingIdeaService', ADWORDS_VERSION); + + // Create selector. + $selector = new TargetingIdeaSelector(); + $selector->requestType = 'IDEAS'; + $selector->ideaType = 'KEYWORD'; + $selector->requestedAttributeTypes = array('KEYWORD_TEXT', 'SEARCH_VOLUME', + 'CATEGORY_PRODUCTS_AND_SERVICES'); + + // Create seed keyword. + $keyword = 'mars cruise'; + // Create related to query search parameter. + $relatedToQuerySearchParameter = new RelatedToQuerySearchParameter(); + $relatedToQuerySearchParameter->queries = array($keyword); + $selector->searchParameters[] = $relatedToQuerySearchParameter; + + // Create language search parameter (optional). + // The ID can be found in the documentation: + // https://developers.google.com/adwords/api/docs/appendix/languagecodes + // Note: As of v201302, only a single language parameter is allowed. + $languageParameter = new LanguageSearchParameter(); + $english = new Language(); + $english->id = 1000; + $languageParameter->languages = array($english); + $selector->searchParameters[] = $languageParameter; + + // Create network search parameter (optional). + $networkSetting = new NetworkSetting(); + $networkSetting->targetGoogleSearch = true; + $networkSetting->targetSearchNetwork = false; + $networkSetting->targetContentNetwork = false; + $networkSetting->targetPartnerSearchNetwork = false; + + $networkSearchParameter = new NetworkSearchParameter(); + $networkSearchParameter->networkSetting = $networkSetting; + $selector->searchParameters[] = $networkSearchParameter; + + // Set selector paging (required by this service). + $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); + + do { + // Make the get request. + $page = $targetingIdeaService->get($selector); + + // Display results. + if (isset($page->entries)) { + foreach ($page->entries as $targetingIdea) { + $data = MapUtils::GetMap($targetingIdea->data); + $keyword = $data['KEYWORD_TEXT']->value; + $search_volume = isset($data['SEARCH_VOLUME']->value) + ? $data['SEARCH_VOLUME']->value : 0; + if ($data['CATEGORY_PRODUCTS_AND_SERVICES']->value === null) { + $categoryIds = ''; + } else { + $categoryIds = + implode(', ', $data['CATEGORY_PRODUCTS_AND_SERVICES']->value); + } + printf("Keyword idea with text '%s', category IDs (%s) and average " + . "monthly search volume '%s' was found.\n", + $keyword, $categoryIds, $search_volume); + } + } else { + print "No keywords ideas were found.\n"; + } + + // Advance the paging index. + $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE; + } while ($page->totalNumEntries > $selector->paging->startIndex); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetKeywordIdeasExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Remarketing/AddAudience.php b/examples/AdWords/v201705/Remarketing/AddAudience.php new file mode 100755 index 000000000..ef27773f1 --- /dev/null +++ b/examples/AdWords/v201705/Remarketing/AddAudience.php @@ -0,0 +1,104 @@ +GetService('AdwordsUserListService', ADWORDS_VERSION); + $conversionTrackerService = + $user->GetService('ConversionTrackerService', ADWORDS_VERSION); + + // Create conversion type (tag). + $conversionType = new UserListConversionType(); + $conversionType->name = 'Mars cruise customers #' . uniqid(); + + // Create remarketing user list. + $userList = new BasicUserList(); + $userList->name = 'Mars cruise customers #' . uniqid(); + $userList->conversionTypes = array($conversionType); + + // Set additional settings (optional). + $userList->description = 'A list of mars cruise customers in the last year'; + $userList->status = 'OPEN'; + $userList->membershipLifeSpan = 365; + + // Create operation. + $operation = new UserListOperation(); + $operation->operand = $userList; + $operation->operator = 'ADD'; + + $operations = array($operation); + + // Make the mutate request. + $result = $userListService->mutate($operations); + $userList = $result->value[0]; + + // Wait a moment before retrieving the conversion snippet. + sleep(1); + + // Create the selector. + $selector = new Selector(); + $selector->fields = array('Id'); + $selector->predicates[] = + new Predicate('Id', 'IN', array($userList->conversionTypes[0]->id)); + + // Make the get request. + $page = $conversionTrackerService->get($selector); + $conversionTracker = $page->entries[0]; + + // Display result. + printf("Audience with name '%s' and ID '%.0f' was added.\n", $userList->name, + $userList->id); + printf("Tag code:\n%s\n", $conversionTracker->snippet); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddAudienceExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201607/Express/AddExpressBusinesses.php b/examples/AdWords/v201705/Remarketing/AddConversionTracker.php similarity index 53% rename from examples/AdWords/v201607/Express/AddExpressBusinesses.php rename to examples/AdWords/v201705/Remarketing/AddConversionTracker.php index 1e12916d9..cdd482cf2 100755 --- a/examples/AdWords/v201607/Express/AddExpressBusinesses.php +++ b/examples/AdWords/v201705/Remarketing/AddConversionTracker.php @@ -1,7 +1,8 @@ GetService('ExpressBusinessService', - ADWORDS_VERSION); - - $business1 = new ExpressBusiness(); - $business1->status = 'ENABLED'; - $business1->name = 'Express Interplanetary Cruise #' . uniqid(); - $business1->website = 'http://www.example.com/cruise1'; + $conversionTrackerService = + $user->GetService('ConversionTrackerService', ADWORDS_VERSION); - $business2 = new ExpressBusiness(); - $business2->status = 'ENABLED'; - $business2->name = 'Express Interplanetary Cruise #' . uniqid(); - $business2->website = 'http://www.example.com/cruise2'; + // Create conversion tracker. + $conversionTracker = new AdWordsConversionTracker(); + $conversionTracker->name = 'Interplanetary Cruise Conversion #' . uniqid(); - $operations = array(); + // Set additional settings (optional). + $conversionTracker->status = 'ENABLED'; + $conversionTracker->category = 'DEFAULT'; + $conversionTracker->viewthroughLookbackWindow = 15; + $conversionTracker->textFormat = 'HIDDEN'; + $conversionTracker->conversionPageLanguage = 'en'; + $conversionTracker->backgroundColor = '#0000FF'; - $operation1 = new ExpressBusinessOperation(); - $operation1->operand = $business1; - $operation1->operator = 'ADD'; - $operations[] = $operation1; + // Create operation. + $operation = new ConversionTrackerOperation(); + $operation->operand = $conversionTracker; + $operation->operator = 'ADD'; - $operation2 = new ExpressBusinessOperation(); - $operation2->operand = $business2; - $operation2->operator = 'ADD'; - $operations[] = $operation2; + $operations = array($operation); - $addedBusinesses = $businessService->mutate($operations); + // Make the mutate request. + $result = $conversionTrackerService->mutate($operations); - foreach($addedBusinesses as $addedBusiness) { - printf("Added express business with ID %d and name '%s'\n", - $addedBusiness->id, $addedBusiness->name); - } + // Display result. + $conversionTracker = $result->value[0]; + printf("Conversion type with name '%s' and ID '%.0f' was added.\n", + $conversionTracker->name, $conversionTracker->id); + printf("Tag code:\n%s\n", $conversionTracker->snippet); } // Don't run the example if the file is being included. @@ -81,7 +81,7 @@ function AddExpressBusinessesExample(AdWordsUser $user) { $user->LogAll(); // Run the example. - AddExpressBusinessesExample($user); + AddConversionTrackerExample($user); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201705/Remarketing/AddCrmBasedUserList.php b/examples/AdWords/v201705/Remarketing/AddCrmBasedUserList.php new file mode 100755 index 000000000..dcd5b3a8e --- /dev/null +++ b/examples/AdWords/v201705/Remarketing/AddCrmBasedUserList.php @@ -0,0 +1,163 @@ + + * Note: It may take up to several hours for the list to be populated + * with members. + * Email addresses must be associated with a Google account. + * For privacy purposes, the user list size will show as zero until the list has + * at least 1,000 members. After that, the size will be rounded to the two most + * significant digits. + *

+ * + * PHP version 5 + * + * Copyright 2016, Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @package GoogleApiAdsAdWords + * @subpackage v201705 + * @category WebServices + * @copyright 2016, Google Inc. All Rights Reserved. + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, + * Version 2.0 + */ + +// Include the initialization file. +require_once dirname(dirname(__FILE__)) . '/init.php'; + +$EMAILS = array('customer1@example.com', 'customer2@example.com', + 'Client3@example.com '); + +/** + * Runs the example. + * @param AdWordsUser $user the user to run the example with + * @param array $EMAILS a list of member emails to be added to a user list + */ +function AddCrmBasedUserList(AdWordsUser $user, array $EMAILS) { + // Get the services, which loads the required classes. + $userListService = $user->GetService('AdwordsUserListService', + ADWORDS_VERSION); + + // Create a user list. + $userList = new CrmBasedUserList(); + $userList->name = 'Customer relationship management list #' . uniqid(); + $userList->description = + 'A list of customers that originated from email addresses'; + + // Maximum life span is 180 days. + $userList->membershipLifeSpan = 180; + + // Create operations to add the user list. + $operation = new UserListOperation(); + $operation->operand = $userList; + $operation->operator = 'ADD'; + + $operations = array($operation); + + // Add user list. + $result = $userListService->mutate($operations); + + // Display user list. + $userListAdded = $result->value[0]; + printf("User list with name '%s' and ID '%d' was added.\n", + $userListAdded->name, $userListAdded->id); + + // Get a user list ID. + $userListId = $userListAdded->id; + + $mutateMembersOperations = array(); + // Create operation to add members to the user list based on email addresses. + $mutateMembersOperation = new MutateMembersOperation(); + $operand = new MutateMembersOperand(); + $operand->userListId = $userListId; + + $members = array(); + // Hash normalized email addresses based on SHA-256 hashing algorithm. + foreach ($EMAILS as $email) { + $memberByEmail = new Member(); + $memberByEmail->hashedEmail = normalizeAndHash($email); + $members[] = $memberByEmail; + } + + // Adding address info is currently available on a whitelist-only basis. + // This code demonstrates how to do it, and you can uncomment it if you are on + // the whitelist. + /* + $firstName = 'John'; + $lastName = 'Doe'; + $countryCode = 'US'; + $zipCode = '10011'; + + $addressInfo = new AddressInfo(); + // First and last name must be normalized and hashed. + $addressInfo->hashedFirstName = normalizeAndHash($firstName); + $addressInfo->hashedLastName = normalizeAndHash($lastName); + // Country code and zip code are sent in plain text. + $addressInfo->countryCode = $countryCode; + $addressInfo->zipCode = $zipCode; + + $memberByAddress = new Member(); + $memberByAddress->addressInfo = $addressInfo; + $members[] = $memberByAddress; + */ + + // Add members to the operand and add the operation to the list. + $operand->membersList = $members; + $mutateMembersOperation->operand = $operand; + $mutateMembersOperation->operator = 'ADD'; + $mutateMembersOperations[] = $mutateMembersOperation; + + // Add members to the user list based on email addresses. + $mutateMembersResult = + $userListService->mutateMembers($mutateMembersOperations); + + // Display results. + // Reminder: it may take several hours for the list to be populated with + // members. + foreach ($mutateMembersResult->userLists as $userListResult) { + printf( + "%d email addresses were uploaded to user list with name '%s' and ID" + . " '%d' and are scheduled for review.\n", + count($EMAILS), + $userListResult->name, + $userListResult->id + ); + } +} + +function normalizeAndHash($value) { + return hash('sha256', strtolower(trim($value))); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddCrmBasedUserList($user, $EMAILS); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Remarketing/AddRuleBasedUserLists.php b/examples/AdWords/v201705/Remarketing/AddRuleBasedUserLists.php new file mode 100755 index 000000000..422837e3d --- /dev/null +++ b/examples/AdWords/v201705/Remarketing/AddRuleBasedUserLists.php @@ -0,0 +1,179 @@ +GetService('AdwordsUserListService', + ADWORDS_VERSION); + + // First rule item group - users who visited the checkout page and had more + // than one item in their shopping cart. + $checkoutStringRuleItem = new StringRuleItem(); + $checkoutStringKey = new StringKey(); + $checkoutStringKey->name = 'ecomm_pagetype'; + $checkoutStringRuleItem->key = $checkoutStringKey; + $checkoutStringRuleItem->op = 'EQUALS'; + $checkoutStringRuleItem->value = 'checkout'; + $checkoutRuleItem = new RuleItem(); + $checkoutRuleItem->StringRuleItem = $checkoutStringRuleItem; + + $cartSizeNumberRuleItem = new NumberRuleItem(); + $cartSizeNumberKey = new NumberKey(); + $cartSizeNumberKey->name = 'cartsize'; + $cartSizeNumberRuleItem->key = $cartSizeNumberKey; + $cartSizeNumberRuleItem->op = 'GREATER_THAN'; + $cartSizeNumberRuleItem->value = 1.0; + $cartSizeRuleItem = new RuleItem(); + $cartSizeRuleItem->NumberRuleItem = $cartSizeNumberRuleItem; + + // Combine the two rule items into a RuleItemGroup so AdWords will AND their + // rules together. + $checkoutMultipleItemGroup = new RuleItemGroup(); + $checkoutMultipleItemGroup->items = array($checkoutRuleItem, + $cartSizeRuleItem); + + // Second rule item group - users who checked out within the next 3 months. + $today = new DateTime(); + $startDateDateRuleItem = new DateRuleItem(); + $startDateDateKey = new DateKey(); + $startDateDateKey->name = 'checkoutdate'; + $startDateDateRuleItem->key = $startDateDateKey; + $startDateDateRuleItem->op = 'AFTER'; + $startDateDateRuleItem->value = $today->format('Ymd'); + $startDateRuleItem = new RuleItem(); + $startDateRuleItem->DateRuleItem = $startDateDateRuleItem; + + $threeMonthsLater = clone($today); + $threeMonthsLater->modify('+3 month'); + $endDateDateRuleItem = new DateRuleItem(); + $endDateDateKey = new DateKey(); + $endDateDateKey->name = 'checkoutdate'; + $endDateDateRuleItem->key = $endDateDateKey; + $endDateDateRuleItem->op = 'BEFORE'; + $endDateDateRuleItem->value = $threeMonthsLater->format('Ymd'); + $endDateRuleItem = new RuleItem(); + $endDateRuleItem->DateRuleItem = $endDateDateRuleItem; + + // Combine the date rule items into a RuleItemGroup. + $checkedOutDateRangeItemGroup = new RuleItemGroup(); + $checkedOutDateRangeItemGroup->items = array($startDateRuleItem, + $endDateRuleItem); + + // Combine the rule item groups into a Rule so AdWords knows how to apply + // the rules. + $rule = new Rule(); + $rule->groups = array($checkoutMultipleItemGroup, + $checkedOutDateRangeItemGroup); + // ExpressionRuleUserLists can use either CNF or DNF for matching. CNF means + // 'at least one item in each rule item group must match', and DNF means 'at + // least one entire rule item group must match'. DateSpecificRuleUserList + // only supports DNF. You can also omit the rule type altogether to default + // to DNF. + $rule->ruleType = 'DNF'; + + // Create the user list with no restrictions on site visit date. + $expressionUserList = new ExpressionRuleUserList(); + $expressionUserList->name = sprintf('Expression based user list created at ' + . '%s', date('Y-m-d_His')); + $expressionUserList->description = 'Users who checked out in three month ' + . 'window OR visited the checkout page with more than one item in ' + . 'their cart'; + $expressionUserList->rule = $rule; + + // Create the user list restricted to users who visit your site within the + // next six months. + $startDate = clone($today); + $endDate = clone($today); + $endDate->modify('+6 month'); + + $dateUserList = new DateSpecificRuleUserList(); + $dateUserList->name = sprintf('Date rule user list created at %s', + date('Y-m-d_His')); + $dateUserList->description = sprintf('Users who visited the site between %s ' + . 'and %s and checked out in three month window OR visited the checkout ' + . 'page with more than one item in their cart', + $startDate->format('Ymd'), + $endDate->format('Ymd')); + $dateUserList->rule = $rule; + + // Set the start and end dates of the user list. + $dateUserList->startDate = $startDate->format('Ymd'); + $dateUserList->endDate = $endDate->format('Ymd'); + + // Create operations to add the user lists. + $operations = array(); + foreach (array($expressionUserList, $dateUserList) as $userList) { + $operation = new UserListOperation(); + $operation->operand = $userList; + $operation->operator = 'ADD'; + $operations[] = $operation; + } + + // Submit the operations. + $result = $userListService->mutate($operations); + + // Display the results. + foreach ($result->value as $userListResult) { + printf("User list added with ID %d, name '%s', status '%s', list type '%s'" + . ", accountUserListStatus '%s', description '%s'.\n", + $userListResult->id, + $userListResult->name, + $userListResult->status, + $userListResult->listType, + $userListResult->accountUserListStatus, + $userListResult->description); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddRuleBasedUserLists($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Remarketing/UploadOfflineCallConversions.php b/examples/AdWords/v201705/Remarketing/UploadOfflineCallConversions.php new file mode 100755 index 000000000..3b88a13a9 --- /dev/null +++ b/examples/AdWords/v201705/Remarketing/UploadOfflineCallConversions.php @@ -0,0 +1,103 @@ +GetService('OfflineCallConversionFeedService', ADWORDS_VERSION); + + // Associate offline call conversions with the existing named conversion + // tracker. If this tracker was newly created, it may be a few hours before + // it can accept conversions. + $feed = new OfflineCallConversionFeed(); + $feed->callerId = $callerId; + $feed->callStartTime = $callStartTime; + $feed->conversionName = $conversionName; + $feed->conversionTime = $conversionTime; + $feed->conversionValue = $conversionValue; + + $offlineCallConversionOperation = new OfflineCallConversionFeedOperation(); + $offlineCallConversionOperation->operator = 'ADD'; + $offlineCallConversionOperation->operand = $feed; + + $offlineCallConversionOperations = array($offlineCallConversionOperation); + // This example uploads only one call conversion, but you can upload multiple + // call conversions by passing additional operations. + $result = + $offlineCallConversionService->mutate($offlineCallConversionOperations); + + $feed = $result->value[0]; + printf("Uploaded offline call conversion value of '%s' for caller ID '%s'.\n", + $feed->conversionValue, $feed->callerId); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + UploadOfflineCallConversionsExample($user, $callerId, $callStartTime, + $conversionName, $conversionTime, $conversionValue); +} catch (OAuth2Exception $e) { + ExampleUtils::CheckForOAuth2Errors($e); +} catch (ValidationException $e) { + ExampleUtils::CheckForOAuth2Errors($e); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Remarketing/UploadOfflineConversions.php b/examples/AdWords/v201705/Remarketing/UploadOfflineConversions.php new file mode 100755 index 000000000..35341badd --- /dev/null +++ b/examples/AdWords/v201705/Remarketing/UploadOfflineConversions.php @@ -0,0 +1,97 @@ +GetService('OfflineConversionFeedService', + ADWORDS_VERSION); + + // Associate offline conversions with the existing named conversion tracker. + // If this tracker was newly created, it may be a few hours before it can + // accept conversions. + $feed = new OfflineConversionFeed(); + $feed->conversionName = $conversionName; + $feed->conversionTime = $conversionTime; + $feed->conversionValue = $conversionValue; + $feed->googleClickId = $gclid; + + $offlineConversionOperation = new OfflineConversionFeedOperation(); + $offlineConversionOperation->operator = 'ADD'; + $offlineConversionOperation->operand = $feed; + + $offlineConversionOperations = array($offlineConversionOperation); + $result = $offlineConversionService->mutate($offlineConversionOperations); + + $feed = $result->value[0]; + printf('Uploaded offline conversion value of %d for Google Click ID = ' . + "'%s' to '%s'.", $feed->conversionValue, $feed->googleClickId, + $feed->conversionName); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + UploadOfflineConversionsExample($user, $conversionName, $gclid, + $conversionTime, $conversionValue); +} catch (OAuth2Exception $e) { + ExampleUtils::CheckForOAuth2Errors($e); +} catch (ValidationException $e) { + ExampleUtils::CheckForOAuth2Errors($e); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Reporting/DownloadCriteriaReport.php b/examples/AdWords/v201705/Reporting/DownloadCriteriaReport.php new file mode 100755 index 000000000..ee324c383 --- /dev/null +++ b/examples/AdWords/v201705/Reporting/DownloadCriteriaReport.php @@ -0,0 +1,102 @@ +LoadService('ReportDefinitionService', ADWORDS_VERSION); + // Optional: Set clientCustomerId to get reports of your child accounts + // $user->SetClientCustomerId('INSERT_CLIENT_CUSTOMER_ID_HERE'); + + // Create selector. + $selector = new Selector(); + $selector->fields = array('CampaignId', 'AdGroupId', 'Id', 'Criteria', + 'CriteriaType', 'Impressions', 'Clicks', 'Cost'); + + // Optional: use predicate to filter out paused criteria. + $selector->predicates[] = new Predicate('Status', 'NOT_IN', array('PAUSED')); + + // Create report definition. + $reportDefinition = new ReportDefinition(); + $reportDefinition->selector = $selector; + $reportDefinition->reportName = 'Criteria performance report #' . uniqid(); + $reportDefinition->dateRangeType = 'LAST_7_DAYS'; + $reportDefinition->reportType = 'CRITERIA_PERFORMANCE_REPORT'; + $reportDefinition->downloadFormat = 'CSV'; + + // Set additional options. + $options = array('version' => ADWORDS_VERSION); + + // Optional: Set skipReportHeader, skipColumnHeader, skipReportSummary to + // suppress headers or summary rows. + // $options['skipReportHeader'] = true; + // $options['skipColumnHeader'] = true; + // $options['skipReportSummary'] = true; + // + // Optional: Set useRawEnumValues to return enum values instead of enum + // display values. + // $options['useRawEnumValues'] = true; + // + // Optional: Set includeZeroImpressions to include zero impression rows in + // the report output. + // $options['includeZeroImpressions'] = true; + + // Download report. + $reportUtils = new ReportUtils(); + $reportUtils->DownloadReport($reportDefinition, $filePath, $user, $options); + printf("Report with name '%s' was downloaded to '%s'.\n", + $reportDefinition->reportName, $filePath); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Download the report to a file in the same directory as the example. + $filePath = dirname(__FILE__) . '/report.csv'; + + // Run the example. + DownloadCriteriaReportExample($user, $filePath); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Reporting/DownloadCriteriaReportWithAwql.php b/examples/AdWords/v201705/Reporting/DownloadCriteriaReportWithAwql.php new file mode 100755 index 000000000..0f5c2f5b6 --- /dev/null +++ b/examples/AdWords/v201705/Reporting/DownloadCriteriaReportWithAwql.php @@ -0,0 +1,98 @@ +SetClientCustomerId('INSERT_CLIENT_CUSTOMER_ID_HERE'); + + // Prepare a date range for the last week. Instead you can use 'LAST_7_DAYS'. + $dateRange = sprintf('%d,%d', + date('Ymd', strtotime('-7 day')), date('Ymd', strtotime('-1 day'))); + + // Create report query. + $reportQuery = 'SELECT CampaignId, AdGroupId, Id, Criteria, CriteriaType, ' + . 'Impressions, Clicks, Cost FROM CRITERIA_PERFORMANCE_REPORT ' + . 'WHERE Status IN [ENABLED, PAUSED] DURING ' . $dateRange; + + // Set additional options. + $options = array('version' => ADWORDS_VERSION); + + // Optional: Set skipReportHeader, skipColumnHeader, skipReportSummary to + // suppress headers or summary rows. + // $options['skipReportHeader'] = true; + // $options['skipColumnHeader'] = true; + // $options['skipReportSummary'] = true; + // + // Optional: Set useRawEnumValues to return enum values instead of enum + // display values. + // $options['useRawEnumValues'] = false; + // + // Optional: Set includeZeroImpressions to include zero impression rows in + // the report output. + // $options['includeZeroImpressions'] = true; + + // Download report. + $reportUtils = new ReportUtils(); + $reportUtils->DownloadReportWithAwql($reportQuery, $filePath, $user, + $reportFormat, $options); + + printf("Report was downloaded to '%s'.\n", $filePath); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Download the report to a file in the same directory as the example. + $filePath = dirname(__FILE__) . '/report.csv'; + $reportFormat = 'CSV'; + + // Run the example. + DownloadCriteriaReportWithAwqlExample($user, $filePath, $reportFormat); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Reporting/GetReportFields.php b/examples/AdWords/v201705/Reporting/GetReportFields.php new file mode 100755 index 000000000..f7a8d098a --- /dev/null +++ b/examples/AdWords/v201705/Reporting/GetReportFields.php @@ -0,0 +1,75 @@ +GetService('ReportDefinitionService', ADWORDS_VERSION); + + // The type of the report to get fields for. + $reportType = 'CAMPAIGN_PERFORMANCE_REPORT'; + + // Get report fields. + $reportDefinitionFields = + $reportDefinitionService->getReportFields($reportType); + + // Display results. + printf("The report type '%s' contains the following fields:\n", $reportType); + foreach ($reportDefinitionFields as $reportDefinitionField) { + printf(' %s (%s)', $reportDefinitionField->fieldName, + $reportDefinitionField->fieldType); + if (isset($reportDefinitionField->enumValues)) { + printf(' := [%s]', implode(', ', $reportDefinitionField->enumValues)); + } + print "\n"; + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetReportFieldsExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Reporting/ParallelReportDownload.php b/examples/AdWords/v201705/Reporting/ParallelReportDownload.php new file mode 100755 index 000000000..a910c08ed --- /dev/null +++ b/examples/AdWords/v201705/Reporting/ParallelReportDownload.php @@ -0,0 +1,196 @@ +LoadService('ReportDefinitionService', ADWORDS_VERSION); + + // Create selector. + $selector = new Selector(); + $selector->fields = + array('CampaignId', 'AdGroupId', 'Impressions', 'Clicks', 'Cost'); + + // Create report definition. + $reportDefinition = new ReportDefinition(); + $reportDefinition->selector = $selector; + $reportDefinition->reportName = 'Custom ADGROUP_PERFORMANCE_REPORT'; + $reportDefinition->dateRangeType = 'LAST_7_DAYS'; + $reportDefinition->reportType = 'ADGROUP_PERFORMANCE_REPORT'; + $reportDefinition->downloadFormat = 'CSV'; + + // Set additional options. + $options = array('version' => ADWORDS_VERSION); + + // Optional: Set skipReportHeader, skipColumnHeader, skipReportSummary to + // suppress headers or summary rows. + // $options['skipReportHeader'] = true; + // $options['skipColumnHeader'] = true; + // $options['skipReportSummary'] = true; + // + // Optional: Set useRawEnumValues to return enum values instead of enum + // display values. + // $options['useRawEnumValues'] = true; + // + // Optional: Set includeZeroImpressions to include zero impression rows in + // the report output. + // $options['includeZeroImpressions'] = true; + + $customerIds = getAllManagedCustomerIds($user); + printf("Downloading reports for %d managed customers.\n", + count($customerIds)); + + $successfulReports = array(); + $failedReports = array(); + $reportDir = sys_get_temp_dir(); + $reportUtils = new ReportUtils(); + + foreach ($customerIds as $customerId) { + $filePath = sprintf('%s.csv', tempnam($reportDir, 'adgroup_')); + $user->SetClientCustomerId($customerId); + + $retryCount = 0; + $doContinue = true; + do { + $retryCount++; + try { + $reportUtils->DownloadReport($reportDefinition, $filePath, $user, + $options); + printf( + "Report for client customer ID %d successfully downloaded to: %s\n", + $customerId, + $filePath + ); + $successfulReports[$customerId] = $filePath; + $doContinue = false; + } catch (ReportDownloadException $e) { + printf( + "Report attempt #%d for client customer ID %d was not downloaded" + . " due to: %s\n", + $retryCount, + $customerId, + $e->getMessage() + ); + + if ($e->GetHttpCode() >= 500 && $retryCount < MAX_RETRIES) { + $sleepTime = $retryCount * BACKOFF_FACTOR; + printf( + "Sleeping %d seconds before retrying report for client customer " + . "ID %d.\n", + $sleepTime, + $customerId + ); + sleep($sleepTime); + } else { + printf( + "Report request failed for client customer ID %d.\n", + $customerId + ); + $failedReports[$customerId] = $filePath; + $doContinue = false; + } + + } + } while ($doContinue === true); + } + + print "All downloads completed. Results:\n"; + print "Successful reports:\n"; + foreach ($successfulReports as $customerId => $filePath) { + printf("\tClient ID %d => '%s'\n", $customerId, $filePath); + } + print "Failed reports:\n"; + foreach ($failedReports as $customerId => $filePath) { + printf("\tClient ID %d => '%s'\n", $customerId, $filePath); + } + print "End of results.\n"; +} + +/** + * Retrieves all the customer IDs under a manager account. + * + * @param AdWordsUser $user the user to run the example with + * @return array the list of customer IDs under a manager account + */ +function getAllManagedCustomerIds(AdWordsUser $user) { + // Optional: Set clientCustomerId to any manager account you want to get + // reports for its client accounts. + // $user->SetClientCustomerId('INSERT_CLIENT_CUSTOMER_ID_HERE'); + $managedCustomerService = + $user->GetService('ManagedCustomerService', ADWORDS_VERSION); + + $selector = new Selector(); + $selector->fields = array('CustomerId'); + $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); + $selector->predicates[] = + new Predicate('CanManageClients', 'EQUALS', 'false'); + + $customerIds = array(); + do { + $page = $managedCustomerService->get($selector); + if (isset($page->entries)) { + foreach ($page->entries as $customer) { + $customerIds[] = $customer->customerId; + } + } + $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE; + } while ($selector->paging->startIndex < $page->totalNumEntries); + + return $customerIds; +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + ParallelReportDownloadExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/ShoppingCampaigns/AddProductPartitionTree.php b/examples/AdWords/v201705/ShoppingCampaigns/AddProductPartitionTree.php new file mode 100755 index 000000000..a5ebce08c --- /dev/null +++ b/examples/AdWords/v201705/ShoppingCampaigns/AddProductPartitionTree.php @@ -0,0 +1,263 @@ +adGroupId = $adGroupId; + } + + /** + * Creates a subdivision node + * @param ProductPartition $parent The node that should be this node's parent + * @param ProductDimension $value The value being partitioned on + * @return ProductPartition A new subdivision node + */ + public function createSubdivision(ProductPartition $parent = null, + ProductDimension $value = null) { + $division = new ProductPartition('SUBDIVISION'); + $division->id = $this->nextId--; + + // The root node has neither a parent nor a value + if (!is_null($parent)) { + $division->parentCriterionId = $parent->id; + $division->caseValue = $value; + } + + $criterion = new BiddableAdGroupCriterion(); + $criterion->adGroupId = $this->adGroupId; + $criterion->criterion = $division; + + $this->createAddOperation($criterion); + + return $division; + } + + /** + * Creates a unit node. + * @param ProductPartition $parent The node that should be this node's parent + * @param ProductDimension $value The value being partitioned on + * @param int $bid_amount The amount to bid for matching products, in micros + * @return ProductPartition A new unit node + */ + public function createUnit(ProductPartition $parent = null, + ProductDimension $value = null, $bid_amount = null) { + $unit = new ProductPartition('UNIT'); + + // The root node has neither a parent nor a value + if (!is_null($parent)) { + $unit->parentCriterionId = $parent->id; + $unit->caseValue = $value; + } + + if (!is_null($bid_amount) && $bid_amount > 0) { + $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); + $biddingStrategyConfiguration->bids = array(); + $cpcBid = new CpcBid(); + $cpcBid->bid = new Money($bid_amount); + $biddingStrategyConfiguration->bids[] = $cpcBid; + + $criterion = new BiddableAdGroupCriterion(); + $criterion->biddingStrategyConfiguration = $biddingStrategyConfiguration; + } else { + $criterion = new NegativeAdGroupCriterion(); + } + + $criterion->adGroupId = $this->adGroupId; + $criterion->criterion = $unit; + + $this->createAddOperation($criterion); + + return $unit; + } + + /** + * Returns the set of mutate operations needed to create the current tree. + * @return array The set of operations + */ + public function getOperations() { + return $this->operations; + } + + /** + * Creates an AdGroupCriterionOperation for the given criterion + * @param AdGroupCriterion $criterion The criterion we want to add + */ + private function createAddOperation(AdGroupCriterion $criterion) { + $operation = new AdGroupCriterionOperation(); + $operation->operand = $criterion; + $operation->operator = 'ADD'; + $this->operations[] = $operation; + } +} + +/** + * Runs the example. + * @param AdWordsUser $user the user to run the example with + * @param int $adGroupId the ad group to add the tree to + */ +function addProductPartitionTreeExample(AdWordsUser $user, $adGroupId) { + // Get the AdGroupCriterionService, which loads the required classes. + $adGroupCriterionService = $user->GetService('AdGroupCriterionService', + ADWORDS_VERSION); + + $helper = new ProductPartitionHelper($adGroupId); + + // The most trivial partition tree has only a unit node as the root: + // $helper->createUnit(null, null, 100000); + + $root = $helper->createSubdivision(); + + $helper->createUnit($root, new ProductCanonicalCondition('NEW'), 200000); + $helper->createUnit($root, new ProductCanonicalCondition('USED'), 100000); + $otherCondition = $helper->createSubdivision($root, + new ProductCanonicalCondition()); + + $helper->createUnit($otherCondition, new ProductBrand('CoolBrand'), 900000); + $helper->createUnit($otherCondition, new ProductBrand('CheapBrand'), 10000); + $otherBrand = + $helper->createSubdivision($otherCondition, new ProductBrand()); + + // The value for the bidding category is a fixed ID for the 'Luggage & Bags' + // category. You can retrieve IDs for categories from the ConstantDataService. + // See the 'GetProductCategoryTaxonomy' example for more details. + $helper->createUnit($otherBrand, + new ProductBiddingCategory('BIDDING_CATEGORY_L1', + '-5914235892932915235'), 750000); + $helper->createUnit($otherBrand, + new ProductBiddingCategory('BIDDING_CATEGORY_L1'), 110000); + + // Make the mutate request. + $result = $adGroupCriterionService->mutate($helper->getOperations()); + + $children = array(); + $rootNode = null; + // For each criterion, make an array containing each of its children + // We always create the parent before the child, so we can rely on that here + foreach ($result->value as $adGroupCriterion) { + $children[$adGroupCriterion->criterion->id] = array(); + + if (isset($adGroupCriterion->criterion->parentCriterionId)) { + $children[$adGroupCriterion->criterion->parentCriterionId][] = + $adGroupCriterion->criterion; + } else { + $rootNode = $adGroupCriterion->criterion; + } + } + + // Show the tree + displayTree($rootNode, $children); +} + +function displayTree($node, $children, $level = 0) { + // Recursively display a node and each of its children + $value = ""; + $type = ""; + + if (isset($node->caseValue)) { + $type = $node->caseValue->ProductDimensionType; + switch ($type) { + case 'ProductCanonicalCondition': + $value = $node->caseValue->condition; + break; + + case 'ProductBiddingCategory': + $value = $node->caseValue->type . "(" . $node->caseValue->value . ")"; + break; + + default: + $value = $node->caseValue->value; + break; + } + } + + printf("%sid: %s, type: %s, value: %s\n", str_repeat(" ", $level), + $node->id, $type, $value); + foreach ($children[$node->id] as $childNode) { + displayTree($childNode, $children, $level + 1); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + addProductPartitionTreeExample($user, $adGroupId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/ShoppingCampaigns/AddProductScope.php b/examples/AdWords/v201705/ShoppingCampaigns/AddProductScope.php new file mode 100755 index 000000000..6f3a57ce1 --- /dev/null +++ b/examples/AdWords/v201705/ShoppingCampaigns/AddProductScope.php @@ -0,0 +1,92 @@ +GetService('CampaignCriterionService', + ADWORDS_VERSION); + + $productScope = new ProductScope(); + // This set of dimensions is for demonstration purposes only. It would be + // extremely unlikely that you want to include so many dimensions in your + // product scope. + $productScope->dimensions[] = new ProductBrand('Nexus'); + $productScope->dimensions[] = new ProductCanonicalCondition('NEW'); + $productScope->dimensions[] = + new ProductCustomAttribute('CUSTOM_ATTRIBUTE_0', 'my attribute value'); + $productScope->dimensions[] = new ProductOfferId('book1'); + $productScope->dimensions[] = new ProductType('PRODUCT_TYPE_L1', 'Media'); + $productScope->dimensions[] = new ProductType('PRODUCT_TYPE_L2', 'Books'); + // The value for the bidding category is a fixed ID for the 'Luggage & Bags' + // category. You can retrieve IDs for categories from the ConstantDataService. + // See the 'GetProductCategoryTaxonomy' example for more details. + $productScope->dimensions[] = + new ProductBiddingCategory('BIDDING_CATEGORY_L1', '-5914235892932915235'); + + $campaignCriterion = new CampaignCriterion(); + $campaignCriterion->campaignId = $campaignId; + $campaignCriterion->criterion = $productScope; + + // Create operation. + $operation = new CampaignCriterionOperation(); + $operation->operand = $campaignCriterion; + $operation->operator = 'ADD'; + + // Make the mutate request. + $result = $campaignCriterionService->mutate(array($operation)); + + printf("Created a ProductScope criterion with ID '%s'", + $result->value[0]->criterion->id); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + addProductScopeExample($user, $campaignId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/ShoppingCampaigns/AddShoppingCampaign.php b/examples/AdWords/v201705/ShoppingCampaigns/AddShoppingCampaign.php new file mode 100755 index 000000000..e7424b486 --- /dev/null +++ b/examples/AdWords/v201705/ShoppingCampaigns/AddShoppingCampaign.php @@ -0,0 +1,142 @@ +GetService('CampaignService', ADWORDS_VERSION); + $adGroupService = $user->GetService('AdGroupService', ADWORDS_VERSION); + $adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION); + + // Create campaign. + $campaign = new Campaign(); + $campaign->name = 'Shopping campaign #' . uniqid(); + // The advertisingChannelType is what makes this a Shopping campaign + $campaign->advertisingChannelType = 'SHOPPING'; + // Recommendation: Set the campaign to PAUSED when creating it to stop + // the ads from immediately serving. Set to ENABLED once you've added + // targeting and the ads are ready to serve. + $campaign->status = 'PAUSED'; + + // Set portfolio budget (required). + $campaign->budget = new Budget(); + $campaign->budget->budgetId = $budgetId; + + // Set bidding strategy (required). + $biddingStrategyConfiguration = new BiddingStrategyConfiguration(); + $biddingStrategyConfiguration->biddingStrategyType = 'MANUAL_CPC'; + + $campaign->biddingStrategyConfiguration = $biddingStrategyConfiguration; + + // All Shopping campaigns need a ShoppingSetting. + $shoppingSetting = new ShoppingSetting(); + $shoppingSetting->salesCountry = 'US'; + $shoppingSetting->campaignPriority = 0; + $shoppingSetting->merchantId = $merchantId; + // Set to "true" to enable Local Inventory Ads in your campaign. + $shoppingSetting->enableLocal = true; + $campaign->settings[] = $shoppingSetting; + + // Create operation. + $operation = new CampaignOperation(); + $operation->operand = $campaign; + $operation->operator = 'ADD'; + + // Make the mutate request. + $result = $campaignService->mutate(array($operation)); + + // Display result. + $campaign = $result->value[0]; + printf("Campaign with name '%s' and ID '%s' was added.\n", $campaign->name, + $campaign->id); + + // Create ad group. + $adGroup = new AdGroup(); + $adGroup->campaignId = $campaign->id; + $adGroup->name = 'Ad Group #' . uniqid(); + + // Create operation. + $operation = new AdGroupOperation(); + $operation->operand = $adGroup; + $operation->operator = 'ADD'; + + // Make the mutate request. + $result = $adGroupService->mutate(array($operation)); + + // Display result. + $adGroup = $result->value[0]; + printf("Ad group with name '%s' and ID '%s' was added.\n", $adGroup->name, + $adGroup->id); + + // Create product ad. + $productAd = new ProductAd(); + + // Create ad group ad. + $adGroupAd = new AdGroupAd(); + $adGroupAd->adGroupId = $adGroup->id; + $adGroupAd->ad = $productAd; + + // Create operation. + $operation = new AdGroupAdOperation(); + $operation->operand = $adGroupAd; + $operation->operator = 'ADD'; + + // Make the mutate request. + $result = $adGroupAdService->mutate(array($operation)); + + // Display result. + $adGroupAd = $result->value[0]; + printf("Product ad with ID '%s' was added.\n", $adGroupAd->ad->id); +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + addShoppingCampaignExample($user, $budgetId, $merchantId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/ShoppingCampaigns/GetProductCategoryTaxonomy.php b/examples/AdWords/v201705/ShoppingCampaigns/GetProductCategoryTaxonomy.php new file mode 100755 index 000000000..598615f4b --- /dev/null +++ b/examples/AdWords/v201705/ShoppingCampaigns/GetProductCategoryTaxonomy.php @@ -0,0 +1,107 @@ +GetService('ConstantDataService', + ADWORDS_VERSION); + + $selector = new Selector(); + $selector->predicates[] = new Predicate('Country', 'IN', array('US')); + + $results = $constantDataService->getProductBiddingCategoryData($selector); + + $biddingCategories = array(); + $rootCategories = array(); + + foreach ($results as $productBiddingCategory) { + $id = $productBiddingCategory->dimensionValue->value; + $parentId = null; + $name = $productBiddingCategory->displayValue[0]->value; + + if ($productBiddingCategory->parentDimensionValue) { + $parentId = $productBiddingCategory->parentDimensionValue->value; + } + + if (!isset($biddingCategories[$id])) { + $biddingCategories[$id] = new StdClass(); + } + + $category = $biddingCategories[$id]; + + if ($parentId) { + if (!isset($biddingCategories[$parentId])) { + $biddingCategories[$parentId] = new StdClass(); + } + $parent = $biddingCategories[$parentId]; + if (!isset($parent->children)) { + $parent->children = array(); + } + $parent->children[] = $category; + } else { + $rootCategories[] = $category; + } + + $category->id = $id; + $category->name = $name; + } + + displayCategories($rootCategories); +} + +function displayCategories($categories, $prefix = "") { + foreach ($categories as $category) { + printf("%s%s [%s]\n", $prefix, $category->name, $category->id); + if (isset($category->children)) { + displayCategories($category->children, "{$prefix}{$category->name} > "); + } + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + getProductCategoryTaxonomyExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Targeting/AddCampaignTargetingCriteria.php b/examples/AdWords/v201705/Targeting/AddCampaignTargetingCriteria.php new file mode 100755 index 000000000..74893fb11 --- /dev/null +++ b/examples/AdWords/v201705/Targeting/AddCampaignTargetingCriteria.php @@ -0,0 +1,132 @@ +GetService('CampaignCriterionService', ADWORDS_VERSION); + + $campaignCriteria = array(); + + // Create locations. The IDs can be found in the documentation or retrieved + // with the LocationCriterionService. + $california = new Location(); + $california->id = 21137; + $campaignCriteria[] = new CampaignCriterion($campaignId, null, $california); + + $mexico = new Location(); + $mexico->id = 2484; + $campaignCriteria[] = new CampaignCriterion($campaignId, null, $mexico); + + // Create languages. The IDs can be found in the documentation or retrieved + // with the ConstantDataService. + $english = new Language(); + $english->id = 1000; + $campaignCriteria[] = new CampaignCriterion($campaignId, null, $english); + + $spanish = new Language(); + $spanish->id = 1003; + $campaignCriteria[] = new CampaignCriterion($campaignId, null, $spanish); + + if ($locationFeedId !== null) { + // Distance targeting. Area of 10 miles around targets above. + $radius = new ConstantOperand(); + $radius->type = 'DOUBLE'; + $radius->unit = 'MILES'; + $radius->doubleValue = 10.0; + $distance = new LocationExtensionOperand($radius); + $locationGroup = new LocationGroups(); + $locationGroup->feedId = intval($locationFeedId); + $locationGroup->matchingFunction = new FeedFunction('IDENTITY', $distance); + $campaignCriteria[] = + new CampaignCriterion($campaignId, null, $locationGroup); + } + + // Create operations. + $operations = array(); + foreach ($campaignCriteria as $campaignCriterion) { + $operations[] = new CampaignCriterionOperation($campaignCriterion, 'ADD'); + } + + // Add a negative campaign criterion. + $negativeKeyword = new Keyword(); + $negativeKeyword->text = 'jupiter cruise'; + $negativeKeyword->matchType = 'BROAD'; + $negativeCriterion = new NegativeCampaignCriterion(); + $negativeCriterion->campaignId = $campaignId; + $negativeCriterion->criterion = $negativeKeyword; + + $operations[] = new CampaignCriterionOperation($negativeCriterion, 'ADD'); + + // Make the mutate request. + $result = $campaignCriterionService->mutate($operations); + + // Display results. + foreach ($result->value as $campaignCriterion) { + printf("Campaign targeting criterion with ID '%s' and type '%s' was " + . "added.\n", $campaignCriterion->criterion->id, + $campaignCriterion->criterion->CriterionType); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + AddCampaignTargetingCriteriaExample($user, $campaignId, $locationFeedId); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201607/Express/GetProductServices.php b/examples/AdWords/v201705/Targeting/GetCampaignTargetingCriteria.php similarity index 62% rename from examples/AdWords/v201607/Express/GetProductServices.php rename to examples/AdWords/v201705/Targeting/GetCampaignTargetingCriteria.php index 8ef3b6cad..a69f6c001 100755 --- a/examples/AdWords/v201607/Express/GetProductServices.php +++ b/examples/AdWords/v201705/Targeting/GetCampaignTargetingCriteria.php @@ -1,7 +1,7 @@ GetService('ProductServiceService', - ADWORDS_VERSION); + $campaignCriterionService = + $user->GetService('CampaignCriterionService', ADWORDS_VERSION); // Create selector. $selector = new Selector(); - $selector->fields = array('ProductServiceText'); + $selector->fields = array('Id', 'CriteriaType'); // Create predicates. - $selector->predicates[] = new Predicate('ProductServiceText', 'EQUALS', - array($productServiceSuggestion)); - $selector->predicates[] = new Predicate('Locale', 'EQUALS', - array($localeText)); + $selector->predicates[] = + new Predicate('CampaignId', 'IN', array($campaignId)); + $selector->predicates[] = new Predicate('CriteriaType', 'IN', + array('LANGUAGE', 'LOCATION', 'AGE_RANGE', 'CARRIER', + 'OPERATING_SYSTEM_VERSION', 'GENDER', 'PROXIMITY', 'PLATFORM')); // Create paging controls. $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE); do { // Make the get request. - $page = $productServiceService->get($selector); + $page = $campaignCriterionService->get($selector); // Display results. if (isset($page->entries)) { - foreach ($page->entries as $productService) { - printf("Product/service with text '%s' found\n", $productService->text); + foreach ($page->entries as $campaignCriterion) { + printf("Campaign targeting criterion with ID '%s' and type '%s' was " + . "found.\n", $campaignCriterion->criterion->id, + $campaignCriterion->criterion->CriterionType); } } else { - print "No products/services were found.\n"; + print "No campaign targeting criteria were found.\n"; } // Advance the paging index. @@ -87,7 +90,7 @@ function GetProductServicesExample(AdWordsUser $user, $productServiceSuggestion, $user->LogAll(); // Run the example. - GetProductServicesExample($user, $productServiceSuggestion, $localeText); + GetCampaignTargetingCriteriaExample($user, $campaignId); } catch (Exception $e) { printf("An error has occurred: %s\n", $e->getMessage()); } diff --git a/examples/AdWords/v201705/Targeting/GetTargetableLanguagesAndCarriers.php b/examples/AdWords/v201705/Targeting/GetTargetableLanguagesAndCarriers.php new file mode 100755 index 000000000..65dc825e0 --- /dev/null +++ b/examples/AdWords/v201705/Targeting/GetTargetableLanguagesAndCarriers.php @@ -0,0 +1,74 @@ +GetService('ConstantDataService', ADWORDS_VERSION); + + // Make the getLanguageCriterion request. + $languages = $constantDataService->getLanguageCriterion(); + + foreach ($languages as $language) { + printf("Language with name '%s' and ID '%s' was found.\n", + $language->name, $language->id); + } + + print "\n"; + + // Make the getCarrierCriterion request. + $carriers = $constantDataService->getCarrierCriterion(); + + foreach ($carriers as $carrier) { + printf("Carrier with name '%s', country code '%s', and ID '%s' was " + . "found.\n", $carrier->name, $carrier->countryCode, $carrier->id); + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + GetTargetableLanguagesAndCarriersExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/Targeting/LookupLocation.php b/examples/AdWords/v201705/Targeting/LookupLocation.php new file mode 100755 index 000000000..915781444 --- /dev/null +++ b/examples/AdWords/v201705/Targeting/LookupLocation.php @@ -0,0 +1,106 @@ +locationName, $location->displayType); +} + +/** + * Runs the example. + * @param AdWordsUser $user the user to run the example with + */ +function LookupLocationExample(AdWordsUser $user) { + // Get the service, which loads the required classes. + $locationCriterionService = + $user->GetService('LocationCriterionService', ADWORDS_VERSION); + + // Location names to look up. + $locationNames = array('Paris', 'Quebec', 'Spain', 'Deutschland'); + // Locale to retrieve location names in. + $locale = 'en'; + + $selector = new Selector(); + $selector->fields = array('Id', 'LocationName', 'CanonicalName', + 'DisplayType', 'ParentLocations', 'Reach', 'TargetingStatus'); + // Location names must match exactly, only EQUALS and IN are supported. + $selector->predicates[] = new Predicate('LocationName', 'IN', $locationNames); + // Only one locale can be used in a request. + $selector->predicates[] = new Predicate('Locale', 'EQUALS', $locale); + + // Make the get request. + $locationCriteria = $locationCriterionService->get($selector); + + // Display results. + if (isset($locationCriteria)) { + foreach ($locationCriteria as $locationCriterion) { + if (isset($locationCriterion->location->parentLocations)) { + $parentLocations = implode(', ', + array_map('GetLocationString', + $locationCriterion->location->parentLocations)); + } else { + $parentLocations = 'N/A'; + } + printf("The search term '%s' returned the location '%s' of type '%s' " + . "with ID '%s', parent locations '%s', and reach '%d' (%s).\n", + $locationCriterion->searchTerm, + $locationCriterion->location->locationName, + $locationCriterion->location->displayType, + $locationCriterion->location->id, + $parentLocations, + $locationCriterion->reach, + $locationCriterion->location->targetingStatus); + } + } else { + print "No location criteria were found.\n"; + } +} + +// Don't run the example if the file is being included. +if (__FILE__ != realpath($_SERVER['PHP_SELF'])) { + return; +} + +try { + // Get AdWordsUser from credentials in "../auth.ini" + // relative to the AdWordsUser.php file's directory. + $user = new AdWordsUser(); + + // Log every SOAP XML request and response. + $user->LogAll(); + + // Run the example. + LookupLocationExample($user); +} catch (Exception $e) { + printf("An error has occurred: %s\n", $e->getMessage()); +} diff --git a/examples/AdWords/v201705/init.php b/examples/AdWords/v201705/init.php new file mode 100755 index 000000000..12267f0d8 --- /dev/null +++ b/examples/AdWords/v201705/init.php @@ -0,0 +1,47 @@ +serviceName === 'PromotionService') { - $soapHeaderClassName = 'ExpressSoapHeader'; - } $headerObject = $this->Create($soapHeaderClassName); foreach (get_object_vars($headerObject) as $var => $value) { $headerObject->$var = $this->GetHeaderValue($var); diff --git a/src/Google/Api/Ads/AdWords/Lib/AdWordsUser.php b/src/Google/Api/Ads/AdWords/Lib/AdWordsUser.php index 7acd5dba1..f97897c73 100755 --- a/src/Google/Api/Ads/AdWords/Lib/AdWordsUser.php +++ b/src/Google/Api/Ads/AdWords/Lib/AdWordsUser.php @@ -277,40 +277,6 @@ public function SetClientCustomerId($clientCustomerId) { $this->SetHeaderValue('clientCustomerId', $clientCustomerId); } - /** - * Gets the AdWords Express business ID required for AdWords Express - * PromotionService - */ - public function GetExpressBusinessId() { - return $this->GetHeaderValue('expressBusinessId'); - } - - /** - * Sets the AdWords Express business ID required for AdWords Express - * PromotionService - * @param string AdWords Express business ID - */ - public function SetExpressBusinessId($businessId) { - $this->SetHeaderValue('expressBusinessId', $businessId); - } - - /** - * Gets the Google My Business page ID used by AdWords Express - * PromotionService - */ - public function GetExpressPlusPageId() { - return $this->GetHeaderValue('pageId'); - } - - /** - * Sets the Google My Business page ID used by AdWords Express - * PromotionService - * @param string Google My Business page ID - */ - public function SetExpressPlusPageId($pageId) { - $this->SetHeaderValue('pageId', $pageId); - } - /** * Gets the raw user agent for this user. * @return string The raw user agent. diff --git a/src/Google/Api/Ads/AdWords/Lib/api.properties b/src/Google/Api/Ads/AdWords/Lib/api.properties index a4681b06b..90e5beaf8 100755 --- a/src/Google/Api/Ads/AdWords/Lib/api.properties +++ b/src/Google/Api/Ads/AdWords/Lib/api.properties @@ -18,12 +18,12 @@ wsdl2php.skipClassNameCheckTypes=Target,Location ;; API api.server=https://adwords.google.com -api.versions=v201607,v201609,v201702 +api.versions=v201607,v201609,v201702,v201705 api.soapClientClassNamespace=Google_Api_Ads_AdWords_Lib ;; v201607 api.versions.v201607.namespace=Google_Api_Ads_AdWords_v201607 -api.versions.v201607.services=AdGroupAdService,AdGroupBidModifierService,AdGroupCriterionService,AdGroupFeedService,AdGroupService,AdParamService,AdwordsUserListService,BatchJobService,BiddingStrategyService,BudgetOrderService,BudgetService,CampaignCriterionService,CampaignFeedService,CampaignService,ConstantDataService,ConversionTrackerService,CustomerFeedService,CustomerService,CustomerSyncService,DataService,ExperimentService,FeedItemService,FeedMappingService,FeedService,LabelService,LocationCriterionService,ManagedCustomerService,MediaService,OfflineConversionFeedService,ReportDefinitionService,TargetingIdeaService,TrafficEstimatorService,ExpressBusinessService,ProductServiceService,BudgetSuggestionService,PromotionService,CampaignSharedSetService,SharedCriterionService,SharedSetService,CampaignExtensionSettingService,AdGroupExtensionSettingService,CustomerExtensionSettingService,AdCustomizerFeedService,AccountLabelService,DraftService,DraftAsyncErrorService,TrialService,TrialAsyncErrorService +api.versions.v201607.services=AdGroupAdService,AdGroupBidModifierService,AdGroupCriterionService,AdGroupFeedService,AdGroupService,AdParamService,AdwordsUserListService,BatchJobService,BiddingStrategyService,BudgetOrderService,BudgetService,CampaignCriterionService,CampaignFeedService,CampaignService,ConstantDataService,ConversionTrackerService,CustomerFeedService,CustomerService,CustomerSyncService,DataService,ExperimentService,FeedItemService,FeedMappingService,FeedService,LabelService,LocationCriterionService,ManagedCustomerService,MediaService,OfflineConversionFeedService,ReportDefinitionService,TargetingIdeaService,TrafficEstimatorService,CampaignSharedSetService,SharedCriterionService,SharedSetService,CampaignExtensionSettingService,AdGroupExtensionSettingService,CustomerExtensionSettingService,AdCustomizerFeedService,AccountLabelService,DraftService,DraftAsyncErrorService,TrialService,TrialAsyncErrorService api.versions.v201607.services.AdGroupAdService.wsdl=${api.server}/api/adwords/cm/v201607/AdGroupAdService?wsdl api.versions.v201607.services.AdGroupBidModifierService.wsdl=${api.server}/api/adwords/cm/v201607/AdGroupBidModifierService?wsdl api.versions.v201607.services.AdGroupCriterionService.wsdl=${api.server}/api/adwords/cm/v201607/AdGroupCriterionService?wsdl @@ -35,7 +35,6 @@ api.versions.v201607.services.BatchJobService.wsdl=${api.server}/api/adwords/cm/ api.versions.v201607.services.BiddingStrategyService.wsdl=${api.server}/api/adwords/cm/v201607/BiddingStrategyService?wsdl api.versions.v201607.services.BudgetOrderService.wsdl=${api.server}/api/adwords/billing/v201607/BudgetOrderService?wsdl api.versions.v201607.services.BudgetService.wsdl=${api.server}/api/adwords/cm/v201607/BudgetService?wsdl -api.versions.v201607.services.BudgetSuggestionService.wsdl=${api.server}/api/adwords/express/v201607/BudgetSuggestionService?wsdl api.versions.v201607.services.CampaignCriterionService.wsdl=${api.server}/api/adwords/cm/v201607/CampaignCriterionService?wsdl api.versions.v201607.services.CampaignFeedService.wsdl=${api.server}/api/adwords/cm/v201607/CampaignFeedService?wsdl api.versions.v201607.services.CampaignService.wsdl=${api.server}/api/adwords/cm/v201607/CampaignService?wsdl @@ -47,7 +46,6 @@ api.versions.v201607.services.CustomerService.wsdl=${api.server}/api/adwords/mcm api.versions.v201607.services.CustomerSyncService.wsdl=${api.server}/api/adwords/ch/v201607/CustomerSyncService?wsdl api.versions.v201607.services.DataService.wsdl=${api.server}/api/adwords/cm/v201607/DataService?wsdl api.versions.v201607.services.ExperimentService.wsdl=${api.server}/api/adwords/cm/v201607/ExperimentService?wsdl -api.versions.v201607.services.ExpressBusinessService.wsdl=${api.server}/api/adwords/express/v201607/ExpressBusinessService?wsdl api.versions.v201607.services.FeedItemService.wsdl=${api.server}/api/adwords/cm/v201607/FeedItemService?wsdl api.versions.v201607.services.FeedMappingService.wsdl=${api.server}/api/adwords/cm/v201607/FeedMappingService?wsdl api.versions.v201607.services.FeedService.wsdl=${api.server}/api/adwords/cm/v201607/FeedService?wsdl @@ -56,8 +54,6 @@ api.versions.v201607.services.LocationCriterionService.wsdl=${api.server}/api/ad api.versions.v201607.services.ManagedCustomerService.wsdl=${api.server}/api/adwords/mcm/v201607/ManagedCustomerService?wsdl api.versions.v201607.services.MediaService.wsdl=${api.server}/api/adwords/cm/v201607/MediaService?wsdl api.versions.v201607.services.OfflineConversionFeedService.wsdl=${api.server}/api/adwords/cm/v201607/OfflineConversionFeedService?wsdl -api.versions.v201607.services.ProductServiceService.wsdl=${api.server}/api/adwords/express/v201607/ProductServiceService?wsdl -api.versions.v201607.services.PromotionService.wsdl=${api.server}/api/adwords/express/v201607/PromotionService?wsdl api.versions.v201607.services.ReportDefinitionService.wsdl=${api.server}/api/adwords/cm/v201607/ReportDefinitionService?wsdl api.versions.v201607.services.SharedCriterionService.wsdl=${api.server}/api/adwords/cm/v201607/SharedCriterionService?wsdl api.versions.v201607.services.SharedSetService.wsdl=${api.server}/api/adwords/cm/v201607/SharedSetService?wsdl @@ -75,7 +71,7 @@ api.versions.v201607.services.TrialAsyncErrorService.wsdl=${api.server}/api/adwo ;; v201609 api.versions.v201609.namespace=Google_Api_Ads_AdWords_v201609 -api.versions.v201609.services=AdGroupAdService,AdGroupBidModifierService,AdGroupCriterionService,AdGroupFeedService,AdGroupService,AdParamService,AdwordsUserListService,BatchJobService,BiddingStrategyService,BudgetOrderService,BudgetService,CampaignCriterionService,CampaignFeedService,CampaignService,ConstantDataService,ConversionTrackerService,CustomerFeedService,CustomerService,CustomerSyncService,DataService,FeedItemService,FeedMappingService,FeedService,LabelService,LocationCriterionService,ManagedCustomerService,MediaService,OfflineConversionFeedService,ReportDefinitionService,TargetingIdeaService,TrafficEstimatorService,ExpressBusinessService,ProductServiceService,BudgetSuggestionService,PromotionService,CampaignSharedSetService,SharedCriterionService,SharedSetService,CampaignExtensionSettingService,AdGroupExtensionSettingService,CustomerExtensionSettingService,AdCustomizerFeedService,AccountLabelService,DraftService,DraftAsyncErrorService,TrialService,TrialAsyncErrorService,OfflineCallConversionFeedService +api.versions.v201609.services=AdGroupAdService,AdGroupBidModifierService,AdGroupCriterionService,AdGroupFeedService,AdGroupService,AdParamService,AdwordsUserListService,BatchJobService,BiddingStrategyService,BudgetOrderService,BudgetService,CampaignCriterionService,CampaignFeedService,CampaignService,ConstantDataService,ConversionTrackerService,CustomerFeedService,CustomerService,CustomerSyncService,DataService,FeedItemService,FeedMappingService,FeedService,LabelService,LocationCriterionService,ManagedCustomerService,MediaService,OfflineConversionFeedService,ReportDefinitionService,TargetingIdeaService,TrafficEstimatorService,CampaignSharedSetService,SharedCriterionService,SharedSetService,CampaignExtensionSettingService,AdGroupExtensionSettingService,CustomerExtensionSettingService,AdCustomizerFeedService,AccountLabelService,DraftService,DraftAsyncErrorService,TrialService,TrialAsyncErrorService,OfflineCallConversionFeedService api.versions.v201609.services.AdGroupAdService.wsdl=${api.server}/api/adwords/cm/v201609/AdGroupAdService?wsdl api.versions.v201609.services.AdGroupBidModifierService.wsdl=${api.server}/api/adwords/cm/v201609/AdGroupBidModifierService?wsdl api.versions.v201609.services.AdGroupCriterionService.wsdl=${api.server}/api/adwords/cm/v201609/AdGroupCriterionService?wsdl @@ -87,7 +83,6 @@ api.versions.v201609.services.BatchJobService.wsdl=${api.server}/api/adwords/cm/ api.versions.v201609.services.BiddingStrategyService.wsdl=${api.server}/api/adwords/cm/v201609/BiddingStrategyService?wsdl api.versions.v201609.services.BudgetOrderService.wsdl=${api.server}/api/adwords/billing/v201609/BudgetOrderService?wsdl api.versions.v201609.services.BudgetService.wsdl=${api.server}/api/adwords/cm/v201609/BudgetService?wsdl -api.versions.v201609.services.BudgetSuggestionService.wsdl=${api.server}/api/adwords/express/v201609/BudgetSuggestionService?wsdl api.versions.v201609.services.CampaignCriterionService.wsdl=${api.server}/api/adwords/cm/v201609/CampaignCriterionService?wsdl api.versions.v201609.services.CampaignFeedService.wsdl=${api.server}/api/adwords/cm/v201609/CampaignFeedService?wsdl api.versions.v201609.services.CampaignService.wsdl=${api.server}/api/adwords/cm/v201609/CampaignService?wsdl @@ -98,7 +93,6 @@ api.versions.v201609.services.CustomerFeedService.wsdl=${api.server}/api/adwords api.versions.v201609.services.CustomerService.wsdl=${api.server}/api/adwords/mcm/v201609/CustomerService?wsdl api.versions.v201609.services.CustomerSyncService.wsdl=${api.server}/api/adwords/ch/v201609/CustomerSyncService?wsdl api.versions.v201609.services.DataService.wsdl=${api.server}/api/adwords/cm/v201609/DataService?wsdl -api.versions.v201609.services.ExpressBusinessService.wsdl=${api.server}/api/adwords/express/v201609/ExpressBusinessService?wsdl api.versions.v201609.services.FeedItemService.wsdl=${api.server}/api/adwords/cm/v201609/FeedItemService?wsdl api.versions.v201609.services.FeedMappingService.wsdl=${api.server}/api/adwords/cm/v201609/FeedMappingService?wsdl api.versions.v201609.services.FeedService.wsdl=${api.server}/api/adwords/cm/v201609/FeedService?wsdl @@ -108,8 +102,6 @@ api.versions.v201609.services.ManagedCustomerService.wsdl=${api.server}/api/adwo api.versions.v201609.services.MediaService.wsdl=${api.server}/api/adwords/cm/v201609/MediaService?wsdl api.versions.v201609.services.OfflineCallConversionFeedService.wsdl=${api.server}/api/adwords/cm/v201609/OfflineCallConversionFeedService?wsdl api.versions.v201609.services.OfflineConversionFeedService.wsdl=${api.server}/api/adwords/cm/v201609/OfflineConversionFeedService?wsdl -api.versions.v201609.services.ProductServiceService.wsdl=${api.server}/api/adwords/express/v201609/ProductServiceService?wsdl -api.versions.v201609.services.PromotionService.wsdl=${api.server}/api/adwords/express/v201609/PromotionService?wsdl api.versions.v201609.services.ReportDefinitionService.wsdl=${api.server}/api/adwords/cm/v201609/ReportDefinitionService?wsdl api.versions.v201609.services.SharedCriterionService.wsdl=${api.server}/api/adwords/cm/v201609/SharedCriterionService?wsdl api.versions.v201609.services.SharedSetService.wsdl=${api.server}/api/adwords/cm/v201609/SharedSetService?wsdl @@ -172,3 +164,54 @@ api.versions.v201702.services.DraftService.wsdl=${api.server}/api/adwords/cm/v20 api.versions.v201702.services.DraftAsyncErrorService.wsdl=${api.server}/api/adwords/cm/v201702/DraftAsyncErrorService?wsdl api.versions.v201702.services.TrialService.wsdl=${api.server}/api/adwords/cm/v201702/TrialService?wsdl api.versions.v201702.services.TrialAsyncErrorService.wsdl=${api.server}/api/adwords/cm/v201702/TrialAsyncErrorService?wsdl + +;; v201705 +api.versions.v201705.namespace=Google_Api_Ads_AdWords_v201705 +api.versions.v201705.services=AdGroupAdService,AdGroupBidModifierService,AdGroupCriterionService,AdGroupFeedService,AdGroupService,AdParamService,AdwordsUserListService,BatchJobService,BiddingStrategyService,BudgetOrderService,BudgetService,CampaignBidModifierService,CampaignCriterionService,CampaignFeedService,CampaignGroupService,CampaignGroupPerformanceTargetService,CampaignService,ConstantDataService,ConversionTrackerService,CustomerFeedService,CustomerService,CustomerSyncService,DataService,FeedItemService,FeedMappingService,FeedService,LabelService,LocationCriterionService,ManagedCustomerService,MediaService,OfflineConversionFeedService,ReportDefinitionService,TargetingIdeaService,TrafficEstimatorService,CampaignSharedSetService,SharedCriterionService,SharedSetService,CampaignExtensionSettingService,AdGroupExtensionSettingService,CustomerExtensionSettingService,AdCustomizerFeedService,AccountLabelService,DraftService,DraftAsyncErrorService,TrialService,TrialAsyncErrorService,OfflineCallConversionFeedService +api.versions.v201705.services.AdGroupAdService.wsdl=${api.server}/api/adwords/cm/v201705/AdGroupAdService?wsdl +api.versions.v201705.services.AdGroupBidModifierService.wsdl=${api.server}/api/adwords/cm/v201705/AdGroupBidModifierService?wsdl +api.versions.v201705.services.AdGroupCriterionService.wsdl=${api.server}/api/adwords/cm/v201705/AdGroupCriterionService?wsdl +api.versions.v201705.services.AdGroupFeedService.wsdl=${api.server}/api/adwords/cm/v201705/AdGroupFeedService?wsdl +api.versions.v201705.services.AdGroupService.wsdl=${api.server}/api/adwords/cm/v201705/AdGroupService?wsdl +api.versions.v201705.services.AdParamService.wsdl=${api.server}/api/adwords/cm/v201705/AdParamService?wsdl +api.versions.v201705.services.AdwordsUserListService.wsdl=${api.server}/api/adwords/rm/v201705/AdwordsUserListService?wsdl +api.versions.v201705.services.BatchJobService.wsdl=${api.server}/api/adwords/cm/v201705/BatchJobService?wsdl +api.versions.v201705.services.BiddingStrategyService.wsdl=${api.server}/api/adwords/cm/v201705/BiddingStrategyService?wsdl +api.versions.v201705.services.BudgetOrderService.wsdl=${api.server}/api/adwords/billing/v201705/BudgetOrderService?wsdl +api.versions.v201705.services.BudgetService.wsdl=${api.server}/api/adwords/cm/v201705/BudgetService?wsdl +api.versions.v201705.services.CampaignBidModifierService.wsdl=${api.server}/api/adwords/cm/v201705/CampaignBidModifierService?wsdl +api.versions.v201705.services.CampaignCriterionService.wsdl=${api.server}/api/adwords/cm/v201705/CampaignCriterionService?wsdl +api.versions.v201705.services.CampaignFeedService.wsdl=${api.server}/api/adwords/cm/v201705/CampaignFeedService?wsdl +api.versions.v201705.services.CampaignGroupService.wsdl=${api.server}/api/adwords/cm/v201705/CampaignGroupService?wsdl +api.versions.v201705.services.CampaignGroupPerformanceTargetService.wsdl=${api.server}/api/adwords/cm/v201705/CampaignGroupPerformanceTargetService?wsdl +api.versions.v201705.services.CampaignService.wsdl=${api.server}/api/adwords/cm/v201705/CampaignService?wsdl +api.versions.v201705.services.CampaignSharedSetService.wsdl=${api.server}/api/adwords/cm/v201705/CampaignSharedSetService?wsdl +api.versions.v201705.services.ConstantDataService.wsdl=${api.server}/api/adwords/cm/v201705/ConstantDataService?wsdl +api.versions.v201705.services.ConversionTrackerService.wsdl=${api.server}/api/adwords/cm/v201705/ConversionTrackerService?wsdl +api.versions.v201705.services.CustomerFeedService.wsdl=${api.server}/api/adwords/cm/v201705/CustomerFeedService?wsdl +api.versions.v201705.services.CustomerService.wsdl=${api.server}/api/adwords/mcm/v201705/CustomerService?wsdl +api.versions.v201705.services.CustomerSyncService.wsdl=${api.server}/api/adwords/ch/v201705/CustomerSyncService?wsdl +api.versions.v201705.services.DataService.wsdl=${api.server}/api/adwords/cm/v201705/DataService?wsdl +api.versions.v201705.services.FeedItemService.wsdl=${api.server}/api/adwords/cm/v201705/FeedItemService?wsdl +api.versions.v201705.services.FeedMappingService.wsdl=${api.server}/api/adwords/cm/v201705/FeedMappingService?wsdl +api.versions.v201705.services.FeedService.wsdl=${api.server}/api/adwords/cm/v201705/FeedService?wsdl +api.versions.v201705.services.LabelService.wsdl=${api.server}/api/adwords/cm/v201705/LabelService?wsdl +api.versions.v201705.services.LocationCriterionService.wsdl=${api.server}/api/adwords/cm/v201705/LocationCriterionService?wsdl +api.versions.v201705.services.ManagedCustomerService.wsdl=${api.server}/api/adwords/mcm/v201705/ManagedCustomerService?wsdl +api.versions.v201705.services.MediaService.wsdl=${api.server}/api/adwords/cm/v201705/MediaService?wsdl +api.versions.v201705.services.OfflineCallConversionFeedService.wsdl=${api.server}/api/adwords/cm/v201705/OfflineCallConversionFeedService?wsdl +api.versions.v201705.services.OfflineConversionFeedService.wsdl=${api.server}/api/adwords/cm/v201705/OfflineConversionFeedService?wsdl +api.versions.v201705.services.ReportDefinitionService.wsdl=${api.server}/api/adwords/cm/v201705/ReportDefinitionService?wsdl +api.versions.v201705.services.SharedCriterionService.wsdl=${api.server}/api/adwords/cm/v201705/SharedCriterionService?wsdl +api.versions.v201705.services.SharedSetService.wsdl=${api.server}/api/adwords/cm/v201705/SharedSetService?wsdl +api.versions.v201705.services.TargetingIdeaService.wsdl=${api.server}/api/adwords/o/v201705/TargetingIdeaService?wsdl +api.versions.v201705.services.TrafficEstimatorService.wsdl=${api.server}/api/adwords/o/v201705/TrafficEstimatorService?wsdl +api.versions.v201705.services.CampaignExtensionSettingService.wsdl=${api.server}/api/adwords/cm/v201705/CampaignExtensionSettingService?wsdl +api.versions.v201705.services.AdGroupExtensionSettingService.wsdl=${api.server}/api/adwords/cm/v201705/AdGroupExtensionSettingService?wsdl +api.versions.v201705.services.CustomerExtensionSettingService.wsdl=${api.server}/api/adwords/cm/v201705/CustomerExtensionSettingService?wsdl +api.versions.v201705.services.AdCustomizerFeedService.wsdl=${api.server}/api/adwords/cm/v201705/AdCustomizerFeedService?wsdl +api.versions.v201705.services.AccountLabelService.wsdl=${api.server}/api/adwords/mcm/v201705/AccountLabelService?wsdl +api.versions.v201705.services.DraftService.wsdl=${api.server}/api/adwords/cm/v201705/DraftService?wsdl +api.versions.v201705.services.DraftAsyncErrorService.wsdl=${api.server}/api/adwords/cm/v201705/DraftAsyncErrorService?wsdl +api.versions.v201705.services.TrialService.wsdl=${api.server}/api/adwords/cm/v201705/TrialService?wsdl +api.versions.v201705.services.TrialAsyncErrorService.wsdl=${api.server}/api/adwords/cm/v201705/TrialAsyncErrorService?wsdl diff --git a/src/Google/Api/Ads/AdWords/Util/v201705/BatchJobClasses.php b/src/Google/Api/Ads/AdWords/Util/v201705/BatchJobClasses.php new file mode 100755 index 000000000..bd479f09b --- /dev/null +++ b/src/Google/Api/Ads/AdWords/Util/v201705/BatchJobClasses.php @@ -0,0 +1,337 @@ +errors = $errors; + } + } +} + +if (!class_exists("MutateResult", false)) { + class MutateResult { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "MutateResult"; + + /** + * @var tnsOperand + */ + public $result; + + /** + * @var tnsErrorList + */ + public $errorList; + + /** + * @var integer + */ + public $index; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct($result = null, $errorList = null, + $index = null) { + $this->result = $result; + $this->errorList = $errorList; + $this->index = $index; + } + } +} + +if (!class_exists("Operand", false)) { + class Operand { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "Operand"; + + /** + * @var AdGroupAdLabel + */ + public $AdGroupAdLabel; + + /** + * @var AdGroupAd + */ + public $AdGroupAd; + + /** + * @var AdGroupBidModifier + */ + public $AdGroupBidModifier; + + /** + * @var AdGroupCriterionLabel + */ + public $AdGroupCriterionLabel; + + /** + * @var AdGroupExtensionSetting + */ + public $AdGroupExtensionSetting; + + /** + * @var AdGroupCriterion + */ + public $AdGroupCriterion; + + /** + * @var AdGroupLabel + */ + public $AdGroupLabel; + + /** + * @var AdGroup + */ + public $AdGroup; + + /** + * @var Ad + */ + public $Ad; + + /** + * @var Budget + */ + public $Budget; + + /** + * @var CampaignCriterion + */ + public $CampaignCriterion; + + /** + * @var CampaignExtensionSetting + */ + public $CampaignExtensionSetting; + + /** + * @var CampaignLabel + */ + public $CampaignLabel; + + /** + * @var Campaign + */ + public $Campaign; + + /** + * @var CustomerExtensionSetting + */ + public $CustomerExtensionSetting; + + /** + * @var FeedItem + */ + public $FeedItem; + + /** + * @var Label + */ + public $Label; + + /** + * @var Media + */ + public $Media; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct( + $AdGroupAdLabel = null, + $AdGroupAd = null, + $AdGroupBidModifier = null, + $AdGroupCriterionLabel = null, + $AdGroupCriterion = null, + $AdGroupExtensionSetting = null, + $AdGroupLabel = null, + $AdGroup = null, + $Ad = null, + $Budget = null, + $CampaignCriterion = null, + $CampaignExtensionSetting = null, + $CampaignLabel = null, + $Campaign = null, + $CustomerExtensionSetting = null, + $FeedItem = null, + $Label = null, + $Media = null + ) { + $this->AdGroupAdLabel = $AdGroupAdLabel; + $this->AdGroupAd = $AdGroupAd; + $this->AdGroupBidModifier = $AdGroupBidModifier; + $this->AdGroupCriterionLabel = $AdGroupCriterionLabel; + $this->AdGroupCriterion = $AdGroupCriterion; + $this->AdGroupExtensionSetting = $AdGroupExtensionSetting; + $this->AdGroupLabel = $AdGroupLabel; + $this->AdGroup = $AdGroup; + $this->Ad = $Ad; + $this->Budget = $Budget; + $this->CampaignCriterion = $CampaignCriterion; + $this->CampaignExtensionSetting = $CampaignExtensionSetting; + $this->CampaignLabel = $CampaignLabel; + $this->Campaign = $Campaign; + $this->CustomerExtensionSetting = $CustomerExtensionSetting; + $this->FeedItem = $FeedItem; + $this->Label = $Label; + $this->Media = $Media; + } + } +} + +if (!class_exists("BatchJobOpsMutate", false)) { + class BatchJobOpsMutate { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = ""; + + /** + * @var Operation[] + */ + public $operations; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct($operations = null) { + $this->operations = $operations; + } + } +} + +if (!class_exists("BatchJobOpsMutateResponse", false)) { + class BatchJobOpsMutateResponse { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = ""; + + /** + * @var MutateResult[] + */ + public $rval; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct($rval = null) { + $this->rval = $rval; + } + } +} diff --git a/src/Google/Api/Ads/AdWords/Util/v201705/BatchJobUtils.php b/src/Google/Api/Ads/AdWords/Util/v201705/BatchJobUtils.php new file mode 100755 index 000000000..d1bef2b4d --- /dev/null +++ b/src/Google/Api/Ads/AdWords/Util/v201705/BatchJobUtils.php @@ -0,0 +1,117 @@ + "ErrorList", + "MutateResult" => "MutateResult", + "Operand" => "Operand", + "mutate" => "BatchJobOpsMutate", + "mutateResponse" => "BatchJobOpsMutateResponse", + ); + + private $batchJobUtilsDelegate; + private $adsUtilityRegistry; + + /** + * Create an instance of BatchJobUtils with the specified upload URL, total + * uploaded content bytes, CurlUtils object, BatchJobUtilsDelegate object and + * ads utility registry. + * + * @param string $uploadUrl the upload URL to which the operations will be + * uploaded + * @param int|null $totalContentBytes the total content bytes uploaded so far, + * used in incremental batch job uploads + * @param CurlUtils|null $curlUtils the CurlUtils object for uploading batch + * job operations and downloading the results of batch job processing + * @param BatchJobUtilsDelegate|null $batchJobUtilsDelegate the batch job + * utils delegate that uploads batch job operations and downloads the + * results + * @param AdsUtilityRegistry|null $adsUtilityRegistry the ads utility registry + */ + public function __construct( + $uploadUrl, + $totalContentBytes = null, + CurlUtils $curlUtils = null, + BatchJobUtilsDelegate $batchJobUtilsDelegate = null, + AdsUtilityRegistry $adsUtilityRegistry = null + ) { + $this->batchJobUtilsDelegate = ($batchJobUtilsDelegate === null) + ? new BatchJobUtilsDelegate($uploadUrl, $totalContentBytes, $curlUtils) + : $batchJobUtilsDelegate; + $this->adsUtilityRegistry = ($adsUtilityRegistry === null) + ? AdsUtilityRegistry::getInstance() + : $adsUtilityRegistry; + } + + /** + * Uploads batch job operations to the specified upload URL. + * + * @param array $operations operations to be uploaded via the upload URL + * $uploadUrl + * @throws BatchJobException if the HTTP code is not equal to 200 + */ + public function UploadBatchJobOperations(array $operations) { + // Starting with v201601, all uploads must go through the incremental + // upload workflow. + $this->UploadIncrementalBatchJobOperations($operations, true); + } + + /** + * Uploads batch job operations incrementally to the specified upload URL. + * + * @param array $operations operations to be uploaded via the upload URL + * @param bool $isLastRequest if this is last upload request + * @throws BatchJobException if the HTTP code is not equal to 200 and 308 + */ + public function UploadIncrementalBatchJobOperations(array $operations, + $isLastRequest = null) { + $this->adsUtilityRegistry->addUtility(AdsUtility::BATCHJOB_UTILS); + $this->batchJobUtilsDelegate + ->UploadIncrementalBatchJobOperations($operations, $isLastRequest); + } + + /** + * Downloads the results of batch processing from $downloadUrl. + * + * @param string $downloadUrl the download URL from which the results are + * downloaded + * @throws BatchJobException if the HTTP code is not equal to 200 + * @return string the contents of returned HTTP response + */ + public function DownloadBatchJobResults($downloadUrl) { + $this->adsUtilityRegistry->addUtility(AdsUtility::BATCHJOB_UTILS); + return $this->batchJobUtilsDelegate->DownloadBatchJobResults($downloadUrl); + } +} diff --git a/src/Google/Api/Ads/AdWords/Util/v201705/BatchJobUtilsDelegate.php b/src/Google/Api/Ads/AdWords/Util/v201705/BatchJobUtilsDelegate.php new file mode 100755 index 000000000..3a6e63371 --- /dev/null +++ b/src/Google/Api/Ads/AdWords/Util/v201705/BatchJobUtilsDelegate.php @@ -0,0 +1,300 @@ +totalContentBytes = + ($totalContentBytes === null) ? 0 : $totalContentBytes; + $this->curlUtils = ($curlUtils === null) ? new CurlUtils() : $curlUtils; + $this->curlSession = null; + // If this is the first upload, then issue a request to get the resumable + // session URI from Google Cloud Storage. + $this->uploadUrl = ($this->totalContentBytes === 0) ? + $this->InitiateResumableUpload($uploadUrl) : + $this->uploadUrl; + } + + /** + * @see BatchJobUtils::UploadIncrementalBatchJobOperations + */ + public function UploadIncrementalBatchJobOperations(array $operations, + $isLastRequest = null) { + if ($isLastRequest === null) { + $isLastRequest = false; + } + + $uploadRequest = + $this->PrepareIncrementalUploadRequest($operations, $isLastRequest); + + $flatHeaders = array(); + foreach ($uploadRequest['headers'] as $name => $value) { + $flatHeaders[] = sprintf('%s: %s', $name, $value); + } + + // If this is the first upload request for this instance, initialize the + // curl session object. + if ($this->curlSession === null) { + $this->curlSession = $this->curlUtils->CreateSession($this->uploadUrl); + $this->curlUtils->SetOpt($this->curlSession, CURLOPT_CUSTOMREQUEST, + 'PUT'); + $this->curlUtils->SetOpt($this->curlSession, CURLINFO_HEADER_OUT, true); + } + $this->curlUtils->SetOpt($this->curlSession, CURLOPT_HTTPHEADER, + $flatHeaders); + $this->curlUtils->SetOpt($this->curlSession, CURLOPT_POSTFIELDS, + $uploadRequest['body']); + + $response = $this->curlUtils->Exec($this->curlSession); + $errorMessage = $this->curlUtils->Error($this->curlSession); + $httpCode = $this->curlUtils->GetInfo($this->curlSession, + CURLINFO_HTTP_CODE); + + if ($isLastRequest === true) { + $this->curlUtils->Close($this->curlSession); + $this->curlSession = null; + $this->uploadUrl = null; + $this->totalContentBytes = 0; + } else { + $this->totalContentBytes += $uploadRequest['length']; + } + + // 308 also indicates that the incremental upload succeeded. + if ($httpCode != 200 && $httpCode != 308) { + $errorMessage .= ' Failed response status from batch upload URL.'; + throw new BatchJobException(sprintf( + "Error message: %s, HTTP code %d.", + $errorMessage, + $httpCode + )); + } + } + + /** + * Creates the HTTP headers and body for incremental upload request that will + * be used to upload batch operations to. Content-Length and Content-Range + * are required for incremental upload. + * + * @param array $operations operations to be uploaded to the upload URL + * @param bool $isLastRequest if this is last upload request + * @return array an associative array containing the HTTP headers, body, and + * content length of incremental upload request + */ + private function PrepareIncrementalUploadRequest(array $operations, + $isLastRequest) { + $headers = array('Content-Type' => self::$UPLOAD_URL_HEADER_CONTENT_TYPE); + + $batchJobOpsMutate = new BatchJobOpsMutate(); + $batchJobOpsMutate->operations = $operations; + // Get body of this HTTP upload request. + $serializer = new XmlSerializer(); + $content = $this->PostProcessContent( + $serializer->ConvertObjectToXml($batchJobOpsMutate, 'ns1:mutate', true), + $this->totalContentBytes == 0, + $isLastRequest + ); + $contentLength = mb_strlen($content, '8bit'); + $headers['Content-Length'] = $contentLength; + + // On the last request, specify the total number of bytes. + // e.g., bytes 500-999/1000 + $lowerBound = $this->totalContentBytes; + $upperBound = $this->totalContentBytes + $contentLength - 1; + $totalBytes = ($isLastRequest) ? strval($upperBound + 1) : '*'; + $contentRange = + sprintf('bytes %d-%d/%s', $lowerBound, $upperBound, $totalBytes); + $headers['Content-Range'] = $contentRange; + return array( + 'headers' => $headers, + 'body' => $content, + 'length' => $contentLength + ); + } + + /** + * @see BatchJobUtils::DownloadBatchJobResults + */ + public function DownloadBatchJobResults($downloadUrl) { + $downloadRequest = array( + 'headers' => + array('Accept-Encoding' => self::$DOWNLOAD_URL_ACCEPT_ENCODING) + ); + $flatHeaders = array(); + foreach ($downloadRequest['headers'] as $name => $value) { + $flatHeaders[] = sprintf('%s: %s', $name, $value); + } + + $ch = $this->curlUtils->CreateSession($downloadUrl); + $this->curlUtils->SetOpt($ch, CURLINFO_HEADER_OUT, true); + $this->curlUtils->SetOpt($ch, CURLOPT_HTTPHEADER, $flatHeaders); + + $response = $this->curlUtils->Exec($ch); + $errorMessage = $this->curlUtils->Error($ch); + $httpCode = $this->curlUtils->GetInfo($ch, CURLINFO_HTTP_CODE); + + $this->curlUtils->Close($ch); + + if ($httpCode != 200) { + throw new BatchJobException( + sprintf("Error message: %s, HTTP code %d.", $errorMessage, + $httpCode)); + } else { + return $response; + } + } + + /** + * Initiates the resumable upload by sending a request to Google Cloud + * Storage. + * + * @param string $uploadUrl the upload URL of a batch job + * @return string the URL for the initiated resumable upload + */ + private function InitiateResumableUpload($uploadUrl) { + // This follows the Google Cloud Storage guidelines for initiating + // resumable uploads: + // https://cloud.google.com/storage/docs/resumable-uploads-xml + $headers = array( + 'Content-Type: ' . self::$UPLOAD_URL_HEADER_CONTENT_TYPE, + 'Content-Length: 0', + 'x-goog-resumable: start' + ); + + $ch = $this->curlUtils->CreateSession($uploadUrl); + $this->curlUtils->SetOpt($ch, CURLOPT_POST, true); + $this->curlUtils->SetOpt($ch, CURLOPT_HEADER, true); + $this->curlUtils->SetOpt($ch, CURLINFO_HEADER_OUT, true); + $this->curlUtils->SetOpt($ch, CURLOPT_HTTPHEADER, $headers); + + // Response returned is composed of only headers. Body is always empty. + $response = $this->curlUtils->Exec($ch); + $errorMessage = $this->curlUtils->Error($ch); + $httpCode = $this->curlUtils->GetInfo($ch, CURLINFO_HTTP_CODE); + + $this->curlUtils->Close($ch); + + // A resumable upload URL is provided in the value of "Location". + preg_match('/Location:\s([^\s]*)\s/', $response, $matches); + if (!isset($matches[1]) || $matches[1] === '') { + $message = 'Internal Google Cloud Storage error - cannot retrieve' + . ' resumable upload URL at this time - please try again later.'; + throw new BatchJobException($message); + } else { + return $matches[1]; + } + } + + /** + * Post-processes the request content to conform to the requirements of + * Google Cloud Storage. + * + * @param string $content the request content + * @param bool $isFirstRequest if this is the first request of the batch job + * @param bool $isLastRequest if this is the last request of the batch job + * @return string the post-processed content + */ + private function PostProcessContent($content, $isFirstRequest, + $isLastRequest) { + if (($isFirstRequest === true) && ($isLastRequest === true)) { + return $content; + } + + $content = $this->TrimStartEndElements($content, $isFirstRequest, + $isLastRequest); + + // The request is part of a set of incremental uploads, so pad to the + // required content length. This is not necessary if all operations for the + // job are being uploaded in a single request. + $numBytes = mb_strlen($content, '8bit'); + $remainder = $numBytes % self::$REQUIRED_CONTENT_BYTES_INCREMENT; + if ($remainder > 0) { + $targetLength = $numBytes + + (self::$REQUIRED_CONTENT_BYTES_INCREMENT - $remainder); + $content = str_pad($content, $targetLength, ' '); + } + return $content; + } + + /** + * Returns the request content with the start or end mutate element removed, + * depending on whether the request is the first and/or last request. + * + * @param string $content the request content + * @param bool $isFirstRequest if this is the first request of the batch job + * @param bool $isLastRequest if this is the last request of the batch job + * @return string the trimmed request content + */ + private function TrimStartEndElements($content, $isFirstRequest, + $isLastRequest) { + if ($isFirstRequest === false) { + $content = preg_replace('/^.*?\s*$/s', '', $content); + } + return $content; + } +} diff --git a/src/Google/Api/Ads/AdWords/Util/v201705/ReportClasses.php b/src/Google/Api/Ads/AdWords/Util/v201705/ReportClasses.php new file mode 100755 index 000000000..95e295c83 --- /dev/null +++ b/src/Google/Api/Ads/AdWords/Util/v201705/ReportClasses.php @@ -0,0 +1,597 @@ +The OGNL field path is provided for parsers to identify the request data + * element that may have caused the error.

+ */ + class ApiError { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "ApiError"; + + /** + * @var string + */ + public $fieldPath; + + /** + * @var string + */ + public $trigger; + + /** + * @var string + */ + public $type; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct($fieldPath = null, + $trigger = null, + $type = null + ) { + $this->fieldPath = $fieldPath; + $this->trigger = $trigger; + $this->type = $type; + } + } +} + +if (!class_exists("ReportDownloadError", false)) { + /** + * Specifies why and how report download contains errors. + */ + class ReportDownloadError { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = ""; + + /** + * @var ApiError[] + */ + public $ApiError; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct($ApiError = null) { + $this->ApiError = $ApiError; + } + } +} + +if (!class_exists("ReportDefinition", false)) { + /** + * Represents a report definition. + */ + class ReportDefinition { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "ReportDefinition"; + + /** + * @var integer + */ + public $id; + + /** + * @var Selector + */ + public $selector; + + /** + * @var string + */ + public $reportName; + + /** + * @var tnsReportDefinitionReportType + */ + public $reportType; + + /** + * @var boolean + */ + public $hasAttachment; + + /** + * @var tnsReportDefinitionDateRangeType + */ + public $dateRangeType; + + /** + * @var tnsDownloadFormat + */ + public $downloadFormat; + + /** + * @var string + */ + public $creationTime; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct( + $id = null, + $selector = null, + $reportName = null, + $reportType = null, + $hasAttachment = null, + $dateRangeType = null, + $downloadFormat = null, + $creationTime = null + ) { + $this->id = $id; + $this->selector = $selector; + $this->reportName = $reportName; + $this->reportType = $reportType; + $this->hasAttachment = $hasAttachment; + $this->dateRangeType = $dateRangeType; + $this->downloadFormat = $downloadFormat; + $this->creationTime = $creationTime; + } + } +} + +if (!class_exists("Selector", false)) { + /** + * A generic selector to specify the type of information to return. + */ + class Selector { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "Selector"; + + /** + * @var string[] + */ + public $fields; + + /** + * @var Predicate[] + */ + public $predicates; + + /** + * @var DateRange + */ + public $dateRange; + + /** + * @var OrderBy[] + */ + public $ordering; + + /** + * @var Paging + */ + public $paging; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct($fields = null, + $predicates = null, + $dateRange = null, + $ordering = null, + $paging = null + ) { + $this->fields = $fields; + $this->predicates = $predicates; + $this->dateRange = $dateRange; + $this->ordering = $ordering; + $this->paging = $paging; + } + } +} + +if (!class_exists("Predicate", false)) { + /** + * Specifies how an entity (eg. adgroup, campaign, criterion, ad) should be + * filtered. + */ + class Predicate { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "Predicate"; + + /** + * @var string + */ + public $field; + + /** + * @var tnsPredicateOperator + */ + public $operator; + + /** + * @var string[] + */ + public $values; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct($field = null, $operator = null, + $values = null) { + $this->field = $field; + $this->operator = $operator; + $this->values = $values; + } + } +} + +if (!class_exists("PredicateOperator", false)) { + /** + * Defines the valid set of operators. + */ + class PredicateOperator { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "Predicate.Operator"; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct() {} + } +} + +if (!class_exists("DateRange", false)) { + /** + * Represents a range of dates that has either an upper or a lower bound. + * The format for the date is YYYYMMDD. + */ + class DateRange { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "DateRange"; + + /** + * @var string + */ + public $min; + + /** + * @var string + */ + public $max; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct($min = null, $max = null) { + $this->min = $min; + $this->max = $max; + } + } +} + +if (!class_exists("OrderBy", false)) { + /** + * Specifies how the resulting information should be sorted. + */ + class OrderBy { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "OrderBy"; + + /** + * @var string + */ + public $field; + + /** + * @var tnsSortOrder + */ + public $sortOrder; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct($field = null, $sortOrder = null) { + $this->field = $field; + $this->sortOrder = $sortOrder; + } + } +} + +if (!class_exists("Paging", false)) { + /** + * Specifies the page of results to return in the response. A page is + * specified by the result position to start at and the maximum number of + * results to return. + */ + class Paging { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "Paging"; + + /** + * @var integer + */ + public $startIndex; + + /** + * @var integer + */ + public $numberResults; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct($startIndex = null, $numberResults = null) { + $this->startIndex = $startIndex; + $this->numberResults = $numberResults; + } +}} + +if (!class_exists("SortOrder", false)) { + /** + * Possible orders of sorting. + */ + class SortOrder { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "SortOrder"; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct() {} + } +} + +if (!class_exists("ReportDefinitionReportType", false)) { + /** + * Enums for report types. + */ + class ReportDefinitionReportType { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "ReportDefinition.ReportType"; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct() {} + } +} + +if (!class_exists("ReportDefinitionDateRangeType", false)) { + /** + * Enums for date range of report. + */ + class ReportDefinitionDateRangeType { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "ReportDefinition.DateRangeType"; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct() {} + } +} + +if (!class_exists("DownloadFormat", false)) { + /** + * Enum class that describes the supported formats for content downloads. + * The order mimics the order in which download options are presented in the + * legacy report center. + */ + class DownloadFormat { + + const WSDL_NAMESPACE = "https://adwords.google.com/api/adwords/cm/v201705"; + const XSI_TYPE = "DownloadFormat"; + + /** + * Gets the namesapce of this class + * @return the namespace of this class + */ + public function getNamespace() { + return self::WSDL_NAMESPACE; + } + + /** + * Gets the xsi:type name of this class + * @return the xsi:type name of this class + */ + public function getXsiTypeName() { + return self::XSI_TYPE; + } + + public function __construct() { + if(get_parent_class('DownloadFormat')) parent::__construct(); + } + } +} diff --git a/src/Google/Api/Ads/AdWords/Util/v201705/ReportUtils.php b/src/Google/Api/Ads/AdWords/Util/v201705/ReportUtils.php new file mode 100755 index 000000000..429238efb --- /dev/null +++ b/src/Google/Api/Ads/AdWords/Util/v201705/ReportUtils.php @@ -0,0 +1,115 @@ +adsUtilityRegistry = ($adsUtilityRegistry === null) + ? AdsUtilityRegistry::getInstance() + : $adsUtilityRegistry; + } + + /** + * Downloads a new instance of an existing report definition. If the path + * parameter is specified it will be downloaded to the file at that path, + * otherwise it will be downloaded to memory and be returned as a string. + * @param mixed $reportDefinition the ReportDefinition to download or the id + * of a stored report definition + * @param string $path an optional path of the file to download the report to + * @param AdWordsUser $user the user that created the ReportDefinition + * @param array $options the option to use when downloading the report: + * {boolean} skipReportHeader: if report responses should skip the header + * row containing the report name and date range + * {boolean} skipColumnHeader: if report responses should skip the header + * row containing column names + * {boolean} skipReportSummary: if report responses should skip the + * summary row containing totals + * {boolean} includeZeroImpressions: if report responses should include + * zero impression rows + * {boolean} useRawEnumValues: if report responses should return enum + * values instead of enum display values + * {string} server: the server to make the request to. If null, + * then the default server will be used + * {string} version: the version to make the request against. If + * null, then the default version will be used + * @param array $customCurlOptions the custom curl options for downloading + * reports + * @return mixed if path isn't specified the contents of the report, + * otherwise the size in bytes of the downloaded report + */ + public function DownloadReport($reportDefinition, $path = null, + AdWordsUser $user, array $options = null, + array $customCurlOptions = null) { + if ($path === null || $path === '') { + $this->adsUtilityRegistry->addUtility(AdsUtility::REPORT_UTILS_STRING); + } else { + $this->adsUtilityRegistry->addUtility(AdsUtility::REPORT_UTILS_FILE); + } + return ReportUtilsDelegate::DownloadReport($reportDefinition, $path, $user, + $options, $customCurlOptions); + } + + /** + * Downloads a report with AWQL. If the path parameter is specified it will be + * downloaded to the file at that path, otherwise it will be downloaded to + * memory and be returned as a string. + * @param string $reportQuery the query to use for the report + * @param string $path an optional path of the file to download the report to + * @param AdWordsUser $user the user to retrieve report with + * @param string $reportFormat: the report format to request + * @param array $options the option to use when downloading the report: + * {string} server: the server to make the request to. If null, + * then the default server will be used + * {string} version: the version to make the request against. If + * null, then the default version will be used + * @param array $customCurlOptions the custom curl options for downloading + * reports + * @return mixed if path isn't specified the contents of the report, + * otherwise the size in bytes of the downloaded report + */ + public function DownloadReportWithAwql($reportQuery, $path = null, + AdWordsUser $user, $reportFormat, array $options = null, + array $customCurlOptions = null) { + if ($path === null || $path === '') { + $this->adsUtilityRegistry->addUtility(AdsUtility::REPORT_UTILS_STRING); + } else { + $this->adsUtilityRegistry->addUtility(AdsUtility::REPORT_UTILS_FILE); + } + return ReportUtilsDelegate::DownloadReportWithAwql($reportQuery, $path, + $user, $reportFormat, $options, $customCurlOptions); + } +} diff --git a/src/Google/Api/Ads/AdWords/Util/v201705/ReportUtilsDelegate.php b/src/Google/Api/Ads/AdWords/Util/v201705/ReportUtilsDelegate.php new file mode 100755 index 000000000..544ec1323 --- /dev/null +++ b/src/Google/Api/Ads/AdWords/Util/v201705/ReportUtilsDelegate.php @@ -0,0 +1,369 @@ + 'ReportDownloadError' + ); + + /** + * @see ReportUtils::DownloadReport + */ + public static function DownloadReport($reportDefinition, $path = null, + AdWordsUser $user, array $options = null, + array $customCurlOptions = null) { + $url = self::GetUrl($user, $options); + $headers = self::GetHeaders($user, $url, $options); + $params = self::GetParams($reportDefinition); + return self::DownloadReportFromUrl($user, $url, $headers, $params, $path, + $customCurlOptions); + } + + /** + * @see ReportUtils::DownloadReportWithAwql + */ + public static function DownloadReportWithAwql($reportQuery, $path = null, + AdWordsUser $user, $reportFormat, array $options = null, + array $customCurlOptions = null) { + $url = self::GetUrl($user, $options); + $headers = self::GetHeaders($user, $url, $options); + $params = self::GetQueryParams($reportQuery, $reportFormat); + return self::DownloadReportFromUrl($user, $url, $headers, $params, $path, + $customCurlOptions); + } + + /** + * Downloads a report using the URL provided. + * @param AdWordsUser $user the AdWords user that makes this request + * @param string $url the URL to make the request to + * @param array $headers the headers to use in the request + * @param array $params the parameters to pass in the request + * @param string $path the optional path to download the report to + * @return mixed if path isn't specified the contents of the report, + * otherwise the size in bytes of the downloaded report + * @param array $customCurlOptions the custom curl options for downloading + * reports + */ + private static function DownloadReportFromUrl( + AdWordsUser $user, + $url, + array $headers, + array $params, + $path = null, + array $customCurlOptions = null + ) { + /* + * This method should not be static and instantiation of this class should + * be allowed so we can "inject" CurlUtils, but would break too many things + * that rely on this method being static. + */ + $curlUtils = new CurlUtils(); + $ch = $curlUtils->CreateSession($url); + + $curlUtils->SetOpt($ch, CURLOPT_POST, true); + $curlUtils->SetOpt($ch, CURLINFO_HEADER_OUT, true); + $curlUtils->SetOpt($ch, CURLOPT_USERAGENT, + $user->GetCombinedUserAgent($user->GetUserAgent())); + + $flatHeaders = array(); + foreach($headers as $name => $value) { + $flatHeaders[] = sprintf('%s: %s', $name, $value); + } + $curlUtils->SetOpt($ch, CURLOPT_HTTPHEADER, $flatHeaders); + + if (isset($params)) { + $curlUtils->SetOpt($ch, CURLOPT_POSTFIELDS, $params); + } + + if (isset($path)) { + $file = fopen($path, 'w'); + $curlUtils->SetOpt($ch, CURLOPT_RETURNTRANSFER, false); + $curlUtils->SetOpt($ch, CURLOPT_FILE, $file); + } + + // Set additional cURL options, e.g., CURLOPT_TIMEOUT, if needed. + if ($customCurlOptions !== null) { + foreach ($customCurlOptions as $curlOption => $curlValue) { + $curlUtils->SetOpt($ch, $curlOption, $curlValue); + } + } + + $response = $curlUtils->Exec($ch); + $curlError = $curlUtils->Error($ch); + $code = $curlUtils->GetInfo($ch, CURLINFO_HTTP_CODE); + $downloadSize = $curlUtils->GetInfo($ch, CURLINFO_SIZE_DOWNLOAD); + $request = $curlUtils->GetInfo($ch, CURLINFO_HEADER_OUT); + + $curlUtils->Close($ch); + if (isset($file)) { + fclose($file); + } + + $exception = null; + if ($code != 200) { + // Get the beginning of the response. + if (isset($path)) { + $file = fopen($path, 'r'); + $snippet = fread($file, self::$SNIPPET_LENGTH); + fclose($file); + } else { + $snippet = substr($response, 0, self::$SNIPPET_LENGTH); + } + // Create exception. + $error = self::ParseApiErrorXml($snippet); + if ($error) { + $errorMessage = "Report download failed. Underlying errors are \n"; + foreach ($error->ApiError as $apiError) { + $errorMessage .= sprintf( + "Type = '%s', Trigger = '%s', FieldPath = '%s'. ", + $apiError->type, + $apiError->trigger, + $apiError->fieldPath + ); + } + $exception = new ReportDownloadException($errorMessage, $code); + } else if (preg_match(self::$ERROR_MESSAGE_REGEX, $snippet, $matches)) { + $exception = new ReportDownloadException($matches[2], $code); + } else if (!empty($error)) { + $exception = new ReportDownloadException($error); + } else if (isset($code)) { + $message = 'Report download failed.'; + if (!empty($curlError)) { + $message .= ' ' . $curlError; + } + $exception = new ReportDownloadException($message, $code); + } + } + + self::LogRequest($request, $code, $params, $exception); + + if (isset($exception)) { + throw $exception; + } else if (isset($path)) { + return $downloadSize; + } else { + return $response; + } + } + + /** + * Tries to parse the error response xml from the AdWords API server as an + * object. + * @param string $responseXml the error response xml + * @return object|null the parsed error object, or null if the response cannot + * be parsed. + */ + private static function ParseApiErrorXml($responseXml) { + try { + $deserializer = new XmlDeserializer(self::$CLASS_MAP); + $retval = $deserializer->ConvertXmlToObject($responseXml); + if (!is_array($retval->ApiError)) { + $retval->ApiError = array($retval->ApiError); + } + return $retval; + } catch (DOMException $e) { + // There was a parse exception and hence this response cannot be + // interpreted as an xml. + return null; + } + } + + /** + * Generates the URL to use for the download request. + * @param AdWordsUser $user the AdWordsUser to make the request for + * @param array $options the options configured for the download + * @return string the download URL + */ + private static function GetUrl($user, array $options = null) { + $server = !empty($options['server']) ? $options['server'] : + $user->GetDefaultServer(); + $version = !empty($options['version']) ? $options['version'] : null; + if (!isset($version) && $user->GetDefaultVersion() >= 'v201109') { + $version = $user->GetDefaultVersion(); + } + if (isset($server) && strpos($server, 'http') !== 0) { + throw new ReportDownloadException('Invalid server: ' . $server); + } + return sprintf(self::$DOWNLOAD_URL_FORMAT, $server, $version); + } + + /** + * Generates the parameters to use for the download request. + * @param mixed $reportDefinition the report definition, as an ID or object + * @return array the parameters + */ + private static function GetParams($reportDefinition) { + $params = array(); + if (is_numeric($reportDefinition)) { + $params['__rd'] = $reportDefinition; + } else if (is_object($reportDefinition) || is_array($reportDefinition)) { + $serializer = new XmlSerializer(); + $params['__rdxml'] = $serializer->ConvertObjectToXml( + $reportDefinition, 'reportDefinition', false); + } else { + throw new ReportDownloadException('Invalid report definition type: ' + . $reportDefinition); + } + return $params; + } + + /** + * Generates the parameters to use for the download request with AWQL. + * @param string $reportQuery the report query, as string + * @param string $reportFormat the format to request report in, as string + * @return array the parameters + */ + private static function GetQueryParams($reportQuery, $reportFormat) { + if (!is_string($reportQuery) or !is_string($reportFormat)) { + throw new ReportDownloadException( + 'Invalid parameter supplied, string is expected' + ); + } + return array('__rdquery' => $reportQuery, '__fmt' => $reportFormat); + } + + /** + * Gets the HTTP headers for the report download request. + * @param AdWordsUser $user the AdWordsUser to get credentials from + * @param string $url the URL the request will be made to + * @param array $options the options for the download + * @return array and array of strings, which are header names and values + */ + private static function GetHeaders($user, $url, array $options = null) { + $headers = array(); + $version = !empty($options['version']) ? $options['version'] : + $user->GetDefaultVersion(); + // Authorization. + $authHeader = null; + $oAuth2Info = $user->GetOAuth2Info(); + $oAuth2Handler = $user->GetOAuth2Handler(); + if (!empty($oAuth2Info)) { + $oAuth2Info = $oAuth2Handler->GetOrRefreshAccessToken($oAuth2Info); + $user->SetOAuth2Info($oAuth2Info); + $authHeader = $oAuth2Handler->FormatCredentialsForHeader($oAuth2Info); + } else { + throw new ServiceException('OAuth 2.0 authentication is required.'); + } + $headers['Authorization'] = $authHeader; + + // Developer token. + $headers['developerToken'] = $user->GetDeveloperToken(); + // Target client. + $clientCustomerId = $user->GetClientCustomerId(); + if (isset($clientCustomerId)) { + $headers['clientCustomerId'] = $clientCustomerId; + } else { + throw new ReportDownloadException('The client customer ID must be ' + . 'specified for report downloads.'); + } + // Flags. + if (isset($options['skipReportHeader'])) { + $headers['skipReportHeader'] = + $options['skipReportHeader'] ? 'true' : 'false'; + } + if (isset($options['skipColumnHeader'])) { + $headers['skipColumnHeader'] = + $options['skipColumnHeader'] ? 'true' : 'false'; + } + if (isset($options['skipReportSummary'])) { + $headers['skipReportSummary'] = + $options['skipReportSummary'] ? 'true' : 'false'; + } + if (isset($options['includeZeroImpressions'])) { + $headers['includeZeroImpressions'] = + $options['includeZeroImpressions'] ? 'true' : 'false'; + } + if (isset($options['useRawEnumValues'])) { + $headers['useRawEnumValues'] = + $options['useRawEnumValues'] ? 'true' : 'false'; + } + return $headers; + } + + /** + * Logs the report download request. + * @param string $requestHeaders the HTTP request headers + * @param integer $responseCode the HTTP response code + * @param array $params the parameters that were sent, if any + * @param Exception $exception the exception that will be thrown, if any + */ + private static function LogRequest($requestHeaders, $responseCode, + $params = null, $exception = null) { + $level = isset($exception) ? Logger::$ERROR : Logger::$INFO; + $messageParts = array(); + $messageParts[] = trim($requestHeaders); + $messageParts[] = ''; // Blank line for readability. + $messageParts[] = "Parameters:"; + foreach ($params as $name => $value) { + $messageParts[] = sprintf('%s: %s', $name, $value); + } + $messageParts[] = ''; // Blank line for readability. + $messageParts[] = sprintf('Response Code: %s', $responseCode); + if (isset($exception)) { + $messageParts[] = sprintf('Error Message: %s', $exception->GetMessage()); + } + $messageParts[] = ''; // Blank line for readability. + $message = implode("\n", $messageParts); + Logger::Log(AdWordsUser::REPORT_LOG_CHANNEL_NAME, $message, $level); + } +} diff --git a/src/Google/Api/Ads/AdWords/settings.ini b/src/Google/Api/Ads/AdWords/settings.ini index e711b7262..46ae5868a 100755 --- a/src/Google/Api/Ads/AdWords/settings.ini +++ b/src/Google/Api/Ads/AdWords/settings.ini @@ -10,7 +10,7 @@ ; includeUtilitiesInUserAgent = "true" [SERVER] -DEFAULT_VERSION = "v201702" +DEFAULT_VERSION = "v201705" DEFAULT_SERVER = "https://adwords.google.com" diff --git a/src/Google/Api/Ads/AdWords/v201607/AdGroupAdService.php b/src/Google/Api/Ads/AdWords/v201607/AdGroupAdService.php index 26b4720ba..625951aae 100755 --- a/src/Google/Api/Ads/AdWords/v201607/AdGroupAdService.php +++ b/src/Google/Api/Ads/AdWords/v201607/AdGroupAdService.php @@ -5276,15 +5276,16 @@ public function __construct($durationMillis = null, $streamingUrl = null, $ready if (!class_exists("DynamicSearchAd", false)) { /** - * Represents a dynamic search ad. This ad will have its headline and - * tracking URL auto-generated at serving time according to domain name - * specific information provided by {@link DomainInfoExtension} linked at the - * campaign level. + * Represents a dynamic search ad. This ad will have its headline and final URL auto-generated at + * serving time according to domain name specific information provided by DynamicSearchAdsSetting at + * the campaign level. * - *

Auto-generated fields: headline and optional tracking URL.

+ *

Auto-generated fields: headline and final URL.

* - *

Required fields: {@code description1}, {@code description2}, - * {@code displayUrl}.

+ *

Note: we recommend using the ExpandedDynamicSearchAd type, introduced in v201705, rather than + * the DynamicSearchAd type.

+ * + *

Required fields: {@code description1}, {@code description2}, {@code displayUrl}.

* *

The tracking URL field must contain at least one of the following placeholder tags * (URL parameters):

diff --git a/src/Google/Api/Ads/AdWords/v201607/AdGroupBidModifierService.php b/src/Google/Api/Ads/AdWords/v201607/AdGroupBidModifierService.php index 62d34c9d5..5b9aaf400 100755 --- a/src/Google/Api/Ads/AdWords/v201607/AdGroupBidModifierService.php +++ b/src/Google/Api/Ads/AdWords/v201607/AdGroupBidModifierService.php @@ -3550,6 +3550,9 @@ public function __construct($errors = null, $message = null, $ApplicationExcepti if (!class_exists("AdGroupBidModifierOperation", false)) { /** * Operation used to create or mutate a AdGroupBidModifier. + * + *

If you try to ADD a criterion that already exists, it will be treated as a SET operation + * on the existing criterion. * @package Google_Api_Ads_AdWords_v201607 * @subpackage v201607 */ diff --git a/src/Google/Api/Ads/AdWords/v201607/AdGroupCriterionService.php b/src/Google/Api/Ads/AdWords/v201607/AdGroupCriterionService.php index c833d0063..72d815d86 100755 --- a/src/Google/Api/Ads/AdWords/v201607/AdGroupCriterionService.php +++ b/src/Google/Api/Ads/AdWords/v201607/AdGroupCriterionService.php @@ -1135,7 +1135,10 @@ public function __construct($BiddingSchemeType = null) { /** * Encapsulates the information about bids and bidding strategies. * - *

Bidding Strategy can be set on campaigns, ad groups or ad group criteria. + *

Note: Starting with v201705, bidding strategies can only be set on + * campaigns. In earlier versions, bidding strategies can be set on campaigns, ad groups and ad + * group criteria. + * *

A bidding strategy can be set using one of the following: *