D2CEBL | Automate PPC Campaigns with Scripts

How to use Google Ads scripts to automate your PPC Campaigns

Published by Marty Paukstys, founder of D2CEBL. 20+ years of Google PPC & Analytics experience. Google Ads Search and Google Analytics certified.
 

Google Ads scripts offer a powerful way to automate tasks and manage your campaigns more efficiently. They use JavaScript code to interact with your Google Ads data, allowing you to make changes, generate reports, and respond to performance changes automatically.

What are Google Ads Scripts?

Google Ads scripts are essentially JavaScript programs that let you control and automate various aspects of your Google Ads campaigns. Instead of manually adjusting bids, pausing keywords, or creating reports, you can write scripts to do these things for you. This can save a significant amount of time and improve campaign performance by reacting to data in real-time.

 


Benefits of Using Google Ads Scripts

  • Automation: Automate repetitive tasks, such as adjusting bids based on weather or day of the week.
  • Efficiency: Save time by automating tasks that would otherwise be done manually.
  • Customization: Create custom solutions tailored to your specific business needs.
  • Real-time Optimization: React to performance data in real-time, making immediate adjustments to improve results.
  • Reporting: Generate custom reports to gain deeper insights into your campaign performance.
     

Best Practices for Google Ads Scripts

  • Start Small: Begin with simple scripts and gradually increase complexity as you become more comfortable.
  • Use Comments: Add comments to your code to explain what each section does. This makes it easier to understand and maintain.
  • Error Handling: Implement error handling to catch and log any issues that may occur during script execution.
  • Testing: Thoroughly test your scripts in a test environment before running them on live campaigns.
  • Logging: Use the Logger.log() function to log important information about script execution. This helps with debugging and monitoring.
  • Frequency: Run scripts frequently enough to take advantage of real-time data, but not so often that you overwhelm the system.
  • Permissions: Ensure the script has the necessary permissions to access and modify the data it needs.
  • Review Regularly: Regularly review your scripts to ensure they are still functioning correctly and meeting your needs.


Examples of Google Ads Scripts


Bidding Automation 

What it does: This script automatically adjusts CPC bids for ad groups based on the CPA (Cost Per Acquisition) from the last week's performance in Google Ads.

Script:



function adjustBids() {

 var TARGET_CPA = 10.00; // Define target CPA in USD

 var campaignIterator = AdsApp.campaigns().get();
 while (campaignIterator.hasNext()) {
   var campaign = campaignIterator.next();
   var stats = campaign.getStatsFor("LAST_WEEK");

   var conversions = stats.getConversions();
   if (conversions === 0) continue; // Avoid division by zero

   var cpa = stats.getCost() / conversions;

   // Iterate through ad groups (Adjusting bids at campaign level is NOT allowed)
   var adGroupIterator = campaign.adGroups().get();
   while (adGroupIterator.hasNext()) {
     var adGroup = adGroupIterator.next();
     var currentCpc = adGroup.bidding().getCpc();

     if (cpa > TARGET_CPA) {
       adGroup.bidding().setCpc(currentCpc * 0.9); // Decrease bid by 10%
       Logger.log("Decreased bid for ad group: " + adGroup.getName() + " in campaign: " + campaign.getName());
     } else if (cpa < TARGET_CPA) {
       adGroup.bidding().setCpc(currentCpc * 1.1); // Increase bid by 10%
       Logger.log("Increased bid for ad group: " + adGroup.getName() + " in campaign: " + campaign.getName());
     }
   }
 }
}
 

Explanation:

  • If CPA is too high β†’ Decreases bids by 10% to reduce costs.
  • If CPA is below target β†’ Increases bids by 10% to capture more conversions.
  • Prevents errors by skipping campaigns with zero conversions.
  • Logs all bid adjustments for tracking in Google Ads Scripts.

Keyword Management

What it does: This script automatically pauses underperforming keywords in Google Ads based on CTR (Click-Through Rate), cost, and conversion performance over the last 30 days.

Script:

function pauseLowPerformingKeywords() {
var keywordIterator = AdsApp.keywords()
  .withCondition("Ctr < 0.01")      // CTR below 1%
  .withCondition("Cost > 1.0")      // Spent more than $1
  .withCondition("Impressions > 100")  // Only consider keywords with enough data
  .withCondition("Conversions = 0") // Ignore keywords that actually drive conversions
  .forDateRange("LAST_30_DAYS")    // Check last 30 days of data
  .get();

var pausedCount = 0;

while (keywordIterator.hasNext()) {
  var keyword = keywordIterator.next();
  keyword.pause();  
  Logger.log("Paused keyword: " + keyword.getText());
  pausedCount++;
}

Logger.log("Script completed: Paused " + pausedCount + " low-performing keywords.");
}

