Call to a member function get() on null

150 views
Skip to first unread message

sz...@mymail.mines.edu

unread,
Jun 29, 2018, 2:45:39 PM6/29/18
to AdWords API and Google Ads API Forum
Hello, I am currently using the adgroup performance report to report on a few metrics, and then pause certain ad groups that are above certain thresholds. However, my selector is coming back as null. Here is my code:

namespace Google\AdsApi\Examples\AdWords\v201806\Reporting;

require __DIR__ . '/../../../../../vendor/autoload.php';

use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\Reporting\v201806\DownloadFormat;
use Google\AdsApi\AdWords\Reporting\v201806\ReportDefinition;
use Google\AdsApi\AdWords\Reporting\v201806\ReportDefinitionDateRangeType;
use Google\AdsApi\AdWords\Reporting\v201806\ReportDownloader;
use Google\AdsApi\AdWords\ReportSettingsBuilder;
use Google\AdsApi\AdWords\v201806\cm\Predicate;
use Google\AdsApi\AdWords\v201806\cm\PredicateOperator;
use Google\AdsApi\AdWords\v201806\cm\ReportDefinitionReportType;
use Google\AdsApi\AdWords\v201806\cm\Selector;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\AdWords\v201806\cm\Paging;
use Google\AdsApi\AdWords\v201806\cm\AdGroup;
use Google\AdsApi\AdWords\v201806\cm\AdGroupOperation;
use Google\AdsApi\AdWords\v201806\cm\AdGroupService;



/**
 * Downloads CRITERIA_PERFORMANCE_REPORT for the specified client customer ID.
 */
class DownloadCriteriaReportWithSelector
{
    const PAGE_LIMIT = 500;
    public static function runExample(AdWordsSession $session, $filePath)
    {
        // Create selector.
        $selector = new Selector(); 
        $selector->setFields(
            [
                'CampaignId',
                'AdGroupId',
                //'Id',
                'Cost',
                'Clicks',
                'AverageCpc',
                'AllConversionRate',
                'AllConversions',
                'CostPerConversion',
                'ValuePerConversion'
                
                // *Add or remove criteria to change what fields you want the report to print out --Sean*
            ]
        );

         //Use a predicate to filter out paused criteria (this is optional).
        $selector->setPredicates(
            [
                new Predicate('AdGroupStatus', PredicateOperator::NOT_IN, ['PAUSED']),
                //new Predicate('AverageCpc', PredicateOperator::GREATER_THAN, array(0))
                
                 //*Edit the number in the array just above to change the threhold of the AverageCpc --Sean*
            ]
        );
        
        $selector->setPaging(new Paging(0, self::PAGE_LIMIT));
        
        $totalNumEntries = 0;
        do {
            // Retrieve ad groups one page at a time, continuing to request pages
            // until all ad groups have been retrieved.
            $page = $adGroupService->get($selector);
            
            // Print out some information for each ad group.
            if ($page->getEntries() !== null) {
                $totalNumEntries = $page->getTotalNumEntries();
                foreach ($page->getEntries() as $adGroup) {
                   
                    //setting up the pause operation
                    $operations = [];
                    $adGroup -> setStatus(AdGroupStatus.PAUSED);
                    $operation = new AdGroupOperation();
                    $operation->setOperand($adGroup);
                    $operation->setOperator(Operator::SET);
                    $operations[] = $operation;
                    // Update the ad group on the server.
                    $result = $adGroupService->mutate($operations);
                    $adGroup->setStatus(AdGroupStatus::PAUSED);

                    $adGroup = $result->getValue()[0];
                    
                    
                    printf(
                        "Ad group with name %d and status '%s' was found.\n",
                        $adGroup->getName(),
                        $adGroup->getStatus()
                         
                    );
                 
                
                }
            }

            $selector->getPaging()->setStartIndex(
                $selector->getPaging()->getStartIndex() + self::PAGE_LIMIT
            );
        } while ($selector->getPaging()->getStartIndex() < $totalNumEntries);

        // Create report definition.
        $reportDefinition = new ReportDefinition();
        $reportDefinition->setSelector($selector);
        $reportDefinition->setReportName(
            date("m-d-Y") . '  Ad Performance Report for Powder7 ' 
        );
        $reportDefinition->setDateRangeType(
            ReportDefinitionDateRangeType::LAST_7_DAYS
            
            // Edit the line above to change the date range you wish the report to look for --Sean*
        );
        $reportDefinition->setReportType(
            ReportDefinitionReportType::ADGROUP_PERFORMANCE_REPORT
        );
        $reportDefinition->setDownloadFormat(DownloadFormat::CSV);

        // Download report.
        $reportDownloader = new ReportDownloader($session);
        // Optional: If you need to adjust report settings just for this one
        // request, you can create and supply the settings override here. Otherwise,
        // default values from the configuration file (adsapi_php.ini) are used.
        $reportSettingsOverride = (new ReportSettingsBuilder())->includeZeroImpressions(true)->build();
        $reportDownloadResult = $reportDownloader->downloadReport(
            $reportDefinition,
            $reportSettingsOverride
            
        );
        $reportDownloadResult->saveToFile($filePath);
        
        printf(
            "Report with name '%s' was downloaded to '%s'.\n",
            $reportDefinition->getReportName(),
            $filePath
        );
    }

    public static function main()
    {
        // Generate a refreshable OAuth2 credential for authentication.
        $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();

        // See: AdWordsSessionBuilder for setting a client customer ID that is
        // different from that specified in your adsapi_php.ini file.
        // Construct an API session configured from a properties file and the
        // OAuth2 credentials above.
        $session = (new AdWordsSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build();
            
        $filePath = sprintf(
            '%s.csv',
            tempnam(sys_get_temp_dir(), 'Ad-Performance-Report-')
        );
        self::runExample($session, $filePath);
    }
}

DownloadCriteriaReportWithSelector::main();

I've been looking around and cant figure out why the selector is null. Any help would be appreciated.

Thanks,

Sean

sz...@mymail.mines.edu

unread,
Jun 29, 2018, 2:58:54 PM6/29/18
to AdWords API and Google Ads API Forum
The error is happening at $page = $adGroupService->get($selector);

Teja Makani

unread,
Jun 29, 2018, 6:10:51 PM6/29/18
to AdWords API and Google Ads API Forum
Hello Sean,

Looks like you are trying to make the report call and the API service call in a single API call. You may have to first download the report save that in your local database and then use the details from that to make a separate AdGroupService.mutate() API call to update the status of your ad groups. Please find the samples in reporting here

You could also explore the option of using AdWords Scripts to do this to run at a specific schedule. If you would like to consider that option, please post your question on the AdWords Scripts forum

Regards,
Sai Teja, AdWords API Team.

sz...@mymail.mines.edu

unread,
Jul 2, 2018, 12:50:03 PM7/2/18
to AdWords API and Google Ads API Forum
Am I not able to do both in a single call?

Teja Makani

unread,
Jul 2, 2018, 3:37:33 PM7/2/18
to AdWords API and Google Ads API Forum
Hello Sean,

You cannot do both in a single API call. As explained earlier you need to download the report and save it in your local database. Then use the details from that to make a separate AdGroupService.mutate() API call to update the status of your ad groups.
Reply all
Reply to author
Forward
0 new messages