CPC Tripling Alert Script Google Ads: Catch 4-Hour Spikes
Build a custom Google Ads script to detect sudden keyword CPC tripling in 4 hours. Protect your budget against runaway smart bidding and auction inflation.
By AdTurbo Team · 2026-10-18
A sudden cost-per-click spike can dismantle an otherwise profitable Google Ads campaign in a matter of hours. Imagine waking up to find that your core transactional keyword—which historically averaged $4.20 per click—surged to $13.50 between 9:00 AM and 1:00 PM. By 2:00 PM, your entire daily budget of $1,500 has evaporated on a fraction of your anticipated traffic, leaving high-converting afternoon queries with zero ad coverage.
Most account managers rely on daily dashboard checks or morning email summaries. By the time those 24-hour summaries arrive, the damage is irreversible. To stop aggressive automated bidding or competitor bidding wars from draining your budget, you need real-time alerting. This guide provides an automated cpc tripling alert script google ads script that checks your account hourly, calculates intraday CPC against a reliable rolling baseline, and alerts you the moment auction costs triple within a four-hour window.
---
The Mechanics of Sudden CPC Spikes: Why Costs Triple in Under 4 Hours
Google Ads operates on dynamic second-price (or generalized first-price variant) auctions where bids adjust dynamically per search query. While automated bidding promises efficiency, programmatic bidding algorithms can produce extreme intraday cost spikes.
Smart Bidding Overcorrection and Auction Intensity
Google's Smart Bidding algorithms (Maximize Conversions, Target CPA, and Target ROAS) calculate bids at query time using hundreds of contextual signals. If the algorithm detects a cluster of signals that it predicts have an abnormally high conversion probability—such as midday desktop searches in high-income zip codes—it may bid 300% to 500% higher than your target to secure absolute top-of-page placement.
If multiple competitors in your auction also deploy Smart Bidding with similar target profiles, an algorithmic arms race occurs. Both bidding algorithms bid aggressively against each other, driving the clearing cost per click upward until one account hits its daily budget cap or the query volume subsides.
Competitor Entry and Bid Escalation Spirals
Sudden CPC tripling frequently stems from competitive shifts. A new competitor launching a venture-funded campaign with aggressive Target Impression Share bidding can force the clearing price upward across shared keywords. Alternatively, an established competitor running a flash sale might raise their bid limits significantly for a 12-hour window. If your account runs on standard automated bidding without bid ceilings, your average CPC will rise in lockstep to maintain position.
Broad Match Keyword Drift and Query Cannibalization
Broad match keywords paired with Smart Bidding can drift into expensive adjacent categories without warning. A broad match keyword like cloud accounting software might suddenly begin matching searches for enterprise erp migration consulting during peak business hours. Because enterprise consulting carries substantially higher commercial intent, the underlying auction CPC is three to four times higher, causing an apparent spike in your keyword-level metrics. If you have ever wondered why Google Ads is so expensive, this intraday query inflation is often one of the main culprits.
---
Script Architecture: Querying Google Ads API Data in Real Time
To build a reliable sudden keyword cpc spike script, you must balance data recency against statistical significance. Running an alert every 10 minutes produces false positives due to single-click outliers. Conversely, checking every 24 hours fails to prevent budget bleed. A 4-hour evaluation window provides the right balance for medium-to-high-volume accounts.
GAQL Query Structure for Hourly Performance Metrics
Modern Google Ads Scripts use the Google Ads Query Language (GAQL) via AdsApp.search(). To detect intraday spikes, our script must query two distinct time periods:
1. The Intraday Window: Performance data from today (segments.date = 'TODAY'). Because the Google Ads reporting pipeline can experience a 1-to-3-hour attribution lag for impression and cost metrics, comparing "TODAY" against a baseline requires evaluating total spend divided by total clicks accumulated so far today.
2. The Baseline Window: The rolling 7-day or 14-day historical average excluding today (segments.date DURING LAST7DAYS). This establishes the standard expected CPC for each keyword.
sql
SELECT
adgroupcriterion.criterionid,
adgroupcriterion.keyword.text,
campaign.name,
adgroup.name,
metrics.clicks,
metrics.costmicros,
metrics.averagecpc
FROM keywordview
WHERE
campaign.status = 'ENABLED'
AND adgroup.status = 'ENABLED'
AND adgroupcriterion.status = 'ENABLED'
AND metrics.clicks 5
AND segments.date = 'TODAY'
Rolling 4-Hour vs. 7-Day Baseline Math
To determine if a keyword's CPC has tripled, the script executes the following mathematical calculation for each active keyword:
$\text{Baseline CPC} = \frac{\sum \text{Cost}{\text{Last 7 Days}}},{\sum \text{Clicks}{\text{Last 7 Days}}}$
$\text{Current Intraday CPC} = \frac{\text{Cost}{\text{Today}}},{\text{Clicks}{\text{Today}}}$
$\text{Inflation Ratio} = \frac{\text{Current Intraday CPC}},{\text{Baseline CPC}}$
If $\text{Inflation Ratio} \ge 3.0$, the keyword is flagged for immediate notification. However, calculating the ratio alone is insufficient; you must also apply volume filters.
Minimum Data Thresholds to Prevent Statistical Noise
Small sample sizes skew averages. If a keyword with a historical $2.00 CPC receives exactly one click at $6.50 at 8:00 AM, its CPC has technically tripled. However, alerting on a single click produces alert fatigue.
To ensure actionable alerts, the script enforces two statistical guardrails:
• Minimum Intraday Clicks: The keyword must have generated at least 5 clicks today.
• Minimum Intraday Spend: The keyword must have spent at least $50 (or a user-defined threshold) during the current day.
• Minimum Historical Volume: The keyword must have at least 20 clicks over the 7-day baseline to ensure the historical average is statistically representative.
---
Full JavaScript Code for the CPC Tripling Alert Script
Below is the complete, production-ready script designed for the modern Google Ads Scripts environment. It scans all enabled keywords, compares today's intraday average CPC against the prior 7-day historical baseline, and sends an urgent email notification if any keyword has tripled in cost.
javascript
/
• Google Ads Script: Sudden Keyword CPC Tripling Alert
• Checks intraday CPC against a 7-day historical baseline.
• Sends an alert when CPC surges by 3x (or configured multiplier).
/
var CONFIG = {
EMAILRECIPIENTS: '[email protected]',
CPCMULTIPLIERTHRESHOLD: 3.0, // 3.0 = 300% (tripling)
MINTODAYCLICKS: 5, // Minimum clicks today to qualify
MINTODAYSPENDMICROS: 50000000, // $50 minimum spend (in micros: $50 1,000,000)
MINBASELINECLICKS: 20, // Minimum clicks in past 7 days for valid baseline
ALERTPREFIX: '[URGENT CPC ALERT]'
};
function main() {
var baselineData = getBaselineMetrics();
var alerts = evaluateIntradaySpikes(baselineData);
if (alerts.length 0) {
sendEmailAlert(alerts);
} else {
Logger.log('Auction scan complete: No anomalous CPC spikes detected.');
}
}
function getBaselineMetrics() {
var baselineMap = {};
var query = 'SELECT ' +
'adgroupcriterion.criterionid, ' +
'metrics.clicks, ' +
'metrics.costmicros ' +
'FROM keywordview ' +
'WHERE campaign.status = "ENABLED" ' +
'AND adgroup.status = "ENABLED" ' +
'AND adgroupcriterion.status = "ENABLED" ' +
'AND segments.date DURING LAST7DAYS';
var response = AdsApp.search(query);
while (response.hasNext()) {
var row = response.next();
var criterionId = row.adGroupCriterion.criterionId;
var clicks = parseInt(row.metrics.clicks, 10) || 0;
var costMicros = parseFloat(row.metrics.costMicros) || 0.0;
if (!baselineMap[criterionId]) {
baselineMap[criterionId] = { clicks: 0, costMicros: 0 };
}
baselineMap[criterionId].clicks += clicks;
baselineMap[criterionId].costMicros += costMicros;
}
return baselineMap;
}
function evaluateIntradaySpikes(baselineMap) {
var flaggedKeywords = [];
var query = 'SELECT ' +
'campaign.name, ' +
'adgroup.name, ' +
'adgroupcriterion.criterionid, ' +
'adgroupcriterion.keyword.text, ' +
'adgroupcriterion.keyword.matchtype, ' +
'metrics.clicks, ' +
'metrics.costmicros ' +
'FROM keywordview ' +
'WHERE campaign.status = "ENABLED" ' +
'AND adgroup.status = "ENABLED" ' +
'AND adgroupcriterion.status = "ENABLED" ' +
'AND metrics.clicks = ' + CONFIG.MINTODAYCLICKS + ' ' +
'AND metrics.costmicros = ' + CONFIG.MINTODAYSPENDMICROS + ' ' +
'AND segments.date = "TODAY"';
var response = AdsApp.search(query);
while (response.hasNext()) {
var row = response.next();
var criterionId = row.adGroupCriterion.criterionId;
var todayClicks = parseInt(row.metrics.clicks, 10);
var todayCostMicros = parseFloat(row.metrics.costMicros);
var baseline = baselineMap[criterionId];
if (baseline && baseline.clicks = CONFIG.MINBASELINECLICKS) {
var baselineAvgCpc = (baseline.costMicros / baseline.clicks) / 1000000;
var todayAvgCpc = (todayCostMicros / todayClicks) / 1000000;
if (baselineAvgCpc 0) {
var ratio = todayAvgCpc / baselineAvgCpc;
if (ratio = CONFIG.CPCMULTIPLIERTHRESHOLD) {
flaggedKeywords.push({
campaignName: row.campaign.name,
adGroupName: row.adGroup.name,
keywordText: row.adGroupCriterion.keyword.text,
matchType: row.adGroupCriterion.keyword.matchType,
todayClicks: todayClicks,
todayCost: (todayCostMicros / 1000000).toFixed(2),
baselineCpc: baselineAvgCpc.toFixed(2),
todayCpc: todayAvgCpc.toFixed(2),
multiplier: ratio.toFixed(2)
});
}
}
}
}
return flaggedKeywords;
}
function sendEmailAlert(alerts) {
var accountName = AdsApp.currentAccount().getName();
var accountId = AdsApp.currentAccount().getCustomerId();
var subject = CONFIG.ALERTPREFIX + ' ' + alerts.length + ' Keywords Tripled in CPC [' + accountName + ']';
var htmlBody = 'Google Ads Auction Inflation Alert' +
'Account: ' + accountName + ' (' + accountId + ')' +
'The following keywords have experienced an intraday CPC surge exceeding ' +
CONFIG.CPCMULTIPLIERTHRESHOLD + 'x their 7-day average baseline:' +
'' +
'' +
'CampaignAd GroupKeywordMatch' +
'Today ClicksToday Spend7-Day Baseline CPCToday CPCSpike' +
'';
for (var i = 0; i < alerts.length; i++) {
var k = alerts[i];
htmlBody += '' +
'' + k.campaignName + '' +
'' + k.adGroupName + '' +
'' + k.keywordText + '' +
'' + k.matchType + '' +
'' + k.todayClicks + '' +
'