Hi Google Support,
I've run into an issue with the Google Ads Query Validator and need some
help debugging.
* API Version: v12
* Query: "var bmmIdentifier = '(_PHR)'; var includeDSA = 'Yes'; var
dsaIdentifier = '(DSA)'; //some other variables, no need to edit var
keywordsPerList = 5000; var chunks; var accountName =
AdWordsApp.currentAccount().getName(); var numLists; function main() {
// Find exact match Keywords var exactKeywords = getExactKeywords();
// Find out if Exact NKL already exists if (lookForExact()) {
// if true, check if the amount of lists is correct
checkNumberOfLists(); // then, remove alle present negative
keywords removeSNK(); } else { // if false, create one
or more new NKL's createNKL(); } // Add keywords to NKL
addKeywordsToNKL(exactKeywords); // Add (mbm) & (dsa) campaigns
applyToCampaigns(); } function getExactKeywords() { var keywordsOne =
[]; var keywordsTwo = []; var map = {}; var keywordsIter =
AdWordsApp.keywords() .withCondition('CampaignStatus = ENABLED')
.withCondition('AdGroupStatus = ENABLED')
.withCondition('Status = ENABLED') .withCondition('KeywordMatchType
= EXACT') .get(); // Build the map for deduplication while
(keywordsIter.hasNext()) { var keyword = keywordsIter.next();
var text = keyword.getText(); if (map[text]) { // If
is already in array then skip continue; } else {
// If not, add a new keyword to the map. map[text] =
text; keywordsOne.push(text); } } chunks =
Math.ceil(keywordsOne.length / keywordsPerList); // split the keywords
in a 2-dimensional array again for (var y = 0; y < chunks; y++) {
keywordsTwo[y] = keywordsOne.splice(0, keywordsPerList); } return
keywordsTwo; } function lookForExact() { var nkls =
AdWordsApp.negativeKeywordLists() .withCondition('Name = "Exact
1"') .get(); if (nkls.totalNumEntities() > 0) { return
true; } } function checkNumberOfLists() { var nkls =
AdWordsApp.negativeKeywordLists() .withCondition('Name CONTAINS
"Exact "') .get(); numLists = nkls.totalNumEntities(); if
(numLists < chunks) { // create an extra list createNKL();
} else if (numLists > chunks) { // it's not possible to remove
NKL's with AdWords scripts Logger.log('Remove any empty Negative
Keyword Lists in the account "' + accountName + '"'); } } function
removeSNK() { for (var y = 1; y < numLists + 1; y++) { var nklI
= AdWordsApp.negativeKeywordLists() .withCondition('Name =
"Exact ' + y + '"') .get(); var snk = []; while
(nklI.hasNext()) { var nkl = nklI.next(); var snkI
= nkl.negativeKeywords().get(); while (snkI.hasNext()) {
snk.push(snkI.next()); } for (var i = 0;
i < snk.length; i++) { snk[i].remove(); }
} } } function createNKL() { for (var y = 1; y < chunks + 1; y++)
{ var nklO = AdWordsApp.newNegativeKeywordListBuilder()
.withName('Exact ' + y) .build(); } } function
addKeywordsToNKL(input) { for (var y = 0; y < chunks; y++) {
var x = 0; var name = 'Exact ' + (y + 1); var keywords =
[]; keywords.push(input[y]); // get the NKL('s) with the
name "Exact" in it var nklI = AdWordsApp.negativeKeywordLists()
.withCondition('Name = "' + name + '"') .get();
// while having one... while (nklI.hasNext()) { var nkl
= nklI.next(); // ...try to add the keywords one by one
try { nkl.addNegativeKeywords(keywords[x]);
x++; } catch (err) { Logger.log('Error: ' +
keywords[x]); } } keywords.length = 0; } }
function applyToCampaigns() { // get all active and paused campaigns
var campaignIDs = []; var campaignIterator = AdWordsApp.campaigns()
.withCondition('Status in [ENABLED, PAUSED]') .get();
while (campaignIterator.hasNext()) { var campaign =
campaignIterator.next(); var campaignName = campaign.getName();
// if campaign name contains (mbm) or (dsa), push to array if
(campaignName.indexOf(bmmIdentifier) != -1) {
campaignIDs.push(campaign.getId()); } if (includeDSA ==
'Yes' && campaignName.indexOf(dsaIdentifier) != -1) {
campaignIDs.push(campaign.getId()); } } // apply all NKL's
to the selected campaigns for (var y = 1; y < chunks + 1; y++) {
var nklI = AdWordsApp.negativeKeywordLists()
.withCondition('Name = "Exact ' + y + '"') .get();
while (nklI.hasNext()) { var nkl = nklI.next(); var
campaignIterator = AdWordsApp.campaigns()
.withIds(campaignIDs) .get(); while
(campaignIterator.hasNext()) { var campaign =
campaignIterator.next();
campaign.addNegativeKeywordList(nkl); } } } }"
* Result: Invalid Query (errors below)
* Query contains invalid syntax. There is a missing quotation mark in
your query.
* Link to query result:
https://gaql-query-builder.uc.r.appspot.com/v12/share?q=var%20bmmIdentifier%20%3D%20'(_PHR)'%3B%20var%20includeDSA%20%3D%20'Yes'%3B%20var%20dsaIdentifier%20%3D%20'(DSA)'%3B%20%2F%2Fsome%20other%20variables%2C%20no%20need%20to%20edit%20var%20keywordsPerList%20%3D%205000%3B%20var%20chunks%3B%20var%20accountName%20%3D%20AdWordsApp.currentAccount().getName()%3B%20var%20numLists%3B%20function%20main()%20%7B%20%20%20%20%20%2F%2F%20Find%20exact%20match%20Keywords%20%20%20%20%20var%20exactKeywords%20%3D%20getExactKeywords()%3B%20%20%20%20%20%2F%2F%20Find%20out%20if%20Exact%20NKL%20already%20exists%20%20%20%20%20if%20(lookForExact())%20%7B%20%20%20%20%20%20%20%20%20%2F%2F%20if%20true%2C%20check%20if%20the%20amount%20of%20lists%20is%20correct%20%20%20%20%20%20%20%20%20checkNumberOfLists()%3B%20%20%20%20%20%20%20%20%20%2F%2F%20then%2C%20remove%20alle%20present%20negative%20keywords%20%20%20%20%20%20%20%20%20removeSNK()%3B%20%20%20%20%20%7D%20else%20%7B%20%20%20%20%20%20%20%20%20%2F%2F%20if%20false%2C%20create%20one%20or%20more%20new%20NKL's%20%20%20%20%20%20%20%20%20createNKL()%3B%20%20%20%20%20%7D%20%20%20%20%20%2F%2F%20Add%20keywords%20to%20NKL%20%20%20%20%20addKeywordsToNKL(exactKeywords)%3B%20%20%20%20%20%2F%2F%20Add%20(mbm)%20%26%20(dsa)%20campaigns%20%20%20%20%20applyToCampaigns()%3B%20%7D%20function%20getExactKeywords()%20%7B%20%20%20%20%20var%20keywordsOne%20%3D%20%5B%5D%3B%20%20%20%20%20var%20keywordsTwo%20%3D%20%5B%5D%3B%20%20%20%20%20var%20map%20%3D%20%7B%7D%3B%20%20%20%20%20var%20keywordsIter%20%3D%20AdWordsApp.keywords()%20%20%20%20%20%20%20%20%20.withCondition('CampaignStatus%20%3D%20ENABLED')%20%20%20%20%20%20%20%20%20.withCondition('AdGroupStatus%20%3D%20ENABLED')%20%20%20%20%20%20%20%20%20.withCondition('Status%20%3D%20ENABLED')%20%20%20%20%20%20%20%20%20.withCondition('KeywordMatchType%20%3D%20EXACT')%20%20%20%20%20%20%20%20%20.get()%3B%20%20%20%20%20%2F%2F%20Build%20the%20map%20for%20deduplication%20%20%20%20%20while%20(keywordsIter.hasNext())%20%7B%20%20%20%20%20%20%20%20%20var%20keyword%20%3D%20keywordsIter.next()%3B%20%20%20%20%20%20%20%20%20var%20text%20%3D%20keyword.getText()%3B%20%20%20%20%20%20%20%20%20if%20(map%5Btext%5D)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20is%20already%20in%20%20array%20then%20skip%20%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%20%20%20%20%20%20%20%20%20%7D%20else%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20not%2C%20add%20a%20new%20keyword%20to%20the%20map.%20%20%20%20%20%20%20%20%20%20%20%20%20map%5Btext%5D%20%3D%20text%3B%20%20%20%20%20%20%20%20%20%20%20%20%20keywordsOne.push(text)%3B%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%7D%20%20%20%20%20chunks%20%3D%20Math.ceil(keywordsOne.length%20%2F%20keywordsPerList)%3B%20%20%20%20%20%2F%2F%20split%20the%20keywords%20in%20a%202-dimensional%20array%20again%20%20%20%20%20for%20(var%20y%20%3D%200%3B%20y%20%3C%20chunks%3B%20y%2B%2B)%20%7B%20%20%20%20%20%20%20%20%20keywordsTwo%5By%5D%20%3D%20keywordsOne.splice(0%2C%20keywordsPerList)%3B%20%20%20%20%20%7D%20%20%20%20%20return%20keywordsTwo%3B%20%7D%20function%20lookForExact()%20%7B%20%20%20%20%20var%20nkls%20%3D%20AdWordsApp.negativeKeywordLists()%20%20%20%20%20%20%20%20%20.withCondition('Name%20%3D%20%22Exact%201%22')%20%20%20%20%20%20%20%20%20.get()%3B%20%20%20%20%20if%20(nkls.totalNumEntities()%20%3E%200)%20%7B%20%20%20%20%20%20%20%20%20return%20true%3B%20%20%20%20%20%7D%20%7D%20function%20checkNumberOfLists()%20%7B%20%20%20%20%20var%20nkls%20%3D%20AdWordsApp.negativeKeywordLists()%20%20%20%20%20%20%20%20%20.withCondition('Name%20CONTAINS%20%22Exact%20%22')%20%20%20%20%20%20%20%20%20.get()%3B%20%20%20%20%20numLists%20%3D%20nkls.totalNumEntities()%3B%20%20%20%20%20if%20(numLists%20%3C%20chunks)%20%7B%20%20%20%20%20%20%20%20%20%2F%2F%20create%20an%20extra%20list%20%20%20%20%20%20%20%20%20createNKL()%3B%20%20%20%20%20%7D%20else%20if%20(numLists%20%3E%20chunks)%20%7B%20%20%20%20%20%20%20%20%20%2F%2F%20it's%20not%20possible%20to%20remove%20NKL's%20with%20AdWords%20scripts%20%20%20%20%20%20%20%20%20Logger.log('Remove%20any%20empty%20Negative%20Keyword%20Lists%20in%20the%20account%20%22'%20%2B%20accountName%20%2B%20'%22')%3B%20%20%20%20%20%7D%20%7D%20function%20removeSNK()%20%7B%20%20%20%20%20for%20(var%20y%20%3D%201%3B%20y%20%3C%20numLists%20%2B%201%3B%20y%2B%2B)%20%7B%20%20%20%20%20%20%20%20%20var%20nklI%20%3D%20AdWordsApp.negativeKeywordLists()%20%20%20%20%20%20%20%20%20%20%20%20%20.withCondition('Name%20%3D%20%22Exact%20'%20%2B%20y%20%2B%20'%22')%20%20%20%20%20%20%20%20%20%20%20%20%20.get()%3B%20%20%20%20%20%20%20%20%20var%20snk%20%3D%20%5B%5D%3B%20%20%20%20%20%20%20%20%20while%20(nklI.hasNext())%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20var%20nkl%20%3D%20nklI.next()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20var%20snkI%20%3D%20nkl.negativeKeywords().get()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20while%20(snkI.hasNext())%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20snk.push(snkI.next())%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20for%20(var%20i%20%3D%200%3B%20i%20%3C%20snk.length%3B%20i%2B%2B)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20snk%5Bi%5D.remove()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%7D%20%7D%20function%20createNKL()%20%7B%20%20%20%20%20for%20(var%20y%20%3D%201%3B%20y%20%3C%20chunks%20%2B%201%3B%20y%2B%2B)%20%7B%20%20%20%20%20%20%20%20%20var%20nklO%20%3D%20AdWordsApp.newNegativeKeywordListBuilder()%20%20%20%20%20%20%20%20%20%20%20%20%20.withName('Exact%20'%20%2B%20y)%20%20%20%20%20%20%20%20%20%20%20%20%20.build()%3B%20%20%20%20%20%7D%20%7D%20function%20addKeywordsToNKL(input)%20%7B%20%20%20%20%20for%20(var%20y%20%3D%200%3B%20y%20%3C%20chunks%3B%20y%2B%2B)%20%7B%20%20%20%20%20%20%20%20%20var%20x%20%3D%200%3B%20%20%20%20%20%20%20%20%20var%20name%20%3D%20'Exact%20'%20%2B%20(y%20%2B%201)%3B%20%20%20%20%20%20%20%20%20var%20keywords%20%3D%20%5B%5D%3B%20%20%20%20%20%20%20%20%20keywords.push(input%5By%5D)%3B%20%20%20%20%20%20%20%20%20%2F%2F%20get%20the%20NKL('s)%20with%20the%20name%20%22Exact%22%20in%20it%20%20%20%20%20%20%20%20%20var%20nklI%20%3D%20AdWordsApp.negativeKeywordLists()%20%20%20%20%20%20%20%20%20%20%20%20%20.withCondition('Name%20%3D%20%22'%20%2B%20name%20%2B%20'%22')%20%20%20%20%20%20%20%20%20%20%20%20%20.get()%3B%20%20%20%20%20%20%20%20%20%2F%2F%20while%20having%20one...%20%20%20%20%20%20%20%20%20while%20(nklI.hasNext())%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20var%20nkl%20%3D%20nklI.next()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20...try%20to%20add%20the%20keywords%20one%20by%20one%20%20%20%20%20%20%20%20%20%20%20%20%20try%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nkl.addNegativeKeywords(keywords%5Bx%5D)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%2B%2B%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20catch%20(err)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Logger.log('Error%3A%20'%20%2B%20keywords%5Bx%5D)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20keywords.length%20%3D%200%3B%20%20%20%20%20%7D%20%7D%20function%20applyToCampaigns()%20%7B%20%20%20%20%20%2F%2F%20get%20all%20active%20and%20paused%20campaigns%20%20%20%20%20var%20campaignIDs%20%3D%20%5B%5D%3B%20%20%20%20%20var%20campaignIterator%20%3D%20AdWordsApp.campaigns()%20%20%20%20%20%20%20%20%20.withCondition('Status%20in%20%5BENABLED%2C%20PAUSED%5D')%20%20%20%20%20%20%20%20%20.get()%3B%20%20%20%20%20while%20(campaignIterator.hasNext())%20%7B%20%20%20%20%20%20%20%20%20var%20campaign%20%3D%20campaignIterator.next()%3B%20%20%20%20%20%20%20%20%20var%20campaignName%20%3D%20campaign.getName()%3B%20%20%20%20%20%20%20%20%20%2F%2F%20if%20campaign%20name%20contains%20(mbm)%20or%20(dsa)%2C%20push%20to%20array%20%20%20%20%20%20%20%20%20if%20(campaignName.indexOf(bmmIdentifier)%20!%3D%20-1)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20campaignIDs.push(campaign.getId())%3B%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20if%20(includeDSA%20%3D%3D%20'Yes'%20%26%26%20campaignName.indexOf(dsaIdentifier)%20!%3D%20-1)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20campaignIDs.push(campaign.getId())%3B%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%7D%20%20%20%20%20%2F%2F%20apply%20all%20NKL's%20to%20the%20selected%20campaigns%20%20%20%20%20for%20(var%20y%20%3D%201%3B%20y%20%3C%20chunks%20%2B%201%3B%20y%2B%2B)%20%7B%20%20%20%20%20%20%20%20%20var%20nklI%20%3D%20AdWordsApp.negativeKeywordLists()%20%20%20%20%20%20%20%20%20%20%20%20%20.withCondition('Name%20%3D%20%22Exact%20'%20%2B%20y%20%2B%20'%22')%20%20%20%20%20%20%20%20%20%20%20%20%20.get()%3B%20%20%20%20%20%20%20%20%20while%20(nklI.hasNext())%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20var%20nkl%20%3D%20nklI.next()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20var%20campaignIterator%20%3D%20AdWordsApp.campaigns()%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.withIds(campaignIDs)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.get()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20while%20(campaignIterator.hasNext())%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20campaign%20%3D%20campaignIterator.next()%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20campaign.addNegativeKeywordList(nkl)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%7D%20%7D
* Expected result: [PLEASE FILL IN (if the query validator produced
results that are different than the Google Ads API server, please include
your complete logs)]
Thanks!
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"Google Ads API and AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/a5dd7bf3-8347-4efd-acb1-efef52bae5abn%40googlegroups.com.