Пример #1
0
        /// <summary>
        /// Creates the customer match user list.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID for which the user list is added.
        /// </param>
        /// <returns>The resource name of the newly created user list</returns>
        private string CreateCustomerMatchUserList(GoogleAdsClient client, long customerId)
        {
            // Get the UserListService.
            UserListServiceClient service = client.GetService(Services.V4.UserListService);

            // Creates the user list.
            UserList userList = new UserList()
            {
                Name        = $"Customer Match list# {ExampleUtilities.GetShortRandomString()}",
                Description = "A list of customers that originated from email and physical" +
                              " addresses",
                // Customer Match user lists can use a membership life span of 10000 to
                // indicate unlimited; otherwise normal values apply.
                // Sets the membership life span to 30 days.
                MembershipLifeSpan = 30,
                CrmBasedUserList   = new CrmBasedUserListInfo()
                {
                    UploadKeyType = CustomerMatchUploadKeyType.ContactInfo
                }
            };
            // Creates the user list operation.
            UserListOperation operation = new UserListOperation()
            {
                Create = userList
            };

            // Issues a mutate request to add the user list and prints some information.
            MutateUserListsResponse response = service.MutateUserLists(
                customerId.ToString(), new[] { operation });
            string userListResourceName = response.Results[0].ResourceName;

            Console.WriteLine($"User list with resource name '{userListResourceName}' " +
                              $"was created.");
            return(userListResourceName);
        }
