Пример #1
0
        /// <summary>
        /// Gets the ad group ad by ID.
        /// </summary>
        /// <param name="adGroupAdService">The AdGroupAdService instance.</param>
        /// <param name="adGroupId">ID of the ad group.</param>
        /// <param name="adId">ID of the ad to be retrieved.</param>
        /// <returns>The AdGroupAd if the item could be retrieved, null otherwise.
        /// </returns>
        private AdGroupAd GetAdGroupAd(AdGroupAdService adGroupAdService, long adGroupId, long adId)
        {
            // Create a selector.
            Selector selector = new Selector();

            selector.fields = new string[] { "Id", "Url" };

            // Restrict the fetch to only the selected ad group ID and ad ID.
            Predicate adGroupPredicate = new Predicate();

            adGroupPredicate.field     = "AdGroupId";
            adGroupPredicate.@operator = PredicateOperator.EQUALS;
            adGroupPredicate.values    = new string[] { adGroupId.ToString() };

            Predicate adPredicate = new Predicate();

            adPredicate.field     = "Id";
            adPredicate.@operator = PredicateOperator.EQUALS;
            adPredicate.values    = new string[] { adId.ToString() };

            selector.predicates = new Predicate[] { adGroupPredicate, adPredicate };

            // Get the ad.
            AdGroupAdPage page = adGroupAdService.get(selector);

            if (page != null && page.entries != null && page.entries.Length > 0)
            {
                return(page.entries[0]);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Gets the ad group ad by ID.
        /// </summary>
        /// <param name="adGroupAdService">The AdGroupAdService instance.</param>
        /// <param name="adGroupId">ID of the ad group.</param>
        /// <param name="adId">ID of the ad to be retrieved.</param>
        /// <returns>The AdGroupAd if the item could be retrieved, null otherwise.
        /// </returns>
        private AdGroupAd GetAdGroupAd(AdGroupAdService adGroupAdService, long adGroupId, long adId)
        {
            // Create a selector.
            Selector selector = new Selector()
            {
                fields     = new string[] { Ad.Fields.Id, Ad.Fields.Url },
                predicates = new Predicate[] {
                    // Restrict the fetch to only the selected ad group ID and ad ID.
                    Predicate.Equals(AdGroupAd.Fields.AdGroupId, adGroupId),
                    Predicate.Equals(Ad.Fields.Id, adId)
                }
            };

            // Get the ad.
            AdGroupAdPage page = adGroupAdService.get(selector);

            if (page != null && page.entries != null && page.entries.Length > 0)
            {
                return(page.entries[0]);
            }
            else
            {
                return(null);
            }
        }
Пример #3
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">Id of the campaign for which disapproved ads
        /// are retrieved.</param>
        public void Run(AdWordsUser user, long campaignId)
        {
            // Get the AdGroupAdService.
            AdGroupAdService service =
                (AdGroupAdService)user.GetService(AdWordsService.v201705.AdGroupAdService);

            // Create the selector.
            Selector selector = new Selector()
            {
                fields = new string[] {
                    Ad.Fields.Id, AdGroupAd.Fields.PolicySummary
                },
                predicates = new Predicate[] {
                    Predicate.Equals(AdGroup.Fields.CampaignId, campaignId),
                },
                paging = Paging.Default
            };

            AdGroupAdPage page = new AdGroupAdPage();
            int           disapprovedAdsCount = 0;

            try {
                do
                {
                    // Get the disapproved ads.
                    page = service.get(selector);

                    // Display the results.
                    if (page != null && page.entries != null)
                    {
                        foreach (AdGroupAd adGroupAd in page.entries)
                        {
                            AdGroupAdPolicySummary policySummary = adGroupAd.policySummary;
                            if (policySummary.combinedApprovalStatus != PolicyApprovalStatus.DISAPPROVED)
                            {
                                // Skip ad group ads that are not disapproved.
                                continue;
                            }
                            disapprovedAdsCount++;
                            Console.WriteLine("Ad with ID {0} and type '{1}' was disapproved with the " +
                                              "following policy topic entries: ", adGroupAd.ad.id, adGroupAd.ad.AdType);
                            foreach (PolicyTopicEntry policyTopicEntry in policySummary.policyTopicEntries)
                            {
                                Console.WriteLine("  topic id: {0}, topic name: '{1}'",
                                                  policyTopicEntry.policyTopicId, policyTopicEntry.policyTopicName);
                            }
                        }
                    }

                    selector.paging.IncreaseOffset();
                } while (selector.paging.startIndex < page.totalNumEntries);
                Console.WriteLine("Number of disapproved ads found: {0}", disapprovedAdsCount);
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to get disapproved ads.", e);
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">Id of the campaign for which disapproved ads
        /// are retrieved.</param>
        public void Run(AdWordsUser user, long campaignId)
        {
            // Get the AdGroupAdService.
            AdGroupAdService service =
                (AdGroupAdService)user.GetService(AdWordsService.v201502.AdGroupAdService);

            // Create the selector.
            Selector selector = new Selector()
            {
                fields = new string[] {
                    Ad.Fields.Id, AdGroupAd.Fields.AdGroupCreativeApprovalStatus,
                    AdGroupAd.Fields.AdGroupAdDisapprovalReasons
                },
                predicates = new Predicate[] {
                    Predicate.Equals(AdGroup.Fields.CampaignId, campaignId),
                    Predicate.Equals(AdGroupAd.Fields.AdGroupCreativeApprovalStatus,
                                     AdGroupAdApprovalStatus.DISAPPROVED.ToString())
                },
                paging = Paging.Default
            };

            AdGroupAdPage page = new AdGroupAdPage();

            try {
                do
                {
                    // Get the disapproved ads.
                    page = service.get(selector);

                    // Display the results.
                    if (page != null && page.entries != null)
                    {
                        int i = selector.paging.startIndex;
                        foreach (AdGroupAd adGroupAd in page.entries)
                        {
                            Console.WriteLine("{0}) Ad id {1} has been disapproved for the following " +
                                              "reason(s):", i + 1, adGroupAd.ad.id);
                            foreach (string reason in adGroupAd.disapprovalReasons)
                            {
                                Console.WriteLine("    {0}", reason);
                            }
                            i++;
                        }
                    }
                    selector.paging.IncreaseOffset();
                } while (selector.paging.startIndex < page.totalNumEntries);
                Console.WriteLine("Number of disapproved ads found: {0}", page.totalNumEntries);
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to get disapproved ads.", e);
            }
        }
Пример #5
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">Id of the ad group from which text ads are
        /// retrieved.</param>
        public void Run(AdWordsUser user, long adGroupId)
        {
            // Get the AdGroupAdService.
            AdGroupAdService service =
                (AdGroupAdService)user.GetService(AdWordsService.v201609.AdGroupAdService);

            // Create a selector.
            Selector selector = new Selector()
            {
                fields = new string[] {
                    TextAd.Fields.Id, AdGroupAd.Fields.Status, TextAd.Fields.Headline,
                    TextAd.Fields.Description1, TextAd.Fields.Description2, TextAd.Fields.DisplayUrl
                },
                ordering   = new OrderBy[] { OrderBy.Asc(TextAd.Fields.Id) },
                predicates = new Predicate[] {
                    // Restrict the fetch to only the selected ad group id.
                    Predicate.Equals(AdGroupAd.Fields.AdGroupId, adGroupId),

                    // Retrieve only text ads.
                    Predicate.Equals("AdType", "TEXT_AD"),

                    // By default disabled ads aren't returned by the selector. To return
                    // them include the DISABLED status in the statuses field.
                    Predicate.In(AdGroupAd.Fields.Status, new string[] {
                        AdGroupAdStatus.ENABLED.ToString(),
                        AdGroupAdStatus.PAUSED.ToString(),
                        AdGroupAdStatus.DISABLED.ToString()
                    })
                },
                paging = Paging.Default
            };

            AdGroupAdPage page = new AdGroupAdPage();

            try {
                do
                {
                    // Get the text ads.
                    page = service.get(selector);

                    // Display the results.
                    if (page != null && page.entries != null)
                    {
                        int i = selector.paging.startIndex;

                        foreach (AdGroupAd adGroupAd in page.entries)
                        {
                            TextAd textAd = (TextAd)adGroupAd.ad;
                            Console.WriteLine("{0}) Ad id is {1} and status is {2}", i + 1, textAd.id,
                                              adGroupAd.status);
                            Console.WriteLine("  {0}\n  {1}\n  {2}\n  {3}", textAd.headline,
                                              textAd.description1, textAd.description2, textAd.displayUrl);
                            i++;
                        }
                    }
                    selector.paging.IncreaseOffset();
                } while (selector.paging.startIndex < page.totalNumEntries);
                Console.WriteLine("Number of text ads found: {0}", page.totalNumEntries);
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to get text ads", e);
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">Id of the campaign for which disapproved ads
        /// are retrieved.</param>
        public void Run(AdWordsUser user, long campaignId)
        {
            using (AdGroupAdService adGroupAdService =
                       (AdGroupAdService)user.GetService(AdWordsService.v201802.AdGroupAdService)) {
                // Create the selector.
                Selector selector = new Selector()
                {
                    fields = new string[] {
                        Ad.Fields.Id, AdGroupAd.Fields.PolicySummary
                    },
                    predicates = new Predicate[] {
                        Predicate.Equals(AdGroup.Fields.CampaignId, campaignId),
                        Predicate.Equals(AdGroupAdPolicySummary.Fields.CombinedApprovalStatus,
                                         PolicyApprovalStatus.DISAPPROVED.ToString())
                    },
                    paging = Paging.Default
                };

                AdGroupAdPage page = new AdGroupAdPage();
                int           disapprovedAdsCount = 0;

                try {
                    do
                    {
                        // Get the disapproved ads.
                        page = adGroupAdService.get(selector);

                        // Display the results.
                        if (page != null && page.entries != null)
                        {
                            foreach (AdGroupAd adGroupAd in page.entries)
                            {
                                AdGroupAdPolicySummary policySummary = adGroupAd.policySummary;
                                disapprovedAdsCount++;
                                Console.WriteLine("Ad with ID {0} and type '{1}' was disapproved with the " +
                                                  "following policy topic entries: ", adGroupAd.ad.id, adGroupAd.ad.AdType);
                                // Display the policy topic entries related to the ad disapproval.
                                foreach (PolicyTopicEntry policyTopicEntry in policySummary.policyTopicEntries)
                                {
                                    Console.WriteLine("  topic id: {0}, topic name: '{1}'",
                                                      policyTopicEntry.policyTopicId, policyTopicEntry.policyTopicName);
                                    // Display the attributes and values that triggered the policy topic.
                                    if (policyTopicEntry.policyTopicEvidences != null)
                                    {
                                        foreach (PolicyTopicEvidence evidence in
                                                 policyTopicEntry.policyTopicEvidences)
                                        {
                                            Console.WriteLine("    evidence type: {0}",
                                                              evidence.policyTopicEvidenceType);
                                            if (evidence.evidenceTextList != null)
                                            {
                                                for (int i = 0; i < evidence.evidenceTextList.Length; i++)
                                                {
                                                    Console.WriteLine("      evidence text[{0}]: {1}",
                                                                      i, evidence.evidenceTextList[i]);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        selector.paging.IncreaseOffset();
                    } while (selector.paging.startIndex < page.totalNumEntries);
                    Console.WriteLine("Number of disapproved ads found: {0}", disapprovedAdsCount);
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to get disapproved ads.", e);
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">Id of the ad group from which text ads are
        /// retrieved.</param>
        public void Run(AdWordsUser user, long adGroupId)
        {
            // Get the AdGroupAdService.
            AdGroupAdService service =
                (AdGroupAdService)user.GetService(AdWordsService.v201402.AdGroupAdService);

            // Create a selector.
            Selector selector = new Selector();

            selector.fields = new string[] { "Id", "Status", "Headline", "Description1", "Description2",
                                             "DisplayUrl" };

            // Set the sort order.
            OrderBy orderBy = new OrderBy();

            orderBy.field     = "Id";
            orderBy.sortOrder = SortOrder.ASCENDING;
            selector.ordering = new OrderBy[] { orderBy };

            // Restrict the fetch to only the selected ad group id.
            Predicate adGroupPredicate = new Predicate();

            adGroupPredicate.field     = "AdGroupId";
            adGroupPredicate.@operator = PredicateOperator.EQUALS;
            adGroupPredicate.values    = new string[] { adGroupId.ToString() };

            // Retrieve only text ads.
            Predicate typePredicate = new Predicate();

            typePredicate.field     = "AdType";
            typePredicate.@operator = PredicateOperator.EQUALS;
            typePredicate.values    = new string[] { "TEXT_AD" };

            // By default disabled ads aren't returned by the selector. To return
            // them include the DISABLED status in the statuses field.
            Predicate statusPredicate = new Predicate();

            statusPredicate.field     = "Status";
            statusPredicate.@operator = PredicateOperator.IN;

            statusPredicate.values = new string[] { AdGroupAdStatus.ENABLED.ToString(),
                                                        AdGroupAdStatus.PAUSED.ToString(), AdGroupAdStatus.DISABLED.ToString() };

            selector.predicates = new Predicate[] { adGroupPredicate, statusPredicate, typePredicate };

            // Select the selector paging.
            selector.paging = new Paging();

            int offset   = 0;
            int pageSize = 500;

            AdGroupAdPage page = new AdGroupAdPage();

            try {
                do
                {
                    selector.paging.startIndex    = offset;
                    selector.paging.numberResults = pageSize;

                    // Get the text ads.
                    page = service.get(selector);

                    // Display the results.
                    if (page != null && page.entries != null)
                    {
                        int i = offset;

                        foreach (AdGroupAd adGroupAd in page.entries)
                        {
                            TextAd textAd = (TextAd)adGroupAd.ad;
                            Console.WriteLine("{0}) Ad id is {1} and status is {2}", i + 1, textAd.id,
                                              adGroupAd.status);
                            Console.WriteLine("  {0}\n  {1}\n  {2}\n  {3}", textAd.headline,
                                              textAd.description1, textAd.description2, textAd.displayUrl);
                            i++;
                        }
                    }
                    offset += pageSize;
                } while (offset < page.totalNumEntries);
                Console.WriteLine("Number of text ads found: {0}", page.totalNumEntries);
            } catch (Exception ex) {
                throw new System.ApplicationException("Failed to get text ads", ex);
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">Id of the ad group from which third party
        /// redirect ads are retrieved.</param>
        public void Run(AdWordsUser user, long adGroupId)
        {
            // Get the AdGroupAdService.
            AdGroupAdService service =
                (AdGroupAdService)user.GetService(AdWordsService.v201509.AdGroupAdService);

            // Create a selector.
            Selector selector = new Selector()
            {
                fields = new string[] {
                    Ad.Fields.Id, AdGroupAd.Fields.Status, ThirdPartyRedirectAd.Fields.Url,
                    ThirdPartyRedirectAd.Fields.DisplayUrl, ThirdPartyRedirectAd.Fields.RichMediaAdSnippet
                },
                ordering   = new OrderBy[] { OrderBy.Asc(Ad.Fields.Id) },
                predicates = new Predicate[] {
                    // Restrict the fetch to only the selected ad group id.
                    Predicate.Equals(AdGroupAd.Fields.AdGroupId, adGroupId.ToString()),

                    // Restrieve only third party redirect ads.
                    Predicate.Equals("AdType", "THIRD_PARTY_REDIRECT_AD"),

                    // By default disabled ads aren't returned by the selector. To return
                    // them include the DISABLED status in the statuses field.
                    Predicate.In(AdGroupAd.Fields.Status, new string[] {
                        AdGroupAdStatus.ENABLED.ToString(),
                        AdGroupAdStatus.PAUSED.ToString(),
                        AdGroupAdStatus.DISABLED.ToString()
                    })
                },
                paging = Paging.Default
            };

            AdGroupAdPage page = new AdGroupAdPage();

            try {
                do
                {
                    // Get the third party redirect ads.
                    page = service.get(selector);

                    // Display the results.
                    if (page != null && page.entries != null)
                    {
                        int i = selector.paging.startIndex;

                        foreach (AdGroupAd adGroupAd in page.entries)
                        {
                            ThirdPartyRedirectAd thirdPartyRedirectAd = (ThirdPartyRedirectAd)adGroupAd.ad;
                            Console.WriteLine("{0}) Ad id is {1} and status is {2}", i, thirdPartyRedirectAd.id,
                                              adGroupAd.status);
                            Console.WriteLine("  Url: {0}\n  Display Url: {1}\n  Snippet:{2}",
                                              thirdPartyRedirectAd.finalUrls[0], thirdPartyRedirectAd.displayUrl,
                                              thirdPartyRedirectAd.snippet);
                            i++;
                        }
                    }
                    selector.paging.IncreaseOffset();
                } while (selector.paging.startIndex < page.totalNumEntries);
                Console.WriteLine("Number of third party redirect ads found: {0}", page.totalNumEntries);
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to get third party redirect ad(s).", e);
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">Id of the ad group from which expanded text ads
        /// are retrieved.</param>
        public void Run(AdWordsUser user, long adGroupId)
        {
            using (AdGroupAdService adGroupAdService =
                       (AdGroupAdService)user.GetService(AdWordsService.v201802.AdGroupAdService)) {
                // Create a selector.
                Selector selector = new Selector()
                {
                    fields = new string[] {
                        ExpandedTextAd.Fields.Id, AdGroupAd.Fields.Status, ExpandedTextAd.Fields.HeadlinePart1,
                        ExpandedTextAd.Fields.HeadlinePart2, ExpandedTextAd.Fields.Description, ExpandedTextAd.Fields.SystemManagedEntitySource
                    },
                    ordering   = new OrderBy[] { OrderBy.Asc(ExpandedTextAd.Fields.Id) },
                    predicates = new Predicate[] {
                        // Restrict the fetch to only the selected ad group id.
                        Predicate.Equals(AdGroupAd.Fields.AdGroupId, adGroupId),

                        // Retrieve only expanded text ads.
                        Predicate.Equals("AdType", "EXPANDED_TEXT_AD"),

                        // By default disabled ads aren't returned by the selector. To return
                        // them include the DISABLED status in the statuses field.
                        Predicate.In(AdGroupAd.Fields.Status, new string[] {
                            AdGroupAdStatus.ENABLED.ToString(),
                            AdGroupAdStatus.PAUSED.ToString(),
                            AdGroupAdStatus.DISABLED.ToString()
                        })
                    },
                    paging = Paging.Default
                };

                AdGroupAdPage page = new AdGroupAdPage();

                try {
                    do
                    {
                        // Get the expanded text ads.
                        page = adGroupAdService.get(selector);

                        // Display the results.
                        if (page != null && page.entries != null)
                        {
                            int i = selector.paging.startIndex;

                            foreach (AdGroupAd adGroupAd in page.entries)
                            {
                                ExpandedTextAd expandedTextAd = (ExpandedTextAd)adGroupAd.ad;
                                Console.WriteLine("{0} : Expanded text ad with ID '{1}', headline '{2} - {3}' " +
                                                  "and description '{4} was found.", i + 1, expandedTextAd.id,
                                                  expandedTextAd.headlinePart1, expandedTextAd.headlinePart2,
                                                  expandedTextAd.description);
                                i++;
                            }
                        }
                        selector.paging.IncreaseOffset();
                    } while (selector.paging.startIndex < page.totalNumEntries);
                    Console.WriteLine("Number of expanded text ads found: {0}", page.totalNumEntries);
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to get expanded text ads", e);
                }
            }
        }
Пример #10
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">Id of the ad group from which expanded text ads
        /// are retrieved.</param>
        public void Run(AdWordsUser user, long adGroupId)
        {
            using (AdGroupAdService adGroupAdService =
                       (AdGroupAdService)user.GetService(AdWordsService.v201806.AdGroupAdService)) {
                // Create a selector to select all ads for the specified ad group.
                Selector selector = new Selector()
                {
                    fields = new string[] {
                        ResponsiveSearchAd.Fields.Id, AdGroupAd.Fields.Status,
                        ResponsiveSearchAd.Fields.ResponsiveSearchAdHeadlines,
                        ResponsiveSearchAd.Fields.ResponsiveSearchAdDescriptions
                    },
                    ordering   = new OrderBy[] { OrderBy.Asc(ResponsiveSearchAd.Fields.Id) },
                    predicates = new Predicate[] {
                        // Restrict the fetch to only the selected ad group id.
                        Predicate.Equals(AdGroupAd.Fields.AdGroupId, adGroupId),

                        // Retrieve only responsive search ads.
                        Predicate.Equals("AdType", AdType.RESPONSIVE_SEARCH_AD.ToString()),
                    },
                    paging = Paging.Default
                };

                AdGroupAdPage page = new AdGroupAdPage();
                try {
                    do
                    {
                        // Get the responsive search ads.
                        page = adGroupAdService.get(selector);

                        // Display the results.
                        if (page != null && page.entries != null)
                        {
                            int i = selector.paging.startIndex;

                            foreach (AdGroupAd adGroupAd in page.entries)
                            {
                                ResponsiveSearchAd ad = (ResponsiveSearchAd)adGroupAd.ad;
                                Console.WriteLine($"{i + 1} New responsive search ad with ID {ad.id} and status " +
                                                  $"{adGroupAd.status} was found.");
                                Console.WriteLine("Headlines:");
                                foreach (AssetLink headline in ad.headlines)
                                {
                                    TextAsset textAsset = headline.asset as TextAsset;
                                    Console.WriteLine($"    {textAsset.assetText}");
                                    if (headline.pinnedFieldSpecified)
                                    {
                                        Console.WriteLine($"      (pinned to {headline.pinnedField})");
                                    }
                                }
                                Console.WriteLine("Descriptions:");
                                foreach (AssetLink description in ad.descriptions)
                                {
                                    TextAsset textAsset = description.asset as TextAsset;
                                    Console.WriteLine($"    {textAsset.assetText}");
                                    if (description.pinnedFieldSpecified)
                                    {
                                        Console.WriteLine($"      (pinned to {description.pinnedField})");
                                    }
                                }
                                i++;
                            }
                        }
                        selector.paging.IncreaseOffset();
                    } while (selector.paging.startIndex < page.totalNumEntries);
                    Console.WriteLine("Number of responsive search ads found: {0}", page.totalNumEntries);
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to get responsive search ads.", e);
                }
            }
        }
    /// <summary>
    /// Gets the ad group ad by ID.
    /// </summary>
    /// <param name="adGroupAdService">The AdGroupAdService instance.</param>
    /// <param name="adGroupId">ID of the ad group.</param>
    /// <param name="adId">ID of the ad to be retrieved.</param>
    /// <returns>The AdGroupAd if the item could be retrieved, null otherwise.
    /// </returns>
    private AdGroupAd GetAdGroupAd(AdGroupAdService adGroupAdService, long adGroupId, long adId) {
      // Create a selector.
      Selector selector = new Selector() {
        fields = new string[] { Ad.Fields.Id, Ad.Fields.Url },
        predicates = new Predicate[] {
          // Restrict the fetch to only the selected ad group ID and ad ID.
          Predicate.Equals(AdGroupAd.Fields.AdGroupId, adGroupId),
          Predicate.Equals(Ad.Fields.Id, adId)
        }
      };

      // Get the ad.
      AdGroupAdPage page = adGroupAdService.get(selector);

      if (page != null && page.entries != null && page.entries.Length > 0) {
        return page.entries[0];
      } else {
        return null;
      }
    }
Пример #12
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">Id of the campaign for which disapproved ads
        /// are retrieved.</param>
        public void Run(AdWordsUser user, long campaignId)
        {
            // Get the AdGroupAdService.
            AdGroupAdService service =
                (AdGroupAdService)user.GetService(AdWordsService.v201502.AdGroupAdService);

            // Create the selector.
            Selector selector = new Selector();

            selector.fields = new string[] { "Id", "AdGroupCreativeApprovalStatus",
                                             "AdGroupAdDisapprovalReasons" };

            // Create the filter.
            Predicate campaignPredicate = new Predicate();

            campaignPredicate.@operator = PredicateOperator.EQUALS;
            campaignPredicate.field     = "CampaignId";
            campaignPredicate.values    = new string[] { campaignId.ToString() };

            Predicate approvalPredicate = new Predicate();

            approvalPredicate.@operator = PredicateOperator.EQUALS;
            approvalPredicate.field     = "AdGroupCreativeApprovalStatus";
            approvalPredicate.values    = new string[] { AdGroupAdApprovalStatus.DISAPPROVED.ToString() };

            selector.predicates = new Predicate[] { campaignPredicate, approvalPredicate };

            // Set the selector paging.
            selector.paging = new Paging();

            int offset   = 0;
            int pageSize = 500;

            AdGroupAdPage page = new AdGroupAdPage();

            try {
                do
                {
                    selector.paging.startIndex    = offset;
                    selector.paging.numberResults = pageSize;

                    // Get the disapproved ads.
                    page = service.get(selector);

                    // Display the results.
                    if (page != null && page.entries != null)
                    {
                        int i = offset;
                        foreach (AdGroupAd adGroupAd in page.entries)
                        {
                            Console.WriteLine("{0}) Ad id {1} has been disapproved for the following " +
                                              "reason(s):", i, adGroupAd.ad.id);
                            foreach (string reason in adGroupAd.disapprovalReasons)
                            {
                                Console.WriteLine("    {0}", reason);
                            }
                            i++;
                        }
                    }
                    offset += pageSize;
                } while (offset < page.totalNumEntries);
                Console.WriteLine("Number of disapproved ads found: {0}", page.totalNumEntries);
            } catch (Exception ex) {
                throw new System.ApplicationException("Failed to get disapproved ads.", ex);
            }
        }
Пример #13
0
        public IEnumerable <GetAdsResponse> GetAllApprovedAds(AdWordsUser user, long campaignId)
        {
            var result = new List <GetAdsResponse>();

            using (AdGroupAdService adGroupAdService = (AdGroupAdService)user.GetService(AdWordsService.v201802.AdGroupAdService))
            {
                try
                {
                    // Create the selector.
                    Selector selector = new Selector()
                    {
                        fields = new string[]
                        {
                            Ad.Fields.Id,
                            AdGroupAd.Fields.PolicySummary
                        },
                        predicates = new Predicate[]
                        {
                            Predicate.Equals(AdGroup.Fields.CampaignId, campaignId),
                            Predicate.Equals(AdGroupAdPolicySummary.Fields.CombinedApprovalStatus, PolicyApprovalStatus.APPROVED.ToString())
                        },
                        paging = Paging.Default
                    };

                    AdGroupAdPage page             = new AdGroupAdPage();
                    int           approvedAdsCount = 0;

                    page = adGroupAdService.get(selector);

                    if (page.entries != null)
                    {
                        foreach (AdGroupAd adGroupAd in page.entries)
                        {
                            AdGroupAdPolicySummary policySummary = adGroupAd.policySummary;
                            approvedAdsCount++;
                            result.Add(new GetAdsResponse
                            {
                                AdId       = adGroupAd.ad.id,
                                AdHeadings = adGroupAd.ad.displayUrl
                            });
                        }
                    }
                    else
                    {
                        //Test Account
                        result.Add(new GetAdsResponse
                        {
                            AdId       = 2,
                            AdHeadings = "Heading Sample 2"
                        });
                        result.Add(new GetAdsResponse
                        {
                            AdId       = 3,
                            AdHeadings = "Heading Sample 3"
                        });
                    }
                }
                catch (Exception x)
                {
                    throw new Exception(x.Message);
                }
                return(result);
            }
        }