Says "AdsApp.Campaign is not a function" but isn't it?

255 views
Skip to first unread message

Michael Buonpane

unread,
Aug 20, 2024, 8:18:40 PM8/20/24
to Google Ads Scripts Forum
Google's Script help is insisting that my function of selecting campaigns in this code is not a function. But isn't this a function that is provided by Google Ads Scripts?

below is the code I'm looking to implement, any help is appreciated! 



2024-08-20_14-24-33.png

2024-08-20_14-25-22.png

// --------------------------------------- Configuration:
const REQUIRED_KEYWORDS = [...];
const CAMPAIGN_ID = "20615517268";
const BLACKLIST = [...];
// --------------------------------------- End of the configuration

function main() {
    const campaignIds = getEnabledCampaignIds();
    const queriesToAddAsNegative = findNegativeKeywordCandidates(campaignIds);

    if (queriesToAddAsNegative.length > 0) {
        Logger.log(`Adding ${queriesToAddAsNegative.length} new negative keywords...`);
        addNegativeKeywordsToAdGroups(campaignIds, queriesToAddAsNegative);
    } else {
        Logger.log("No negative keyword additions required.");
    }
}

function getEnabledCampaignIds() {
    const campaignIds = [];
    const campaignSelector = AdsApp.Campaign()
        .withCondition("Status = ENABLED");

    const campaigns = campaignSelector.get();
    while (campaigns.hasNext()) {
        const campaign = campaigns.next();
        campaignIds.push(campaign.getId());
    }

    return campaignIds;
}

function findNegativeKeywordCandidates(campaignIds) {
    const negativeCandidates = [];
    const queryReport = AdsApp.report(
        'SELECT Query FROM SEARCH_QUERY_PERFORMANCE_REPORT ' +
        'WHERE CampaignId IN [' + campaignIds.join(",") + '] ' +
        'DURING LAST 7 DAYS');

    const rows = queryReport.rows();
    while (rows.hasNext()) {
        const row = rows.next();
        const query = row['Query'].toLowerCase();

        // Check if query contains any blacklisted term
        const isBlacklisted = BLACKLIST.some(blacklistedTerm => query.includes(blacklistedTerm));
        if (isBlacklisted) {
            continue; // Skip to next query
        }

        // Add to negative keywords if not in required keywords
        if (!REQUIRED_KEYWORDS.some(requiredWord => query.includes(requiredWord))) {
            negativeCandidates.push(query);
        }
    }

    return negativeCandidates;
}

function addNegativeKeywordsToAdGroups(campaignIds, negativeQueries) {
    campaignIds.forEach(campaignId => {
        const campaign = AdsApp.Campaign().withIds(campaignId).get().next();
        Logger.log(`Selected campaign: ${campaign.getName()}`);

        // Get ad groups within the campaign
        const adGroups = campaign.adGroups().get();
        while (adGroups.hasNext()) {
            const adGroup = adGroups.next();
            Logger.log(`Selected ad group: ${adGroup.getName()}`);

            // Add negative keywords to the ad group
            negativeQueries.forEach(query => {
                adGroup.createNegativeKeyword(`[${query}]`);
                Logger.log(`${query} --> added as an exact negative keyword`);
            });
        }
    });
}

Thanks so much
Michael B

Nils Rooijmans

unread,
Aug 21, 2024, 2:53:48 AM8/21/24
to Google Ads Scripts Forum
The language of Google Ads Scripts is Javascript, and because Javascript is case sensitive, you have to use the lower case naming of the method:  

try replacing
AdsApp.Campaign()
with
AdsApp.campaign() 

Hope this helps,

Nils Rooijmans
https://nilsrooijmans.com
See my Google Ads Scripts FAQ to avoid the same mistakes I made: https://nilsrooijmans.com/google-ads-scripts-faq/

Google Ads Scripts Forum

unread,
Aug 21, 2024, 5:16:45 AM8/21/24
to Google Ads Scripts Forum
Hi,

@Nils - thanks for your insight  on this concern.

I would like to inform you that the "AdsApp.Campaign()" is not a valid function in the Google Ads Scripts.
The correct function to select campaigns is "AdsApp.campaigns()". I would recommend that you refer to the document "AdsApp.CampainSelector" for a sample script to retrieve search and display campaigns.
I hope this helps! Kindly get back to us if you still face any issues.

Michael Buonpane

unread,
Aug 21, 2024, 8:22:03 PM8/21/24
to Google Ads Scripts Forum
Hello!

