Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/***********
* Find all the keywords that were active daysAgo
* Return those Ids in a mapping so we can easily filter other
* queries.
***********/
function getKeywordsActiveDaysAgo(daysAgo) {
// This will be a mapping of Ids we will return
var keyword_ids = {};
// We are only concerned with the Ids, so let's
// make this as small as possible.
var columns = ['ExternalCustomerId',
'CampaignId',
'AdGroupId',
'Id'];
var date_string = dateStringDaysAgo(daysAgo);
// Our date range is going to be a single day
var date_range = [date_string,date_string].join(',');
// A simple AWQL Query to grab active keywords from that day
var query_str = [
'SELECT',columns.join(','),
'FROM','KEYWORDS_PERFORMANCE_REPORT',
'WHERE Status = ENABLED',
'DURING',date_range
].join(' ');
var report = AdWordsApp.report(query_str);
var rows = report.rows();
while (rows.hasNext()) {
var row = rows.next();
// Our Id will be a combination of Campaign Id, AdGroup Id, and Criteria (Keyword) Id
var key = [row.CampaignId,row.AdGroupId,row.Id].join('-');
keyword_ids[key] = true;
}
return keyword_ids;
}
/***********
* Helper function to calculate the date daysAgo
* Returns a string such as 20151125
***********/
function dateStringDaysAgo(daysAgo) {
var ONE_DAY = (24 * 60 * 60 * 1000);
var today = new Date();
var daysAgo = new Date(today.getTime() - (daysAgo * ONE_DAY));
return Utilities.formatDate(daysAgo, AdWordsApp.currentAccount().getTimeZone(), 'yyyyMMdd');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment