/// <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.v201508.InventoryService);

      // Create a statement to get all ad units.
      StatementBuilder statementBuilder = new StatementBuilder()
          .OrderBy("id ASC")
          .Limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);

      // Set default for page.
      AdUnitPage page = new AdUnitPage();

      try {
        do {
          // Get ad units by statement.
          page = inventoryService.getAdUnitsByStatement(statementBuilder.ToStatement());

          if (page.results != null && page.results.Length > 0) {
            int i = page.startIndex;
            foreach (AdUnit adUnit in page.results) {
              Console.WriteLine("{0}) Ad unit with ID = '{1}', name = '{2}' and status = '{3}' " +
                  "was found.", i, adUnit.id, adUnit.name, adUnit.status);
              i++;
            }
          }
          statementBuilder.IncreaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
        } while (statementBuilder.GetOffset() < page.totalResultSetSize);

        Console.WriteLine("Number of results found: {0}", page.totalResultSetSize);
      } catch (Exception e) {
        Console.WriteLine("Failed to get ad unit. Exception says \"{0}\"", e.Message);
      }
    }
    /// <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.v201511.InventoryService);

      // Set the ID of the ad unit to deactivate.
      int adUnitId = int.Parse(_T("INSERT_AD_UNIT_ID_HERE"));

      // Create a statement to select the ad unit.
      StatementBuilder statementBuilder = new StatementBuilder()
          .Where("id = :id")
          .OrderBy("id ASC")
          .Limit(1)
          .AddValue("id", adUnitId);

      // Set default for page.
      AdUnitPage page = new AdUnitPage();
      List<string> adUnitIds = new List<string>();

      try {
        do {
          // Get ad units by statement.
          page = inventoryService.getAdUnitsByStatement(statementBuilder.ToStatement());

          if (page.results != null && page.results.Length > 0) {
            int i = page.startIndex;
            foreach (AdUnit adUnit in page.results) {
              Console.WriteLine("{0}) Ad unit with ID ='{1}', name = {2} and status = {3} will" +
                  " be deactivated.", i, adUnit.id, adUnit.name, adUnit.status);
              adUnitIds.Add(adUnit.id);
              i++;
            }
          }

          statementBuilder.IncreaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
        } while (statementBuilder.GetOffset() < page.totalResultSetSize);

        Console.WriteLine("Number of ad units to be deactivated: {0}", adUnitIds.Count);

        // Modify statement for action.
        statementBuilder.RemoveLimitAndOffset();

        // Create action.
        DeactivateAdUnits action = new DeactivateAdUnits();

        // Perform action.
        UpdateResult result = inventoryService.performAdUnitAction(action,
            statementBuilder.ToStatement());

        // Display results.
        if (result != null && result.numChanges > 0) {
          Console.WriteLine("Number of ad units deactivated: {0}", result.numChanges);
        } else {
          Console.WriteLine("No ad units were deactivated.");
        }

      } catch (Exception e) {
        Console.WriteLine("Failed to deactivate ad units. Exception says \"{0}\"", e.Message);
      }
    }
