Welcome back to our Intro to AdWords scripts series where we’re training you to automate your account management using scripts.
Landing pages with broken URLs can wreak havoc in your AdWords account.
Whether the links have moved or were inputted incorrectly from the start, the outcome is the same: you’re paying for dead-end clicks.
This week’s article will be an introduction to automated destination URL checking in your AdWords account.
We’ll go through a basic URL checking script and explain what’s going on, line by line.
This article will gear you up to write your own URL checker or prepare you to customise either the script below or the advanced URL checking scripts we’ll be giving you next time.
What is a broken URL?
Whenever you navigate to a website via its URL, the website will give you a response code. The response code is a three-digit number that the website uses to tell your browser how it’s doing.
The code can let you know everything is working fine (200) or if what you’re looking for can’t be found: the dreaded 404.
Check the link at the bottom of the article for a list of all response codes and what they mean.
The image below shows a script for scanning the URLs in your account. The script works by iterating through your keywords and getting their associated destination URLs.
This URL can be set at the ad or keyword level. In our script we’ve chosen to show you how to get the URLs from keywords but if your destination URLs are associated with your ads instead, don’t worry – it’s easy to change in the script.
Once a list of URLs in the account has been compiled, the response code they give can be checked.
Anything that doesn’t give a whitelisted response code (200, as well as 301 and 302, which signify redirects) will be considered broken and emailed out to an account manager and other interested parties to investigate.
Now let’s go through the script line by line:
- Line 1: We start as usual with function main().
- Line 3: This array contains the values of whitelisted URL response codes. A code of 200 means everything is working fine. A 301 or 302 code means the page you’re looking for has been moved and any users will be redirected to the correct page.
- Line 4: This is the string containing the recipients of the broken URLs email. To add more addresses, just separate them out using commas inside the quote marks. To have just one email, wrap one email address in quote marks, e.g. “userA@example.com”.
- Lines 7-8: Here we’re creating two arrays to house URLs. The array urls will contain a list of all the unique URLs in the AdWords account and badUrls will have a list of all the URLs which didn’t return an acceptable response code. These are the URLs which will be emailed out at the end of the script.
- Lines 10-14: Here we construct a keyword iterator. The selector picks keywords with active keywords in active campaigns within enabled ad groups filtered using the withCondition method.
To construct an iterator which will get ads and not keywords, change AdWordsApp.keywords() to AdWordsApp.ads().
- Line 15: The while loop combined with the iterator’s hasNext method is the standard way to loop through the keywords.
- Line 16: Create a keyword object in the variable keyword.
- Line 17: The getDestinationUrl method will return the destination URL associated with the keyword.
- Line 18: The if condition is just a check to make sure that the URL actually exists before continuing.
- Line 19: Many people use tracking parameters in their URLs. The tracking parameters can be used for various tasks such as signalling the source of the traffic to your database. The parameters are placed after a URL and separated using a question mark, ?.Tracking parameters are unnecessary when checking if a URL is functioning correctly and can even cause errors, so it’s best practice to remove them here.
The split method of a string is used to split the string up at the specified parameter and returns the different portions as an array.
Choosing the question mark as we do on line 19 splits the string into an array with two elements. Element zero contains the URL so we specify it by writing . Take a look at the picture below to better understand what’s going on.
- Lines 20-21: This if condition is here to make sure we have a list of unique URLs in the account. For larger accounts it can take a lot of time to check over all the URLs. By making sure you are only checking each URL once your scripts will run much more quickly.
Here’s what’s happening in plain English: the if condition asks if the current URL in url is contained within the array urls. If the URL is absent (indexOf returns -1) then the if condition is satisfied and the URL is pushed to the array on the next line.
If, however, the URL was already contained in the array, then the code will skip the if condition.
- Line 26: Set up the URL checking options.
- Line 29-41: We start a for loop to go through all the unique URLs that we put into the urls array. We’ll check the urls one by one.
- Line 30: Here we’re introducing something new: the try-catch. If the code executed in the try block has an error, rather than bringing the script to a halt, the script jumps to the catch and executes the code contained there instead. This part of the script is useful for containing errors and not letting them stop your scripts mid way.
- Line 31: AdWords has a collection of tools which you can find as the methods belonging to the Utilities object (see end of article). One of these methods lets you fetch a web page. Within this for loop we’re fetching all our URLs and putting the results in the response variable.
- Line 32: Once fetched we can get data from our web page. The getResponseCode method will give us the response code we’re after to evaluate the status of the page. We stick the response code in our code variable.
- Lines 37-40: We check code is acceptable by comparing it to the array of valid codes defined at the top of the script. If not we’ll put the URL in the bad_urls array.
- Lines 43+: After an initial check to make sure bad_urls isn’t empty (to make sure that broken URLs have actually been found), an email is sent containing the details in the body of the message and in an attachment.
And that’s the end. Give yourself a pat on the back for making it this far. It was pretty hard work!
Our next article will be a little different. Rather than explaining a script line by line, we’ll be giving you a URL checking script to copy and paste into your account. It will be a more advanced version of what we’ve seen today, being able to handle Kenshoo URLs and will run at the MCC level.
Finally, two other handy links for you: