How to pull reports of campaign which has a certain label and keywords which doesn't contain a certain label as the same time or probably keywords with no label

51 views
Skip to first unread message

Harsh Kohli

unread,
Jul 25, 2019, 3:49:56 PM7/25/19
to Google Ads Scripts Forum
I am trying to pull up reports of keywords where campaign has a certain label lets call it " Test 123 " and only those keywords in those campaign which have no label on it. I tried to test it with NO_IN but its not working. Here is the script:


function main(){
    var labelIds = [];
    var labelIterator = AdsApp.labels()
      .withCondition("KeywordsCount > 0")
      .withCondition("Name CONTAINS 'kw'")
      .get();
    while (labelIterator.hasNext()){
        var label = labelIterator.next();
        labelIds.push(label.getId());
    }  
    var campaignIterator = AdsApp.campaigns()
        .withCondition("Status = ENABLED")
        .withCondition("LabelNames CONTAINS_ANY ['Test 123']")
        .forDateRange(dateFrom, dateTo)
        .get();
    var campaignIds = [];
    //var count = 0;
    while(campaignIterator.hasNext()){
        var campaign = campaignIterator.next();
        campaignIds.push(campaign.getId());
        //count++;
    }
    //Logger.log(labelIds);

var query = "SELECT LabelIds,CampaignName,AdGroupName,KeywordMatchType,Id,Conversions,ConversionRate,TopOfPageCpc,FirstPositionCpc,FirstPageCpc,CampaignId,AdGroupId,CpcBid,Criteria FROM KEYWORDS_PERFORMANCE_REPORT WHERE CampaignId IN [" + campaignIds.join() + "] AND AdGroupStatus  = 'ENABLED' AND CampaignStatus  = 'ENABLED' AND Conversions > 0 AND Status = 'ENABLED' AND LabelIds NOT_IN [" + labelIds.join() + "] DURING " + dateFrom + "," + dateTo;
var report = AdsApp.report(query);
//return false;
  var reportRows = report.rows();
var campaigns = {};
    while (reportRows.hasNext()){      
    var reportRow = reportRows.next();
      //Logger.log(reportRow.LabelIds);
    var CampaignName = reportRow.CampaignName;
        var AdGroupName = reportRow.AdGroupName;
        var KeywordMatchType = reportRow.KeywordMatchType;
        var Id = reportRow.Id;
        var Conversions = reportRow.Conversions;
        var ConversionRate = reportRow.ConversionRate;
        var TopOfPageCpc = reportRow.TopOfPageCpc;
        var FirstPositionCpc = reportRow.FirstPositionCpc;
        var FirstPageCpc = reportRow.FirstPageCpc;
        var CampaignId = reportRow.CampaignId;
        var AdGroupId  = reportRow.AdGroupId;
        var CpcBid = reportRow.CpcBid;
        var newMaxCpc = CpcBid;
        var Criteria = reportRow.Criteria;
        var breakeven = (0.07 * parseFloat(reportRow.ConversionRate.replace("%",""))).toFixed(2);
        if(isNaN(parseFloat(FirstPositionCpc)) || parseFloat(FirstPositionCpc) == 0){
    if(isNaN(parseFloat(TopOfPageCpc)) || parseFloat(TopOfPageCpc) == 0){
    if(breakeven > parseFloat(FirstPageCpc)){
               newMaxCpc = parseFloat(FirstPageCpc);
           }else{
            newMaxCpc = breakeven;
           }
    }else{
    if(breakeven > parseFloat(TopOfPageCpc)){
               newMaxCpc = parseFloat(TopOfPageCpc);
           }else{
               newMaxCpc = breakeven;
           }
    }
    }else{
    if(breakeven > parseFloat(FirstPositionCpc)){
    newMaxCpc = parseFloat(FirstPositionCpc);
    }else{
    newMaxCpc = breakeven;
    }
    }
          if(newMaxCpc != CpcBid){
        if(!(CampaignId in campaigns)){
        campaigns[CampaignId] = AdWordsApp.campaigns().withCondition("Id = '" + CampaignId + "'").get().next();
        }
        var campaign = campaigns[CampaignId];
        var keyword = campaign.keywords().withCondition("AdGroupId = '" + AdGroupId + "'").withCondition("Id = '" + Id + "'").get().next();
        keyword.bidding().setCpc(newMaxCpc);
        }
    }
}

