Snippets

Frederick Vallaeys Ad Group Impressions With Sitelinks

Created by Frederick Vallaeys
var map = new Array();

function main() {
  runReportForAdGroups(map);
  runReportForExtensions(map);
  reportResults(map);
}
  
  
function reportResults(map) {
  
  var sheet = SpreadsheetApp.create("ad groups with sitelink impressions");
  var sheet1 = sheet.getActiveSheet();
  var rowData = new Array("Campaign Name", "Ad Group Name", "Impressions With Site Links", "Total Impressions", "Percentage");
  sheet1.appendRow(rowData);
  
  
  for(var id in map) {
    var item = map[id];
    var campaignName = item.campaignName;
    var adGroupName = item.adGroupName;
    var totalImpressions = item.totalImpressions;
    var impressionsWithSiteLinks = item.impressionsWithSiteLinks;
    
    var percentage = impressionsWithSiteLinks / totalImpressions*100;
    
    Logger.log(adGroupName + " (" + id + '): imp with sitelinks:' + percentage.toFixed(0) + "% (" + impressionsWithSiteLinks + " out of " + totalImpressions + ")");
    var rowData = new Array(campaignName, adGroupName, impressionsWithSiteLinks, totalImpressions, percentage);
    sheet1.appendRow(rowData);
    sheet1.setFrozenRows(1);
    sheet1.sort(5);
  }  
}


function runReportForExtensions(adMap) {
  // AdWords reports return data faster than campaign management methods
  //   and can be used to retrieve basic structural information on
  //   your Account, Campaigns, AdGroups, Ads, Keywords, etc. You can refer to
  //   https://developers.google.com/adwords/api/docs/guides/structure-reports
  //   for more details.
  
  // See https://developers.google.com/adwords/api/docs/appendix/reports
  //   for all the supported report types.
  // See https://developers.google.com/adwords/api/docs/guides/awql for
  //   details on how to use AWQL.
  // See https://developers.google.com/adwords/api/docs/guides/uireports
  //   for details on how to map an AdWords UI feature to the corresponding
  //   reporting API feature.
  // ExtensionPlaceholderCreativeId
  var report = AdWordsApp.report(
    'SELECT AdGroupId, AdGroupName, CampaignName, Impressions, ExtensionPlaceholderType ' +
    'FROM   PLACEHOLDER_REPORT ' +  
    'WHERE ExtensionPlaceholderType = 1 ' +
    'AND AdGroupStatus = ENABLED ' +
    'AND CampaignStatus = ENABLED '+
    'DURING LAST_30_DAYS');
  
  var rows = report.rows();
  while (rows.hasNext()) {
    
    var row = rows.next();
    var adGroupId = row['AdGroupId'];
    var campaignName = row['CampaignName'];
    var adGroupName = row['AdGroupName'];
    var impressions = row['Impressions'];
    var type = row['ExtensionPlaceholderType'];
    
    if(!map[adGroupId]) {
      map[adGroupId] = new Object();
      map[adGroupId].adGroupName = adGroupName;
      map[adGroupId].campaignName = campaignName;
      Logger.log("This should not happen because there can't be sitelink impressions without impressions");
    } else {
      //Logger.log("This should not happen");
    }
    map[adGroupId].impressionsWithSiteLinks = impressions;
  }
}



function runReportForAdGroups(map) {
  
  var report = AdWordsApp.report(
    'SELECT AdGroupId, Impressions, AdGroupName, CampaignName ' +
    'FROM   ADGROUP_PERFORMANCE_REPORT ' +
    'WHERE AdGroupStatus = ENABLED ' +
    'AND CampaignStatus = ENABLED ' +
    'AND Impressions > 0 ' +
    'DURING LAST_30_DAYS');
  
  var rows = report.rows();
  while (rows.hasNext()) {
    
    var row = rows.next();
    var adGroupId = row['AdGroupId'];
    var impressions = row['Impressions'];
    var adGroupName = row['AdGroupName'];
    var campaignName = row['CampaignName'];
    
    if(!map[adGroupId]) {
      map[adGroupId] = new Object();
      map[adGroupId].campaignName = campaignName;
      map[adGroupId].adGroupName = adGroupName;
      map[adGroupId].impressionsWithSiteLinks = 0;
      map[adGroupId].totalImpressions = 0;
    }    
    map[adGroupId].totalImpressions = impressions;
  }
}

Comments (0)