If you’re running Google Ads campaigns, you know how important it is to keep your ads up-to-date and relevant. But what happens when you have events or holidays that affect your business? Manually updating your ads can be time-consuming and error-prone. That’s where my Google Ads script comes in!

My script reads events from a Google Calendar, matches them to predefined label mappings, and then enables or disables ads based on whether the event is an all-day event or not. Here’s how it works:

First, you’ll need to set up your Google Calendar and label mappings. The label mappings are simply a list of calendar names and corresponding label names. For example, you might have a calendar called “Ramadhan” and a label called “ramadhan”. The script will use these mappings to determine which ads to enable or disable.

function main() {
    var calendarId = 'your_calendar_id'; // replace with your calendar ID
    var labelMappings = [
        {
            calendarName: 'Ramadhan', // name of the calendar event
            adsLabelName: 'ramadhan' // name of the AdWords label to apply
        },
        {
            calendarName: 'New Year',
            adsLabelName: 'new_year'
        }
    ]; // replace with your own label mappings
    var timeZone = 'America/Los_Angeles'; // replace with your time zone

    var calendar = CalendarApp.getCalendarById(calendarId); // get the calendar by ID
    var now = new Date(); // get the current date and time
    var startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate()); // set the start of the day to the current date at midnight
    var endOfDay = new Date(startOfDay.getTime() + 24 * 60 * 60 * 1000); // set the end of the day to the start of the day plus 24 hours
    var events = calendar.getEvents(startOfDay, endOfDay); // get all events for the current day

    for (var i = 0; i < events.length; i++) { // loop through each event
        var event = events[i]; // get the current event
        var eventName = event.getTitle(); // get the name of the event
        var mapping = getMappingForEvent(eventName, labelMappings); // get the label mapping for the event
        if (mapping) { // if a label mapping exists for the event
            var adsLabel = getOrCreateAdsLabel(mapping.adsLabelName); // get or create the AdWords label
            var ads = AdWordsApp.ads().withCondition("LabelNames CONTAINS '" + mapping.adsLabelName + "'").get(); // get all ads with the label
            while (ads.hasNext()) { // loop through each ad with the label
                var ad = ads.next(); // get the current ad
                if (event.isAllDayEvent()) { // if the event is an all-day event
                    ad.enable(); // enable the ad
                    Logger.log('Enabled ad ' + ad.getId() + ' with label ' + mapping.adsLabelName); // log that the ad was enabled
                } else { // if the event is not an all-day event
                    ad.disable(); // disable the ad
                    Logger.log('Disabled ad ' + ad.getId() + ' with label ' + mapping.adsLabelName); // log that the ad was disabled
                }
            }
        }
    }
}

function getMappingForEvent(eventName, labelMappings) { // function to get the label mapping for an event
    for (var i = 0; i < labelMappings.length; i++) { // loop through each label mapping
        var mapping = labelMappings[i]; // get the current label mapping
        if (mapping.calendarName == eventName) { // if the label mapping matches the event name
            return mapping; // return the label mapping
        }
    }
    return null; // if no label mapping is found, return null
}

function getOrCreateAdsLabel(name) { // function to get or create an AdWords label
    var labelIterator = AdWordsApp.labels().withCondition("Name = '" + name + "'").get(); // get the label by name
    if (labelIterator.hasNext()) { // if the label exists
        return labelIterator.next(); // return the label
    } else { // if the label does not exist
        return AdWordsApp.labels().create(name); // create the label and return it
    }
}

Next, you’ll need to run the script. The script will retrieve events from the current day and match them to the label mappings. For each event that matches, the script will enable or disable the corresponding ads based on whether the event is an all-day event or not.

The benefits of using this script are clear. It saves time and reduces the risk of errors, allowing you to focus on other aspects of your business. It also allows you to keep your ads relevant and up-to-date even during busy periods, such as holidays or events.

You’ll also need to customize the label mappings to match your own calendars and ads. Once you’ve done that, simply run the script and let it do the rest! By automating the process of enabling and disabling ads based on events, you can save time and reduce the risk of errors.