forked from googleads/google-ads-php
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Global examples: getLanguages and getUserInterest by taxonomyType
- Loading branch information
1 parent
6c7cb97
commit 4d9ae2a
Showing
2 changed files
with
243 additions
and
0 deletions.
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,116 @@ | ||
<?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\BasicOperations; | ||
|
||
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\V1\GoogleAdsClient; | ||
use Google\Ads\GoogleAds\Lib\V1\GoogleAdsClientBuilder; | ||
use Google\Ads\GoogleAds\Lib\V1\GoogleAdsException; | ||
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; | ||
use Google\Ads\GoogleAds\V1\Errors\GoogleAdsError; | ||
use Google\Ads\GoogleAds\V1\Services\GoogleAdsRow; | ||
use Google\ApiCore\ApiException; | ||
|
||
/** This example retrieves languages. */ | ||
class GetLanguages | ||
{ | ||
const CUSTOMER_ID = 'INSERT_CUSTOMER_ID_HERE'; | ||
|
||
const PAGE_SIZE = 1000; | ||
|
||
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 | ||
]); | ||
|
||
// Generate a refreshable OAuth2 credential for authentication. | ||
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); | ||
|
||
// Construct a Google Ads client configured from a properties file and the | ||
// OAuth2 credentials above. | ||
$googleAdsClient = (new GoogleAdsClientBuilder())->fromFile() | ||
->withOAuth2Credential($oAuth2Credential) | ||
->build(); | ||
|
||
try { | ||
self::runExample( | ||
$googleAdsClient, | ||
$options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID | ||
); | ||
} catch (GoogleAdsException $googleAdsException) { | ||
printf( | ||
"Request with ID '%s' has failed.%sGoogle Ads failure details:%s", | ||
$googleAdsException->getRequestId(), | ||
PHP_EOL, | ||
PHP_EOL | ||
); | ||
foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) { | ||
/** @var GoogleAdsError $error */ | ||
printf( | ||
"\t%s: %s%s", | ||
$error->getErrorCode()->getErrorCode(), | ||
$error->getMessage(), | ||
PHP_EOL | ||
); | ||
} | ||
} catch (ApiException $apiException) { | ||
printf( | ||
"ApiException was thrown with message '%s'.%s", | ||
$apiException->getMessage(), | ||
PHP_EOL | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Runs the example. | ||
* | ||
* @param GoogleAdsClient $googleAdsClient the Google Ads API client | ||
* @param int $customerId the client customer ID without hyphens | ||
*/ | ||
public static function runExample(GoogleAdsClient $googleAdsClient, $customerId) | ||
{ | ||
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); | ||
$query = 'SELECT language_constant.id, language_constant.name FROM language_constant'; | ||
|
||
// Issues a search request by specifying page size. | ||
$response = | ||
$googleAdsServiceClient->search($customerId, $query, ['pageSize' => self::PAGE_SIZE]); | ||
|
||
// Iterates over all rows in all pages and prints the requested field values for | ||
// the language in each row. | ||
foreach ($response->iterateAllElements() as $googleAdsRow) { | ||
/** @var GoogleAdsRow $googleAdsRow */ | ||
printf( | ||
"Language Constant with ID %d and name '%s' was found.%s", | ||
$googleAdsRow->getLanguageConstant()->getId()->getValue(), | ||
$googleAdsRow->getLanguageConstant()->getName()->getValue(), | ||
PHP_EOL | ||
); | ||
} | ||
} | ||
} | ||
|
||
GetLanguages::main(); |
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,127 @@ | ||
<?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\BasicOperations; | ||
|
||
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\V1\GoogleAdsClient; | ||
use Google\Ads\GoogleAds\Lib\V1\GoogleAdsClientBuilder; | ||
use Google\Ads\GoogleAds\Lib\V1\GoogleAdsException; | ||
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; | ||
use Google\Ads\GoogleAds\V1\Errors\GoogleAdsError; | ||
use Google\Ads\GoogleAds\V1\Services\GoogleAdsRow; | ||
use Google\Ads\GoogleAds\V1\Enums\UserInterestTaxonomyTypeEnum\UserInterestTaxonomyType; | ||
use Google\ApiCore\ApiException; | ||
|
||
/** This example retrieves user insters. */ | ||
class GetUserInterest | ||
{ | ||
const CUSTOMER_ID = 'INSERT_CUSTOMER_ID_HERE'; | ||
const TAXONOMY_TYPE = 'INSERT_TAXONOMY_TYPE_HERE'; | ||
|
||
const PAGE_SIZE = 1000; | ||
|
||
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::TAXONOMY_TYPE => GetOpt::OPTIONAL_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::TAXONOMY_TYPE] ?: self::TAXONOMY_TYPE | ||
); | ||
} 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 $taxonomyType the taxonomy type for which user insterests will be retrieved. If `null`, | ||
* returns from all user insterests | ||
*/ | ||
public static function runExample(GoogleAdsClient $googleAdsClient, $customerId, $taxonomyType) | ||
{ | ||
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); | ||
// Creates a query that retrieves user insters by taxonomy type. | ||
$query = 'SELECT user_interest.user_interest_id, user_interest.name, user_interest.taxonomy_type FROM user_interest'; | ||
if(!empty($taxonomyType)){ | ||
$query .= " WHERE user_interest.taxonomy_type = '$taxonomyType'"; | ||
} | ||
|
||
// Issues a search request by specifying page size. | ||
$response = | ||
$googleAdsServiceClient->search($customerId, $query, ['pageSize' => self::PAGE_SIZE]); | ||
|
||
// Iterates over all rows in all pages and prints the requested field values for | ||
// the user insterest in each row. | ||
foreach ($response->iterateAllElements() as $googleAdsRow) { | ||
/** @var GoogleAdsRow $googleAdsRow */ | ||
printf( | ||
"User insterest with ID %d and taxonomy type '%s' and name '%s' was found.%s", | ||
$googleAdsRow->getUserInterest()->getUserInterestId()->getValue(), | ||
UserInterestTaxonomyType::name($googleAdsRow->getUserInterest()->getTaxonomyType()), | ||
$googleAdsRow->getUserInterest()->getName()->getValue(), | ||
PHP_EOL | ||
); | ||
} | ||
} | ||
} | ||
|
||
GetUserInterest::main(); |