Explanation:

  • Finds keywords with a CTR below 1% (0.01) in the last 30 days.
  • Only pauses keywords that have spent over $1.00 to avoid stopping low-budget keywords.
  • Requires at least 100 impressions so new keywords aren’t unfairly paused.
  • Ignores keywords that have conversions, even if CTR is low.
  • Logs all paused keywords so advertisers can review changes.

Ad Scheduling

What it does: This script automatically adjusts bids for ad groups based on the day of the week, ensuring optimal bidding during high and low-performance periods.
Script:


function adjustBidsByTime() {
 var now = new Date();
 var dayOfWeek = now.getDay(); // 0 = Sunday, 1 = Monday, ..., 6 = Saturday

 var bidMultiplier = 1.2;  // 20% increase on weekends
 var resetMultiplier = 1.0; // Reset to normal on weekdays

 var adGroupIterator = AdsApp.adGroups().get();
 
 while (adGroupIterator.hasNext()) {
   var adGroup = adGroupIterator.next();
   var currentCpc = adGroup.bidding().getCpc();

   if (dayOfWeek == 6 || dayOfWeek == 0) { //If it's Saturday or Sunday, increase bid
     var newCpc = currentCpc * bidMultiplier;
     adGroup.bidding().setCpc(newCpc);
     Logger.log("Increased bid for ad group: " + adGroup.getName() + " on the weekend. New CPC: " + newCpc);
   } else { //If it's a weekday (Monday-Friday), reset bid
     var newCpc = currentCpc * resetMultiplier;
     adGroup.bidding().setCpc(newCpc);
     Logger.log("Reset bid for ad group: " + adGroup.getName() + " on a weekday. New CPC: " + newCpc);
   }
 }
}
 

Explanation:

  • On weekends (Saturday & Sunday), bids increase by 20% to maximize visibility during high-traffic days.
  • On weekdays (Monday-Friday), bids reset to normal, preventing unnecessary overbidding.
  • Adjusts bids at the ad group level (since campaign-level CPC adjustments are not allowed in Google Ads Scripts).
  • Logs all bid changes for tracking and review.
  • Prevents compounding bid increases by ensuring adjustments only apply once per day.

Reporting

What it does: This script automatically generates a weekly performance report for all Google Ads campaigns, retrieving key metrics from the last 7 days and storing them in a structured format.
Script:


function generateWeeklyReport() {
 var report = AdsApp.report(
     "SELECT CampaignName, Clicks, Impressions, Cost, Conversions, ConversionValue " +
     "FROM CAMPAIGN_PERFORMANCE_REPORT " +
     "WHERE Date DURING LAST_WEEK " +
     "LIMIT 500"); // Prevent excessive data pulls

 var rows = report.rows();
 var reportData = [];
 
 while (rows.hasNext()) {
   var row = rows.next();
   reportData.push([
     row["CampaignName"],
     parseInt(row["Clicks"]), // Ensure Clicks are stored as an integer
     parseInt(row["Impressions"]), // Ensure Impressions are stored as an integer
     parseFloat(row["Cost"]), // Ensure Cost is stored as a float
     parseInt(row["Conversions"]), // Ensure Conversions are stored as an integer
     parseFloat(row["ConversionValue"]) // Ensure ConversionValue is stored as a float
   ]);
 }

 // Code to format and email the reportData would go here
 Logger.log("Weekly report generated with " + reportData.length + " campaigns.");
}
 

Explanation:

  • Extracts campaign-level data, including Clicks, Impressions, Cost, Conversions, and Conversion Value.
  • Limits data retrieval to 500 campaigns to prevent performance issues.
  • Ensures numeric values (Cost, Conversion Value) are stored correctly for accurate calculations.
  • Logs the total number of campaigns included in the report for transparency.
  • Prepares data for further processing (e.g., saving to Google Sheets or emailing the report).

Setting Up Google Ads Scripts

  • Access the Scripts Section: In your Google Ads account, navigate to "Tools & Settings" and then "Scripts" under the "Bulk actions" section.
  • Create a New Script: Click the "+" button to create a new script.
  • Write Your Script: Use the script editor to write your JavaScript code.
  • Authorize the Script: When you run the script for the first time, you'll need to authorize it to access your Google Ads data.
  • Preview and Test: Use the preview option to test your script without making any actual changes.
  • Schedule the Script: Once you're satisfied with the script, schedule it to run automatically at the desired frequency.

Important Considerations

  • API Limits: Be aware of Google Ads API limits to avoid exceeding them.
  • Security: Protect your scripts and API keys to prevent unauthorized access.
  • Documentation: Refer to the Google Ads Scripts documentation for detailed information on available methods and objects.


By using Google Ads scripts effectively, you can automate tasks, optimize campaigns, and save valuable time. Remember to start with simple scripts, test thoroughly, and monitor performance regularly.

Sources

Back to How To
  • Share
  • facebook
  • icon_linkedin
  • icon_x