Esempio n. 1
0
        public void UpdateManagedClients()
        {
            try
            {
                var user = new AdWordsUser();
                // Get the ManagedCustomerService.
                ManagedCustomerService managedCustomerService =
                    (ManagedCustomerService)user.GetService(AdWordsService.v201609.ManagedCustomerService);
                managedCustomerService.RequestHeader.clientCustomerId = Settings.Default.RootMCCClientCustomerId;

                // Create selector.
                var selector = new Selector()
                {
                    fields = new string[]
                    {
                        ManagedCustomer.Fields.CanManageClients,
                        ManagedCustomer.Fields.CurrencyCode,
                        ManagedCustomer.Fields.CustomerId,
                        ManagedCustomer.Fields.DateTimeZone,
                        ManagedCustomer.Fields.Name,
                        ManagedCustomer.Fields.TestAccount,
                    },
                    paging = new Paging
                    {
                        numberResults = 5000,
                        startIndex    = 0
                    }
                };

                // list to build up all pages from api
                var managedCustomerPageList = new List <ManagedCustomerPage>();

                // holds the page between each call to the api
                ManagedCustomerPage page = null;

                // get all pages
                do
                {
                    // get current page
                    page = managedCustomerService.get(selector);

                    // add page to list
                    managedCustomerPageList.Add(page);

                    // advance paging to next page
                    selector.paging.IncreaseOffset();
                } while (selector.paging.startIndex < page.totalNumEntries);

                // serialize as xml
                var managedCustomerPageXml = managedCustomerPageList.ToArray().ToXml();

                // send to db for processing
                Repository.UploadMagagedClients(managedCustomerPageXml);
            }
            catch (Exception ex)
            {
                var newEx = new System.ApplicationException("Failed to Refresh Managed Client List", ex);
                Repository.LogError(newEx.ToString(), "ManagedClientProcessor");
            }
        }
        /// <summary>
        /// Gets the list of all descendant advertiser accounts under the manager
        /// account.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <returns>A list of customer IDs for descendant advertiser accounts.</returns>
        public static List <long> GetDescendantAdvertiserAccounts(AdWordsUser user)
        {
            List <long> retval = new List <long>();

            // Get the ManagedCustomerService.
            ManagedCustomerService managedCustomerService =
                (ManagedCustomerService)user.GetService(AdWordsService.v201809
                                                        .ManagedCustomerService);

            // Create selector.
            Selector selector = new Selector()
            {
                fields = new string[]
                {
                    ManagedCustomer.Fields.CustomerId
                },
                predicates = new Predicate[]
                {
                    // Select only advertiser accounts.
                    Predicate.Equals(ManagedCustomer.Fields.CanManageClients, false.ToString())
                },
                paging = Paging.Default
            };

            ManagedCustomerPage page = null;

            try
            {
                do
                {
                    page = managedCustomerService.get(selector);

                    if (page.entries != null)
                    {
                        foreach (ManagedCustomer customer in page.entries)
                        {
                            retval.Add(customer.customerId);
                        }
                    }

                    selector.paging.IncreaseOffset();
                } while (selector.paging.startIndex < page.totalNumEntries);
            }
            catch (Exception)
            {
                Console.WriteLine(
                    "Failed to retrieve advertiser accounts under the manager account.");
                throw;
            }

            return(retval);
        }