Пример #2
0
        public ActionResult List()
        {
            UserListServiceClient client = new UserListServiceClient();
            User[] users = client.GetUserList();

            return View(users);
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The client customer ID.</param>
        /// <param name="conversionActionIds">The IDs of the conversion actions for the basic user
        ///     list.</param>
        public void Run(GoogleAdsClient client, long customerId, long[] conversionActionIds)
        {
            // Creates the service client.
            UserListServiceClient userListServiceClient =
                client.GetService(Services.V4.UserListService);

            List <UserListActionInfo> userListActionInfoList = new List <UserListActionInfo>();

            foreach (long conversionActionId in conversionActionIds)
            {
                // Creates the UserListActionInfo object for a given conversion action. This
                // specifies the conversion action that, when triggered, will cause a user to be
                // added to a UserList.
                userListActionInfoList.Add(new UserListActionInfo
                {
                    ConversionAction =
                        ResourceNames.ConversionAction(customerId, conversionActionId)
                });
            }

            // Creates a basic user list info object with all of the conversion actions.
            BasicUserListInfo basicUserListInfo = new BasicUserListInfo();

            basicUserListInfo.Actions.Add(userListActionInfoList);

            // Creates the basic user list.
            UserList basicUserList = new UserList
            {
                Name               = $"Example BasicUserList #{ExampleUtilities.GetShortRandomString()}",
                Description        = "A list of people who have triggered one or more conversion actions",
                MembershipLifeSpan = 365L,
                BasicUserList      = basicUserListInfo,
                MembershipStatus   = UserListMembershipStatus.Open
            };

            // Creates the operation.
            UserListOperation operation = new UserListOperation
            {
                Create = basicUserList
            };

            try
            {
                // Adds the new user list.
                MutateUserListsResponse response = userListServiceClient.MutateUserLists
                                                       (customerId.ToString(), new[] { operation });

                // Prints the result.
                Console.WriteLine("Created basic user list with resource name: " +
                                  $"{response.Results.First().ResourceName}");
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }
Пример #4
0
 /// <summary>Snippet for GetUserList</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void GetUserListResourceNames()
 {
     // Create client
     UserListServiceClient userListServiceClient = UserListServiceClient.Create();
     // Initialize request argument(s)
     UserListName resourceName = UserListName.FromCustomerUserList("[CUSTOMER_ID]", "[USER_LIST_ID]");
     // Make the request
     UserList response = userListServiceClient.GetUserList(resourceName);
 }
Пример #5
0
 /// <summary>Snippet for GetUserList</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void GetUserList()
 {
     // Create client
     UserListServiceClient userListServiceClient = UserListServiceClient.Create();
     // Initialize request argument(s)
     string resourceName = "customers/[CUSTOMER]/userLists/[USER_LIST]";
     // Make the request
     UserList response = userListServiceClient.GetUserList(resourceName);
 }
        /// <summary>Snippet for GetUserListAsync</summary>
        /// <remarks>
        /// This snippet has been automatically generated for illustrative purposes only.
        /// It may require modifications to work in your environment.
        /// </remarks>
        public async Task GetUserListAsync()
        {
            // Create client
            UserListServiceClient userListServiceClient = await UserListServiceClient.CreateAsync();

            // Initialize request argument(s)
            string resourceName = "customers/[CUSTOMER]/userLists/[USER_LIST]";
            // Make the request
            UserList response = await userListServiceClient.GetUserListAsync(resourceName);
        }
Пример #7
0
        /// <summary>Snippet for GetUserListAsync</summary>
        /// <remarks>
        /// This snippet has been automatically generated for illustrative purposes only.
        /// It may require modifications to work in your environment.
        /// </remarks>
        public async Task GetUserListResourceNamesAsync()
        {
            // Create client
            UserListServiceClient userListServiceClient = await UserListServiceClient.CreateAsync();

            // Initialize request argument(s)
            UserListName resourceName = UserListName.FromCustomerUserList("[CUSTOMER_ID]", "[USER_LIST_ID]");
            // Make the request
            UserList response = await userListServiceClient.GetUserListAsync(resourceName);
        }
 /// <summary>Snippet for GetUserList</summary>
 public void GetUserListResourceNames()
 {
     // Snippet: GetUserList(UserListName, CallSettings)
     // Create client
     UserListServiceClient userListServiceClient = UserListServiceClient.Create();
     // Initialize request argument(s)
     UserListName resourceName = UserListName.FromCustomerUserList("[CUSTOMER]", "[USER_LIST]");
     // Make the request
     UserList response = userListServiceClient.GetUserList(resourceName);
     // End snippet
 }
Пример #9
0
 public void ListAsync()
 {
     AsyncManager.OutstandingOperations.Increment();
     UserListServiceClient client = new UserListServiceClient();
     client.GetUserListCompleted += (sender, e) =>
     {
         AsyncManager.Parameters["users"] = e.Result;
         AsyncManager.OutstandingOperations.Decrement();
     };
     client.GetUserListAsync();
 }
Пример #10
0
 /// <summary>Snippet for GetUserList</summary>
 public void GetUserList()
 {
     // Snippet: GetUserList(string, CallSettings)
     // Create client
     UserListServiceClient userListServiceClient = UserListServiceClient.Create();
     // Initialize request argument(s)
     string resourceName = "customers/[CUSTOMER_ID]/userLists/[USER_LIST_ID]";
     // Make the request
     UserList response = userListServiceClient.GetUserList(resourceName);
     // End snippet
 }
Пример #11
0
 /// <summary>Snippet for GetUserList</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void GetUserListRequestObject()
 {
     // Create client
     UserListServiceClient userListServiceClient = UserListServiceClient.Create();
     // Initialize request argument(s)
     GetUserListRequest request = new GetUserListRequest
     {
         ResourceNameAsUserListName = UserListName.FromCustomerUserList("[CUSTOMER]", "[USER_LIST]"),
     };
     // Make the request
     UserList response = userListServiceClient.GetUserList(request);
 }
Пример #12
0
 /// <summary>Snippet for MutateUserLists</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void MutateUserLists()
 {
     // Create client
     UserListServiceClient userListServiceClient = UserListServiceClient.Create();
     // Initialize request argument(s)
     string customerId = "";
     IEnumerable <UserListOperation> operations = new UserListOperation[]
     {
         new UserListOperation(),
     };
     // Make the request
     MutateUserListsResponse response = userListServiceClient.MutateUserLists(customerId, operations);
 }
Пример #13
0
        /// <summary>Snippet for GetUserListAsync</summary>
        public async Task GetUserListAsync()
        {
            // Snippet: GetUserListAsync(string, CallSettings)
            // Additional: GetUserListAsync(string, CancellationToken)
            // Create client
            UserListServiceClient userListServiceClient = await UserListServiceClient.CreateAsync();

            // Initialize request argument(s)
            string resourceName = "customers/[CUSTOMER_ID]/userLists/[USER_LIST_ID]";
            // Make the request
            UserList response = await userListServiceClient.GetUserListAsync(resourceName);

            // End snippet
        }
Пример #14
0
        /// <summary>Snippet for GetUserListAsync</summary>
        public async Task GetUserListResourceNamesAsync()
        {
            // Snippet: GetUserListAsync(UserListName, CallSettings)
            // Additional: GetUserListAsync(UserListName, CancellationToken)
            // Create client
            UserListServiceClient userListServiceClient = await UserListServiceClient.CreateAsync();

            // Initialize request argument(s)
            UserListName resourceName = UserListName.FromCustomerUserList("[CUSTOMER_ID]", "[USER_LIST_ID]");
            // Make the request
            UserList response = await userListServiceClient.GetUserListAsync(resourceName);

            // End snippet
        }
Пример #15
0
        /// <summary>Snippet for MutateUserListsAsync</summary>
        public async Task MutateUserListsAsync()
        {
            // Snippet: MutateUserListsAsync(string, IEnumerable<UserListOperation>, CallSettings)
            // Additional: MutateUserListsAsync(string, IEnumerable<UserListOperation>, CancellationToken)
            // Create client
            UserListServiceClient userListServiceClient = await UserListServiceClient.CreateAsync();

            // Initialize request argument(s)
            string customerId = "";
            IEnumerable <UserListOperation> operations = new UserListOperation[]
            {
                new UserListOperation(),
            };
            // Make the request
            MutateUserListsResponse response = await userListServiceClient.MutateUserListsAsync(customerId, operations);

            // End snippet
        }
 /// <summary>Snippet for MutateUserLists</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void MutateUserListsRequestObject()
 {
     // Create client
     UserListServiceClient userListServiceClient = UserListServiceClient.Create();
     // Initialize request argument(s)
     MutateUserListsRequest request = new MutateUserListsRequest
     {
         CustomerId = "",
         Operations =
         {
             new UserListOperation(),
         },
         PartialFailure = false,
         ValidateOnly   = false,
     };
     // Make the request
     MutateUserListsResponse response = userListServiceClient.MutateUserLists(request);
 }
Пример #17
0
        /// <summary>Snippet for MutateUserListsAsync</summary>
        public async Task MutateUserListsRequestObjectAsync()
        {
            // Snippet: MutateUserListsAsync(MutateUserListsRequest, CallSettings)
            // Additional: MutateUserListsAsync(MutateUserListsRequest, CancellationToken)
            // Create client
            UserListServiceClient userListServiceClient = await UserListServiceClient.CreateAsync();

            // Initialize request argument(s)
            MutateUserListsRequest request = new MutateUserListsRequest
            {
                CustomerId = "",
                Operations =
                {
                    new UserListOperation(),
                },
                PartialFailure = false,
                ValidateOnly   = false,
            };
            // Make the request
            MutateUserListsResponse response = await userListServiceClient.MutateUserListsAsync(request);

            // End snippet
        }
Пример #18
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID.</param>
        /// <param name="userListIds">The IDs of the lists to be used for the new combination user
        ///     list.</param>
        public void Run(GoogleAdsClient client, long customerId, long[] userListIds)
        {
            // Gets the UserListService client.
            UserListServiceClient userListServiceClient =
                client.GetService(Services.V6.UserListService);

            // Adds each of the provided list IDs to a list of rule operands specifying which lists
            // the operator should target.
            List <LogicalUserListOperandInfo> logicalUserListOperandInfoList =
                userListIds.Select(userListId => new LogicalUserListOperandInfo
            {
                UserList = ResourceNames.UserList(customerId, userListId)
            }).ToList();

            // Creates the UserListLogicalRuleInfo specifying that a user should be added to the new
            // list if they are present in any of the provided lists.
            UserListLogicalRuleInfo userListLogicalRuleInfo = new UserListLogicalRuleInfo
            {
                // Using ANY means that a user should be added to the combined list if they are
                // present on any of the lists targeted in the LogicalUserListOperandInfo. Use ALL
                // to add users present on all of the provided lists or NONE to add users that
                // aren't present on any of the targeted lists.
                Operator = UserListLogicalRuleOperatorEnum.Types.UserListLogicalRuleOperator.Any,
            };

            userListLogicalRuleInfo.RuleOperands.Add(logicalUserListOperandInfoList);

            LogicalUserListInfo logicalUserListInfo = new LogicalUserListInfo();

            logicalUserListInfo.Rules.Add(userListLogicalRuleInfo);

            // Creates the new combination user list.
            UserList userList = new UserList
            {
                Name = "My combination list of other user lists " +
                       $"#{ExampleUtilities.GetRandomString()}",
                LogicalUserList = logicalUserListInfo
            };

            // Creates the operation.
            UserListOperation operation = new UserListOperation
            {
                Create = userList
            };

            try
            {
                // Sends the request to add the user list and prints the response.
                MutateUserListsResponse response = userListServiceClient.MutateUserLists
                                                       (customerId.ToString(), new[] { operation });
                Console.WriteLine("Created combination user list with resource name: " +
                                  $"{response.Results.First().ResourceName}");
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }
Пример #19
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID for the conversion action is
        /// added.</param>
        public void Run(GoogleAdsClient client, long customerId)
        {
            // Get the user list service client.
            UserListServiceClient userListServiceClient =
                client.GetService(Services.V6.UserListService);

            try
            {
                // Create a rule targeting any user that visited the checkout page.
                UserListRuleItemInfo checkoutRule = new UserListRuleItemInfo
                {
                    // The rule variable name must match a corresponding key name fired from a pixel.
                    // To learn more about setting up remarketing tags, visit
                    // https://support.google.com/google-ads/answer/2476688.
                    // To learn more about remarketing events and parameters, visit
                    // https://support.google.com/google-ads/answer/7305793.
                    Name           = "ecomm_pagetype",
                    StringRuleItem = new UserListStringRuleItemInfo
                    {
                        Operator = UserListStringRuleItemOperator.Equals,
                        Value    = "checkout"
                    }
                };

                // Create a rule targeting any user that had more than one item in their cart.
                UserListRuleItemInfo cartSizeRule = new UserListRuleItemInfo
                {
                    // The rule variable name must match a corresponding key name fired from a pixel.
                    Name           = "cart_size",
                    NumberRuleItem = new UserListNumberRuleItemInfo
                    {
                        Operator = UserListNumberRuleItemOperator.GreaterThan,
                        Value    = 1.0
                    }
                };

                // Create a rule group that includes the checkout and cart size rules. Combining the
                // two rule items into a UserListRuleItemGroupInfo object causes Google Ads to AND
                // their rules together. To instead OR the rules together, each rule should be
                // placed in its own rule item group.
                UserListRuleItemGroupInfo checkoutAndCartSizeRuleGroup =
                    new UserListRuleItemGroupInfo();
                checkoutAndCartSizeRuleGroup.RuleItems.Add(checkoutRule);
                checkoutAndCartSizeRuleGroup.RuleItems.Add(cartSizeRule);

                // Create the RuleItem for checkout start date.
                // The tags and keys used below must have been in place in the past for the date
                // range specified in the rules.
                UserListRuleItemInfo startDateRule = new UserListRuleItemInfo
                {
                    // The rule variable name must match a corresponding key name fired from a pixel.
                    Name         = "checkoutdate",
                    DateRuleItem = new UserListDateRuleItemInfo
                    {
                        // Available UserListDateRuleItemOperators can be found at
                        // https://developers.google.com/google-ads/api/reference/rpc/latest/UserListDateRuleItemOperatorEnum.UserListDateRuleItemOperator
                        Operator = UserListDateRuleItemOperator.After,
                        Value    = "20191031"
                    }
                };

                // Create the RuleItem for checkout end date.
                UserListRuleItemInfo endDateRule = new UserListRuleItemInfo
                {
                    // The rule variable name must match a corresponding key name fired from a pixel.
                    Name         = "checkoutdate",
                    DateRuleItem = new UserListDateRuleItemInfo
                    {
                        Operator = UserListDateRuleItemOperator.Before,
                        Value    = "20200101"
                    }
                };

                // Create a rule group targeting users who checked out between November and December
                // by using the start and end date rules. Combining the two rule items into a
                // UserListRuleItemGroupInfo object causes Google Ads to AND their rules together.
                // To instead OR the rules together, each rule should be placed in its own rule item
                // group.
                UserListRuleItemGroupInfo checkoutDateRuleGroup = new UserListRuleItemGroupInfo();
                checkoutDateRuleGroup.RuleItems.Add(startDateRule);
                checkoutDateRuleGroup.RuleItems.Add(endDateRule);

                // Create an ExpressionRuleUserListInfo object, or a boolean rule that defines this
                // user list. The default rule_type for a UserListRuleInfo object is OR of ANDs
                // (disjunctive normal form). That is, rule items will be ANDed together within rule
                // item groups and the groups themselves will be ORed together.
                ExpressionRuleUserListInfo expressionRuleUserListInfo =
                    new ExpressionRuleUserListInfo
                {
                    Rule = new UserListRuleInfo()
                };
                expressionRuleUserListInfo.Rule.RuleItemGroups.Add(checkoutAndCartSizeRuleGroup);
                expressionRuleUserListInfo.Rule.RuleItemGroups.Add(checkoutDateRuleGroup);

                // Defines a representation of a user list that is generated by a rule.
                RuleBasedUserListInfo ruleBasedUserListInfo = new RuleBasedUserListInfo
                {
                    // Optional: To include past users in the user list, set the
                    // prepopulation status to REQUESTED.
                    PrepopulationStatus    = UserListPrepopulationStatus.Requested,
                    ExpressionRuleUserList = expressionRuleUserListInfo
                };

                // Create a user list.
                UserList userList = new UserList
                {
                    Name        = $"My expression rule user list #{ExampleUtilities.GetRandomString()}",
                    Description =
                        "Users who checked out in November or December OR visited the checkout " +
                        "page with more than one item in their cart",
                    MembershipLifeSpan = 90L,
                    MembershipStatus   = UserListMembershipStatus.Open,
                    RuleBasedUserList  = ruleBasedUserListInfo
                };

                // Create the operation.
                UserListOperation userListOperation = new UserListOperation
                {
                    Create = userList
                };

                // Add the user list and print the results.
                MutateUserListsResponse response =
                    userListServiceClient.MutateUserLists(customerId.ToString(),
                                                          new[] { userListOperation });

                string userListResourceName = response.Results.First().ResourceName;
                Console.WriteLine(
                    $"Created user list with resource name '{userListResourceName}'.");
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID.</param>
        // [START add_combined_rule_user_list]
        public void Run(GoogleAdsClient client, long customerId)
        {
            // Get the UserListServiceClient.
            UserListServiceClient userListServiceClient =
                client.GetService(Services.V10.UserListService);

            // Creates a rule targeting any user that visited a url that equals
            // 'http://example.com/example1'.
            UserListRuleInfo userVisitedSite1RuleInfo =
                BuildVisitedSiteRuleInfo("http://example.com/example1");

            // Creates a rule targeting any user that visited a url that equals
            // 'http://example.com/example2'.
            UserListRuleInfo userVisitedSite2RuleInfo =
                BuildVisitedSiteRuleInfo("http://example.com/example2");

            // Creates the user list where "Visitors of a page who did visit another page".
            // To create a user list where "Visitors of a page who did not visit another page",
            // change the UserListCombinedRuleOperator from .And to .AndNot.
            CombinedRuleUserListInfo combinedRuleUserListInfo = new CombinedRuleUserListInfo()
            {
                LeftOperand  = userVisitedSite1RuleInfo,
                RightOperand = userVisitedSite2RuleInfo,
                RuleOperator = UserListCombinedRuleOperator.And
            };

            // Defines a representation of a user list that is generated by a rule.
            RuleBasedUserListInfo ruleBasedUserListInfo = new RuleBasedUserListInfo()
            {
                // Optional: To include past users in the user list, set the prepopulation_status to
                // REQUESTED.
                PrepopulationStatus  = UserListPrepopulationStatus.Requested,
                CombinedRuleUserList = combinedRuleUserListInfo
            };

            // Creates a user list.
            UserList userList = new UserList()
            {
                Name = "All visitors to http://example.com/example1 AND " +
                       $"http://example.com/example2 #{ExampleUtilities.GetShortRandomString()}",
                Description = "Visitors of both http://example.com/example1 AND " +
                              "http://example.com/example2",
                MembershipStatus   = UserListMembershipStatus.Open,
                MembershipLifeSpan = 365L,
                RuleBasedUserList  = ruleBasedUserListInfo
            };

            // Creates the operation.
            UserListOperation operation = new UserListOperation()
            {
                Create = userList
            };

            try
            {
                // Adds the new user list and prints the result.
                MutateUserListsResponse response = userListServiceClient.MutateUserLists
                                                       (customerId.ToString(), new[] { operation });
                Console.WriteLine("Created user list with resource name:" +
                                  $"{response.Results.First().ResourceName}");
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }
Пример #21
0
        /// <summary>
        /// Creates a user list targeting users that have visited a given url.
        /// </summary>
        /// <param name="client">The Google Ads API client.</param>
        /// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
        /// <returns>The resource name of the newly created user list.</returns>
        // [START setup_remarketing]
        private string CreateUserList(GoogleAdsClient client, long customerId)
        {
            // Get the UserListService client.
            UserListServiceClient userListServiceClient =
                client.GetService(Services.V10.UserListService);

            // Create a rule targeting any user that visited a url containing 'example.com'.
            UserListRuleItemInfo rule = new UserListRuleItemInfo
            {
                // Use a built-in parameter to create a domain URL rule.
                Name           = "url__",
                StringRuleItem = new UserListStringRuleItemInfo
                {
                    Operator = UserListStringRuleItemOperator.Contains,
                    Value    = "example.com"
                }
            };

            // Specify that the user list targets visitors of a page based on the provided rule.
            ExpressionRuleUserListInfo expressionRuleUserListInfo = new ExpressionRuleUserListInfo
            {
                Rule = new UserListRuleInfo()
            };
            UserListRuleItemGroupInfo userListRuleItemGroupInfo = new UserListRuleItemGroupInfo();

            userListRuleItemGroupInfo.RuleItems.Add(rule);
            expressionRuleUserListInfo.Rule.RuleItemGroups.Add(userListRuleItemGroupInfo);

            // Define a representation of a user list that is generated by a rule.
            RuleBasedUserListInfo ruleBasedUserListInfo = new RuleBasedUserListInfo
            {
                // Optional: To include past users in the user list, set the prepopulation_status to
                // REQUESTED.
                PrepopulationStatus    = UserListPrepopulationStatus.Requested,
                ExpressionRuleUserList = expressionRuleUserListInfo
            };

            // Create the user list.
            UserList userList = new UserList
            {
                Name               = $"All visitors to example.com #{ExampleUtilities.GetRandomString()}",
                Description        = "Any visitor to any page of example.com",
                MembershipStatus   = UserListMembershipStatus.Open,
                MembershipLifeSpan = 365L,
                RuleBasedUserList  = ruleBasedUserListInfo
            };

            // Create the operation.
            UserListOperation userListOperation = new UserListOperation
            {
                Create = userList
            };

            // Add the user list, then print and return the new list's resource name.
            MutateUserListsResponse mutateUserListsResponse = userListServiceClient
                                                              .MutateUserLists(customerId.ToString(), new[] { userListOperation });
            string userListResourceName = mutateUserListsResponse.Results.First().ResourceName;

            Console.WriteLine($"Created user list with resource name '{userListResourceName}'.");

            return(userListResourceName);
        }
Пример #22
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID to which the new user list will be
        ///     added.</param>
        public void Run(GoogleAdsClient client, long customerId)
        {
            // Gets the UserListService.
            UserListServiceClient userListServiceClient =
                client.GetService(Services.V4.UserListService);

            // Creates the user targeting rules for each URL.
            UserListRuleItemInfo rule1 = BuildVisitedSiteRuleInfo("example.com/section1");
            UserListRuleItemInfo rule2 = BuildVisitedSiteRuleInfo("example.com/section2");

            // Combine the two rule items into a UserListRuleItemGroupInfo object so Google Ads will
            // AND their rules together. To instead OR the rules together, each rule should be
            // placed in its own rule item group.
            UserListRuleItemGroupInfo userListRuleItemGroupInfo = new UserListRuleItemGroupInfo();

            userListRuleItemGroupInfo.RuleItems.Add(rule1);
            userListRuleItemGroupInfo.RuleItems.Add(rule2);

            UserListRuleInfo userListRuleInfo = new UserListRuleInfo();

            userListRuleInfo.RuleItemGroups.Add(userListRuleItemGroupInfo);

            // Creates an ExpressionRuleUserListInfo object, or a boolean rule that defines this
            // user list. The default rule_type for a UserListRuleInfo object is OR of ANDs
            // (disjunctive normal form). That is, rule items will be ANDed together within rule
            // item groups and the groups themselves will be ORed together.
            ExpressionRuleUserListInfo expressionRuleUserListInfo = new ExpressionRuleUserListInfo
            {
                Rule = userListRuleInfo
            };

            // Defines a representation of a user list that is generated by a rule.
            RuleBasedUserListInfo ruleBasedUserListInfo = new RuleBasedUserListInfo
            {
                // Optional: To include past users in the user list, set the prepopulation_status to
                // REQUESTED.
                PrepopulationStatus    = UserListPrepopulationStatus.Requested,
                ExpressionRuleUserList = expressionRuleUserListInfo
            };

            // Creates a new user list.
            UserList userList = new UserList
            {
                Name = "All visitors to example.com/section1 AND example.com/section2 " +
                       $"#{ExampleUtilities.GetRandomString()}",
                Description        = "Visitors of both example.com/section1 AND example.com/section2",
                MembershipStatus   = UserListMembershipStatus.Open,
                MembershipLifeSpan = 365L,
                RuleBasedUserList  = ruleBasedUserListInfo
            };

            // Creates the operation.
            UserListOperation operation = new UserListOperation()
            {
                Create = userList
            };

            try
            {
                // Adds the user list.
                MutateUserListsResponse response = userListServiceClient.MutateUserLists
                                                       (customerId.ToString(), new[] { operation });

                // Displays the results.
                Console.WriteLine("Created new user list with resource name: " +
                                  $"'{response.Results.First().ResourceName}'.");
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }