Issue with getAllCampaigns Function Reporting Incorrect Campaign Count

171 views
Skip to first unread message

mingji lau

unread,
Feb 12, 2025, 9:48:09 PM2/12/25
to Google Ads Scripts Forum

I am experiencing an issue when using the getAllCampaigns function to check the number of campaigns in my account. The function returns a count of 6 campaigns, but I actually have 36 campaigns in my account. The account ID is 157-619-8444.

Thank you for your assistance!

Google Ads Scripts Forum Advisor

unread,
Feb 13, 2025, 12:56:25 AM2/13/25
to adwords...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads Scripts Support team.

On reviewing the script, I would like to inform you that "AdsApp.campaigns().get()" method will only return the search and display campaigns. As per the Google Ads UI we can see 6 campaigns that belong to search and display which are displayed as expected in the script execution. In order to get all the campaign details, I would recommend you to use the reporting option available in Google Ads Scripts. You can refer to the below code to get the campaign data. If you would like to retrieve more data related to campaigns, you can use the campaign query builder to add the required fields to the GAQL query.
 
function main() {

  const searchResults = AdsApp.search(
      'SELECT campaign.name,campaign.advertising_channel_type FROM campaign');

  for (const row of searchResults) {
    const campaignName = row.campaign.name;
    const campaignType = row.campaign.advertisingChannelType;
    console.log(`${campaignName}, ${campaignType}`);
  }
}
 
This message is in relation to case "ref:!00D1U01174p.!5004Q02vH1ea:ref" (ADR-00288165)

Thanks,
 
Google Logo Google Ads Scripts Team

Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5



mingji lau

unread,
Feb 13, 2025, 1:37:41 AM2/13/25
to Google Ads Scripts Forum
Hi,

Thank you for your helpful response regarding the AdsApp.campaigns().get() method and the suggestions to use the reporting options. I appreciate the information you've provided.

However, I still have some confusion. When using the reporting method, I am unable to query the geo.country dimension. Can you please provide guidance on how to include this specific segmentation in my reports?

Thank you for your assistance!


Google Ads Scripts Forum

unread,
Feb 13, 2025, 3:51:50 AM2/13/25
to Google Ads Scripts Forum
Hi,

I would like to inform you that 'geo.country' is not a valid field in the campaign resource. You are recommended to use only the fields available in the campaign query builder.

Feel free to get back in case of any further queries.

mingji lau

unread,
Feb 13, 2025, 5:55:47 AM2/13/25
to Google Ads Scripts Forum
Hi,

Thank you for your assistance so far.

I would like to utilize the method outlined in the Geographic View Query Builder to query the spend for a specific region. Could you please provide a sample script for this purpose?

I appreciate your help and look forward to your response.

Best regards,


Google Ads Scripts Forum

unread,
Feb 13, 2025, 6:35:03 PM2/13/25
to Google Ads Scripts Forum

Hi,

I would suggest that you use the metric ‘geographic_view.country_criterion_id’ in the WHERE field to get metrics for specified locations. You may refer to the sample query to understand this better.

SELECT metrics.cost_micros FROM geographic_view WHERE geographic_view.country_criterion_id IN (criterion_id)

You can add or edit the above query and validate it by using the Google Ads Query Validator. After building the required query, I would suggest that you use the Report method to retrieve the data in logs.

I hope this helps.

Thanks,
Google Ads Scripts team

mingji lau

unread,
Feb 13, 2025, 9:44:53 PM2/13/25
to Google Ads Scripts Forum

Hi,

I hope this message finds you well.

I have been using the method outlined in the script below to process data:

