SearchStream Vs Search

952 views
Skip to first unread message

sreenu malae

unread,
Apr 1, 2020, 6:39:30 AM4/1/20
to AdWords API and Google Ads API Forum
In getcampaigns instead of using search stream im trying to use search.
But I'm getting null values


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\V3\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V3\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\V3\GoogleAdsException;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V3\GoogleAdsServerStreamDecorator;
use Google\Ads\GoogleAds\V3\Errors\GoogleAdsError;
use Google\Ads\GoogleAds\V3\Services\GoogleAdsRow;
use Google\Ads\GoogleAds\V3\Services\SearchGoogleAdsStreamResponse;
use Google\Ads\GoogleAds\V3\Services\SearchGoogleAdsResponse;
use Google\ApiCore\ApiException;
/** This example gets all campaigns. To add campaigns, run AddCampaigns.php. */
class GetCampaigns
{
    const CUSTOMER_ID = '**********';
    
    public static function main()
    {
               $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();
        $oAuth2Credential = (new OAuth2TokenBuilder())
            ->withClientId('*********-p4i6a80tjrm4kgp5akfs6bp00liq2601.apps.googleusercontent.com')
            ->withClientSecret("**********")
            ->withRefreshToken('***********************************************')
            ->build();
        $googleAdsClient = (new GoogleAdsClientBuilder())
            ->withOAuth2Credential($oAuth2Credential)
            ->withDeveloperToken('**************')
            ->withLoginCustomerId('**********')
            ->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 customer ID
     */
    public static function runExample(GoogleAdsClient $googleAdsClient, int $customerId)
    {
        
        $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
        // Creates a query that retrieves all campaigns.
        $query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id';
        // Issues a search stream request.        
        $stream =$googleAdsServiceClient->search($customerId, $query);
        $emptyarray=array();
        foreach ($stream->iterateAllElements() as $googleAdsRow) {
            /** @var GoogleAdsRow $googleAdsRow */
            array_push($emptyarray,$googleAdsRow->getAdGroup()->getId()->getValue());
            printf(
                "Ad group with ID %d and name '%s' was found in campaign with ID .%s",
                $googleAdsRow->getCampaign()->getId()->getValue(),
                $googleAdsRow->getCampaign()->getName()->getValue(),
                PHP_EOL
            );
        }
        echo count($emptyarray);

    }
}

GetCampaigns::main();

Google Ads API Forum Advisor Prod

unread,
Apr 1, 2020, 3:29:41 PM4/1/20
to sreen...@gmail.com, adwor...@googlegroups.com

Hi Sreenu,

Thanks for reaching out to us. I see that in your code you're using the get_campaigns example, but are trying to push ad_groups into your array. For reference, here is the code snippet I'm talking about.



array_push($emptyarray,$googleAdsRow->getAdGroup()->getId()->getValue());
printf( "Ad group with ID %d and name '%s' was found in campaign with ID .%s", $googleAdsRow->getCampaign()->getId()->getValue(), $googleAdsRow->getCampaign()->getName()->getValue(), PHP_EOL

Where you have "getAdGroup()->getId()->getValue()" you're trying to push into an array AdGroups when your code is querying Campaigns.

If you would like to query Ad Groups, I'd suggest using this example to obtain a list of ad groups under a campaign. Let me know if you have further questions.

Thank you,
Bryan, Google Ads API Team



ref:_00D1U1174p._5001UZVXb6:ref
Message has been deleted

sreenu malae

unread,
Apr 1, 2020, 3:57:26 PM4/1/20
to AdWords API and Google Ads API Forum
Hey Bryan,

Thanks I got it. 

sreenu malae

unread,
Apr 9, 2020, 5:58:25 PM4/9/20
to AdWords API and Google Ads API Forum
Hey Bryan,

Thanks for your continuous support, 

I would like to know one thing.
In google ads api V3 using segemts.device we are able to get the device type.
Either mobile computer etc..
Is there any way to get data either the mobile phones are Android, IOS or something else

Google Ads API Forum Advisor Prod

unread,
Apr 10, 2020, 3:17:51 PM4/10/20
to sreen...@gmail.com, adwor...@googlegroups.com
Hi Sreenu,

You should be able to use the Operating_System_Version_Constant report to get the OS of the device. Let me know if you have further questions.

sreenu malae

unread,
Apr 10, 2020, 8:28:02 PM4/10/20
to AdWords API and Google Ads API Forum
Hey Bryan, 

I'm Facing a error

`
Google Ads failure details:
        query_error: Cannot select fields from the following resource: 'OPERATING_SYSTEM_VERSION_CONSTANT', since the resource is incompatible with the resource in FROM clause.
`
 Can you please provide me some example ?
 

Google Ads API Forum Advisor Prod

unread,
Apr 13, 2020, 2:38:01 PM4/13/20
to sreen...@gmail.com, adwor...@googlegroups.com
Hi Sreenu,

Could you provide me with the search query you've used? You can only query fields in Operating_System_Version_Constant if this report is specified in the "From" field in your query. Please let me know.

sreenu malae

unread,
Apr 13, 2020, 6:38:44 PM4/13/20
to AdWords API and Google Ads API Forum
Hey Bryan,

$query = "SELECT campaign_criterion.operating_system_version.operating_system_version_constant FROM campaign_criterion"
$response = $googleAdsServiceClient->search($customerId$query, ['pageSize' => self::PAGE_SIZE]); 
foreach ($response->iterateAllElements() as $googleAdsRow) {
echo($googleAdsRow->getCampaignCriterion()->getOperatingSystemVersion());
}

I tried the above code But still i'm not getting the proper output. I'm getting a object and i think has null values 
Can you please help me out. 
 

Google Ads API Forum Advisor Prod

unread,
Apr 14, 2020, 1:33:06 PM4/14/20
to sreen...@gmail.com, adwor...@googlegroups.com
Hi Sreenu,

Thank you for your query. I see that you're putting the incorrect field in the "FROM" clause. I've written an example query for you below that you can populate with fields from this document.

"SELECT operating_system_version_constant.id, operating_system_version_constant.name, operating_system_version_constant.os_major_version, operating_system_version_constant.os_minor_version FROM operating_system_version_constant"

You can see that the FROM clause is pulling from the operating system version constant report type. Let me know if you have further questions.
Reply all
Reply to author
Forward
0 new messages