Google Ads Scripts 101 – What is a Selector?

What is a Selector?

Every year, me and a couple of motorcycle buddies fly into the West for a motorcycle road trip. For that, we need motorcycles. And I don’t mean just any motorcycle. We need sporty bikes. Not the Harley Davidson like choppers, not the big fat BMW 1200’s…no, we are picky. We want to rent Ducati’s and the likes.

Now, imagine you’re on a rental website with thousands of motorcycles, and you want to find a specific type of motorcycle, like “900cc”.  Instead of searching through every motorcycle manually, you use a catalog or a search tool to help you find the bikes you’re interested in. This search tool helps you filter and select only the motorcycles that match your criteria.

In the world of coding and Google Ads, a Selector is like that search tool. It helps you filter and choose specific items (such as campaigns, ads, keywords, etc.) from a large collection based on certain criteria.

Key Points about Selectors:

    1. Filtering: Selectors help you filter items based on specific conditions (e.g., campaigns with a certain status or performance metrics).
    2. Targeted Selection: You get only the items you are interested in, without having to go through the entire collection manually.
    3. Efficiency: Using selectors makes your code more efficient and easier to manage.

Example: Campaign Selector and Iterator in Google Ads

Let’s say you want to look at all the active campaigns in your Google Ads account. You can use a selector to filter and get only the active campaigns, and then use an iterator to go through each campaign one by one.

How It Works:

    1. Create the Selector: You set up the selector to define the criteria for selecting campaigns (e.g., status is active).
    2. Create the Iterator: You use the selector to get an iterator that allows you to go through each selected campaign.

Pseudo-code Example:

Here’s a simple example in pseudo-code to illustrate this:

Create the Selector:

This line of code sets up a selector to get all campaigns with the status “ENABLED” (i.e., active campaigns).

Use the Iterator:

    • while (campaignSelector.hasNext()): This checks if there are more campaigns to look at.
    • var campaign = campaignSelector.next(): This gets the next campaign.
    • Logger.log(campaign.getName()): This prints the name of the campaign.

Putting It All Together

Here’s the complete example in a Google Ads script:

Explanation:

    • AdsApp.campaigns(): Starts the process of creating a selector for campaigns.
    • .withCondition("Status = 'ENABLED'"): Adds a condition to the selector to filter only active campaigns.
    • .get(): Retrieves the iterator for the selected campaigns.
    • while (campaignSelector.hasNext()): Checks if there are more campaigns to process.
    • campaignSelector.next(): Retrieves the next campaign.
    • Logger.log(campaign.getName()): Prints the name of each campaign to the log.

Summary

A selector is like a search tool that helps you find and filter specific items based on certain conditions. In Google Ads scripts, you can use selectors to filter campaigns, ads, keywords, and more, making it easier to manage and analyze your advertising efforts.

An iterator is then used to go through the selected items one by one, allowing you to perform actions on each item, such as logging their names or adjusting their settings.

NOTE:
Unfortunately, only search and display campaigns are supported in the standard campaign selector.
or other campaign types you should use the specific selectors (such as ShoppingCampaignSelector for shopping ,  VideoCampaignSelector for video campaigns, and PerformanceMaxCampaignSelector for Performance Max campaigns).

 

I hope this helps, happy scripting!