A Google Ads Script That Emails You When Clicks Come From the Wrong Pin

2026-12-05来源 dev.toEnglish

Location settings in Google Ads drift. New campaigns arrive with Google's default, "Presence or interest", and suddenly a clinic in Koramangala is paying for clicks from another city. This post shows a small Google Ads Script that checks the last 7 days and emails you if too much spend falls outside the pin codes you serve.

What you need
Access to Tools, then Bulk actions, then Scripts in your Google Ads account.
A list of the pin codes you serve. Google supports postal code targeting in India, so the report can return Indian postal codes.
The script
javascript
// Out-of-area spend alert for Google Ads Scripts.
// Test in Preview first. Edit ALLOWED and EMAIL before running.

const ALLOWED = ['560034', '560095', '560102', '560068']; // pin codes you serve
const EMAIL = 'you@example.com';
const LIMIT = 0.10; // alert if more than 10% of postal-code cost is outside ALLOWED

function main() {
const rows = AdsApp.search(
'SELECT segments.geo_target_postal_code, metrics.cost_micros ' +
'FROM geographic_view ' +
'WHERE segments.date DURING LAST_7_DAYS AND metrics.cost_micros > 0'
);

const costByCode = {};
while (rows.hasNext()) {
const r = rows.next();
const code = r.segments.geoTargetPostalCode;
if (!code) continue;
costByCode[code] = (costByCode[code] || 0) + Number(r.metrics.costMicros);
}

let total = 0;
let outside = 0;
const offenders = [];

for (const resource in costByCode) {
const name = lookupName(resource);
const cost = costByCode[resource] / 1000000;
total += cost;
if (!ALLOWED.includes(name)) {
outside += cost;
offenders.push(name + ': Rs ' + cost.toFixed(0));
}
}

const share = total > 0 ? outside / total : 0;
const pct = (share / 0.01).toFixed(1); // share as a percentage
Logger.log('Outside share: ' + pct + '%');

if (share > LIMIT) {
MailApp.sendEmail(
EMAIL,
'Google Ads: ' + pct + '% of spend outside your pin codes',
'Top areas outside your list:\n' + offenders.slice(0, 15).join('\n')
);
}
}

function lookupName(resource) {
const q = AdsApp.search(
"SELECT geo_target_constant.name FROM geo_target_constant " +
"WHERE geo_target_constant.resource_name = '" + resource + "'"
);
return q.hasNext() ? q.next().geoTargetConstant.name : resource;
}
How to use it
Paste the script, edit ALLOWED and EMAIL, click Preview and read the logs.
Schedule it weekly.
When an alert arrives, open campaign settings and check that Location options is set to Presence, then add the offending areas as exclusions.
Things the script can't fix

Google describes location targeting as a best effort, so a few stray clicks will always appear. Very small targets may barely serve (no radius under 1 km is allowed). And a perfect pin code list still loses money if the click lands on a generic home page; pair it with a page made for the ad. Here's a plain explanation of why ads need a landing page.

The marketing-side walkthrough (the manual five-minute version) is on Blogger. If you work with healthcare clients, location discipline matters even more; this is how BMU structures Google Ads for clinics and doctors.

原文:https://www.a8q9.cn/en/article/a-google-ads-script-that-emails-you-when-clicks-come-from-the-wrong-pin-62017f.html

相关文章