Thank you very much for the help, that did do the trick, but I ran into an additional problem with the script. Now it gives me this error message: 

Ga: Error in query: Invalid date range value, LAST. SELECT Query FROM SEARCH_QUERY_PERFORMANCE_REPORT WHERE CampaignId IN [17369173660,17488699851,19828859756,20308819560,20319398119,20615517268,20911458687,21151794717,21199736101,21221699935,21508170474] DURING LAST 7 DAYS ^ at findNegativeKeywordCandidates (Code:35:32) at main (Code:9:36)

Your help is most appreciated! 
Michael B

Sigurd Fabrin

unread,
Aug 22, 2024, 4:28:14 AM8/22/24
to Google Ads Scripts Forum
The error message actually says it very succinctly: "Error in query: Invalid date range value, LAST"

So, in other words, you are trying to fetch data for a date range that you call "LAST". This is not a date range that the system understands.

The problem is that you have written "DURING LAST 7 DAYS" as a date range. 

While a human can understand that LAST 7 DAYS should be understood as one entity and not 3 separate words, a computer has no clue. It expects the first thing after the word DURING to be a valid pre-defined date range. 
You can see what date values are supported by GAQL here https://developers.google.com/google-ads/api/docs/query/date-ranges#date-range


Sigurd

Google Ads Scripts Forum

unread,
Aug 22, 2024, 4:45:26 AM8/22/24
to Google Ads Scripts Forum
Hi,

@Sigurd - thank you for providing the solution.

I would recommend that you use "DURING LAST_7_DAYS" instead of "DURING LAST 7 DAYS". You can also validate your query by using Google Ads Query Validator.

I hope this helps! kindly get back to us with the below details if you still face any issues.
  • Google Ads account ID/CID
  • Name of the affected script
Thanks,
Google Ads Scripts team 

Michael Buonpane

unread,
Aug 22, 2024, 8:22:06 PM8/22/24
to Google Ads Scripts Forum
That was a good call out! Thank you very much!

Now I am running into an Array issue in the configuration area: 


// --------------------------------------- Configuration:
const REQUIRED_KEYWORDS = ["..."];
const CAMPAIGN_ID = [20615517268,]; // Replace with your campaign ID
const BLACKLIST = ["..."];
const AD_GROUP_IDS = [152056030537, 152056030697, 152056030297, 154034257126, 152056030497, 152056030457, 163723464897, 165760952133, 155375080256, 152056030257, 152056030217]; // Replace with your desired ad group IDs

// --------------------------------------- End of the configuration

It is saying the array is invalid, but my script currently runs with that array?

Ga: Invalid input: 152056030537. Expected an array of ids. Ex: [100, 101, 102, ...] at Code:64:39 at Array.forEach (<anonymous>) at addNegativeKeywordsToAdGroups (Code:63:14) at main (Code:15:5)

Shulinen Herpich

unread,
Aug 22, 2024, 10:57:09 PM8/22/24
to Michael Buonpane via Google Ads Scripts Forum
hello

--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scrip...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/f318b482-0975-48c9-b554-e742167ef954n%40googlegroups.com.

Michael Buonpane

unread,
Aug 23, 2024, 8:21:39 PM8/23/24
to Google Ads Scripts Forum
Hello, 

May I please have assistance on this issue?

Sigurd Fabrin

unread,
Aug 26, 2024, 3:18:40 AM8/26/24
to Google Ads Scripts Forum
You have added this array to the code after the original post. It is not possible to guess what other changes you may have made based on your screenshot.


Also ..

Screenshot 2024-08-26 at 09.11.15.png


Google Ads Scripts Forum Advisor

unread,
Aug 26, 2024, 3:32:38 AM8/26/24
to adwords...@googlegroups.com

Hi Michael,

I would suggest that you change the code 

WHERE CampaignId IN [17369173660,17488699851,19828859756,20308819560,20319398119,20615517268,20911458687,21151794717,21199736101,21221699935,21508170474] AND segments.date DURING LAST_7_DAYS

with

WHERE campaignId IN (152056030537, 152056030697, 152056030297, 154034257126, 152056030497, 152056030457, 163723464897, 165760952133, 155375080256, 152056030257, 152056030217) AND segments.date DURING LAST_7_DAYS

