-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added DismissRecommendation example. #9
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
/** | ||
* Copyright 2018 Google LLC | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
namespace Google\Ads\GoogleAds\Examples\Recommendations; | ||
|
||
require __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
use GetOpt\GetOpt; | ||
use Google\Ads\GoogleAds\Examples\Utils\ArgumentNames; | ||
use Google\Ads\GoogleAds\Examples\Utils\ArgumentParser; | ||
use Google\Ads\GoogleAds\Lib\GoogleAdsClient; | ||
use Google\Ads\GoogleAds\Lib\GoogleAdsClientBuilder; | ||
use Google\Ads\GoogleAds\Lib\GoogleAdsException; | ||
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; | ||
use Google\Ads\GoogleAds\Util\ResourceNames; | ||
use Google\Ads\GoogleAds\V0\Errors\GoogleAdsError; | ||
use Google\Ads\GoogleAds\V0\Resources\Recommendation; | ||
use Google\Ads\GoogleAds\V0\Services\DismissRecommendationRequest\DismissRecommendationOperation; | ||
use Google\ApiCore\ApiException; | ||
|
||
/** | ||
* This example dismisses a given recommendation. To retrieve recommendations for text ads, | ||
* run GetTextAdRecommendations.php. | ||
*/ | ||
class DismissRecommendation | ||
{ | ||
const CUSTOMER_ID = 'INSERT_CUSTOMER_ID_HERE'; | ||
// Recommendation ID is the last alphanumeric portion of resource name obtained from | ||
// ResourceNames::forRecommendation(), which has the format of | ||
// `customers/<customer_id>/recommendations/<recommendation_id>`. | ||
// An example can be retrieved from GetTextAdRecommendations.php | ||
const RECOMMENDATION_ID = 'INSERT_RECOMMENDATION_ID_HERE'; | ||
|
||
public static function main() | ||
{ | ||
// Either pass the required parameters for this example on the command line, or insert them | ||
// into the constants above. | ||
$options = (new ArgumentParser())->parseCommandArguments([ | ||
ArgumentNames::CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT, | ||
ArgumentNames::RECOMMENDATION_ID => GetOpt::REQUIRED_ARGUMENT | ||
]); | ||
|
||
// Generate a refreshable OAuth2 credential for authentication. | ||
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); | ||
|
||
// Construct a Google Ads client configured from a properties file and the | ||
// OAuth2 credentials above. | ||
$googleAdsClient = (new GoogleAdsClientBuilder())->fromFile() | ||
->withOAuth2Credential($oAuth2Credential) | ||
->build(); | ||
|
||
try { | ||
self::runExample( | ||
$googleAdsClient, | ||
$options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID, | ||
$options[ArgumentNames::RECOMMENDATION_ID] ?: self::RECOMMENDATION_ID | ||
); | ||
} catch (GoogleAdsException $googleAdsException) { | ||
printf( | ||
"Request with ID '%s' has failed.%sGoogle Ads failure details:%s", | ||
$googleAdsException->getRequestId(), | ||
PHP_EOL, | ||
PHP_EOL | ||
); | ||
foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) { | ||
/** @var GoogleAdsError $error */ | ||
printf( | ||
"\t%s: %s%s", | ||
$error->getErrorCode()->getErrorCode(), | ||
$error->getMessage(), | ||
PHP_EOL | ||
); | ||
} | ||
} catch (ApiException $apiException) { | ||
printf( | ||
"ApiException was thrown with message '%s'.%s", | ||
$apiException->getMessage(), | ||
PHP_EOL | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Runs the example. | ||
* | ||
* @param GoogleAdsClient $googleAdsClient the Google Ads API client | ||
* @param int $customerId the client customer ID without hyphens | ||
* @param string $recommendationId the recommendation ID to dismiss | ||
*/ | ||
public static function runExample( | ||
GoogleAdsClient $googleAdsClient, | ||
$customerId, | ||
$recommendationId | ||
) { | ||
$recommendationResourceName = | ||
ResourceNames::forRecommendation($customerId, $recommendationId); | ||
|
||
$dismissRecommendationOperation = new DismissRecommendationOperation(); | ||
$dismissRecommendationOperation->setResourceName($recommendationResourceName); | ||
|
||
// Issues a mutate request to dismiss the recommendation. | ||
$recommendationServiceClient = $googleAdsClient->getRecommendationServiceClient(); | ||
$response = $recommendationServiceClient->dismissRecommendation( | ||
$customerId, | ||
// Sets the partial failure mode to false. | ||
false, | ||
[$dismissRecommendationOperation] | ||
); | ||
/** @var Recommendation $dismissedRecommendation */ | ||
$dismissedRecommendation = $response->getResults()[0]; | ||
|
||
printf( | ||
"Dismissed recommendation with resource name: '%s'.%s", | ||
$dismissedRecommendation->getResourceName(), | ||
PHP_EOL | ||
); | ||
} | ||
} | ||
|
||
DismissRecommendation::main(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"resource name" -> "the resource name"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.