Google.Api.Ads.AdWords.Examples.CSharp.v201306.AddThirdPartyRedirectAds.Run C# (CSharp) Method

Run() public method

Runs the code example.
public Run ( Google.Api.Ads.AdWords.Lib.AdWordsUser user, long adGroupId ) : void
user Google.Api.Ads.AdWords.Lib.AdWordsUser The AdWords user.
adGroupId long Id of the ad group to which ads are added. ///
return void
        public void Run(AdWordsUser user, long adGroupId)
        {
            // Get the AdGroupAdService.
              AdGroupAdService service =
              (AdGroupAdService) user.GetService(AdWordsService.v201306.AdGroupAdService);

              // Create standard third party redirect ad.
              ThirdPartyRedirectAd standardAd = new ThirdPartyRedirectAd();
              standardAd.name = String.Format("Example third party ad #{0}",
              ExampleUtilities.GetRandomString());
              standardAd.url = "http://www.example.com";

              standardAd.dimensions = new Dimensions();
              standardAd.dimensions.height = 250;
              standardAd.dimensions.width = 300;

              standardAd.snippet = "<img src=\"http://goo.gl/HJM3L\"/>";

              // DoubleClick Rich Media Expandable format ID.
              standardAd.certifiedVendorFormatId = 232;
              standardAd.isCookieTargeted = false;
              standardAd.isUserInterestTargeted = false;
              standardAd.isTagged = false;
              standardAd.richMediaAdType = RichMediaAdRichMediaAdType.STANDARD;

              // Expandable Ad properties.
              standardAd.expandingDirections = new ThirdPartyRedirectAdExpandingDirection[] {
              ThirdPartyRedirectAdExpandingDirection.EXPANDING_UP,
              ThirdPartyRedirectAdExpandingDirection.EXPANDING_DOWN
              };

              standardAd.adAttributes = new RichMediaAdAdAttribute[] {
              RichMediaAdAdAttribute.ROLL_OVER_TO_EXPAND};

              // Create in-stream third party redirect ad.
              ThirdPartyRedirectAd inStreamAd = new ThirdPartyRedirectAd();
              inStreamAd.name = String.Format("Example third party ad #{0}",
              ExampleUtilities.GetRandomString());
              inStreamAd.url = "http://www.example.com";
              // Set the duration to 15 secs.
              inStreamAd.adDuration = 15000;
              inStreamAd.sourceUrl = "http://ad.doubleclick.net/pfadx/N270.126913.6102203221521/B3876671.21;dcadv=2215309;sz=0x0;ord=%5btimestamp%5d;dcmt=text/xml";
              inStreamAd.certifiedVendorFormatId = 303;
              inStreamAd.richMediaAdType = RichMediaAdRichMediaAdType.IN_STREAM_VIDEO;

              List<AdGroupAdOperation> operations = new List<AdGroupAdOperation>();

              foreach (ThirdPartyRedirectAd redirectAd in new
              ThirdPartyRedirectAd[] {standardAd, inStreamAd}) {
            // Set the ad group id.
            AdGroupAd adGroupAd = new AdGroupAd();
            adGroupAd.adGroupId = adGroupId;
            adGroupAd.ad = redirectAd;

            // Create the operation.
            AdGroupAdOperation operation = new AdGroupAdOperation();
            operation.@operator = Operator.ADD;
            operation.operand = adGroupAd;

            operations.Add(operation);
              }

              AdGroupAdReturnValue retVal = null;

              try {
            // Create the ads
            retVal = service.mutate(operations.ToArray());
            if (retVal != null && retVal.value != null) {
              // If you are adding multiple type of Ads, then you may need to check
              // for
              //
              // if (adGroupAd.ad is ThirdPartyRedirectAd) { ... }
              //
              // to identify the ad type.
              foreach (AdGroupAd newAdGroupAd in retVal.value) {
            Console.WriteLine("New third party redirect ad with url = \"{0}\" and id = {1}" +
                " was created.", ((ThirdPartyRedirectAd) newAdGroupAd.ad).url,
                newAdGroupAd.ad.id);
              }
            } else {
              Console.WriteLine("No third party redirect ads were created.");
            }
              } catch (Exception ex) {
            throw new System.ApplicationException("Failed to create third party redirect ads.", ex);
              }
        }

Usage Example

 /// <summary>
 /// Main method, to run this code example as a standalone application.
 /// </summary>
 /// <param name="args">The command line arguments.</param>
 public static void Main(string[] args)
 {
     AddThirdPartyRedirectAds codeExample = new AddThirdPartyRedirectAds();
       Console.WriteLine(codeExample.Description);
       try {
     long adGroupId = long.Parse("INSERT_ADGROUP_ID_HERE");
     codeExample.Run(new AdWordsUser(), adGroupId);
       } catch (Exception ex) {
     Console.WriteLine("An exception occurred while running this code example. {0}",
     ExampleUtilities.FormatException(ex));
       }
 }
AddThirdPartyRedirectAds