-
Notifications
You must be signed in to change notification settings - Fork 1
/
single_account_prepare_data.js
82 lines (74 loc) · 2.87 KB
/
single_account_prepare_data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* _ _ __ __ _
* __ _ __| | |_ _ __ __ _ / _|/ _(_) ___
* / _` |/ _` | __| '__/ _` | |_| |_| |/ __|
* | (_| | (_| | |_| | | (_| | _| _| | (__
* \__,_|\__,_|\__|_| \__,_|_| |_| |_|\___|
*
* E: [email protected] | W: www.adtraffic.de
*
* Search Query Deduplicator 22.1
*
* Single-Account-Version: Prepare Data Script (1/2)
*
* @desc: Use this Google Ads Script to identify all
* search terms that do not match in the intended ad
* group and automatically exclude them.
*
* @author: @ric_sti (Twitter)
* @version: 1.0
* @github-slug: google-ads-scripts-search-query-deduplicator
*
*/
const sheetFileId = '{{Search Query Deduplicator Google Sheet ID}}';
function main() {
const accountId = AdsApp.currentAccount().getCustomerId();
const ocid = '{{ocid}}';
let sheetStatus = prepareSheet(sheetFileId,accountId);
let activeSearchTerms = getActiveSearchTerms(ocid);
writeData(activeSearchTerms,sheetFileId,accountId)
}
// FUNCTIONS
function prepareSheet(sheetFileId,accountId){
var ss = SpreadsheetApp.openById(sheetFileId);
if (!ss.getSheetByName(accountId)){
let templateSheet = ss.getSheetByName('templateSheet');
ss.insertSheet(accountId, {template: templateSheet});
return 2;
} else {
let sh = ss.getSheetByName(accountId);
let lastRow = sh.getLastRow();
let lastColumn = sh.getLastColumn();
sh.getRange(2, 1, lastRow, lastColumn).clear();
return 1;
}
}
function getActiveSearchTerms(ocid){
var activeSearchTerms = [];
const query = 'SELECT search_term_view.search_term,segments.search_term_match_type,metrics.impressions,metrics.clicks,metrics.conversions,campaign.id,campaign.name,ad_group.id,ad_group.name,ad_group.type FROM search_term_view WHERE campaign.status IN ("ENABLED") AND ad_group.status IN ("ENABLED") AND ad_group.type IN ("SEARCH_STANDARD") AND segments.search_term_match_type NOT IN ("EXACT","NEAR_EXACT") AND metrics.impressions > 30 AND segments.date DURING LAST_30_DAYS ORDER BY metrics.impressions DESC'
let search = AdsApp.search(query);
while (search.hasNext()) {
let row = search.next();
let googleAdsDeeplink = '';
if (ocid.length > 0) {
googleAdsDeeplink = 'https://ads.google.com/aw/keywords/searchterms?campaignId='+row.campaign.id+'&adGroupId='+row.adGroup.id+'&ocid=' + ocid;
}
activeSearchTerms.push([
row.searchTermView.searchTerm,
row.segments.searchTermMatchType,
row.campaign.id,
row.adGroup.id,
row.campaign.name,
row.adGroup.name,
googleAdsDeeplink,
row.metrics.impressions,
row.metrics.clicks,
row.metrics.conversions
]);
}
return activeSearchTerms;
}
function writeData(data,sheetFileId,sheetName){
if (data.length === 0) { return; }
var sheet = SpreadsheetApp.openById(sheetFileId).getSheetByName(sheetName);
sheet.getRange(2, 1, data.length, data[0].length).setValues(data);
}