Esempio n. 3
0
        public IEnumerable <GetAdAccountsResponse> GetAllAccounts(AdWordsUser user)
        {
            ManagedCustomer customer = new ManagedCustomer();
            var             result   = new List <GetAdAccountsResponse>();

            using (ManagedCustomerService managedCustomerService = (ManagedCustomerService)user.GetService(AdWordsService.v201802.ManagedCustomerService))
            {
                try
                {
                    // Create selector.
                    Selector selector = new Selector();
                    selector.fields = new String[]
                    {
                        ManagedCustomer.Fields.CustomerId, ManagedCustomer.Fields.Name
                    };
                    selector.paging = Paging.Default;
                    ManagedCustomerPage accounts = null;
                    accounts = managedCustomerService.get(selector);

                    if (accounts.entries != null)
                    {
                        foreach (var account in accounts.entries)
                        {
                            result.Add(new GetAdAccountsResponse
                            {
                                CustomerId   = account.customerId,
                                CustomerName = account.name
                            });
                        }
                    }
                    else
                    {
                        //Test Account
                        result.Add(new GetAdAccountsResponse
                        {
                            CustomerId   = 3,
                            CustomerName = "Test account 3"
                        });
                        result.Add(new GetAdAccountsResponse
                        {
                            CustomerId   = 4,
                            CustomerName = "Test account 4"
                        });
                    }
                }
                catch (Exception x)
                {
                    throw new Exception(x.Message);
                }
                return(result);
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            using (ManagedCustomerService managedCustomerService =
                       (ManagedCustomerService)user.GetService(
                           AdWordsService.v201802.ManagedCustomerService)) {
                // Create selector.
                Selector selector = new Selector();
                selector.fields = new String[] {
                    ManagedCustomer.Fields.CustomerId, ManagedCustomer.Fields.Name
                };
                selector.paging = Paging.Default;

                // Map from customerId to customer node.
                Dictionary <long, ManagedCustomerTreeNode> customerIdToCustomerNode =
                    new Dictionary <long, ManagedCustomerTreeNode>();

                // Temporary cache to save links.
                List <ManagedCustomerLink> allLinks = new List <ManagedCustomerLink>();

                ManagedCustomerPage page = null;
                try {
                    do
                    {
                        page = managedCustomerService.get(selector);

                        if (page.entries != null)
                        {
                            // Create account tree nodes for each customer.
                            foreach (ManagedCustomer customer in page.entries)
                            {
                                ManagedCustomerTreeNode node = new ManagedCustomerTreeNode();
                                node.Account = customer;
                                customerIdToCustomerNode.Add(customer.customerId, node);
                            }

                            if (page.links != null)
                            {
                                allLinks.AddRange(page.links);
                            }
                        }
                        selector.paging.IncreaseOffset();
                    } while (selector.paging.startIndex < page.totalNumEntries);

                    // For each link, connect nodes in tree.
                    foreach (ManagedCustomerLink link in allLinks)
                    {
                        ManagedCustomerTreeNode managerNode =
                            customerIdToCustomerNode[link.managerCustomerId];
                        ManagedCustomerTreeNode childNode = customerIdToCustomerNode[link.clientCustomerId];
                        childNode.ParentNode = managerNode;
                        if (managerNode != null)
                        {
                            managerNode.ChildAccounts.Add(childNode);
                        }
                    }

                    // Find the root account node in the tree.
                    ManagedCustomerTreeNode rootNode = null;
                    foreach (ManagedCustomerTreeNode node in customerIdToCustomerNode.Values)
                    {
                        if (node.ParentNode == null)
                        {
                            rootNode = node;
                            break;
                        }
                    }

                    // Display account tree.
                    Console.WriteLine("CustomerId, Name");
                    Console.WriteLine(rootNode.ToTreeString(0, new StringBuilder()));
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to create ad groups.", e);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            // Get the ManagedCustomerService.
            ManagedCustomerService managedCustomerService = (ManagedCustomerService)user.GetService(
                AdWordsService.v201502.ManagedCustomerService);

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

            selector.fields = new String[] {
                ManagedCustomer.Fields.CustomerId, ManagedCustomer.Fields.Name
            };

            try {
                // Get results.
                ManagedCustomerPage page = managedCustomerService.get(selector);

                // Display serviced account graph.
                if (page.entries != null)
                {
                    // Create map from customerId to customer node.
                    Dictionary <long, ManagedCustomerTreeNode> customerIdToCustomerNode =
                        new Dictionary <long, ManagedCustomerTreeNode>();

                    // Create account tree nodes for each customer.
                    foreach (ManagedCustomer customer in page.entries)
                    {
                        ManagedCustomerTreeNode node = new ManagedCustomerTreeNode();
                        node.Account = customer;
                        customerIdToCustomerNode.Add(customer.customerId, node);
                    }

                    // For each link, connect nodes in tree.
                    if (page.links != null)
                    {
                        foreach (ManagedCustomerLink link in page.links)
                        {
                            ManagedCustomerTreeNode managerNode =
                                customerIdToCustomerNode[link.managerCustomerId];
                            ManagedCustomerTreeNode childNode = customerIdToCustomerNode[link.clientCustomerId];
                            childNode.ParentNode = managerNode;
                            if (managerNode != null)
                            {
                                managerNode.ChildAccounts.Add(childNode);
                            }
                        }
                    }

                    // Find the root account node in the tree.
                    ManagedCustomerTreeNode rootNode = null;
                    foreach (ManagedCustomer account in page.entries)
                    {
                        if (customerIdToCustomerNode[account.customerId].ParentNode == null)
                        {
                            rootNode = customerIdToCustomerNode[account.customerId];
                            break;
                        }
                    }

                    // Display account tree.
                    Console.WriteLine("CustomerId, Name");
                    Console.WriteLine(rootNode.ToTreeString(0, new StringBuilder()));
                }
                else
                {
                    Console.WriteLine("No serviced accounts were found.");
                }
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to create ad groups.", e);
            }
        }
Esempio n. 6
0
        private void GetAccountHierarchy_Click(object sender, EventArgs e)
        {
            Dictionary <string, string> headers = new Dictionary <string, string>()
            {
                { "DeveloperToken", this.DeveloperToken.Text },
                { "UserAgent", String.Format("Edge File Manager (version {0})", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()) },
                { "EnableGzipCompression", this.EnableGzipCompression.Text },
                { "ClientCustomerId", this.ClientCustomerId.Text },
                { "Email", this.Email.Text }
            };


            User = new AdWordsUser(headers);

            try
            {
                //Getting AuthToken
                (User.Config as AdWordsAppConfig).AuthToken = AdwordsUtill.GetAuthToken(User);
            }
            catch (Exception exc)
            {
                this.rchtxt.Text = exc.Message + " #### " + exc.InnerException != null? exc.InnerException.Message:string.Empty;
            }

            // Get the ManagedCustomerService.
            ManagedCustomerService managedCustomerService = (ManagedCustomerService)User.GetService(
                AdWordsService.v201302.ManagedCustomerService);

            managedCustomerService.RequestHeader.clientCustomerId = null;

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

            selector.fields = new String[] { "Login", "CustomerId", "Name" };

            try
            {
                // Get results.
                ManagedCustomerPage page = managedCustomerService.get(selector);

                // Display serviced account graph.
                if (page.entries != null)
                {
                    // Create map from customerId to customer node.
                    Dictionary <long, ManagedCustomerTreeNode> customerIdToCustomerNode = new Dictionary <long, ManagedCustomerTreeNode>();

                    // Create account tree nodes for each customer.
                    foreach (ManagedCustomer customer in page.entries)
                    {
                        ManagedCustomerTreeNode node = new ManagedCustomerTreeNode();
                        node.Account = customer;
                        customerIdToCustomerNode.Add(customer.customerId, node);
                    }

                    // For each link, connect nodes in tree.
                    if (page.links != null)
                    {
                        foreach (ManagedCustomerLink link in page.links)
                        {
                            ManagedCustomerTreeNode managerNode =
                                customerIdToCustomerNode[link.managerCustomerId];
                            ManagedCustomerTreeNode childNode = customerIdToCustomerNode[link.clientCustomerId];
                            childNode.ParentNode = managerNode;
                            if (managerNode != null)
                            {
                                managerNode.ChildAccounts.Add(childNode);
                            }
                        }
                    }

                    // Find the root account node in the tree.
                    ManagedCustomerTreeNode rootNode = null;
                    foreach (ManagedCustomer account in page.entries)
                    {
                        if (customerIdToCustomerNode[account.customerId].ParentNode == null)
                        {
                            rootNode = customerIdToCustomerNode[account.customerId];
                            break;
                        }
                    }

                    // Display account tree.
                    rchtxt.AppendText("Login, CustomerId, Name");
                    rchtxt.AppendText(rootNode.ToTreeString(0, new StringBuilder()));
                    // Console.WriteLine("Login, CustomerId, Name");
                    // Console.WriteLine(rootNode.ToTreeString(0, new StringBuilder()));
                }
                else
                {
                    Console.WriteLine("No serviced accounts were found.");
                }
            }
            catch (Exception ex)
            {
                throw new System.ApplicationException("Failed to create ad groups.", ex);
            }
        }