function main() { // Get all sub-accounts const customerIterator = MccApp.accounts().get(); // Iterate through each sub-account while (customerIterator.hasNext()) { const customerAccount = customerIterator.next(); MccApp.select(customerAccount); // Select the current account // Query for costs by country const searchResults = AdsApp.search( `SELECT campaign.name, geographic_view.country_criterion_id, metrics.cost_micros FROM geographic_view WHERE segments.date DURING LAST_30_DAYS` ); // Iterate through query results and log each campaign's name, country, and cost for (const row of searchResults) { const campaignName = row.campaign.name; const countryId = row.geographic_view.country_criterion_id; // This line is causing the error const costMicros = row.metrics.cost_micros; // Convert costs from micros to standard units const cost = costMicros / 1000000; // Convert micros to standard units console.log(`Account ID: ${customerAccount.getCustomerId()}, Campaign: ${campaignName}, Country ID: ${countryId}, Cost: ${cost}`); } } }

However, I am facing the following error:

TypeError: Cannot read properties of undefined (reading 'country_criterion_id')

This has left me quite confused. The account ID I am working with is 787-394-3146.

Could you please provide some guidance on how to resolve this issue? I appreciate your help!

Thank you!

Nils Rooijmans

unread,
Feb 14, 2025, 3:39:35 AM2/14/25
to Google Ads Scripts Forum
it has to do with an illogical implementation of the AdsApp.search() method; in the query, you use snake_case to select the attributes, but the method returns an object that holds the attributes in camelCase.

So you have to change these two lines

      const countryId = row.geographic_view.country_criterion_id;  // This line is causing the error
      const costMicros = row.metrics.cost_micros;

into

      const countryId = row.geographicView.countryCriterionId; 
      const costMicros = row.metrics.costMicros;

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/

mingji lau

unread,
Feb 16, 2025, 11:15:49 PM2/16/25
to Google Ads Scripts Forum
Hi

I wanted to express my sincere thanks for your assistance; I’ve finally resolved the issue! Your help has been invaluable.

However, I have another question: I’m looking to query ad spend broken down by device platform (such as ios and android,not desktop and mobile), but I can’t seem to find the corresponding table or data source. Could you provide some guidance on where I can find this information?

Thank you for your help, and I look forward to your reply!


Google Ads Scripts Forum Advisor

unread,
Feb 17, 2025, 2:16:57 AM2/17/25
to adwords...@googlegroups.com
Hi

I have raised your concern with the rest of our team and one of my teammates will reach out to you once we have an update. Meanwhile, your patience is highly appreciated. 

mingji lau

unread,
Feb 17, 2025, 1:27:20 PM2/17/25
to Google Ads Scripts Forum
Hi

I hope this message finds you well.

I wanted to follow up regarding the concern I raised earlier. Have there been any updates from the rest of the team? I appreciate your assistance and look forward to hearing back from you soon.


Google Ads Scripts Forum

unread,
Feb 18, 2025, 10:34:37 AM2/18/25
to Google Ads Scripts Forum
Hi,
Please be informed that your concern is still under investigation and we have no updates at the moment. However, allow me to follow up on this with the rest of the team. Thank you for your patience.

Thanks & Regards,
Google Ads Scripts support team.

Google Ads Scripts Forum Advisor

unread,
Feb 18, 2025, 9:02:08 PM2/18/25
to adwords...@googlegroups.com
Greetings!

Unfortunately, the mobile platform such as Apple IOS or Android is unavailable when selecting metrics.cost from any report. You can only select these values without metrics if you created criteria that had these set up as criteria. 

For example, if you run the following query on your account, you can see if you set up any campaign criteria that are specifically targeting one of these values.

SELECT mobile_device_constant.operating_system_name, operating_system_version_constant.name, campaign_criterion.criterion_id FROM campaign_criterion LIMIT 5

I don't know of a current report that gives you metrics with that granularity. If you find anything in the Google Ads UI where you can get this information, please let me know with screenshot, and I can see if it's possible to replicate in a query. However, I do not currently know of getting this information in any interface of the Google Ads product.


Thanks,
 
Google Logo
Nadine Wang
Google Ads Scripts Team


Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5

[2025-02-19 02:01:38Z GMT] This message is in relation to case "ref:!00D1U01174p.!5004Q02vH1ea:ref" (ADR-00288165)

 



Richard Rapanan

unread,
Feb 19, 2025, 3:34:22 AM2/19/25
to Google Ads Scripts Forum on behalf of adsscripts


richardrapanan

--
-- 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 visit https://groups.google.com/d/msgid/adwords-scripts/BkkzM000000000000000000000000000000000000000000000SRWQYQ00YWmTdn0YQxu-sA_tY9VW3A%40sfdc.net.
Reply all
Reply to author
Forward
0 new messages