function main() { // Settings var campaignName = "Campaign Name"; var metricsToTrack = ["Clicks", "Conversions", "Cost"]; var changeThreshold = 0.2; // 20% increase or decrease var comparisonPeriod = "LAST_7_DAYS"; // Timeframe for comparison var recipientEmail = "your_email@example.com"; // Get the campaign var campaign = AdsApp.campaigns().withCondition("Name = '" + campaignName + "'").get().next(); // Fetch performance data for the current and previous periods var currentStats = campaign.getStatsFor("TODAY"); var previousStats = campaign.getStatsFor("PREVIOUS_" + comparisonPeriod); // Analyze each metric for (var i = 0; i < metricsToTrack.length; i++) { var metric = metricsToTrack[i]; var currentValue = currentStats.get(metric); var previousValue = previousStats.get(metric); var changePercentage = (currentValue - previousValue) / previousValue; // Send alert if the change is significant if (Math.abs(changePercentage) >= changeThreshold) { var direction = changePercentage > 0 ? "increased" : "decreased"; var subject = "Performance Alert: " + campaignName; var body = metric + " has " + direction + " by " + (changePercentage * 100).toFixed(0) + "% compared to the previous " + comparisonPeriod.toLowerCase() + "."; MailApp.sendEmail(recipientEmail, subject, body); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter