Tuesday, July 30, 2013

Figuring Out When Your Ad, AdGroup, Keyword, or Campaign Was Created

Knowing when an Ad (or entity) was created is impossible using scripts. That information is simply not tracked in AdWords. The next best thing is to find out when your Ad first started receiving impressions and assume that is when it was created (If an Ad is created but no one sees it, does it really exist?).

So in order to help me keep track of when my Ads entities were created, I put together the following script to apply labels to my Ads entities with the date of the first impression. That way, I can find out what ads entities I created and make sure I don't take action on anything that is too young. I can also make changes to all the ads entities built on a given day relatively easily in the AdWords UI by just selecting the right label.

Thanks,
Russ



/**************************************
* Track Entity Creation Date
* Version 1.4
* Changelog v1.4
*  - Removed apiVersion from reporting call
* Changelog v1.3
*  - Updated script to handle all entities
* Changelog v1.2
*  - Fixed an issue with comparing dates
* ChangeLog v1.1
*  - Updated logic to work with larger accounts
* Created By: Russ Savage
* http://www.FreeAdWordsScripts.com
**************************************/
//All my labels will start with this. For example: Created:2013-05-01
var LABEL_PREFIX = 'Created:';
var DAYS_IN_REPORT = 30;
var ENTITY = 'ad'; //or adgroup or keyword or campaign
 
function main() {
  //First we get the impression history of our entity
  var ret_map = getImpressionHistory();
  //Then we apply our labels
  applyLabels(ret_map);
}
 
//Function to apply labels to the ads in an account
function applyLabels(ret_map) {
  var iter;
  if(ENTITY === 'campaign') { iter = AdWordsApp.campaigns().get(); }
  if(ENTITY === 'adgroup') { iter = AdWordsApp.adGroups().get(); }
  if(ENTITY === 'ad') { iter = AdWordsApp.ads().get(); }
  if(ENTITY === 'keyword') { iter = AdWordsApp.keywords().get(); }
  
  while(iter.hasNext()) {
    var entity = iter.next();
    var id = entity.getId();
    if(ret_map[id]) {
      var label_name = LABEL_PREFIX+Utilities.formatDate(ret_map[id], AdWordsApp.currentAccount().getTimeZone(), "yyyy-MM-dd");
      createLabelIfNeeded(label_name);
      entity.applyLabel(label_name);
    }
  }
}
 
//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 getImpressionHistory() {
  var API_VERSION = { 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 = { 
    'campaign' : 'CAMPAIGN_PERFORMANCE_REPORT',
    'adgroup' : 'ADGROUP_PERFORMANCE_REPORT',
    'ad' : 'AD_PERFORMANCE_REPORT',
    'keyword' : 'KEYWORDS_PERFORMANCE_REPORT'}[ENTITY];
  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 [year,month,day] = (row['Date']).split('-');
        var from_row = new Date(year, parseFloat(month)-1, day);
        var from_map = ret_map[row['Id']];
         
        if(from_row < from_map) {
          ret_map[row['Id']] = from_row;
        }
      } else {
        var [year,month,day] = (row['Date']).split('-');
        ret_map[row['Id']] = new Date(year, parseFloat(month)-1, day);
      }
    }
    prev_days_ago = i;
  }
  return ret_map;
}

