How to get all adgroups with campaign status enable and pause?

438 views
Skip to first unread message

Ken Dan Tinio

unread,
Jan 18, 2018, 2:44:10 AM1/18/18
to AdWords API Forum
    $selector = new Selector();
    $selector->setFields(['Id', 'Name', 'Status', 'CampaignId', 'CampaignName']);
    $selector->setOrdering([new OrderBy('Name', SortOrder::ASCENDING)]);
    $selector->setPaging(new Paging(0, self::PAGE_LIMIT));

I have this so far. I have already retrieved all adgroups using this selector.

The problem is that the adgroups that I fetch includes adgroups with removed campaign.

Vincent Racaza (AdWords API Team)

unread,
Jan 18, 2018, 3:52:56 AM1/18/18
to AdWords API Forum
Hi Ken,

The CampaignStatus is not a supported field in the AdGroup entity of AdGroupService. If you really wish to get the ad groups from non-removed campaigns, then I suggest to use the Adgroup Performance Report instead as you can filter by CampaignStatus in this report type.

Let me know if you have further clarifications.

Thanks,
Vincent
AdWords API Team

Ken Dan Tinio

unread,
Jan 18, 2018, 4:13:32 AM1/18/18
to AdWords API Forum
I did try that. But the problem is this:
$reportDefinition->setDownloadFormat(DownloadFormat::CSV);

How do I get reports as array or json? I don't want to generate a csv.



Vincent Racaza (AdWords API Team)

unread,
Jan 18, 2018, 4:58:09 AM1/18/18
to AdWords API Forum
Hi Ken,

Upon further checking, you can filter by CampaignStatus in AdGroupService.get() but you cannot get its actual value as field. You can try the code snippet below and let me know if this works:

    $selector = new Selector();
    $selector->setFields(['Id', 'Name']);

    $selector->setOrdering([new OrderBy('Name', SortOrder::ASCENDING)]);
    $selector->setPredicates(
        [new Predicate('CampaignStatus', PredicateOperator::NOT_IN, ['REMOVED'])]);

    $selector->setPaging(new Paging(0, self::PAGE_LIMIT));

In regards to reports, aside from generating reports in a file, you can only get the report data as String using getAsString() method. As a suggestion, you can use this method and do a post-processing on your end to transform the String object into an Array or JSON object.

Ken Dan Tinio

unread,
Jan 18, 2018, 5:04:10 AM1/18/18
to AdWords API Forum
getAsString() method, I have already seen it. 


Thats like sounds difficult... how to do it? can you give me a term or word of the function for me to research about.. thanks!


Vincent Racaza (AdWords API Team)

unread,
Jan 18, 2018, 6:46:04 AM1/18/18
to AdWords API Forum
Hi Ken,

Could you confirm if you have tried filtering the AdGroupService.get() by CampaignStatus as I have suggested?

In regards to your latest question, you can examine first what is the content of the String object by the getAsString() method (see this line on how to use this method). From there, you can see that the per row of data in your String object is separated by a new line (\n). With that in mind, you can check for a method in PHP that splits a String object into array with a new line as parameter. You can then manipulate your array of Strings. The data in per index in your array of Strings is the row data which is separated by comma (,). So in your end, you can also separate the per index by comma. This is just a suggestion, and you can always implement your own code logic that you think is better.

Ken Dan Tinio

unread,
Jan 19, 2018, 12:09:07 AM1/19/18
to AdWords API Forum
OKay im using AWQL, works fine. BUt....

WHERE CampaignStatus != 'removed'

gives me an error Details: [fieldPath: ; trigger: ; errorString: QueryError.PARSING_FAILED]


Full code of AWQL:

        $reportQuery = 'SELECT '.$imploded_fields.' FROM '.self::$report_type.' '
            . 'WHERE CampaignStatus IN [REMOVED]'
            . 'DURING '.self::$date_range_type.'';

Ken Dan Tinio

unread,
Jan 19, 2018, 1:04:17 AM1/19/18
to AdWords API Forum
Switching back to selector, works fine:
$selector->setPredicates([new Predicate('CampaignStatus', PredicateOperator::NOT_IN, ['REMOVED'])]);

but adding another Predicate:
        $selector->setPredicates(
            [new Predicate('AdGroupStatus', PredicateOperator::NOT_IN, ['REMOVED'])], 
            [new Predicate('CampaignStatus', PredicateOperator::NOT_IN, ['REMOVED'])]
        );
Doesn't work. Only the first array works fine. 

I am retrieving adgroups without removed status, but campaign status with removed is included.

The thing that I am trying to do is, Fetch adgroup with no removed status that are not associated with removed campaign. I don't want to see removed status.

Ken Dan Tinio

unread,
Jan 19, 2018, 1:35:59 AM1/19/18
to AdWords API Forum
Switching back to AWQL haha


This one works good for me
WHERE AdGroupStatus NOT_IN ["REMOVED"] AND CampaignStatus NOT_IN ["REMOVED"]

How do you convert this into Selector Method?

Vincent Racaza (AdWords API Team)

unread,
Jan 19, 2018, 1:37:07 AM1/19/18
to AdWords API Forum
Hi Ken,

For your reporting error (QueryError.PARSING_FAILED), it means that there maybe some incorrect syntax in your AWQL query. Kindly double check on your end if your syntax and spacing is correct. Based on your first code snippet, it seems that there is no space after 'WHERE CampaignStatus IN [REMOVED]', so this could possibly be the cause of the error.  For your second code snippet, the syntax of setting predicates (see closing parenthesis and brackets) is incorrect.

You can refer to the two code snippets below (AWQL and non-AWQL) below that worked successfully on my end:

Using AWQL:
    $reportQuery = 'SELECT AdGroupName, CampaignStatus, AdGroupStatus '
        . 'FROM ADGROUP_PERFORMANCE_REPORT '
        . 'WHERE CampaignStatus IN  [REMOVED] ';

Using selector:
    $selector = new Selector();
    $selector->setFields(['AdGroupName', 'CampaignStatus', 'AdGroupStatus']);
    
    $selector->setPredicates([
        new Predicate('AdGroupStatus', PredicateOperator::NOT_IN, ['REMOVED']),

        new Predicate('CampaignStatus', PredicateOperator::NOT_IN, ['REMOVED'])]);
 
Let me know if this helps.

Ken Dan Tinio

unread,
Jan 19, 2018, 1:39:21 AM1/19/18
to AdWords API Forum
Magnificent! Thank you very much!
Reply all
Reply to author
Forward
0 new messages