/// <summary>
        /// Run the code example.
        /// </summary>
        public void Run(DfpUser user)
        {
            using (ForecastService forecastService =
                       (ForecastService)user.GetService(DfpService.v201802.ForecastService)) {
                // Set the placement that the prospective line item will target.
                long[] targetPlacementIds = new long[] { long.Parse(_T("INSERT_PLACEMENT_ID_HERE")) };

                // Set the ID of the advertiser (company) to forecast for. Setting an advertiser
                // will cause the forecast to apply the appropriate unified blocking rules.
                long advertiserId = long.Parse(_T("INSERT_ADVERTISER_ID_HERE"));

                // Create prospective line item.
                LineItem lineItem = new LineItem();

                lineItem.targeting = new Targeting();
                lineItem.targeting.inventoryTargeting = new InventoryTargeting();
                lineItem.targeting.inventoryTargeting.targetedPlacementIds = targetPlacementIds;

                Size size = new Size();
                size.width  = 300;
                size.height = 250;

                // Create the creative placeholder.
                CreativePlaceholder creativePlaceholder = new CreativePlaceholder();
                creativePlaceholder.size = size;

                lineItem.creativePlaceholders = new CreativePlaceholder[] { creativePlaceholder };

                lineItem.lineItemType      = LineItemType.SPONSORSHIP;
                lineItem.startDateTimeType = StartDateTimeType.IMMEDIATELY;

                // Set the line item to run for one month.
                lineItem.endDateTime =
                    DateTimeUtilities.FromDateTime(System.DateTime.Now.AddMonths(1), "America/New_York");

                // Set the cost type to match the unit type.
                lineItem.costType = CostType.CPM;
                Goal goal = new Goal();
                goal.goalType        = GoalType.DAILY;
                goal.unitType        = UnitType.IMPRESSIONS;
                goal.units           = 50L;
                lineItem.primaryGoal = goal;

                try {
                    // Get availability forecast.
                    AvailabilityForecastOptions options = new AvailabilityForecastOptions();
                    options.includeContendingLineItems        = true;
                    options.includeTargetingCriteriaBreakdown = true;
                    ProspectiveLineItem prospectiveLineItem = new ProspectiveLineItem();
                    prospectiveLineItem.advertiserId = advertiserId;
                    prospectiveLineItem.lineItem     = lineItem;
                    AvailabilityForecast forecast =
                        forecastService.getAvailabilityForecast(prospectiveLineItem, options);

                    // Display results.
                    long   matched          = forecast.matchedUnits;
                    double availablePercent = (double)(forecast.availableUnits / (matched * 1.0)) * 100;
                    String unitType         = forecast.unitType.ToString().ToLower();
                    Console.WriteLine("{0} {1} matched.\n{2}% {3} available.", matched, unitType,
                                      availablePercent, unitType);

                    if (forecast.possibleUnitsSpecified)
                    {
                        double possiblePercent = (double)(forecast.possibleUnits / (matched * 1.0)) * 100;
                        Console.WriteLine("{0}% {1} possible.\n", possiblePercent, unitType);
                    }
                    Console.WriteLine("{0} contending line items.", (forecast.contendingLineItems != null) ?
                                      forecast.contendingLineItems.Length : 0);
                } catch (Exception e) {
                    Console.WriteLine("Failed to get forecast. Exception says \"{0}\"", e.Message);
                }
            }
        }
        /// <summary>
        /// Run the code example.
        /// </summary>
        public void Run(AdManagerUser user)
        {
            using (ForecastService forecastService = user.GetService <ForecastService>())
                using (NetworkService networkService = user.GetService <NetworkService>())
                {
                    // Set the ID of the advertiser (company) to forecast for. Setting an advertiser
                    // will cause the forecast to apply the appropriate unified blocking rules.
                    long advertiserId = long.Parse(_T("INSERT_ADVERTISER_ID_HERE"));

                    String rootAdUnitId = networkService.getCurrentNetwork().effectiveRootAdUnitId;

                    System.DateTime tomorrow = System.DateTime.Now.AddDays(1);

                    // Create prospective line item.
                    LineItem lineItem = new LineItem()
                    {
                        targeting = new Targeting()
                        {
                            inventoryTargeting = new InventoryTargeting()
                            {
                                targetedAdUnits = new AdUnitTargeting[] {
                                    new AdUnitTargeting()
                                    {
                                        adUnitId           = rootAdUnitId,
                                        includeDescendants = true
                                    }
                                }
                            }
                        },
                        creativePlaceholders = new CreativePlaceholder[] {
                            new CreativePlaceholder()
                            {
                                size = new Size()
                                {
                                    width  = 300,
                                    height = 250
                                }
                            }
                        },
                        lineItemType = LineItemType.SPONSORSHIP,
                        // Set the line item to run for 5 days.
                        startDateTime = DateTimeUtilities.FromDateTime(
                            tomorrow, "America/New_York"),
                        endDateTime = DateTimeUtilities.FromDateTime(
                            tomorrow.AddDays(5), "America/New_York"),
                        // Set the cost type to match the unit type.
                        costType    = CostType.CPM,
                        primaryGoal = new Goal()
                        {
                            goalType = GoalType.DAILY,
                            unitType = UnitType.IMPRESSIONS,
                            units    = 50L
                        }
                    };

                    try
                    {
                        // Get availability forecast.
                        AvailabilityForecastOptions options = new AvailabilityForecastOptions()
                        {
                            includeContendingLineItems = true,
                            // Targeting criteria breakdown can only be included if breakdowns
                            // are not speficied.
                            includeTargetingCriteriaBreakdown = false,
                            breakdown = new ForecastBreakdownOptions
                            {
                                timeWindows = new DateTime[] {
                                    lineItem.startDateTime,
                                    DateTimeUtilities.FromDateTime(tomorrow.AddDays(1),
                                                                   "America/New_York"),
                                    DateTimeUtilities.FromDateTime(tomorrow.AddDays(2),
                                                                   "America/New_York"),
                                    DateTimeUtilities.FromDateTime(tomorrow.AddDays(3),
                                                                   "America/New_York"),
                                    DateTimeUtilities.FromDateTime(tomorrow.AddDays(4),
                                                                   "America/New_York"),
                                    lineItem.endDateTime
                                },
                                targets = new ForecastBreakdownTarget[] {
                                    new ForecastBreakdownTarget()
                                    {
                                        // Optional name field to identify this breakdown
                                        // in the response.
                                        name      = "United States",
                                        targeting = new Targeting()
                                        {
                                            inventoryTargeting = new InventoryTargeting()
                                            {
                                                targetedAdUnits = new AdUnitTargeting[] {
                                                    new AdUnitTargeting()
                                                    {
                                                        adUnitId           = rootAdUnitId,
                                                        includeDescendants = true
                                                    }
                                                }
                                            },
                                            geoTargeting = new GeoTargeting()
                                            {
                                                targetedLocations = new Location[] {
                                                    new Location()
                                                    {
                                                        id = 2840L
                                                    }
                                                }
                                            }
                                        }
                                    }, new ForecastBreakdownTarget()
                                    {
                                        // Optional name field to identify this breakdown
                                        // in the response.
                                        name      = "Geneva",
                                        targeting = new Targeting()
                                        {
                                            inventoryTargeting = new InventoryTargeting()
                                            {
                                                targetedAdUnits = new AdUnitTargeting[] {
                                                    new AdUnitTargeting()
                                                    {
                                                        adUnitId           = rootAdUnitId,
                                                        includeDescendants = true
                                                    }
                                                }
                                            },
                                            geoTargeting = new GeoTargeting()
                                            {
                                                targetedLocations = new Location[] {
                                                    new Location()
                                                    {
                                                        id = 20133L
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        };
                        ProspectiveLineItem prospectiveLineItem = new ProspectiveLineItem()
                        {
                            advertiserId = advertiserId,
                            lineItem     = lineItem
                        };
                        AvailabilityForecast forecast =
                            forecastService.getAvailabilityForecast(prospectiveLineItem, options);

                        // Display results.
                        long   matched          = forecast.matchedUnits;
                        double availablePercent =
                            (double)(forecast.availableUnits / (matched * 1.0)) * 100;
                        String unitType = forecast.unitType.ToString().ToLower();
                        Console.WriteLine($"{matched} {unitType} matched.");
                        Console.WriteLine($"{availablePercent}% {unitType} available.");

                        if (forecast.possibleUnitsSpecified)
                        {
                            double possiblePercent =
                                (double)(forecast.possibleUnits / (matched * 1.0)) * 100;
                            Console.WriteLine($"{possiblePercent}% {unitType} possible.");
                        }
                        var contendingLineItems =
                            forecast.contendingLineItems ?? new ContendingLineItem[] { };
                        Console.WriteLine($"{contendingLineItems.Length} contending line items.");

                        if (forecast.breakdowns != null)
                        {
                            foreach (ForecastBreakdown breakdown in forecast.breakdowns)
                            {
                                Console.WriteLine("Forecast breakdown for {0} to {1}",
                                                  DateTimeUtilities.ToString(breakdown.startTime, "yyyy-MM-dd"),
                                                  DateTimeUtilities.ToString(breakdown.endTime, "yyyy-MM-dd"));
                                foreach (ForecastBreakdownEntry entry in breakdown.breakdownEntries)
                                {
                                    Console.WriteLine($"\t{entry.name}");
                                    long breakdownMatched = entry.forecast.matched;
                                    Console.WriteLine($"\t\t{breakdownMatched} {unitType} matched.");
                                    if (breakdownMatched > 0)
                                    {
                                        long   breakdownAvailable        = entry.forecast.available;
                                        double breakdownAvailablePercent =
                                            (double)(breakdownAvailable / (breakdownMatched * 1.0)) * 100;
                                        Console.WriteLine(
                                            $"\t\t{breakdownAvailablePercent}% {unitType} available");
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Failed to get forecast. Exception says \"{0}\"", e.Message);
                    }
                }
        }
        public void TestGetAvailabilityForecast()
        {
            TestUtils utils = new TestUtils();

            LineItem lineItem = new LineItem();

            lineItem.name = string.Format("Line item #{0}", utils.GetTimeStamp());

            lineItem.orderId = orderId;

            lineItem.targeting = new Targeting();
            lineItem.targeting.inventoryTargeting = new InventoryTargeting();
            lineItem.targeting.inventoryTargeting.targetedPlacementIds = new long[] { placementId };

            Size size1 = new Size();

            size1.width  = 300;
            size1.height = 250;

            Size size2 = new Size();

            size2.width  = 120;
            size2.height = 600;

            // Create the creative placeholders.
            CreativePlaceholder creativePlaceholder1 = new CreativePlaceholder();

            creativePlaceholder1.size = size1;

            CreativePlaceholder creativePlaceholder2 = new CreativePlaceholder();

            creativePlaceholder1.size = size2;

            lineItem.creativePlaceholders =
                new CreativePlaceholder[] { creativePlaceholder1, creativePlaceholder2 };

            lineItem.lineItemType = LineItemType.STANDARD;

            // Set start date time and end date time.
            lineItem.startDateTime =
                DateTimeUtilities.FromDateTime(System.DateTime.Today.AddDays(1), "America/New_York");
            lineItem.endDateTime =
                DateTimeUtilities.FromDateTime(System.DateTime.Today.AddMonths(1), "America/New_York");

            lineItem.costType    = CostType.CPM;
            lineItem.costPerUnit = new Money();
            lineItem.costPerUnit.currencyCode = "USD";
            lineItem.costPerUnit.microAmount  = 2000000;

            lineItem.creativeRotationType = CreativeRotationType.EVEN;
            lineItem.discountType         = LineItemDiscountType.PERCENTAGE;

            Goal goal = new Goal();

            goal.units           = 500000;
            goal.unitType        = UnitType.IMPRESSIONS;
            lineItem.primaryGoal = goal;

            ProspectiveLineItem prospectiveLineItem = new ProspectiveLineItem();

            prospectiveLineItem.lineItem = lineItem;

            AvailabilityForecast forecast = null;

            Assert.DoesNotThrow(delegate() {
                forecast = forecastService.getAvailabilityForecast(prospectiveLineItem,
                                                                   new AvailabilityForecastOptions());
            });
            Assert.NotNull(forecast);
            Assert.AreEqual(forecast.orderId, orderId);
            Assert.AreEqual(forecast.unitType, lineItem.primaryGoal.unitType);
        }
    public void TestGetAvailabilityForecast() {
      TestUtils utils = new TestUtils();

      LineItem lineItem = new LineItem();
      lineItem.name = string.Format("Line item #{0}", utils.GetTimeStamp());

      lineItem.orderId = orderId;

      lineItem.targeting = new Targeting();
      lineItem.targeting.inventoryTargeting = new InventoryTargeting();
      lineItem.targeting.inventoryTargeting.targetedPlacementIds = new long[] {placementId};

      Size size1 = new Size();
      size1.width = 300;
      size1.height = 250;

      Size size2 = new Size();
      size2.width = 120;
      size2.height = 600;

      // Create the creative placeholders.
      CreativePlaceholder creativePlaceholder1 = new CreativePlaceholder();
      creativePlaceholder1.size = size1;

      CreativePlaceholder creativePlaceholder2 = new CreativePlaceholder();
      creativePlaceholder1.size = size2;

      lineItem.creativePlaceholders =
          new CreativePlaceholder[] {creativePlaceholder1, creativePlaceholder2};

      lineItem.lineItemType = LineItemType.STANDARD;

      // Set start date time and end date time.
      lineItem.startDateTime =
          DateTimeUtilities.FromDateTime(System.DateTime.Today.AddDays(1), "America/New_York");
      lineItem.endDateTime =
          DateTimeUtilities.FromDateTime(System.DateTime.Today.AddMonths(1), "America/New_York");

      lineItem.costType = CostType.CPM;
      lineItem.costPerUnit = new Money();
      lineItem.costPerUnit.currencyCode = "USD";
      lineItem.costPerUnit.microAmount = 2000000;

      lineItem.creativeRotationType = CreativeRotationType.EVEN;
      lineItem.discountType = LineItemDiscountType.PERCENTAGE;

      Goal goal = new Goal();
      goal.units = 500000;
      goal.unitType = UnitType.IMPRESSIONS;
      lineItem.primaryGoal = goal;

      ProspectiveLineItem prospectiveLineItem = new ProspectiveLineItem();
      prospectiveLineItem.lineItem = lineItem;

      AvailabilityForecast forecast = null;
      Assert.DoesNotThrow(delegate() {
        forecast = forecastService.getAvailabilityForecast(prospectiveLineItem,
            new AvailabilityForecastOptions());
      });
      Assert.NotNull(forecast);
      Assert.AreEqual(forecast.orderId, orderId);
      Assert.AreEqual(forecast.unitType, lineItem.primaryGoal.unitType);
    }
    /// <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 ForecastService.
      ForecastService forecastService =
          (ForecastService)user.GetService(DfpService.v201508.ForecastService);

      // Set the placement that the prospective line item will target.
      long[] targetPlacementIds = new long[] {long.Parse(_T("INSERT_PLACEMENT_ID_HERE"))};

      // Set the ID of the advertiser (company) to forecast for. Setting an advertiser
      // will cause the forecast to apply the appropriate unified blocking rules.
      long advertiserId = long.Parse(_T("INSERT_ADVERTISER_ID_HERE"));

      // Create prospective line item.
      LineItem lineItem = new LineItem();

      lineItem.targeting = new Targeting();
      lineItem.targeting.inventoryTargeting = new InventoryTargeting();
      lineItem.targeting.inventoryTargeting.targetedPlacementIds = targetPlacementIds;

      Size size = new Size();
      size.width = 300;
      size.height = 250;

      // Create the creative placeholder.
      CreativePlaceholder creativePlaceholder = new CreativePlaceholder();
      creativePlaceholder.size = size;

      lineItem.creativePlaceholders = new CreativePlaceholder[] {creativePlaceholder};

      lineItem.lineItemType = LineItemType.SPONSORSHIP;
      lineItem.startDateTimeType = StartDateTimeType.IMMEDIATELY;

      // Set the line item to run for one month.
      lineItem.endDateTime =
          DateTimeUtilities.FromDateTime(System.DateTime.Now.AddMonths(1), "America/New_York");

      // Set the cost type to match the unit type.
      lineItem.costType = CostType.CPM;
      Goal goal = new Goal();
      goal.goalType = GoalType.DAILY;
      goal.unitType = UnitType.IMPRESSIONS;
      goal.units = 50L;
      lineItem.primaryGoal = goal;

      try {
        // Get availability forecast.
        AvailabilityForecastOptions options = new AvailabilityForecastOptions();
        options.includeContendingLineItems = true;
        options.includeTargetingCriteriaBreakdown = true;
        ProspectiveLineItem prospectiveLineItem = new ProspectiveLineItem();
        prospectiveLineItem.advertiserId = advertiserId;
        prospectiveLineItem.lineItem = lineItem;
        AvailabilityForecast forecast =
            forecastService.getAvailabilityForecast(prospectiveLineItem, options);

        // Display results.
        long matched = forecast.matchedUnits;
        double availablePercent = (double) (forecast.availableUnits / (matched * 1.0)) * 100;
        String unitType = forecast.unitType.ToString().ToLower();
        Console.WriteLine("{0} {1} matched.\n{2}%  available.", matched, unitType,
            availablePercent, unitType);

        if (forecast.possibleUnitsSpecified) {
          double possiblePercent = (double) (forecast.possibleUnits / (matched * 1.0)) * 100;
          Console.WriteLine("{0}% {1} possible.\n", possiblePercent, unitType);
        }
        Console.WriteLine("{0} contending line items.", (forecast.contendingLineItems != null) ?
            forecast.contendingLineItems.Length : 0);
      } catch (Exception e) {
        Console.WriteLine("Failed to get forecast. Exception says \"{0}\"", e.Message);
      }
    }
Пример #6
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 ForecastService.
            ForecastService forecastService =
                (ForecastService)user.GetService(DfpService.v201502.ForecastService);

            // Set the placement that the prospective line item will target.
            long[] targetPlacementIds = new long[] { long.Parse(_T("INSERT_PLACEMENT_ID_HERE")) };

            // Set the end date time to have the line line item run till.
            string endDateTime = "INSERT_END_DATE_TIME_HERE (yyyyMMdd HH:mm:ss)";

            // Create prospective line item.
            LineItem lineItem = new LineItem();

            lineItem.targeting = new Targeting();
            lineItem.targeting.inventoryTargeting = new InventoryTargeting();
            lineItem.targeting.inventoryTargeting.targetedPlacementIds = targetPlacementIds;

            Size size = new Size();

            size.width  = 300;
            size.height = 250;

            // Create the creative placeholder.
            CreativePlaceholder creativePlaceholder = new CreativePlaceholder();

            creativePlaceholder.size = size;

            lineItem.creativePlaceholders = new CreativePlaceholder[] { creativePlaceholder };

            lineItem.lineItemType      = LineItemType.SPONSORSHIP;
            lineItem.startDateTimeType = StartDateTimeType.IMMEDIATELY;

            lineItem.endDateTime = DateTimeUtilities.FromString(endDateTime);

            // Set the cost type to match the unit type.
            lineItem.costType = CostType.CPM;
            Goal goal = new Goal();

            goal.goalType        = GoalType.DAILY;
            goal.unitType        = UnitType.IMPRESSIONS;
            goal.units           = 50L;
            lineItem.primaryGoal = goal;

            try {
                // Get availability forecast.
                AvailabilityForecastOptions options = new AvailabilityForecastOptions();
                options.includeContendingLineItems        = true;
                options.includeTargetingCriteriaBreakdown = true;
                ProspectiveLineItem prospectiveLineItem = new ProspectiveLineItem();
                prospectiveLineItem.lineItem = lineItem;
                AvailabilityForecast forecast =
                    forecastService.getAvailabilityForecast(prospectiveLineItem, options);

                // Display results.
                long   matched          = forecast.matchedUnits;
                double availablePercent = (double)(forecast.availableUnits / (matched * 1.0)) * 100;
                String unitType         = forecast.unitType.ToString().ToLower();
                Console.WriteLine("{0} {1} matched.\n{2}%  available.", matched, unitType,
                                  availablePercent, unitType);

                if (forecast.possibleUnitsSpecified)
                {
                    double possiblePercent = (double)(forecast.possibleUnits / (matched * 1.0)) * 100;
                    Console.WriteLine("{0}% {1} possible.\n", possiblePercent, unitType);
                }
                Console.WriteLine("{0} contending line items.", (forecast.contendingLineItems != null) ?
                                  forecast.contendingLineItems.Length : 0);
            } catch (Exception ex) {
                Console.WriteLine("Failed to get forecast. Exception says \"{0}\"", ex.Message);
            }
        }