How to Automate Ads Creation in Google Ads using PHP- and XML-feedsHow to
Every job has its routine tasks that take a lot of time and efforts. Fortunately, in most IT specialties, they can be automated to work more productively. And PPC is no exception.
In this post, you'll learn how to automate the creation of campaigns, groups, ads and keywords for Google Ads account.
1. Automation Types
Google Ads has 2 types of automation that need programming languages:
- Google Ads Script
- Google Ads API
Scripts allow you to conveniently automate part of the processes and help with work, but they are not powerful enough, especially in technical terms. The thing is that:
- Google Ads scripts for advertisers' accounts can run for no more than 30 minutes, after that they will be canceled. All changes made before the cancellation of the script will be applied.
- Ads Manager scripts can usually run no more than 30 minutes, after which they will be canceled. However, if the Ads Manager script uses the executeInParallel method for parallel processing of user accounts and specification of a callback method, it can take no more than 60 minutes, after which it gets canceled. You can see it at the illustration below, where processAccount is a parallel function, and allFinished is a callback method when calling executeInParallel.
1.1. Google Ads scripts
Keep in mind that according to Google Ads scripts reference:
- A single iterator will return at most 50,000 entities (keywords, ads, ad groups, or campaigns). iterator.hasNext() will return false after that, and a warning will be logged.
- A single selector can handle at most 10,000 IDs in selector.withIds(). If 10,000 or more IDs are specified, selector.get() will throw a runtime error. Similarly, specifying an Id IN [LIST] condition with a list of IDs > 10,000 will result in a runtime error.
- A single script can process at most 250,000 entities of all types. iterator.hasNext() will return false afterwards for any iterator, and a warning will be logged.
- A single script can create 250,000 keywords and ads at the most. Successive attempts to create entities will fail, and a warning will be logged.
1.2. Scripts of Google Ads manager account
- Each account processed by an Ads Manager script gets its own quota as listed above.
- When using executeInParallel method, a script can process up to 50 accounts.
Simply put, no matter how hard you or your programmer tries, you will have only 30 minutes to run the script. After the first minute, the script can stop working, and you will not know about it.
1.3. How to Work with Google API
API (application programming interface) is a description of methods (a set of classes, procedures, functions, structures, or constants) using which one computer program can interact with another program.
Automation with the Google API gives much more features:
- The limits are much higher when you work with the API.
- There is no 30-minute work limit. If it's done properly, the script can work day and night.
- More operations when working with the API: 10,000 operations per day or even more depending on the access level.
- You can work with all sorts of databases: MySQL, Postgresql, NoSQL.
- Multitasking: the same script will perform several tasks at once.
- You can interact with multiple APIs in one place: for example, you can connect Google Ads and Google Analytics, and immediately receive data from both places at the same time.
1.4. How to Automate Processes
You can automate processes in two ways. The first one is to use the source code that immediately creates / changes data in the account. It is important that all data in this case is written directly to the code. This makes it difficult to read and change the code, and reduces operation speed.
The second one is to use such storages as .csv, .txt files or database as data. You can also use scraping of XML feed from a website or even scrape a web page (HTML code) using site spider software. This allows customizing the code to use data from different sources and save any intermediary results in a convenient format.
2. Scraping and How to Use It
Warning: if the information above seems too complicated for you, the next part will be even more complicated. So may the force be with you, young padawans of automation.
Scraping (of something) is sorting and structuring data according to certain parameters and extracting only the information you need.
Let's find out what can be scraped and what is not.
You can scrape:
- CSV, XLS (XLSX) files,
- TXT files,
- HTML code***,
- XML code (text), well-known product feeds,
- And even images.
Why there are 3 stars next to the HTML code? Because not all HTML code can be easily scraped. For this you need an experienced developer, because now there are many ways to create a website. It may be:
- normal static HTML code
- dynamic code formed from scratch by any CMS system
- website created with Angular, React, Vue.js technologies. There is a completely different code building logic, so the usual scraping tools will not work.
In this post, we will work with XML files.
XML is a document with requirements for the basic concepts inside and a set of recommendations for the data placed in it. That's why services such as Google Ads or Google Merchant have their own document structure.
Scraping of such document is the key to automation of some tasks. XML contains, for example, all products from the website. There may be 10,000+ products. How long does it take for a person to create 10,000 ad feeds? So you've got what I mean.
So how can you scrape an XML document, and who can help with this? The best option, of course, is to have a developer who knows one of such languages as PHP, Java, C #, Ruby, Python. Any server-side programming language can help you with it.
In any language, this task will sound something like this: open a file, scrape all the data, then work with the Google Ads API and 'stuff' your data into Google to get the benefit.
3. How to Automate Correctly: Preparations
Now let's make clear goals and tasks that can be automated in each team of PPC specialists. In order not to discuss abstract things, let's take the real task of the Penguin-team. Of course, some of your points may differ in your own tasks.
Automatic creation of campaigns, ad groups and keywords, and ads based on the site's XML feed.
- Scrape XML files of 10–20 thousand products in each.
- Create campaigns based on product categories.
- Create ad groups based on sales offers for the product (at the same time the product on the site has several options that differ in color, size).
- Create keywords for these groups based on product brand, model and color.
- Create and update ads based on the product *, so you need to deliver data from the product (price, name, brand).
*Ad based on the product – this can be both a product ad and a simple search ad, the main thing is that the product is the data source.
This is how a minimal technical task for a developer may look. It will allow to see every step and every goal in the automation process.
Business logic can be different for everyone. For example, you can divide your account into campaigns not by product category, but by brand or model. These nuances are different for each PPC-specialist and don't affect the specifics of automation.
I also want to give some technical comments so you'll know what you may face as a customer.
If we look at these tasks from a technical point of view, we'll see two main points that can influence the way of development. It is the scraping of large feeds and the modification of already created ads.
- The first task is scraping. Files for 10-20 thousand products are too large to be quickly sent and read on the fly. So you can, for example, divide the file read and write operations to your Google Ads account, because otherwise you can go beyond the technical time frame of the programming language, or such operations can consume huge amount of RAM on your server.
- The second task is to change already existing ads in your account. It sounds simple enough, even technically, but in fact there is a nuance. Unlike groups and campaigns (which have not only the ID number, but also the name), the ad has no name. It means that you can get it only with the help of the ID number. But if your script will work once a month, how will it know the ID of each ad in the system the second time? So it needs to be saved somewhere. This will help us to divide all tasks into separate ones and to have access to any ad at any time of the day in the future.
Because of these technical peculiarities, you need to use a database.
The database gives huge advantages in work:
- you can store all the products from the feed,
- monitor products for which no ads were created,
- see a list of created campaigns, groups and the ads,
- update some ads if necessary.
So it can look like this:
This plan shows how to structure and store data. As a result, in our example, we have clearer chain of actions for the developer:
Scrape products from feed → on their basis we create campaigns and groups → create ads → assign to groups
This is a short work plan which can help you develop the task you need and start implementing it.
4. Practical Part of Automation
If you are ready for productive work, let's move on to the practical part.
Attention! Further text contains instructions on the use of technical tools for automation. Without PHP knowledge, reading is not recommended – it is bad for karma and well-being.
In this part, I will show you how to interact with the Google Ads API using the PHP language, the standard Google library and the programmer hypothalamus.
The Google API gives you access to its libraries, which are on developers.google.com and are freely distributed via Github. My advice – do not reinvent the wheel and look for ready-made solutions from experts. This will save your time and more likely bring excellent results, which have been tested by specialists.
We will use the PHP language library.
The overall technology stack is as follows:
- PHP v7.0,
- Apache 2.4,
- MySQL v5.7,
- PHP Slim framework 3,
- Google Ads API PHP Library.
Important: for the work, you need to create a developer token. Read more about this in the post 'Obtain Your Developer Token'. Without this, it is impossible to move further, so study the documentation carefully and create a token for the future application. Obtaining a token must be done along with the programmer, you should not do it yourself, since there are a lot of technical nuances there.
After that you need to customize the application for yourself.
It must be done in two files:
- Core/Config.php – here we register access to the database.
- App/models/google/v1/GoogleAuth.php – here we specify tokens to work with the API and the customer ID of the account in which the task will be performed.
After that, the application is ready to query and receive data. It remains to disassemble the main logic and divide the application into modules.
Below is a UML class diagram.
The most important class for a programmer is the Account.php class. It contains all the main logic.
I will give a brief description of the class methods:
- public function prepareFile(Request $request, Response $response, $args) – the method is responsible for scraping the XML file by the specified link. It adds new products to the database and updates existing ones if the price has been changed in the feed.
- public function checkProducts(Request $request, Response $response) – the method selects all products for which no ads have been created from the database, and starts checking. If there is no campaign for a product, the system creates it. Next, it checks the group and if it doesn't exist, it creates the group. The third step is to create an ad and attach it to a previously created group. After successful execution, the system marks the product as used so that it does not use it during the next iteration.
- public function createCampaign($session, $campaignName, $budgetService, $adWordsServices) – creates campaigns based on the specified parameter.
- public function createAdGroup($adGroupService, $adwordsItem, $CampaignID) – creates ad groups according to the parameters you specified.
- public function createAd($session, $adWordsServices, $adGroupAdService, $adwordsItems, $GroupID) – creates ads with customizable options.
- public function createKeywords($adGroupCriterionService, $adGroupId, $productsGroup) – adds keywords to an ad group.
Important: this tool is not a universal ready-made solution. It needs to be customized for each site. But for its work you just need to specify the minimum set of parameters.
This case is one of the many possible automation processes. It is suitable for stores with a large product range, but you still need to adapt it to your needs, based on your case.
- The post is complex, it can help PPC specialists to understand the basic concepts and set the task for the programmer more clearly.
- The whole practical part is a detailed instruction that you can pass to the programmer and then it is up to him.
Automate your work, make technology work for you, and then your productivity and expertise will grow.
If you have questions about the script, write them in the comments below. I'll be happy to answer and tell you how to organize the process in the best way ;)