コード例 #1
0
    /// <summary>
    /// Run the code example.
    /// </summary>
    /// <param name="user">The DFP user object running the code example.</param>
    public override void Run(DfpUser user) {
      ReportService reportService = (ReportService) user.GetService(
          DfpService.v201311.ReportService);

      // Get the NetworkService.
      NetworkService networkService = (NetworkService) user.GetService(
            DfpService.v201311.NetworkService);

      // Get the root ad unit ID to filter on.
      String rootAdUnitId = networkService.getCurrentNetwork().effectiveRootAdUnitId;

      // Create statement to filter on an ancestor ad unit with the root ad unit ID to include all
      // ad units in the network.
      StatementBuilder statementBuilder = new StatementBuilder(
          "where AD_UNIT_ANCESTOR_AD_UNIT_ID = :ancestorAdUnitId").
          AddValue("ancestorAdUnitId", long.Parse(rootAdUnitId));

      // Create report query.
      ReportQuery reportQuery = new ReportQuery();
      reportQuery.dimensions =
          new Dimension[] { Dimension.AD_UNIT_ID, Dimension.AD_UNIT_NAME };
      reportQuery.columns = new Column[] {Column.AD_SERVER_IMPRESSIONS,
        Column.AD_SERVER_CLICKS, Column.DYNAMIC_ALLOCATION_INVENTORY_LEVEL_IMPRESSIONS,
        Column.DYNAMIC_ALLOCATION_INVENTORY_LEVEL_CLICKS,
        Column.TOTAL_INVENTORY_LEVEL_IMPRESSIONS,
        Column.TOTAL_INVENTORY_LEVEL_CPM_AND_CPC_REVENUE};

      // Set the filter statement.
      reportQuery.statement = statementBuilder.ToStatement();

      reportQuery.adUnitView = ReportQueryAdUnitView.HIERARCHICAL;
      reportQuery.dateRangeType = DateRangeType.LAST_WEEK;

      // Create report job.
      ReportJob reportJob = new ReportJob();
      reportJob.reportQuery = reportQuery;

      try {
        // Run report.
        reportJob = reportService.runReportJob(reportJob);
        // Wait for report to complete.
        while (reportJob.reportJobStatus == ReportJobStatus.IN_PROGRESS) {
          Console.WriteLine("Report job with id = '{0}' is still running.", reportJob.id);
          Thread.Sleep(30000);
          // Get report job.
          reportJob = reportService.getReportJob(reportJob.id);
        }

        if (reportJob.reportJobStatus == ReportJobStatus.COMPLETED) {
          Console.WriteLine("Report job with id = '{0}' completed successfully.", reportJob.id);
        } else if (reportJob.reportJobStatus == ReportJobStatus.FAILED) {
          Console.WriteLine("Report job with id = '{0}' failed to complete successfully.",
              reportJob.id);
        }
      } catch (Exception ex) {
        Console.WriteLine("Failed to run inventory report. Exception says \"{0}\"",
            ex.Message);
      }
    }
コード例 #2
0
    /// <summary>
    /// Run the code example.
    /// </summary>
    /// <param name="user">The DFP user object running the code example.</param>
    public override void Run(DfpUser user) {
      // Get the PublisherQueryLanguageService.
      PublisherQueryLanguageService pqlService =
          (PublisherQueryLanguageService) user.GetService(
              DfpService.v201311.PublisherQueryLanguageService);

      int pageSize = 500;

      // Create statement to select all line items.
      String selectStatement = "SELECT Id, Name, Status FROM Line_Item where Name LIKE " +
            "'line item%' order by Id ASC LIMIT " + pageSize;
      int offset = 0;
      int resultSetSize = 0;
      List<Row> allRows = new List<Row>();
      ResultSet resultSet;

      try {
        do {
          StatementBuilder statementBuilder =
              new StatementBuilder(selectStatement + " OFFSET " + offset);

          // Get line items like 'line item%'.
          resultSet = pqlService.select(statementBuilder.ToStatement());

          // Collect all line items from each page.
          allRows.AddRange(resultSet.rows);

          // Display results.
          Console.WriteLine(PqlUtilities.ResultSetToString(resultSet));

          offset += pageSize;
          resultSetSize = resultSet.rows == null ? 0 : resultSet.rows.Length;
        } while (resultSetSize == pageSize);

        Console.WriteLine("Number of results found: " + allRows.Count);

        // Optionally, save all rows to a CSV.
        // Get a string array representation of the data rows.
        resultSet.rows = allRows.ToArray();
        List<String[]> rows = PqlUtilities.ResultSetToStringArrayList(resultSet);

        // Write the contents to a csv file.
        CsvFile file = new CsvFile();
        file.Headers.AddRange(rows[0]);
        file.Records.AddRange(rows.GetRange(1, rows.Count - 1).ToArray());
        file.Write("line_items_named_like_" + GetTimeStamp() + ".csv");
      } catch (Exception ex) {
        Console.WriteLine("Failed to get line items. Exception says \"{0}\"", ex.Message);
      }
    }
コード例 #3
0
    public void TestAllStatementBuilderPartialFunctions() {
      StatementBuilder statementBuilder = new StatementBuilder()
          .Select("Name, Id")
          .From("Geo_Target")
          .Where("Targetable = :targetable")
          .OrderBy("Id DESC")
          .Limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
          .Offset(0)
          .AddValue("targetable", true)
          .IncreaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT)
          .RemoveLimitAndOffset();
      Assert.AreEqual(null, statementBuilder.GetOffset());

      Statement expectedStatement = new Statement();
      expectedStatement.query = "SELECT Name, Id FROM Geo_Target"
          + " WHERE Targetable = :targetable ORDER BY Id DESC";
      String_ValueMapEntry targetableEntry = new String_ValueMapEntry();
      targetableEntry.key = "targetable";
      BooleanValue targetableValue = new BooleanValue();
      targetableValue.value = true;
      targetableEntry.value = targetableValue;
      expectedStatement.values = new String_ValueMapEntry[] {targetableEntry};
      Assert.True(StatementsAreEqual(expectedStatement, statementBuilder.ToStatement()));
    }
コード例 #4
0
    public void TestStatementBuilderIncludesKeywordPrefix() {
      StatementBuilder statementBuilder = new StatementBuilder()
          .Where("WHEREWITHALL = 1");

      Statement expectedStatement = new Statement();
      expectedStatement.query = "WHERE WHEREWITHALL = 1";
      expectedStatement.values = new String_ValueMapEntry[0];

      Assert.True(StatementsAreEqual(expectedStatement,
        statementBuilder.ToStatement()));
    }
コード例 #5
0
    public void TestStatementBuilderBasicStatement() {
      StatementBuilder statementBuilder = new StatementBuilder()
          .Where("ID = 1");

      Statement expectedStatement = new Statement();
      expectedStatement.query = "WHERE ID = 1";
      expectedStatement.values = new String_ValueMapEntry[0];

      Assert.True(StatementsAreEqual(expectedStatement,
                                     statementBuilder.ToStatement()));
    }
コード例 #6
0
    public void TestStatementBuilderBlankStatement() {
      StatementBuilder statementBuilder = new StatementBuilder();

      Statement expectedStatement = new Statement();
      expectedStatement.query = "";
      expectedStatement.values = new String_ValueMapEntry[0];

      Assert.True(StatementsAreEqual(expectedStatement,
                                     statementBuilder.ToStatement()));
    }