public void Init() {
   TestUtils utils = new TestUtils();
   placementService = (PlacementService) user.GetService(DfpService.v201505.PlacementService);
   adUnit1 = utils.CreateAdUnit(user);
   adUnit2 = utils.CreateAdUnit(user);
   placement = utils.CreatePlacement(user, new string[] {adUnit1.id, adUnit2.id});
 }
    public void TestCreatePlacements() {
      TestUtils utils = new TestUtils();
      Placement placement1 = new Placement();
      placement1.name = string.Format("Medium Square AdUnit Placement #{0}", utils.GetTimeStamp());
      placement1.description = "Contains ad units that can hold creatives of size 300x250";
      placement1.targetedAdUnitIds = new string[] {adUnit1.id, adUnit2.id};

      Placement placement2 = new Placement();
      placement2.name = string.Format("Medium Square AdUnit Placement #{0}", utils.GetTimeStamp());
      placement2.description = "Contains ad units that can hold creatives of size 300x250";
      placement2.targetedAdUnitIds = new string[] {adUnit1.id, adUnit2.id};

      Placement[] newPlacements = null;

      Assert.DoesNotThrow(delegate() {
        newPlacements = placementService.createPlacements(new Placement[] {placement1, placement2});
      });

      Assert.NotNull(newPlacements);
      Assert.AreEqual(newPlacements.Length, 2);

      Assert.NotNull(newPlacements[0]);
      Assert.AreEqual(newPlacements[0].name, placement1.name);
      Assert.AreEqual(newPlacements[0].description, placement1.description);
      Assert.Contains(adUnit1.id, newPlacements[0].targetedAdUnitIds);
      Assert.Contains(adUnit2.id, newPlacements[0].targetedAdUnitIds);

      Assert.NotNull(newPlacements[1]);
      Assert.AreEqual(newPlacements[1].name, placement2.name);
      Assert.AreEqual(newPlacements[1].description, placement2.description);
      Assert.Contains(adUnit1.id, newPlacements[1].targetedAdUnitIds);
      Assert.Contains(adUnit2.id, newPlacements[1].targetedAdUnitIds);
    }
    /// <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 InventoryService.
      InventoryService inventoryService =
          (InventoryService) user.GetService(DfpService.v201505.InventoryService);

      // Get the PlacementService.
      PlacementService placementService =
          (PlacementService) user.GetService(DfpService.v201505.PlacementService);

      // Create local placement object to store skyscraper ad units.
      Placement skyscraperAdUnitPlacement = new Placement();
      skyscraperAdUnitPlacement.name = string.Format("Skyscraper AdUnit Placement #{0}",
          this.GetTimeStamp());
      skyscraperAdUnitPlacement.description = "Contains ad units that can hold creatives " +
          "of size 120x600";

      // Create local placement object to store medium square ad units.
      Placement mediumSquareAdUnitPlacement = new Placement();
      mediumSquareAdUnitPlacement.name = string.Format("Medium Square AdUnit Placement #{0}",
          this.GetTimeStamp());
      mediumSquareAdUnitPlacement.description = "Contains ad units that can hold creatives " +
          "of size 300x250";

      // Create local placement object to store banner ad units.
      Placement bannerAdUnitPlacement = new Placement();
      bannerAdUnitPlacement.name = string.Format("Banner AdUnit Placement #{0}",
          this.GetTimeStamp());
      bannerAdUnitPlacement.description = "Contains ad units that can hold creatives " +
          "of size 468x60";

      List<Placement> placementList = new List<Placement>();

      // Get the first 500 ad units.
      StatementBuilder statementBuilder = new StatementBuilder()
          .OrderBy("id ASC")
          .Limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);

      List<string> mediumSquareTargetedUnitIds = new List<string>();
      List<string> skyscraperTargetedUnitIds = new List<string>();
      List<string> bannerTargetedUnitIds = new List<string>();

      try {
        AdUnitPage page = inventoryService.getAdUnitsByStatement(statementBuilder.ToStatement());

        // Separate the ad units by size.
        if (page.results != null) {
          foreach (AdUnit adUnit in page.results) {
            if (adUnit.parentId != null && adUnit.adUnitSizes != null) {
              foreach (AdUnitSize adUnitSize in adUnit.adUnitSizes) {
                Size size = adUnitSize.size;
                if (size.width == 300 && size.height == 250) {
                  if (!mediumSquareTargetedUnitIds.Contains(adUnit.id)) {
                    mediumSquareTargetedUnitIds.Add(adUnit.id);
                  }
                } else if (size.width == 120 && size.height == 600) {
                  if (!skyscraperTargetedUnitIds.Contains(adUnit.id)) {
                    skyscraperTargetedUnitIds.Add(adUnit.id);
                  }
                } else if (size.width == 468 && size.height == 60) {
                  if (!bannerTargetedUnitIds.Contains(adUnit.id)) {
                    bannerTargetedUnitIds.Add(adUnit.id);
                  }
                }
              }
            }
          }
        }
        mediumSquareAdUnitPlacement.targetedAdUnitIds = mediumSquareTargetedUnitIds.ToArray();
        skyscraperAdUnitPlacement.targetedAdUnitIds = skyscraperTargetedUnitIds.ToArray();
        bannerAdUnitPlacement.targetedAdUnitIds = bannerTargetedUnitIds.ToArray();


        // Only create placements with one or more ad unit.
        if (mediumSquareAdUnitPlacement.targetedAdUnitIds.Length != 0) {
          placementList.Add(mediumSquareAdUnitPlacement);
        }

        if (skyscraperAdUnitPlacement.targetedAdUnitIds.Length != 0) {
          placementList.Add(skyscraperAdUnitPlacement);
        }

        if (bannerAdUnitPlacement.targetedAdUnitIds.Length != 0) {
          placementList.Add(bannerAdUnitPlacement);
        }

        Placement[] placements =
          placementService.createPlacements(placementList.ToArray());

        // Display results.
        if (placements != null) {
          foreach (Placement placement in placements) {
            Console.Write("A placement with ID = '{0}', name ='{1}', and containing " +
                "ad units {{", placement.id, placement.name);

            foreach (string adUnitId in placement.targetedAdUnitIds) {
              Console.Write("{0}, ", adUnitId);
            }
            Console.WriteLine("} was created.");
          }
        } else {
          Console.WriteLine("No placements created.");
        }

      } catch (Exception ex) {
        Console.WriteLine("Failed to create placements. Exception says \"{0}\"",
            ex.Message);
      }
    }
Ejemplo n.º 4
0
    public Placement CreatePlacement(DfpUser user, string[] targetedAdUnitIds) {
      // Get InventoryService.
      PlacementService placementService =
          (PlacementService) user.GetService(DfpService.v201505.PlacementService);

      Placement placement = new Placement();
      placement.name = string.Format("Test placement #{0}", this.GetTimeStamp());
      placement.description = "Test placement";
      placement.targetedAdUnitIds = targetedAdUnitIds;

      return placementService.createPlacements(new Placement[] {placement})[0];
    }