I hope this helps! If you still face any issues afterwards, kindly provide us with the below details.

  • Google Ads account ID or CID
  • Name of the affected script
    You can send the details via Reply privately to the author option, or direct private reply to this email. 

    This message is in relation to case "ref:!00D1U01174p.!5004Q02vFCC9:ref" (ADR-00264164)

    Thanks,
     
    Google Logo Google Ads Scripts Team


    Michael Buonpane

    unread,
    Aug 26, 2024, 8:13:47 PM8/26/24
    to Google Ads Scripts Forum
    Hello, the Google Ads Account ID is: 
    Name of the Script is: Gabby Test

    This is the script as is:


    // --------------------------------------- Configuration:
    const REQUIRED_KEYWORDS = ["..."];
    const CAMPAIGN_ID = [20615517268]; // Replace with your campaign ID
    const BLACKLIST = ["..."];
    const AD_GROUP_IDS = (152056030537, 152056030697, 152056030297, 154034257126, 152056030497, 152056030457, 163723464897, 165760952133, 155375080256, 152056030257, 152056030217); // Replace with your desired ad group IDs

    // --------------------------------------- End of the configuration

    function main() {
      const campaignIds = [CAMPAIGN_ID]; // Use an array for a single campaign
      const queriesToAddAsNegative = findNegativeKeywordCandidates(campaignIds);
      const adgroupIds = [AD_GROUP_IDS];


      if (queriesToAddAsNegative.length > 0) {
        Logger.log(`Adding ${queriesToAddAsNegative.length} new negative keywords...`);
        addNegativeKeywordsToAdGroups(AD_GROUP_IDS, queriesToAddAsNegative);

      } else {
        Logger.log("No negative keyword additions required.");
      }
    }

    function getEnabledCampaignId() {
      const campaignIds = [];
      const campaignSelector = AdsApp.campaigns()

        .withCondition("Status = ENABLED");

      const campaigns = campaignSelector.get();
      while (campaigns.hasNext()) {
        const campaign = campaigns.next();
        campaignIds.push(campaign.getId());
      }

      return campaignIds;
    }

    function findNegativeKeywordCandidates(campaignIds) {
      const negativeCandidates = [];
      const queryReport = AdsApp.report(
        'SELECT Query FROM SEARCH_QUERY_PERFORMANCE_REPORT ' +
          'WHERE CampaignId IN [' + campaignIds.join(",") + '] ' +
          'DURING LAST_7_DAYS');


      const rows = queryReport.rows();
      while (rows.hasNext()) {
        const row = rows.next();
        const query = row['Query'].toLowerCase();

        // Check if query contains any blacklisted term
        const isBlacklisted = BLACKLIST.some(blacklistedTerm => query.includes(blacklistedTerm));
        if (isBlacklisted) {
          continue; // Skip to next query
        }

        // Add to negative keywords if not in required keywords
        if (!REQUIRED_KEYWORDS.some(requiredWord => query.includes(requiredWord))) {
          negativeCandidates.push(query);
        }
      }

      return negativeCandidates;
    }

    function addNegativeKeywordsToAdGroups(adGroupIds, negativeQueries) {
      adGroupIds.forEach(adGroupId => {
        const adGroup = AdsApp.adGroups().withIds(adGroupId).get().next();

        Logger.log(`Selected ad group: ${adGroup.getName()}`);

        // Add negative keywords to the ad group
        negativeQueries.forEach(query => {
          adGroup.createNegativeKeyword(`[${query}]`);
          Logger.log(`${query} --> added as an exact negative keyword`);
        });
      });
    }


    Currently I get this error code after changing the AD Group IDs as suggested:


    TypeError: adGroupIds.forEach is not a function at addNegativeKeywordsToAdGroups (Code:63:14) at main (Code:15:5) at Object.<anonymous> (adsapp_compiled:20552:54)

    Google Ads Scripts Forum Advisor

    unread,
    Aug 27, 2024, 5:06:35 AM8/27/24
    to adwords...@googlegroups.com
    Hi,

    We noticed that you've only provided the script name. Could you please share the CID of the Google Ads account again to investigate the issue further? 
    Message has been deleted

    Google Ads Scripts Forum Advisor

    unread,
    Aug 28, 2024, 3:44:28 AM8/28/24
    to adwords...@googlegroups.com

    Hi Michael,

    Please be informed that I have removed your response from the forum as it contains the PII information.

    You have misunderstood our previous suggestion of changes in the code. I would recommend that you change the code at line 5 with

    const AD_GROUP_IDS = [152056030537, 152056030697, 152056030297, 154034257126, 152056030497, 152056030457, 163723464897, 165760952133, 155375080256, 152056030257, 152056030217]; // Replace with your desired ad group IDs

    and at line 64 with

    const adGroup = AdsApp.adGroups().withIds([152056030537, 152056030697, 152056030297, 154034257126, 152056030497, 152056030457, 163723464897, 165760952133, 155375080256, 152056030257, 152056030217]).get().next();

    Kindly refer to the copy of the script that I have created to avoid any further confusion.

    I hope this helps! If you still face any issues, feel free to contact us. 

    Cionaodh Arnason

    unread,
    Aug 29, 2024, 4:36:28 AM8/29/24
    to Michael Buonpane via Google Ads Scripts Forum
    okay

    On Wed, Aug 28, 2024 at 7:09 AM Michael Buonpane via Google Ads Scripts Forum <adwords...@googlegroups.com> wrote:
    Apologies, the CID is:  287-404-3786
    --
    -- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
    ---
    You received this message because you are subscribed to the Google Groups "Google Ads Scripts Forum" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scrip...@googlegroups.com.

    Michael Buonpane

    unread,
    Sep 10, 2024, 8:21:02 PM9/10/24
    to Google Ads Scripts Forum
    Hello, 

    I still haven't received a possible solution for this issue. I'm unsure of what I can provide you that wouldn't give any PII information but would still allow you to see the scripts needed and what functions I could change in order to make the search term negation script work.

    Thank you 

    Google Ads Scripts Forum Advisor

    unread,
    Sep 11, 2024, 4:44:47 AM9/11/24
    to adwords...@googlegroups.com

    Hi Michael,

    As informed earlier, I would recommend that you change the code at line 5 with

    const AD_GROUP_IDS = [152056030537, 152056030697, 152056030297, 154034257126, 152056030497, 152056030457, 163723464897, 165760952133, 155375080256, 152056030257, 152056030217];
    

    and at line 64 with

    const adGroup = AdsApp.adGroups().withIds([152056030537, 152056030697, 152056030297, 154034257126, 152056030497, 152056030457, 163723464897, 165760952133, 155375080256, 152056030257, 152056030217]).get().next();
    

    Kindly refer to the copy of the script that I have created, to avoid any further confusion.

    I hope this helps! If you still face any issues, feel free to contact us. 

    This message is in relation to case "ref:!00D1U01174p.!5004Q02vFCC9:ref" (ADR-00264164)


    Thanks,
     
    Google Logo Google Ads Scripts Team


    Michael Buonpane

    unread,
    Sep 11, 2024, 8:33:14 PM9/11/24
    to Google Ads Scripts Forum
    Hello,

    I'm no longer running into an error code, which is great! However, I do notice that when running a preview, the script only selects one ad group and then begins listing negated keywords. How can I guarantee its doing the same for the rest of the ad groups simultaneously? 

    Google Ads Scripts Forum Advisor

    unread,
    Sep 12, 2024, 2:36:52 AM9/12/24
    to adwords...@googlegroups.com

    Hi Michael,

    Please be informed that to select all the ad groups with given IDs, you have to use a while loop. I would suggest that you refer to the “AdsApp.​AdGroupSelector” document for more information about selecting ad groups.

    The code after adding the while loop will look like below (I am adding the whole function to avoid any confusion).

    function addNegativeKeywordsToAdGroups(adGroupIds, negativeQueries) {
    
      adGroupIds.forEach(adGroupId => {
    
        const adGroups = AdsApp.adGroups().withIds([152056030537, 152056030697, 152056030297, 154034257126, 152056030497, 152056030457, 163723464897, 165760952133, 155375080256, 152056030257, 152056030217]).get();
    
        while (adGroups.hasNext()) {
    
      var adGroup = adGroups.next();
    
        Logger.log(`Selected ad group: ${adGroup.getName()}`);
    
        // Add negative keywords to the ad group
    
        negativeQueries.forEach(query => {
    
          adGroup.createNegativeKeyword(`[${query}]`);
    
          Logger.log(`${query} --> added as an exact negative keyword`);
    
        });}
    
      });
    
    }
    

    I hope this helps! Kindly get back to us if you still face any issues. 

    This message is in relation to case "ref:!00D1U01174p.!5004Q02vFCC9:ref" (ADR-00264164)


    Thanks,
     
    Google Logo Google Ads Scripts Team


    Reply all
    Reply to author
    Forward
    0 new messages