Trying to Run a script on certain login

26 views
Skip to first unread message

Murtaza Rangwala

unread,
May 3, 2024, 1:00:14 PMMay 3
to Google Ads Scripts Forum
1. It has to be run only for search ads 2. Google has to run it for enabled campaigns 3. Google has to check for this formula Search lost IS (Budget) if it is greater than 50% & Conversion value per cost ('Conv. value / cost') is greater than 10 4. Send email to the admin with the listed campaigns
i created a script but it does not work

function main() {
  // Get all enabled campaigns
  var campaignsIterator = AdsApp.campaigns()
    .withCondition("Status = ENABLED")
    .get();

  // Initialize an empty array to store campaigns that meet the criteria
  var eligibleCampaigns = [];

  // Loop through each campaign
  while (campaignsIterator.hasNext()) {
    var campaign = campaignsIterator.next();

    // Retrieve the campaign's statistics
    var stats = campaign.getStatsFor("LAST_30_DAYS");

    // Retrieve search lost top impression share (Budget)
    var searchLostTopImpressionShare = stats.getSearchLostTopImpressionShare();

    // Retrieve conversion value per cost
    var convValuePerCost = stats.getConversionValue() / stats.getCost();

    // Check if conditions are met
    if (searchLostTopImpressionShare > 0.5 && convValuePerCost > 10) {
      // If conditions are met, add the campaign to the eligibleCampaigns array
      eligibleCampaigns.push(campaign);
    }
  }

  // Check if there are any eligible campaigns
  if (eligibleCampaigns.length > 0) {
    // If there are eligible campaigns, construct the email content
    var emailBody = "The following campaigns meet the criteria:\n\n";

    for (var i = 0; i < eligibleCampaigns.length; i++) {
      emailBody += "Campaign Name: " + eligibleCampaigns[i].getName() + "\n";
      // Add more details if needed
      emailBody += "\n";
    }

    // Send email to the admin
    MailApp.sendEmail({
      to: "ad...@example.com", // Replace with the admin's email address
      subject: "Eligible Campaigns for Optimization",
      body: emailBody,
    });
  }
}

Google Ads Scripts Forum Advisor

unread,
May 3, 2024, 8:18:28 PMMay 3
to adwords...@googlegroups.com

Hi,

Thank you for contacting the Google Ads Scripts team. 

I have verified the provided script, and I would like to inform you that the functions “getSearchLostTopImpressionShare” and “getConversionValue” you are using are not functions of the Stats entity. Please refer to the AdsApp.stats documentation for supported functions and update your script. If you are still facing any issues, then please provide the below details for further investigation.

  • Google Ads account ID, or CID
  • Name of the script
  • Error details or a screenshot of the issue (if any)
This message is in relation to case "ref:!00D1U01174p.!5004Q02tIsNc:ref" (ADR-00233626)

Thanks,
 
Google Logo Google Ads Scripts Team


Nils Rooijmans

unread,
Jun 5, 2024, 1:33:21 AMJun 5
to Google Ads Scripts Forum
to give some more guidance,  as mentioned, getSearchBudgetLostImpressionShare() isn't a standard function/method that can be applied to a stats object.

to fix your issue you need another way to get the campaigns that have 'Search Lost IS due to budget > 50%'.

Here's some code to help you get started:

you can use the campaign selector to get the campaigns that  have 'Search Lost IS due to budget > 50%' , ie: 

var TARGET = 0.5; // 50%
var campaignSelector = AdsApp.campaigns().withCondition(`metrics.search_budget_lost_top_impression_share > ${TARGET}`).withCondition(`campaign.status = ENABLED`).forDateRange(`LAST_7_DAYS`).get();


this will select these campaigns for you, next you can use a campaign iterator to go over them and adjust the budgets.

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/
Reply all
Reply to author
Forward
0 new messages