Created
November 13, 2015 08:39
-
-
Save russorat/e77a978daa954e347d55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*********** | |
* 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