What is an Iterator?
In my past life, I used to make money playing vinyl records in crowded places. Part of my job was curating hundreds of newly released records each week. I loved it. Every Thursday and Friday I would enter the record store, select ~50 records from the new arrivals, and start listening to each one.
Let’s imagine you have a stack of vinyl in front of you, and you want to listen to each, one by one. Instead of dumping all the records on the Technics SL-1200 at once, you pick up an individual record from the top, put the needle on the record…scan the track…decide if you want to play it that night, and then move on to the next record. An iterator is like your hand that helps you pick each record from the stack, one at a time.
In the world of coding, an iterator is a special tool that allows us to go through a collection of items (like records on a stack) one at a time. This collection could be anything. Examples from our world of Google Ads Scripts include : a set of Google Ads campaigns, a list of keywords, a group of ads, or a bunch of sitelinks .
Key Points about Iterators:
- Step-by-Step Access: An iterator helps you access items one at a time.
- No Need for All at Once: You don’t need to handle all items at once; you can focus on one item at a time.
- Keeps Track: It remembers where you are in the collection so you can continue from where you left off.
Example: Campaign Iterator in Google Ads
Let’s say you have several advertising campaigns in your Google Ads account, and you want to look at each one to check its status. A campaign iterator can help you do this easily. Here’s how it works:
- Create a Selector: First, you ask Google Ads to give you a Selector that selects the campaigns you are looking for.
- Create an Iterator based on the Selector .
- Use the Iterator: Then, you use the iterator to go through each campaign, one by one.
Pseudo-code Example:
Here’s a simple example to illustrate this:
- Create the Iterator:
This line of code asks Google Ads to give you a Selector that returns all campaigns , and the give you an iterator for the selected campaigns. Read more about Selectors here.
- Use the Iterator:
while (campaignIterator.hasNext())
: This checks if there are more campaigns to look at.var campaign = campaignIterator.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().get()
: Gets an iterator for all campaigns in your account.while (campaignIterator.hasNext())
: Checks if there are more campaigns to process.campaignIterator.next()
: Retrieves the next campaign.Logger.log(campaign.getName())
: Prints the name of each campaign to the log.
Summary
An iterator is like a helpful hand that lets you go through items in a collection one by one. In the context of Google Ads, a campaign iterator allows you to go through your Google Ads entities, like campaigns, one at a time, making it easier to manage and analyze them without handling everything at once.
I hope this helps, happy scripting!