Anomaly Script Error

26 views
Skip to first unread message

John Calle

unread,
Jun 20, 2024, 8:44:12 PM (12 days ago) Jun 20
to Google Ads Scripts Forum
I am running a script that should sent an alert email notification when CPC's drop by a certain percentage.

I am getting this error:

ReferenceError: CONFIG is not defined (line 1)
Here is the code I am working with:

function main() {
  let metric = 'CPC'; // Tracking CPC
  let threshold = 0.3; // 30% decrease threshold
  let campaignContains = []; // Example filter
  let campaignDoesNotContain = []; // Example negative filter
  let emailRecipient = "jo...@talmarket.com"; // Email for alerts

  var yesterdayCPC = getCPC('YESTERDAY', campaignContains, campaignDoesNotContain);
  var lastWeekCPC = getCPC('LAST_WEEK', campaignContains, campaignDoesNotContain);
  var difference = Math.abs((lastWeekCPC - yesterdayCPC) / lastWeekCPC);

  if (difference > threshold) {
    sendEmail(metric, threshold, emailRecipient, difference);
  }
}

function sendEmail(metric, threshold, email, difference){
  MailApp.sendEmail(email, "Google Ads Script Threshold Met",
                    "Yesterday's Google Ads CPC changed by more than " +
                    (threshold * 100).toString() + "% week-on-week. The change was " +
                    (difference * 100).toString() + "%");
}

function getCPC(period, positiveFilter, negativeFilter) {
  var query = 'SELECT campaign.name, metrics.cost_micros, metrics.clicks ' +
              'FROM campaign ' +
              'WHERE segments.date DURING ' + period;

  if (positiveFilter.length > 0) {
    positiveFilter.forEach(filter => {
      query += ' AND campaign.name LIKE "%' + filter.replace(/"/g, '\\"') + '%" ';
    });
  }

  if (negativeFilter.length > 0) {
    negativeFilter.forEach(filter => {
      query += ' AND campaign.name NOT LIKE "%' + filter.replace(/"/g, '\\"') + '%" ';
    });
  }

  const report = AdsApp.report(query);
  var reportIter = report.rows();
  var totalCost = 0;
  var totalClicks = 0;

  while (reportIter.hasNext()) {
    var row = reportIter.next();
    totalCost += parseFloat(row['metrics.cost_micros']) * 0.000001;
    totalClicks += parseFloat(row['metrics.clicks']);
  }

  var cpc = totalClicks > 0 ? totalCost / totalClicks : 0;
  return cpc;
}

Google Ads Scripts Forum

unread,
Jun 21, 2024, 8:29:48 AM (12 days ago) Jun 21
to Google Ads Scripts Forum

Hi,

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

I would like to inform you that there is no CONFIG variable in the code you have provided. In order to replicate your issue at our end, kindly provide us with the below details.

  • Google Ads account ID/CID
  • Name of the affected script
  • Shareable spreadsheet link if you are using in your script

You can share the requested details via Reply privately to the author option or a direct private reply to this email.

Thanks,
Google Ads Scripts team
Reply all
Reply to author
Forward
0 new messages