Пример #1
0
        /// <summary>
        /// Run the code examples.
        /// </summary>
        public void Run(AdManagerUser user)
        {
            using (ProductTemplateService productTemplateService =
                       user.GetService <ProductTemplateService>())

                using (NetworkService networkService =
                           user.GetService <NetworkService>())
                {
                    // Create a product template.
                    ProductTemplate productTemplate = new ProductTemplate();
                    productTemplate.name = "Programmatic product template #" +
                                           new Random().Next(int.MaxValue);
                    productTemplate.description =
                        "This product template creates programmatic proposal line " +
                        "items targeting all ad units with product segmentation on 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 required Marketplace information.
                    productTemplate.productTemplateMarketplaceInfo =
                        new ProductTemplateMarketplaceInfo()
                    {
                        adExchangeEnvironment = AdExchangeEnvironment.DISPLAY,
                    };

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

                    // Create the creative placeholder.
                    CreativePlaceholder creativePlaceholder = new CreativePlaceholder();
                    creativePlaceholder.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[]
                    {
                        creativePlaceholder
                    };

                    // 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 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 programmatic product template with ID \"{0}\" " +
                                "and name \"{1}\" was created.", createdProductTemplate.id,
                                createdProductTemplate.name);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(
                            "Failed to create product templates. Exception says \"{0}\"",
                            e.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 ProductTemplateService.
            ProductTemplateService productTemplateService =
                (ProductTemplateService)user.GetService(DfpService.v201505.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);
            }
        }
Пример #3
0
        /// <summary>
        /// Run the code example.
        /// </summary>
        public void Run(DfpUser user)
        {
            // Get the ProductTemplateService.
            ProductTemplateService productTemplateService =
                (ProductTemplateService)user.GetService(DfpService.v201702.ProductTemplateService);

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

            // [START product_template_statement] MOE:strip_line
            // Create statement to select a product template by ID.
            StatementBuilder statementBuilder = new StatementBuilder()
                                                .Where("id = :id")
                                                .OrderBy("id ASC")
                                                .Limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
                                                .AddValue("id", productTemplateId);
            // [END product_template_statement] MOE:strip_line

            // Set default for page.
            ProductTemplatePage page = new ProductTemplatePage();
            List <string>       productTemplateIds = new List <string>();

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

                    if (page.results != null)
                    {
                        int i = page.startIndex;
                        foreach (ProductTemplate productTemplate in page.results)
                        {
                            Console.WriteLine("{0}) Product template with ID ='{1}' will be activated.",
                                              i++, productTemplate.id);
                            productTemplateIds.Add(productTemplate.id.ToString());
                        }
                    }

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

                Console.WriteLine("Number of product templates to be activated: {0}",
                                  productTemplateIds.Count);

                if (productTemplateIds.Count > 0)
                {
                    // Modify statement.
                    statementBuilder.RemoveLimitAndOffset();

                    // [START its_activation_time] MOE:strip_line
                    // Create action.
                    Google.Api.Ads.Dfp.v201702.ActivateProductTemplates action =
                        new Google.Api.Ads.Dfp.v201702.ActivateProductTemplates();

                    // Perform action.
                    UpdateResult result = productTemplateService.performProductTemplateAction(action,
                                                                                              statementBuilder.ToStatement());
                    // [END its_activation_time] MOE:strip_line

                    // Display results.
                    if (result != null && result.numChanges > 0)
                    {
                        Console.WriteLine("Number of product templates activated: {0}", result.numChanges);
                    }
                    else
                    {
                        Console.WriteLine("No product templates were activated.");
                    }
                }
            } catch (Exception e) {
                Console.WriteLine("Failed to activate product templates. Exception says \"{0}\"",
                                  e.Message);
            }
        }