Esempio n. 3
0
        /// <summary>
        /// Run the sample code.
        /// </summary>
        public void Run(DfpUser user)
        {
            // Get the InventoryService.
            InventoryService inventoryService =
                (InventoryService)user.GetService(DfpService.v201608.InventoryService);

            // Set the ID of the ad unit to update.
            int adUnitId = int.Parse(_T("INSERT_AD_UNIT_ID_HERE"));

            // Create a statement to get the ad unit.
            StatementBuilder statementBuilder = new StatementBuilder()
                                                .Where("id = :id")
                                                .OrderBy("id ASC")
                                                .Limit(1)
                                                .AddValue("id", adUnitId);

            try {
                // Get ad units by statement.
                AdUnitPage page = inventoryService.getAdUnitsByStatement(statementBuilder.ToStatement());

                AdUnit adUnit = page.results[0];
                adUnit.inheritedAdSenseSettings.value.adSenseEnabled = true;

                // Update the ad units on the server.
                AdUnit[] updatedAdUnits = inventoryService.updateAdUnits(new AdUnit[] { adUnit });

                foreach (AdUnit updatedAdUnit in updatedAdUnits)
                {
                    Console.WriteLine("Ad unit with ID \"{0}\", name \"{1}\", and is AdSense enabled " +
                                      "\"{2}\" was updated.", updatedAdUnit.id, updatedAdUnit.name,
                                      updatedAdUnit.inheritedAdSenseSettings.value.adSenseEnabled);
                }
            } catch (Exception e) {
                Console.WriteLine("Failed to update ad units. Exception says \"{0}\"", e.Message);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Run the code example.
        /// </summary>
        public void Run(AdManagerUser user)
        {
            using (InventoryService inventoryService = user.GetService <InventoryService>())
            {
                // Create a statement to select ad units.
                int pageSize = StatementBuilder.SUGGESTED_PAGE_LIMIT;
                StatementBuilder statementBuilder =
                    new StatementBuilder().OrderBy("id ASC").Limit(pageSize);

                // Retrieve a small amount of ad units at a time, paging through until all
                // ad units have been retrieved.
                int totalResultSetSize = 0;
                do
                {
                    AdUnitPage page =
                        inventoryService.getAdUnitsByStatement(statementBuilder.ToStatement());

                    // Print out some information for each ad unit.
                    if (page.results != null)
                    {
                        totalResultSetSize = page.totalResultSetSize;
                        int i = page.startIndex;
                        foreach (AdUnit adUnit in page.results)
                        {
                            Console.WriteLine(
                                "{0}) Ad unit with ID \"{1}\" and name \"{2}\" was found.", i++,
                                adUnit.id, adUnit.name);
                        }
                    }

                    statementBuilder.IncreaseOffsetBy(pageSize);
                } while (statementBuilder.GetOffset() < totalResultSetSize);

                Console.WriteLine("Number of results found: {0}", totalResultSetSize);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Run the sample code.
        /// </summary>
        public void Run(AdManagerUser user, long adUnitId)
        {
            using (InventoryService inventoryService = user.GetService <InventoryService>())
            {
                // Create a statement to get the ad unit.
                StatementBuilder statementBuilder = new StatementBuilder()
                                                    .Where("id = :id")
                                                    .OrderBy("id ASC")
                                                    .Limit(1)
                                                    .AddValue("id", adUnitId);

                try
                {
                    // Get ad units by statement.
                    AdUnitPage page =
                        inventoryService.getAdUnitsByStatement(statementBuilder.ToStatement());

                    // Create a 480x60 web ad unit size.
                    AdUnitSize adUnitSize = new AdUnitSize()
                    {
                        size = new Size()
                        {
                            width  = 480,
                            height = 60
                        },
                        environmentType = EnvironmentType.BROWSER
                    };

                    AdUnit adUnit = page.results[0];
                    adUnit.adUnitSizes = new AdUnitSize[]
                    {
                        adUnitSize
                    };

                    // Update the ad units on the server.
                    AdUnit[] updatedAdUnits = inventoryService.updateAdUnits(new AdUnit[]
                    {
                        adUnit
                    });

                    foreach (AdUnit updatedAdUnit in updatedAdUnits)
                    {
                        List <string> adUnitSizeStrings = new List <string>();
                        foreach (AdUnitSize size in updatedAdUnit.adUnitSizes)
                        {
                            adUnitSizeStrings.Add(size.fullDisplayString);
                        }

                        Console.WriteLine(
                            "Ad unit with ID \"{0}\", name \"{1}\", and sizes [{2}] was " +
                            "updated.", updatedAdUnit.id, updatedAdUnit.name,
                            String.Join(",", adUnitSizeStrings));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Failed to update ad units. Exception says \"{0}\"",
                                      e.Message);
                }
            }
        }
        /// <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.v201602.InventoryService);

            // Get the PlacementService.
            PlacementService placementService =
                (PlacementService)user.GetService(DfpService.v201602.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 e) {
                Console.WriteLine("Failed to create placements. Exception says \"{0}\"",
                                  e.Message);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Run the code example.
        /// </summary>
        public void Run(DfpUser user)
        {
            // Get the InventoryService.
            InventoryService inventoryService =
                (InventoryService)user.GetService(DfpService.v201611.InventoryService);

            // Set the ID of the ad unit to deactivate.
            int adUnitId = int.Parse(_T("INSERT_AD_UNIT_ID_HERE"));

            // Create a statement to select the ad unit.
            StatementBuilder statementBuilder = new StatementBuilder()
                                                .Where("id = :id")
                                                .OrderBy("id ASC")
                                                .Limit(1)
                                                .AddValue("id", adUnitId);

            // Set default for page.
            AdUnitPage    page      = new AdUnitPage();
            List <string> adUnitIds = new List <string>();

            try {
                do
                {
                    // Get ad units by statement.
                    page = inventoryService.getAdUnitsByStatement(statementBuilder.ToStatement());

                    if (page.results != null)
                    {
                        int i = page.startIndex;
                        foreach (AdUnit adUnit in page.results)
                        {
                            Console.WriteLine("{0}) Ad unit with ID ='{1}', name = {2} and status = {3} will" +
                                              " be deactivated.", i, adUnit.id, adUnit.name, adUnit.status);
                            adUnitIds.Add(adUnit.id);
                            i++;
                        }
                    }

                    statementBuilder.IncreaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
                } while (statementBuilder.GetOffset() < page.totalResultSetSize);

                Console.WriteLine("Number of ad units to be deactivated: {0}", adUnitIds.Count);

                // Modify statement for action.
                statementBuilder.RemoveLimitAndOffset();

                // Create action.
                DeactivateAdUnits action = new DeactivateAdUnits();

                // Perform action.
                UpdateResult result = inventoryService.performAdUnitAction(action,
                                                                           statementBuilder.ToStatement());

                // Display results.
                if (result != null && result.numChanges > 0)
                {
                    Console.WriteLine("Number of ad units deactivated: {0}", result.numChanges);
                }
                else
                {
                    Console.WriteLine("No ad units were deactivated.");
                }
            } catch (Exception e) {
                Console.WriteLine("Failed to deactivate ad units. Exception says \"{0}\"", e.Message);
            }
        }
Esempio n. 8
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 InventoryService.
            InventoryService inventoryService =
                (InventoryService)user.GetService(DfpService.v201311.InventoryService);

            // Create Statement text to select active ad units.
            string statementText = "WHERE status = :status LIMIT 500";

            Statement statement = new StatementBuilder("").AddValue("status",
                                                                    InventoryStatus.ACTIVE.ToString()).ToStatement();

            // Sets defaults for page and offset.
            AdUnitPage    page      = new AdUnitPage();
            int           offset    = 0;
            List <string> adUnitIds = new List <string>();

            try {
                do
                {
                    // Create a Statement to page through active ad units.
                    statement.query = string.Format("{0} OFFSET {1}", statementText, offset);

                    // Get ad units by Statement.
                    page = inventoryService.getAdUnitsByStatement(statement);

                    if (page.results != null && page.results.Length > 0)
                    {
                        int i = page.startIndex;
                        foreach (AdUnit adUnit in page.results)
                        {
                            Console.WriteLine("{0}) Ad unit with ID ='{1}', name = {2} and status = {3} will" +
                                              " be deactivated.", i, adUnit.id, adUnit.name, adUnit.status);
                            adUnitIds.Add(adUnit.id);
                            i++;
                        }
                    }

                    offset += 500;
                } while (offset < page.totalResultSetSize);

                Console.WriteLine("Number of ad units to be deactivated: {0}", adUnitIds.Count);

                if (adUnitIds.Count > 0)
                {
                    // Create action Statement.
                    statement = new StatementBuilder(
                        string.Format("WHERE id IN ({0})", string.Join(",", adUnitIds.ToArray()))).
                                ToStatement();

                    // Create action.
                    DeactivateAdUnits action = new DeactivateAdUnits();

                    // Perform action.
                    UpdateResult result = inventoryService.performAdUnitAction(action, statement);

                    // Display results.
                    if (result != null && result.numChanges > 0)
                    {
                        Console.WriteLine("Number of ad units deactivated: {0}", result.numChanges);
                    }
                    else
                    {
                        Console.WriteLine("No ad units were deactivated.");
                    }
                }
            } catch (Exception ex) {
                Console.WriteLine("Failed to deactivate ad units. Exception says \"{0}\"", ex.Message);
            }
        }
    /// <summary>
    /// Gets all ad units for this user.
    /// </summary>
    /// <param name="user">The DfpUser to get the ad units for.</param>
    /// <returns>All ad units for this user.</returns>
    private static AdUnit[] GetAllAdUnits(DfpUser user) {
      // Create list to hold all ad units.
      List<AdUnit> adUnits = new List<AdUnit>();

      // Get InventoryService.
      InventoryService inventoryService =
          (InventoryService) user.GetService(DfpService.v201511.InventoryService);

      // Create a statement to get all ad units.
      StatementBuilder statementBuilder = new StatementBuilder()
          .OrderBy("id ASC")
          .Limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);

      // Set default for page.
      AdUnitPage page = new AdUnitPage();

      do {
        // Get ad units by statement.
        page = inventoryService.getAdUnitsByStatement(statementBuilder.ToStatement());

        if (page.results != null && page.results.Length > 0) {
          adUnits.AddRange(page.results);
        }
        statementBuilder.IncreaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
      } while (statementBuilder.GetOffset() < page.totalResultSetSize);
      return adUnits.ToArray();
    }