Created
November 10, 2024 11:37
-
-
Save themacmarketer/00506f15e13ef7830d727e4cc28050fd to your computer and use it in GitHub Desktop.
This file contains 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
// Adds a label to adverts with an estimated creation date | |
var LABEL_PREFIX = 'Created:'; | |
var DAYS_IN_REPORT = 30; | |
function main() { | |
//First we get the impression history of our ads | |
var ret_map = getAccountImpressionHistory(); | |
//Then we apply our labels | |
applyAdLabels(ret_map); | |
} | |
//Function to apply labels to the ads in an account | |
function applyAdLabels(ret_map) { | |
var ad_iter = AdWordsApp.ads().get(); | |
while(ad_iter.hasNext()) { | |
var ad = ad_iter.next(); | |
var ad_id = ad.getId(); | |
if(ret_map[ad_id]) { | |
createLabelIfNeeded(LABEL_PREFIX+ret_map[ad_id]); | |
ad.applyLabel(LABEL_PREFIX+ret_map[ad_id]); | |
} | |
} | |
} | |
//This is a helper function to create the label if it does not already exist | |
function createLabelIfNeeded(name) { | |
if(!AdWordsApp.labels().withCondition("Name = '"+name+"'").get().hasNext()) { | |
AdWordsApp.createLabel(name); | |
} | |
} | |
//A helper function to find the date days ago | |
function getDateDaysAgo(days) { | |
var the_past = new Date(); | |
the_past.setDate(the_past.getDate() - days); | |
return Utilities.formatDate(the_past,AdWordsApp.currentAccount().getTimeZone(),"yyyyMMdd"); | |
} | |
//A helper function to compare dates. | |
//Copied from: http://goo.gl/uW48a | |
function diffDays(firstDate,secondDate) { | |
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds | |
return Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay))); | |
} | |
function getAccountImpressionHistory() { | |
var API_VERSION = { apiVersion: 'v201809', includeZeroImpressions : false }; | |
var first_date = new Date('10/23/2000'); | |
var max_days_ago = diffDays(first_date,new Date()); | |
var cols = ['Date','Id','Impressions']; | |
var report = 'AD_PERFORMANCE_REPORT'; | |
var ret_map = {}; | |
var prev_days_ago = 0; | |
for(var i = DAYS_IN_REPORT; i < max_days_ago; i+=DAYS_IN_REPORT) { | |
var start_date = getDateDaysAgo(i); | |
var end_date = getDateDaysAgo(prev_days_ago); | |
var date_range = start_date+','+end_date; | |
Logger.log('Getting data for ' + date_range); | |
var query = ['select',cols.join(','),'from',report,'during',date_range].join(' '); | |
var report_iter = AdWordsApp.report(query, API_VERSION).rows(); | |
if(!report_iter.hasNext()) { Logger.log('No more impressions found. Breaking.'); break; } // no more entries | |
while(report_iter.hasNext()) { | |
var row = report_iter.next(); | |
if(ret_map[row['Id']]) { | |
var from_row = new Date(row['Date']); | |
var from_map = new Date(ret_map[row['Id']]); | |
if(from_row < from_map) { | |
ret_map[row['Id']] = row['Date']; | |
} | |
} else { | |
ret_map[row['Id']] = row['Date']; | |
} | |
} | |
prev_days_ago = i; | |
} | |
return ret_map; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment