Script for checking the presence of targeting in GDN campaigns

If you are gradually cleaning up inefficient targeting criteria, or just filling in a new ad group — there may be a situation where there is a group, there are ads, but there is no targeting. In this situation, the group is supposedly active, but does not work.

Here is a script that checks for such groups:

function main() {
    // Collect active GDN campaigns
    var campaignActiveReport = 'SELECT campaign.id, segments.ad_network_type ' +
        'FROM campaign ' +
        'WHERE segments.ad_network_type = "CONTENT" ' +
        'AND campaign.status = "ENABLED"';
    var report = AdsApp.search(campaignActiveReport, {
        apiVersion: 'v8'
    });
    var campaignsIDs = [];
    while (report.hasNext()) {
        var row = report.next();
        var campaign_id = row.campaign.id;
        // collect campaign IDs
        campaignsIDs.push(campaign_id);
    }
    // remove possible duplicates
    var campaignsIDs = unique(campaignsIDs).sort();

    if (campaignsIDs.length != +0) {
        var campaigns = AdsApp.campaigns()
            .withIds(campaignsIDs)
            .get();
        while (campaigns.hasNext()) {
            var campaign = campaigns.next();
            // get active groups that had no impressions yesterday
            var adGroupSelector = campaign
                .adGroups()
                .withCondition('Status = ENABLED')
                .withCondition('Impressions = 0')
                .forDateRange('YESTERDAY');
            var adGroupIterator = adGroupSelector.get();
            while (adGroupIterator.hasNext()) {
                var adGroup = adGroupIterator.next();
                // Get all targeting criteria
                var adGroupAudiences = adGroup.display().audiences().get();
                var adGroupKeywords = adGroup.display().keywords().get();
                var adGroupPlacements = adGroup.display().placements().get();
                var adGroupTopics = adGroup.display().topics().get();

                if ((!adGroupAudiences.hasNext()) &&
                    (!adGroupKeywords.hasNext()) && 
                    (!adGroupPlacements.hasNext()) && 
                    (!adGroupTopics.hasNext())) {
                    // If none of the targeting types is found, write a message to the log
                    var string = getCurrentAccountDetails() + ' - ' + adGroup.getCampaign().getName() + ' - ' + adGroup.getName() + ' - отсутствует таргетинг!';
                    // Here you can easily attach the report function to the messenger or to the mail
                    Logger.log(string);
                }
            }
        }
    }
}

function unique(arr) { // remove duplicates
    var tmp = {};
    return arr.filter(function (a) {
        return a in tmp ? 0 : tmp[a] = 1;
    });
}