Help with exporting all labels for given keyword to sheet

141 views
Skip to first unread message

Karl

unread,
Oct 4, 2022, 3:14:50 PM10/4/22
to Google Ads Scripts Forum
Hi,

I am trying to get keyword data to a Google sheet, but I'm struggling to find a way to structure the labels in a way that lets me export them correctly (all of them, on the same row as campaign and keyword). The structure I'm looking for is the way the data is pushed to the report array below, but I can't figure out how to loop through the labels in a way that accomplishes it. Been experimenting with sub/super arrays to no avail. Can anyone offer some guidance? 

function main() {
 
  const query = "SELECT campaign.name, ad_group_criterion.keyword.text, ad_group_criterion.labels FROM keyword_view WHERE campaign.name IN ('Brand')";
 
  const search = AdsApp.search(query);
 
  let report = [];
 
  while (search.hasNext()) {
   let row = search.next();
   let campaign = row.campaign.name;
   let keyword = row.adGroupCriterion.keyword.text;
   let label = row.adGroupCriterion.labels;
   
    report.push([[campaign],[keyword],[label[0]],[label[1]],[label[2]]]);
   
  }
  console.log(report);
}


Any help or pointer much appreciated!

Karl

unread,
Oct 10, 2022, 7:56:59 AM10/10/22
to Google Ads Scripts Forum
Would also be very happy just to get all labels in one cell, as long as I can get all labels on the same row as Campaign and Keyword.

Thanks!

Sigurd Fabrin

unread,
Oct 11, 2022, 7:16:32 AM10/11/22
to Google Ads Scripts Forum
Hi Karl,

I think it is easier to use AdsApp.keywords rather than reports for this

Here's an example script that will add campaign name and keyword text in columns A:B and all keyword label names in column C - in a Google sheet:
let settings = {  
  campaign:'Some campaign name or part of name', 
  url:'https://docs.google.com/spreadsheets/etc', 
  sheet:'Name of sheet'
}
function main() {
  let keywordIter = AdsApp.keywords()
    .withCondition('campaign.name LIKE "%'+settings.campaign+'%"')
    .get();
  let data = [];
  while (keywordIter.hasNext()) {
    let keyword = keywordIter.next();
    let campaign = keyword.getCampaign().getName();
    let kwText = keyword.getText();
    let labelIter = keyword.labels().get();
    let labels = [];
    while (labelIter.hasNext()) {
      let label = labelIter.next();
      labels.push([label.getName()])
    }
    if (labels.length > 0) {
      labels = '["'+labels.join('","').toString()+'"]';
    }
    data.push([campaign,kwText,labels]);
  }
  data.unshift(['Campaign','Keyword','Labels']); // prepend headlines
  let ss = SpreadsheetApp.openByUrl(settings.url);
  let sheet = ss.getSheetByName(settings.sheet);
  sheet.clearContents(); // clear any old data in sheet
  sheet.getRange(1,1,data.length,data[0].length).setValues(data);
}


Sigurd

Karl

unread,
Oct 13, 2022, 6:19:29 AM10/13/22
to Google Ads Scripts Forum
Thanks a lot!

That works great, and also gets label names rather than just resource names.
Is iterating though the keywords a lot slower than using AdsApp.search, or is it just my impression?

Google Ads Scripts Forum

unread,
Oct 20, 2022, 4:15:51 AM10/20/22
to Google Ads Scripts Forum
Reposting the last inquiry (https://groups.google.com/g/adwords-scripts/c/mUVrHJnDMHs) from the forum as it wasn't routed to our support queue.

Regards,
Yasmin
Google Ads Scripts Team

Google Ads Scripts Forum

unread,
Oct 20, 2022, 5:22:21 AM10/20/22
to Google Ads Scripts Forum
Hi Karl,

This is Yasmin from the Google Ads scripts team. Please excuse us as your message failed to be routed to our support queue.

As per our best practices, when you want to retrieve large amounts of entities and their stats, it is often better to use reports rather than standard AdsApp methods. The use of reports is preferred due to the following reasons:
  • Reports give you better performance for large queries
  • Reports will not hit normal fetching quotas
You may check the coding approaches that are included in the documentation. If you're noticing a discrepancy or issue and would need further assistance, kindly provide the following information so we can further our investigation:
  • Google Ads ID or CID
  • Script name
@Sigurd - Thank you for helping a community member out. It is highly appreciated from our end.

Regards,
Yasmin
Google Ads Scripts Team

Reply all
Reply to author
Forward
0 new messages