Harsh

unread,
Jul 25, 2019, 4:00:57 PM7/25/19
to Google Ads Scripts Forum
If someone could tell me how would i run the script to get adwords keyword performance reports where there is no keyword label that will also work for me.

Google Ads Scripts Forum Advisor Prod

unread,
Jul 25, 2019, 4:33:48 PM7/25/19
to adwords-scripts+apn2wqdpn0ppmxqr...@googlegroups.com, adwords-scripts+apn2wqdpn0ppmxqr...@googlegroups.co, adwords...@googlegroups.com
Hello,

A few comments on the script:
  • the variables 'dateTo' and 'dateFrom' are not defined
  • this script also makes keyword bidding changes (that is, it does more than report in case you aren't the original author)
Can you also provide me more information as to why this is not working in your account? After substituting the undefined variables and adjusting the report accordingly, the script does work on my end. Also, if you would like me to investigate in your account directly, please provide your CID and script name.

Thanks,
Matt 
Google Ads Scripts Team

ref:_00D1U1174p._5001UEGRUf:ref

Harsh

unread,
Jul 25, 2019, 4:58:33 PM7/25/19
to Google Ads Scripts Forum
Sorry forgot to paste the above part of the script, Here is the full script:

var DURATION = 75;
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
var now = new Date();
var from = new Date(now.getTime() - DURATION * MILLIS_PER_DAY - MILLIS_PER_DAY);
var to = new Date(now.getTime() - 1 * MILLIS_PER_DAY);
var timeZone = AdsApp.currentAccount().getTimeZone();
var dateFrom = Utilities.formatDate(from, timeZone, 'yyyyMMdd');
var dateTo = Utilities.formatDate(to, timeZone, 'yyyyMMdd');
I tried logging to see if the script is logging the right labels/label ids which worked fine but the script just gives an error which isn't really clear to work with.

Check the screenshot on the attachment. I have tried removing LabelIds NOT_IN [" + labelIds.join() + "] from the SELECT condition on line 34 and the script works but that would apply the changes on all the keywords which is not my motive for it. I need to get reports of keywords under these labelled campaign name " Test 123" and on the keywords where there is no labels.
Screenshot 2019-07-26 at 2.22.34 AM.jpg

Harsh

unread,
Jul 25, 2019, 5:00:21 PM7/25/19
to Google Ads Scripts Forum
This is the error i am getting every time i run it : 

Failed to read from AdWords. Please wait a bit and try again. (file Code.gs, line 35)

Google Ads Scripts Forum Advisor Prod

unread,
Jul 26, 2019, 10:24:25 AM7/26/19
to adwords-scripts+apn2wqdpn0ppmxqr...@googlegroups.com, adwords-scripts+apn2wqdpn0ppmxqr...@googlegroups.co, adwords...@googlegroups.com
Hello,

Can you please provide your CID and script name so I may further investigate?

Harsh

unread,
Jul 26, 2019, 4:40:04 PM7/26/19
to Google Ads Scripts Forum
Hello Matt,

Were you able to review the script ?

Or as an alternative could you provide me script where we can pull keywords which has no labels, i might be able to use my conditions like i have in above mentioned script.

Google Ads Scripts Forum Advisor Prod

unread,
Jul 26, 2019, 4:43:06 PM7/26/19
to adwords-scripts+apn2wqdpn0ppmxqr...@googlegroups.com, adwords-scripts+apn2wqdpn0ppmxqr...@googlegroups.co, adwords...@googlegroups.com
Hello,

Can you please confirm if you received my private email?

My reply was: I have replicated the issue in my environment and alerted our team. As soon as I hear back on this issue, I will provide an update here. Thank you for bringing this to our attention.

Harsh

unread,
Jul 26, 2019, 4:50:53 PM7/26/19
to Google Ads Scripts Forum
Haven't received your private email. But sure keep me posted about any update.

Thanks
Reply all
Reply to author
Forward
0 new messages