34 comments:

  1. It’s amazing in support of me to truly have a web site that is valuable meant for my knowledge.

    ReplyDelete
  2. Niche revises position very seriously 90Per cent in our enterprise and why we certainly have in no way essentially exposed our system for the community.https://bizzebee.com/4-simple-seo-tips-for-new-bloggers/ Establish Links of our website - the audience will probably be open up for your personal temporary, until lastly finally we fulfill track of up by including more quite a bit ranges customers accessible

    ReplyDelete
  3. who made the product even offered to set everything up for me for $10 bucks (ideal for me!), so I was following in like 5 minutes. Backlinks service

    ReplyDelete
  4. In keyword research I use the broad match because it will yield the largest number of keyword ideas from which to harvest final keyword gems.google keyword wrapper

    ReplyDelete
  5. Seo firm inside had been large together with his time, while we put in hrs going through the requirements my business in addition to what choosing a company steps browse around here. He's very professional in addition to reputable. Advocate finest search engine marketing into the nearby organization that is looking for.

    ReplyDelete
  6. I have installed the script. It is running, enabled, green icon lighting. But I don´t see the label anywhere. I looked for a colummn Labels, but didn´t find. Please help me. It is my first script I have ever used.

    ReplyDelete
  7. who made the product even offered to set everything up for me for $10 bucks (ideal for me!), so I was following in like 5 minutes.
    field technician job description

    ReplyDelete
  8. Each of the handsets connected with the base unit also has its own speakerphone, so just at the touch of a button, you can enjoy hands-free conversation while you are cooking or doing something else.

    Cordless Phones For Senior

    ReplyDelete
  9. The product has delighted both end users and internal stakeholders. Due to their effective project management and the experience of user experience design agency team they has been able to deliver a high-quality app within a reasonable timeframe. Their professionalism and transparency set them apart.

    ReplyDelete
  10. I appreciate the time you’ve taken out to show me the ropes and ensure that I have the required knowledge to do a great job. Thank you.
    read more

    ReplyDelete
  11. We are a certified troubleshooting guide, available round the clock to provide the best technical support or specialized technical assistance for canon printer users. We work online with live technical support team to help all frustrated canon printer users. If your canon printer has stopped working or showing canon printer not responding issue, our canon support team is very dedicated to solve this error simply. Our canon printer troubleshooting team has the great skills and extensive experience for solving canon printer not printing problem within a few seconds. Our live technical support team is ready to provide quick help for any issues.

    ReplyDelete
  12. We are the simplest , successful, and self-governing third party QuickBooks support provider, which is providing unlimited QuickBooks support services for all frustrated QuickBooks in very nominal charges. once you plan to open a corporation file not located on another computing system , you'll undergo QuickBooks Error H505. This error code is an annoying error, which can't be resolved easily. This error code appears when QuickBooks company file needs some extra configuration. If you're experiencing this error code again and again, you'll call our QuickBooks professionals to urge the specialized assistance for solving this error code simply. So now, you'll enjoy the simplest services with QuickBooks excellently.

    ReplyDelete
  13. Perfect piece of work you have done, this website is really cool with wonderful information.
    Feel free to visit my web blog: Movie reviews - Movie Laundry provides info on movies, movie reviews, ratings, cast and crew details of movies. Get updated movie reviews from movie Laundry.checkout their works at- movielaundry





    ReplyDelete
  14. It has been just unfathomably liberal with you to give straightforwardly what precisely numerous people would've promoted for an eBook to wind up making some money for their end, basically given that you could have attempted it in the occasion you needed.
    CRM with Invoicing

    ReplyDelete
  15. Thankfulness to my dad who informed me relating to this blog, this website is really amazing.
    weblink

    ReplyDelete
  16. Get ready to boost your business with a strong online presence. Hire India’s most trusted online website development company and get instant quotes. 100% Customer Satisfaction. 1000+ Dynamic Website Designs with built-in SEO. Mobile friendly. Top-notch technology. 24*7 Technical support and much more. Reach us now!

    ReplyDelete
  17. Thanks for compiling such nicest information in your blogs. Articles are very informative and hope again I’ll find more like that.
    web design companies

    ReplyDelete
  18. Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. 100 high quality backlinks for $2

    ReplyDelete
  19. Hi to all, the blog has really the dreadful information I really enjoyed a lot.
    UI design company

    ReplyDelete
  20. Sometimes you don’t have time to complete your assignment before deadlines. And due to late submission you can’t get full marks, if you are looking help for completing your assignment then you can contact us. We are providing Online assignment help and we have a team of professional’s academic writers who are more eligible to complete your assignments on time and accurate. For more information visit our website or call us on or Toll free numbers.

    ReplyDelete
  21. The creative minds of Brandphic provide innovative solutions for your business identity and recognition among the audiences. We ensure to make your journey ecstatic by demonstrating our work of art with enthusiasm and determination. branding package

    ReplyDelete