/// <summary> /// Creates and returns a UserListRuleInfo object targeting a visit to a specified URL. /// </summary> /// <param name="url">The URL at which the rule will be targeted.</param> /// <returns>A populated UserListRuleInfo object.</returns> private UserListRuleInfo BuildVisitedSiteRuleInfo(string url) { // Creates a rule targeting any user that visited the specified URL. UserListRuleItemInfo userVisitedSiteRule = new UserListRuleItemInfo() { // Uses a built-in parameter to create a domain URL rule. Name = "url__", StringRuleItem = new UserListStringRuleItemInfo() { Operator = UserListStringRuleItemOperator.Equals, Value = url } }; // Creates a UserListRuleInfo object containing the new rule. UserListRuleInfo userVisitedSiteRuleInfo = new UserListRuleInfo() { RuleItemGroups = { new UserListRuleItemGroupInfo() { RuleItems ={ userVisitedSiteRule } } } }; return(userVisitedSiteRuleInfo); }
/// <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; } }
/// <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; } }