As per my question, I want to filter the campaign's metrics based on the date range filter read the below code:

 /**  * Runs the example.  *  * @param GoogleAdsClient $googleAdsClient the Google Ads API client  * @param int $customerId the customer ID  */ public static function getGoogleAdCampaigns(GoogleAdsClient $googleAdsClient, int $customerId){     $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();     // Creates a query that retrieves all campaigns.     $query = "SELECT campaign.id, campaign.name,campaign.status, metrics.impressions, metrics.clicks, metrics.conversions, metrics.ctr, metrics.average_cpc, metrics.cost_micros, campaign.start_date, campaign.end_date FROM campaign WHERE campaign.status = 'ENABLED'";     // Issues a search stream request.     /** @var GoogleAdsServerStreamDecorator $stream */     $stream = $googleAdsServiceClient->searchStream(         SearchGoogleAdsStreamRequest::build($customerId, $query)     );     // Iterates over all rows in all messages and prints the requested field values for     // the campaign in each row.     // Initialize an array to hold campaign data    $campaigns = [];    // Iterates over all rows in all messages and collects the requested field values for    // the campaign in each row.    foreach ($stream->iterateAllElements() as $googleAdsRow) {        /** @var GoogleAdsRow $googleAdsRow */        $campaigns[] = [            'id' => $googleAdsRow->getCampaign()->getId(),            'name' => $googleAdsRow->getCampaign()->getName(),            'start_date' => $googleAdsRow->getCampaign()->getStartDate(),            'end_date' => $googleAdsRow->getCampaign()->getEndDate(),            'status' => $googleAdsRow->getCampaign()->getStatus(),            'impressions' => $googleAdsRow->getMetrics()->getImpressions(),            'clicks' => $googleAdsRow->getMetrics()->getClicks(),            'conversions' => $googleAdsRow->getMetrics()->getConversions(),            'ctr' => $googleAdsRow->getMetrics()->getCtr(),            'average_cpc' => $googleAdsRow->getMetrics()->getAverageCpc(),            'cost_micros' => $googleAdsRow->getMetrics()->getCostMicros(),        ];    }    // print_r($stream->iterateAllElements());    // Return the collected campaign data array     echo json_encode($campaigns);     exit(); } 

Suppose I want to get last week's clicks, impressions last week's cost, etc.

Reference : https://groups.google.com/g/adwords-api/c/H-6qp8v-k-o?pli=1

Tag:google-ads-api, php, google-api-php-client

Only one comment.

  1. Dave Davis

    You need to add a segments.date into the select clause in your $query.

    You then need to add an additional AND into your where clause (where segments.date BETWEEN '2022-01-01' AND '2022-01-31')

Add a new comment.