What is the best way to get campaign report from campaigns id and how process the data in stream not as download format?

47 views
Skip to first unread message

Ravi Chandran

unread,
Dec 20, 2019, 12:40:22 AM12/20/19
to AdWords API and Google Ads API Forum
Hi Team,

I need to fetch campaign details from Campaign IDs and what will be best to do this, right now I using the following code

$selector->setFields(
        [
            'CampaignId',
            'AdGroupId',
            'Id',
            'Criteria',
            'CriteriaType',
            'Impressions',
            'Clicks',
            'Cost',
            'AverageCpc',
            'Conversions',
            'Ctr',
            'ConversionRate'
        ]
    );

if(!empty($campaignId)){
        $selector->setPredicates(
            [
                new Predicate('CampaignId', PredicateOperator::IN, [$campaignId])
            ]
        );
    }

$reportDefinition = new ReportDefinition();
    $reportDefinition->setSelector($selector);
    $reportDefinition->setReportName(
        'Campaign performance report #' . uniqid()
    );
    $reportDefinition->setDateRangeType(
        ReportDefinitionDateRangeType::LAST_7_DAYS
    );
    $reportDefinition->setReportType(
        ReportDefinitionReportType::CRITERIA_PERFORMANCE_REPORT
    );
    $reportDefinition->setDownloadFormat(DownloadFormat::CSVFOREXCEL);

    // Download report.
    $reportDownloader = new ReportDownloader($session);
    $reportSettingsOverride = (new ReportSettingsBuilder())->includeZeroImpressions(true)->build();
    $reportDownloadResult = $reportDownloader->downloadReport(
        $reportDefinition,
        $reportSettingsOverride
    );
    $result = $reportDownloadResult->getAsString();



but it prints as like CSV format, not as an array. 

Thanks in advance. 

Ravi Chandran

unread,
Dec 20, 2019, 7:32:40 AM12/20/19
to AdWords API and Google Ads API Forum
I have used DownloadFormat:: XML(instead of CSVFOREXCEL) and convert those data to JSON and it working fine. 

The code which I modified,

       $result = $reportDownloadResult->getStream(); 
        $xml = simplexml_load_string($result->getContents(), "SimpleXMLElement", LIBXML_NOCDATA);
        $json = json_encode($xml);
        $array = json_decode($json,TRUE);


Cheers :) 
Reply all
Reply to author
Forward
0 new messages