您可以组合使用两个或多个现有用户名单,以实现更精细的定位。
创建用户列表
您可以构建现有用户名单的自定义组合,作为具有 UserListLogicalRuleInfo 字段的 logical_user_list。logical_user_list 中的规则是 AND 的,因此用户必须与每个规则匹配才能被视为在列表中。不过,每条规则都允许您指定其操作数是 AND 还是 OR。换句话说,您可以指定用户必须满足所有规则操作数还是只需满足其中一个。
此外,规则还允许您指定其他 logical_user_list 对象作为操作数,从而有效地创建这些对象的树。logical_user_list 可以非常有效地为定位定义复杂的群组层次结构。您可以合并具有不同 AccessReason 字段的列表。不过,如果访问权限被撤消,那么在评估 logical_user_list 的规则时,该 UserList 将被视为不含任何成员的列表。
以下代码展示了如何创建一个 logical_user_list,其中包含两个 basic_user_list 实例中的用户:
创建逻辑用户列表的代码示例
Java
private void runExample( GoogleAdsClient googleAdsClient, long customerId, List<Long> userListIds) { // Adds each of the provided list IDs to a list of rule operands specifying which lists the // operator should target. List<LogicalUserListOperandInfo> logicalUserListOperandInfoList = new ArrayList<>(); for (long userListId : userListIds) { String userListResourceName = ResourceNames.userList(customerId, userListId); logicalUserListOperandInfoList.add( LogicalUserListOperandInfo.newBuilder().setUserList(userListResourceName).build()); } // 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 = UserListLogicalRuleInfo.newBuilder() // 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. .setOperator(UserListLogicalRuleOperator.ANY) .addAllRuleOperands(logicalUserListOperandInfoList) .build(); // Creates the new combination user list. UserList userList = UserList.newBuilder() .setName("My combination list of other user lists #" + getPrintableDateTime()) .setLogicalUserList( LogicalUserListInfo.newBuilder().addRules(userListLogicalRuleInfo).build()) .build(); // Creates the operation. UserListOperation operation = UserListOperation.newBuilder().setCreate(userList).build(); // Creates the service client. try (UserListServiceClient userListServiceClient = googleAdsClient.getLatestVersion().createUserListServiceClient()) { // Adds the user list. MutateUserListsResponse response = userListServiceClient.mutateUserLists( Long.toString(customerId), ImmutableList.of(operation)); // Prints the response. System.out.printf( "Created combination user list with resource name, '%s'.%n", response.getResults(0).getResourceName()); } }
C#
public void Run(GoogleAdsClient client, long customerId, long[] userListIds) { // Gets the UserListService client. UserListServiceClient userListServiceClient = client.GetService(Services.V25.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; } }
PHP
public static function runExample( GoogleAdsClient $googleAdsClient, int $customerId, array $userListIds ) { // Adds each of the provided list IDs to a list of rule operands specifying which lists the // operator should target. $logicalUserListOperandInfoList = []; foreach ($userListIds as $userListId) { $logicalUserListOperandInfoList[] = new LogicalUserListOperandInfo([ 'user_list' => ResourceNames::forUserList($customerId, $userListId) ]); } // 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 = 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' => UserListLogicalRuleOperator::ANY, 'rule_operands' => $logicalUserListOperandInfoList ]); // Creates the new combination user list. $userList = new UserList([ 'name' => 'My combination list of other user lists #' . Helper::getPrintableDatetime(), 'logical_user_list' => new LogicalUserListInfo([ 'rules' => [$userListLogicalRuleInfo] ]) ]); // Creates the operation. $operation = new UserListOperation(); $operation->setCreate($userList); // Issues a mutate request to add the user list and prints some information. $userListServiceClient = $googleAdsClient->getUserListServiceClient(); $response = $userListServiceClient->mutateUserLists( MutateUserListsRequest::build($customerId, [$operation]) ); printf( "Created combination user list with resource name '%s'.%s", $response->getResults()[0]->getResourceName(), PHP_EOL ); }