Beispiel #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)
        {
            // Get the ProductTemplateService.
            ProductTemplateService productTemplateService =
                (ProductTemplateService)user.GetService(DfpService.v201502.ProductTemplateService);

            // Set the ID of the product template.
            long productTemplateId = long.Parse(_T("INSERT_PRODUCT_TEMPLATE_ID_HERE"));

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

            try {
                // Get product templates by statement.
                ProductTemplatePage page = productTemplateService
                                           .getProductTemplatesByStatement(statementBuilder.ToStatement());

                ProductTemplate productTemplate = page.results[0];

                // Add geo targeting for Canada to the product template.
                Location countryLocation = new Location();
                countryLocation.id = 2124L;

                ProductTemplateTargeting productTemplateTargeting = productTemplate.targeting;
                GeoTargeting             geoTargeting             = productTemplateTargeting.geoTargeting;

                List <Location> existingTargetedLocations = new List <Location>();

                if (geoTargeting == null)
                {
                    geoTargeting = new GeoTargeting();
                }
                else if (geoTargeting.targetedLocations != null)
                {
                    existingTargetedLocations = new List <Location>(geoTargeting.targetedLocations);
                }

                existingTargetedLocations.Add(countryLocation);

                Location[] newTargetedLocations = new Location[existingTargetedLocations.Count];
                existingTargetedLocations.CopyTo(newTargetedLocations);
                geoTargeting.targetedLocations = newTargetedLocations;

                productTemplateTargeting.geoTargeting = geoTargeting;
                productTemplate.targeting             = productTemplateTargeting;

                // Update the product template on the server.
                ProductTemplate[] productTemplates = productTemplateService
                                                     .updateProductTemplates(new ProductTemplate[] { productTemplate });

                if (productTemplates != null)
                {
                    foreach (ProductTemplate updatedProductTemplate in productTemplates)
                    {
                        Console.WriteLine("A product template with ID = '{0}' and name '{1}' was updated.",
                                          updatedProductTemplate.id, updatedProductTemplate.name);
                    }
                }
                else
                {
                    Console.WriteLine("No product templates updated.");
                }
            } catch (Exception ex) {
                Console.WriteLine("Failed to update product templates. Exception says \"{0}\"",
                                  ex.Message);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Run the code examples.
        /// </summary>
        /// <param name="user">The DFP user object running the code examples.</param>
        public override void Run(DfpUser user)
        {
            // Get the ProductTemplateService.
            ProductTemplateService productTemplateService =
                (ProductTemplateService)user.GetService(DfpService.v201505.ProductTemplateService);

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

            // Create a product template.
            ProductTemplate productTemplate = new ProductTemplate();

            productTemplate.name        = "Product template #" + new Random().Next(int.MaxValue);
            productTemplate.description = "This product template creates standard proposal line items "
                                          + "targeting Chrome browsers with product segmentation on ad units and geo targeting.";

            // Set the name macro which will be used to generate the names of the products.
            // This will create a segmentation based on the line item type, ad unit, and location.
            productTemplate.nameMacro = "<line-item-type> - <ad-unit> - <template-name> - <location>";

            // Set the product type so the created proposal line items will be trafficked in DFP.
            productTemplate.productType = ProductType.DFP;

            // Set rate type to create CPM priced proposal line items.
            productTemplate.rateType = RateType.CPM;

            // Optionally set the creative rotation of the product to serve one or more creatives.
            productTemplate.roadblockingType = RoadblockingType.ONE_OR_MORE;

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

            creativeMasterPlaceholder.size =
                new Size()
            {
                width = 728, height = 90, isAspectRatio = false
            };

            // Create companion creative placeholders.
            CreativePlaceholder companionCreativePlaceholder = new CreativePlaceholder();

            companionCreativePlaceholder.size =
                new Size()
            {
                width = 300, height = 250, isAspectRatio = false
            };

            // Set the size of creatives that can be associated with the product template.
            productTemplate.creativePlaceholders =
                new CreativePlaceholder[] { creativeMasterPlaceholder, companionCreativePlaceholder };

            // Set the type of proposal line item to be created from the product template.
            productTemplate.lineItemType = LineItemType.STANDARD;

            // Get the root ad unit ID used to target the whole site.
            String rootAdUnitId = networkService.getCurrentNetwork().effectiveRootAdUnitId;

            // Create ad unit targeting for the root ad unit (i.e. the whole network).
            AdUnitTargeting adUnitTargeting = new AdUnitTargeting();

            adUnitTargeting.adUnitId           = rootAdUnitId;
            adUnitTargeting.includeDescendants = true;

            // Create geo targeting for the US.
            Location countryLocation = new Location();

            countryLocation.id = 2840L;

            // Create geo targeting for Hong Kong.
            Location regionLocation = new Location();

            regionLocation.id = 2344L;

            GeoTargeting geoTargeting = new GeoTargeting();

            geoTargeting.targetedLocations = new Location[] { countryLocation, regionLocation };

            // Add browser targeting to Chrome on the product template distinct from product
            // segmentation.
            Browser chromeBrowser = new Browser();

            chromeBrowser.id = 500072L;

            BrowserTargeting browserTargeting = new BrowserTargeting();

            browserTargeting.browsers = new Browser[] { chromeBrowser };

            ProductTemplateTargeting productTemplateTargeting = new ProductTemplateTargeting();

            productTemplateTargeting.browserTargeting = browserTargeting;

            productTemplate.targeting = productTemplateTargeting;

            // Add inventory and geo targeting as product segmentation.
            ProductSegmentation productSegmentation = new ProductSegmentation();

            productSegmentation.adUnitSegments = new AdUnitTargeting[] { adUnitTargeting };
            productSegmentation.geoSegment     = geoTargeting;

            productTemplate.productSegmentation = productSegmentation;

            try {
                // Create the product template on the server.
                ProductTemplate[] productTemplates = productTemplateService.createProductTemplates(
                    new ProductTemplate[] { productTemplate });

                foreach (ProductTemplate createdProductTemplate in productTemplates)
                {
                    Console.WriteLine("A product template with ID \"{0}\" and name \"{1}\" was created.",
                                      createdProductTemplate.id, createdProductTemplate.name);
                }
            } catch (Exception ex) {
                Console.WriteLine("Failed to create product templates. Exception says \"{0}\"",
                                  ex.Message);
            }
        }