Deutsche Version: LIESMICH.md
Use this Google Ads Script to identify all search terms that do not match in the intended ad group and automatically exclude them.
- Installation
- First execution & overview of the columns in the sheet
- Planning / regular execution
- Function descriptions
- Function parameters
Open your Google Ads account or MCC and navigate to "Tools and Settings > Bulk Actions > Scripts".
The Search Query Duplicator is divided into two scripts.
The first script prepares the data for further processing and writes it to a Google Sheet.
Setup:
Create a new Google Ads script (suggested name: "SQ Deduplicator 22.1 Prepare Data") and overwrite the content with the appropriate code template:
- Single account: single_account_prepare_data.js
- MCC version: mcc_version_prepare_data.js
The second script processes the unprocessed rows from the Google Sheet and writes the processing status back to the respective data row.
Setup:
Create a new Google Ads script (suggested name: "SQ Deduplicator 22.1 Process Data") and overwrite the content with the appropriate code template:
- Single account: single_account_process_data.js
- MCC version: mcc_version_process_data.js
A Google Sheet is used for the temporary storage and further processing of the search terms. You can copy the template with the following Google Drive link: adtraffic Search Query Deduplicator Google Sheet Template
Paste the ID of your newly created Google Sheet into both scripts at the position of "{{Search Query Deduplicator Google Sheet ID}}".
In order to generate direct links to the Google Ads ad groups, the "ocid" parameter for the respective Google Ads account is required.
The "ocid" parameter can be read from the response of the BatchService at MCC level for all child accounts. To make the response available as an object in the script, proceed as follows:
- Open the relevant MCC and navigate to "Accounts > Performance"
- Open the browser's developer tools and select the "Network" tab
- Reload the page and filter the entries with "batch account"
- Click on the entry that starts with "Batch?authuser=" and select "Response" on the right
- Copy the entire content of the response and paste it in Script 1 - "SQ Deduplicator 22.1 Prepare Data" at the porsition of "{{BatchService Response}}".
Alternatively:
Remove the placeholder "{{BatchService Response}}", leaving the following content in the line:
let batchAccountSnippet = new Object();
---
For single accounts, the "ocid" parameter can be read from the address bar.
- Log in to the relevant Google Ads account and copy the digits after "ocid=" from your browser's address bar
- Paste the copied digits into Script 1 - "SQ Deduplicator 22.1 Prepare Data" at the postion of "{{ocid}}".
Alternatively:
Remove the placeholder "{{ocid}}", leaving the following content in the line:
const ocid = '';
---
Add the Account IDs of the accounts to be processed as comma-separated strings in both scripts at the postion of "{{Account IDs Array}}".
Example syntax:
'123-456-7890','456-123-7890','654-321-7890','321-654-7890'
Alternatively:
Remove the placeholder "Account IDs Array", leaving the following content in the line:
let accountIds = [];
When the Script 1 - "SQ Deduplicator 22.1 Prepare Data" is executed for the first time, a new worksheet with the respective account ID as the worksheet name with eleven columns is created in the Google Sheet for each processed account:
- searchTerm
- matchType
- campaignId
- adgroupId
- campaignName
- adGroupName
- deepLink
- impressions
- clicks
- conversions
- processed
The first six columns are self-explanatory.
The "deepLink" column contains a direct link to the ad group of the respective search term (if the "ocid" parameter is available - see Install Step III). The "impressions", "clicks" and "conversions" columns contain the respective values ​​for the queried period (by default the last 30 days).
The last column "processed" can contain six different return values ​​after executing the Script 2 - "SQ Deduplicator 22.1 Process Data":
Value | Meaning |
---|---|
Term skipped |  The search term does not match an actively booked keyword in the account. No negative keyword was created. |
Campaign Negative added |  The search term corresponds to an actively booked keyword in another campaign. A negative keyword was created in the campaign of the found search term. (*) |
Campaign Negative skipped |  The search term corresponds to an actively booked keyword in another campaign. However, no negative keyword was created in the campaign of the found search term because a corresponding negative keyword was already booked there. |
AdGroup Negative added |  The search term matches an actively booked keyword in another ad group in the same campaign. A negative keyword was created in the ad group of the found search term. (*) |
AdGroup Negative skipped |  The search term matches an actively booked keyword in another ad group in the same campaign. However, no negative keyword was created in the ad group of the found search term because a corresponding negative keyword was already booked there. |
Comparison failed |  At least one duplicate was found when processing the data from the sheet, but an error occurred when matching the campaignId. |
(*) Running the script in preview mode will not create negative keywords. In the script editor under "Changes" you can see in which campaigns or ad groups negative keywords would be booked.
By executing the Script 1 - "SQ Deduplicator 22.1 Prepare Data" all data in the Google Sheet will be overwritten. Running the script weekly should be sufficient for most accounts. For large accounts with several hundred newly identified search queries every day, daily execution makes perfect sense.
The Script 2 - "SQ Deduplicator 22.1 Process Data" only processes rows without a value in the "processed" column. A limit for the maximum number of lines to be processed can be defined (default: 1,000 lines) so that the script does not run into a timeout when processing a very large number of search terms. Script 2 should be planned to run enough times to process all of the identified search terms.
- main() ⇒
MCC version: Loads the accounts to be processed and starts the parallel execution of the script.
Single account version: Basically the same as the MCC function processAccount()
- processAccount() ⇒
Runs the Google Sheet preparation, then loads the active search terms and initiates the export of the aggregated search terms to the Google Sheet.
- prepareSheet() ⇒
Creates a new sheet for the account to be processed in the Google Sheet or deletes the data it contains if a sheet already exists for the respective account.
- getActiveSearchTerms() ⇒
Retrieves all search terms that did not match with EXACT or NEAR EXACT match type and generated a minimum number of impressions (30 by default) in the given time period (30 days by default).
- writeData() ⇒
Writes the prepared data to the Google Sheet.
- main() ⇒
MCC version: Loads the accounts to be processed and starts the parallel execution of the script.
Single account version: Basically the same as the MCC function processAccount()
- processAccount() ⇒
Runs the Google Sheet preparation, then loads the active search terms and starts writing the aggregated search terms to the Google Sheet.
- checkForDuplicates() ⇒
Based on the processed search term in all other ad groups, it searches for active keywords that exactly match the respective search term, initiates the creation of a keyword to be excluded if it is found and has the result of the processing written back to the Google Sheet.
- createCampaignNegative() ⇒
Creates an exact match negative keyword at the campaign level, using the processed search term as the keyword text.
- createAdGroupNegative() ⇒
Creates an exact match negative keyword at the ad group level, using the processed search term as the keyword text.
- readSheet() ⇒
Reads the data (without header) from the Google Sheet into an array.
- updateData() ⇒
Writes the result for each search term to the Google Sheet in the "processed" column of the respective row.
Parameter | Type | Description |
---|---|---|
sheetFileId | String | The ID of the Google Sheet to use. |
batchAccountSnippet (MCC Version only) |
Object | The content of the "Batch Service Response". (Required for automatic determination of the ocid parameter.) |
MCC version below.
Single account version see processAccount()
Parameter | Type | Description |
---|---|---|
accountIds | Â Array | Contains the account IDs of the Google Ads accounts to be processed. |
MCC version below.
Runs as main() in single account version
Parameter | Type | Description |
---|---|---|
accountId | Â String | The account ID of the processed account. |
ocid | String | Â The ocid parameter matching the processed account. (Required for creating Google Ads deep links.) |
sheetStatus | Integer | 2 = new spreadsheet was created. 1 = content was removed from existing spreadsheet. |
activeSearchTerms |  Array |  Return values ​​from getActiveSearchTerms() |
Parameter | Type | Description |
---|---|---|
sheetFileId | String | The ID of the Google Sheet to use. |
accountId | Â String | The account ID of the processed account. |
ss |  Spreadsheet Object | The Google Sheet to use |
templateSheet | Sheet Object | The empty sheet template within the Google Sheets used. (Used for creating new sheets for processed accounts.) |
Parameter | Type | Description |
---|---|---|
ocid | String | Â The ocid parameter matching the processed account. (Required for creating Google Ads deep links.) |
 activeSearchTerms | Array | Return values for processAccount() |
