Cannot fetch search terms filtered on ad group label

46 views
Skip to first unread message

Rik Mathijssen

unread,
Sep 27, 2023, 10:10:37 AM9/27/23
to Google Ads Scripts Forum
Hi,

I'm trying to fetch all search terms in ad groups containing the same label.

I'm fetching label resource name like this:
function getLabelResource() {
  var labelSelector = AdsApp.labels().withCondition(`label.name = '${LABEL}'`);
  var labelIterator = labelSelector.get();
  while (labelIterator.hasNext()) {
    var label = labelIterator.next();
    return label.getResourceName();
  }
}

Then I fetch the search terms like this:
function getSearchTerms(labelResourceName) {
  return AdsApp.search(`SELECT search_term_view.search_term
  FROM search_term_view
  WHERE ad_group.labels CONTAINS ANY ('${labelResourceName}')`);
}

It returns this error message:
Ca: MutateError.UNSPECIFIED: Call to GoogleAdsService.Search failed: The error code is not in this version. at main (Code:13:29)

Not sure what to do with that.. According to documentation, this should work..




Sigurd Fabrin

unread,
Sep 28, 2023, 5:14:14 AM9/28/23
to Google Ads Scripts Forum
Could the problem be that ad_group.labels are not "Sortable"? They are "Filterable" however. Not sure what the definitions of these terms are though.

Screenshot 2023-09-28 at 11.01.28.png

Anyway, you can use ad_group.id instead (which is both "Sortable" and "Filterable") and it should produce the desired result

Something like this approach:
function main() {
  const labelIter = AdsApp.labels()
    .withCondition('label.name LIKE "%Name of Label%"')
    .get();
  const adGroupIds = [];
  while (labelIter.hasNext()) {
    adGroupIds.push(labelIter.next().adGroups().get().next().getId())
  }
  const query = `

    SELECT
      search_term_view.search_term
    FROM
      search_term_view
    WHERE
      segments.date DURING YESTERDAY
      AND ad_group.id IN ("${adGroupIds.join('","')}")`;
  const response = AdsApp.search(query);
  while (response.hasNext()) {
    console.log(response.next().searchTermView.searchTerm)
  }
}


Btw: consider also using AdsApp.search to fetch the IDs. Should be faster


sigurd

Rik Mathijssen

unread,
Sep 28, 2023, 5:24:27 AM9/28/23
to Google Ads Scripts Forum

Hi Sigurd,

Could be, but the query builder also gave me this: WHERE ad_group.labels CONTAINS ANY ('${labelResourceName}')`

In the meantime, I indeed already built something else using ad group id's via AdsApp.search. Would still like to know if filtering on label has a bug, or is just not possible. For future cases..

Cheers,
Rik
Reply all
Reply to author
Forward
0 new messages