using System; using System.Collections.Generic; using System.IO; using System.Web; using Google.Api.Ads.AdWords.Lib; using Google.Api.Ads.AdWords.Util.Reports; using Google.Api.Ads.AdWords.v201306; namespace GoogleAdwards.Utility { public class GoogleSelectedAccountsPerformanceReport { private const string Service = "adwords"; public string AuthenticationEmail { get; set; } public string AuthenticationPassword { get; set; } public string DeveloperToken { get; set; } public string ClientCustomerId { get; set; } public string ReportName { get; set; } public string ReportFolder { get; set; } public string ReportFileName { get; set; } public string[] SelectorFields { get { return new[] { "Login", "CustomerId", "Name", "CanManageClients" }; } } public ReportDefinitionReportType[] ReportTypes { get; set; } public ReportDefinitionReportType ReportType { get; set; } public string FilePath { get; set; } public string ReportFileFolder { get; set; } public string BaseFolder { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public string ReportSuffix { get; set; } public string DownloadReportData(long clientId, string clientName) { var customDictionary = new Dictionary { { "clientCustomerId", clientId.ToString() } }; var predicates = new List(); var predicate = new Predicate { field = "Status", @operator = PredicateOperator.IN, values = new[] {"ACTIVE", "PAUSED", "DELETED"} }; predicates.Add(predicate); var selector = new Selector { fields = SelectorFields, predicates = predicates.ToArray() }; var user = new AdWordsUser(customDictionary); foreach (var type in ReportTypes) { ReportType = type; switch (type) { case ReportDefinitionReportType.CAMPAIGN_PERFORMANCE_REPORT: ReportSuffix = "_Campaign_Performance_Report"; selector.fields = new[] { PerformanceSelector.Date, PerformanceSelector.AccountId, PerformanceSelector.AccountName, PerformanceSelector.CampaignId, PerformanceSelector.CampaignName, PerformanceSelector.Impressions, PerformanceSelector.Clicks, PerformanceSelector.AveragePosition, PerformanceSelector.Cost, PerformanceSelector.AverageCpc, PerformanceSelector.ClickThroughRate, PerformanceSelector.Conversions, PerformanceSelector.ConversionRate, PerformanceSelector.CostPerConversion, PerformanceSelector.ClickThroughRate, }; var dates = new DateRange { min = String.Format("{0:yyyyMMdd}", StartDate), max = String.Format("{0:yyyyMMdd}", EndDate) }; selector.dateRange = dates; break; case ReportDefinitionReportType.ADGROUP_PERFORMANCE_REPORT: ReportSuffix = "_Adgroup_Performance_Report"; selector.fields = new[] { PerformanceSelector.Date, PerformanceSelector.CampaignId, PerformanceSelector.CampaignName, PerformanceSelector.AdGroupId, PerformanceSelector.AdGroupName, PerformanceSelector.Impressions, PerformanceSelector.Clicks, PerformanceSelector.AveragePosition, PerformanceSelector.Cost, PerformanceSelector.AverageCpc, PerformanceSelector.ClickThroughRate, PerformanceSelector.Conversions, PerformanceSelector.ConversionRate, PerformanceSelector.CostPerConversion, PerformanceSelector.ClickThroughRate, }; dates = new DateRange { min = String.Format("{0:yyyyMMdd}", StartDate), max = String.Format("{0:yyyyMMdd}", EndDate) }; selector.dateRange = dates; break; case ReportDefinitionReportType.KEYWORDS_PERFORMANCE_REPORT: ReportSuffix = "_Keyword_Performance_Report"; selector.fields = new[] { PerformanceSelector.Date, PerformanceSelector.KeywordId, PerformanceSelector.KeywordText, PerformanceSelector.AccountName, PerformanceSelector.CampaignId, PerformanceSelector.CampaignName, PerformanceSelector.AdGroupId, PerformanceSelector.AdGroupName, PerformanceSelector.KeywordMatchType, PerformanceSelector.Impressions, PerformanceSelector.Clicks, PerformanceSelector.AveragePosition, PerformanceSelector.Cost, PerformanceSelector.AverageCpc, PerformanceSelector.ClickThroughRate, PerformanceSelector.Conversions, PerformanceSelector.ConversionRate, PerformanceSelector.CostPerConversion, }; dates = new DateRange { min = String.Format("{0:yyyyMMdd}", StartDate), max = String.Format("{0:yyyyMMdd}", EndDate) }; selector.dateRange = dates; break; case ReportDefinitionReportType.DISPLAY_KEYWORD_PERFORMANCE_REPORT: ReportSuffix = "_Display_Keyword_Performance_Report"; selector.fields = new[] { PerformanceSelector.Date, PerformanceSelector.KeywordId, PerformanceSelector.KeywordText, PerformanceSelector.CampaignId, PerformanceSelector.CampaignName, PerformanceSelector.AdGroupId, PerformanceSelector.AdGroupName, PerformanceSelector.Impressions, PerformanceSelector.Clicks, PerformanceSelector.Cost, PerformanceSelector.AverageCpc, PerformanceSelector.Conversions, PerformanceSelector.ConversionRate, PerformanceSelector.CostPerConversion, }; dates = new DateRange { min = String.Format("{0:yyyyMMdd}", StartDate), max = String.Format("{0:yyyyMMdd}", EndDate) }; selector.dateRange = dates; break; case ReportDefinitionReportType.AD_PERFORMANCE_REPORT: ReportSuffix = "_Ad_Performance_Report"; selector.fields = new[] { PerformanceSelector.Date, PerformanceSelector.AccountName, PerformanceSelector.CampaignName, PerformanceSelector.AdGroupName, PerformanceSelector.Id, PerformanceSelector.Headline, PerformanceSelector.Description1, PerformanceSelector.Description2, PerformanceSelector.AdStatus, PerformanceSelector.DestinationUrl, PerformanceSelector.DisplayUrl, PerformanceSelector.Impressions, PerformanceSelector.Clicks, PerformanceSelector.AveragePosition, PerformanceSelector.Cost, PerformanceSelector.AverageCpc, PerformanceSelector.ClickThroughRate, PerformanceSelector.Conversions, PerformanceSelector.ConversionRate, PerformanceSelector.CostPerConversion }; dates = new DateRange { min = String.Format("{0:yyyyMMdd}", StartDate), max = String.Format("{0:yyyyMMdd}", EndDate) }; selector.dateRange = dates; break; } } var reportDefinition = new ReportDefinition { reportName = ReportName, dateRangeType = ReportDefinitionDateRangeType.CUSTOM_DATE, includeZeroImpressions = true, reportType = ReportType, downloadFormat = DownloadFormat.XML, selector = selector }; //_baseFolder = HttpContext.Current.Server.MapPath("\\downloads\\" + DateTime.Now.Ticks); //_reportFileFolder = _baseFolder + "\\keyword-performance-report"; FilePath = BaseFolder + "\\" + clientName.Replace(" ", "_") + ReportSuffix + ".xml"; if (!Directory.Exists(BaseFolder)) Directory.CreateDirectory(BaseFolder); //if (!Directory.Exists(ReportFolder)) // Directory.CreateDirectory(ReportFolder); var utilities = new ReportUtilities(user) { ReportVersion = "v201306" }; var report = utilities.DownloadClientReport(reportDefinition, false, FilePath); return FilePath; } } }