query | String | GAQL query to perform search for active search terms using AdsApp.search() |
 search |  AdsApp.SearchRowIterator | see AdsApp.SearchRowIterator |
googleAdsDeeplink | String | The Google Ads deep link to the ad group of the respective search term |
Parameter | Type | Description |
---|---|---|
data | Array | Return values from getActiveSearchTerms(). |
sheetFileId | String | The ID of the Google Sheet to use. |
sheetName | Â String | The account ID of the processed account. |
sheet |  Spreadsheet Object | The Google Sheet used. |
Parameter | Type | Description |
---|---|---|
sheetFileId | String | The ID of the Google Sheet to use. |
MCC version below.
Single account version see processAccount()
Parameter | Type | Description |
---|---|---|
accountIds | Â Array | Contains the account IDs of the Google Ads accounts to be processed. |
MCC version below.
Runs as main() in single account version
Parameter | Type | Description |
---|---|---|
accountId | Â String | The account ID of the processed account. |
sheet |  Spreadsheet Object | The Google Sheet used. |
 existingValues |  Array | All data rows (without header) of the processed account spreadsheet. |
 filteredValues | Array | Filtered rows from existingValues ​​with no values ​​in column "processed". |
count | Â Integer | Auxiliary variable for counting the rows already processed |
status | String | Return value from checkForDuplicates(). (See Return values ​​from Script 2) |
Parameter | Type | Description |
---|---|---|
rowData | Array | Input values ​​from processAccount(). (The row to process from filteredValues.) |
accountId | Â String | The account ID of the processed account. |
query | String | GAQL query to search for already booked keywords in other ad groups using AdsApp.search() |
 search |  AdsApp.SearchRowIterator | See AdsApp.SearchRowIterator |
Parameter | Type | Description |
---|---|---|
term | Â String | Keyword text to create the negative keyword. |
campaign | Â Integer | ID of the campaign in which to create the negative keyword. |
Parameter | Type | Description |
---|---|---|
term | Â String | Keyword text to create the negative keyword. |
adgroup | Â Integer | ID of the ad group in which to create the negative keyword. |
Parameter | Type | Description |
---|---|---|
sheet |  Spreadsheet Object | The Google Sheet used. |
Parameter | Type | Description |
---|---|---|
sheet |  Spreadsheet Object | The Google Sheet used. |
index |  Integer | Relative row number of the processed search term in the spreadsheet |
status | String | Return value from checkForDuplicates(). (See Return values ​​from Script 2) |