uwekeim

Retrieve daily costs through Google AdWords API v201309

Nov 26th, 2013
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. // See http://stackoverflow.com/questions/20114242 for details.
  2.  
  3. private static decimal coreGetAdwordsSumInRange(DateTime start, DateTime end)
  4. {
  5.     var reportDefinition =
  6.         new ReportDefinition
  7.         {
  8.             reportName = string.Format(@"Campaign performance report #{0}", DateTime.Now.Ticks),
  9.             dateRangeType = ReportDefinitionDateRangeType.CUSTOM_DATE,
  10.             reportType = ReportDefinitionReportType.CAMPAIGN_PERFORMANCE_REPORT,
  11.             downloadFormat = DownloadFormat.XML,
  12.             includeZeroImpressions = false,
  13.             selector = new Selector
  14.             {
  15.                 fields = new[] { @"Cost" },
  16.                 dateRange = new DateRange {min = start.ToString(@"yyyyMMdd"), max = end.ToString(@"yyyyMMdd")}
  17.             }
  18.         };
  19.  
  20.     // --
  21.  
  22.     var sum = decimal.Zero;
  23.  
  24.     var tempFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + @".xml");
  25.     try
  26.     {
  27.         var utils = new ReportUtilities(new AdWordsUser()) { ReportVersion = @"v201309" };
  28.         utils.DownloadClientReport(reportDefinition, true, tempFilePath);
  29.  
  30.         var doc = new XmlDocument();
  31.         doc.Load(tempFilePath);
  32.  
  33.         var costNodes = doc.SelectNodes(@"/report/table/row/@cost");
  34.         if (costNodes != null)
  35.         {
  36.             foreach (XmlNode costNode in costNodes)
  37.             {
  38.                 var cost = Convert.ToDecimal(costNode.InnerText);
  39.                 sum += cost/1000000m;
  40.             }
  41.         }
  42.     }
  43.     finally
  44.     {
  45.         File.Delete(tempFilePath);
  46.     }
  47.  
  48.     return sum;
  49. }
Add Comment
Please, Sign In to add comment