接口文档SDK业务专题开发者工具

创建复杂定向

                <?php
require_once __DIR__ . '/../../vendor/autoload.php'; // change path as needed
use TencentAds\TencentAds;
use TencentAds\Exception\TencentAdsResponseException;
use TencentAds\Exception\TencentAdsSDKException;

/*****
 * 本文件提供了一个创建定向的示例
 */
class AddComplexTargetings
{
    public static $tads;
    public static $ACCESS_TOKEN = 'YOUR ACCESS TOKEN';
    public static $ACCOUNT_ID   = 'YOUR ACCOUNT ID';
    // 本示例会提供创建两个定向人群包,其中一个是包含的人群包,另一个是排除的人群包
    public static $AUDIENCE_FILE_POS_IMEI = 'YOUR AUDIENCE FILE PATH';
    public static $AUDIENCE_FILE_NEG_IMEI = 'YOUR AUDIENCE FILE PATH';

    public function init()
    {
        $tads = TencentAds::init([
            'access_token' => static::$ACCESS_TOKEN,
            'is_debug'     => true,
        ]);
        $tads->useSandbox(); // 默认访问沙箱环境,如访问正式环境,请切换为$tads->useProduction()
        static::$tads = $tads;

        return $tads;
    }

    public function main()
    {
        try {
            /* @var TencentAds $tads */
            $tads = static::$tads;

            // 第一步,获取地域ID,用于地域定向,微信朋友圈广告必须
            $regionNames = ['北京市', '上海市', '广东省'];
            $regions = $this->getTargetingRegion($tads, $regionNames);
            // echo 'Region IDs: ' . implode(', ', $regions) . PHP_EOL;

            // 第二步,创建人群包,可选,可创建定向用户群和排除用户群
            $positiveAudienceId = $this->addCustomAudience($tads, static::$AUDIENCE_FILE_POS_IMEI);
            $negativeAudienceId = $this->addCustomAudience($tads, static::$AUDIENCE_FILE_NEG_IMEI);
            // echo 'Audience IDs: ' . $positiveAudienceId . ', -' . $negativeAudienceId . PHP_EOL;

            // 第三步,创建定向包
            $targetingId = $this->addTargeting($tads, $regions, [$positiveAudienceId], [$negativeAudienceId]);
            // echo 'Targeting ID: ' . $targetingId . PHP_EOL;

            return $targetingId;
        } catch (TencentAdsResponseException $e) {
            // When Api returns an error
            echo 'Tencent ads returned an error: ' . $e->getMessage() . PHP_EOL;
            throw $e;
        } catch (TencentAdsSDKException $e) {
            // When validation fails or other local issues
            echo 'Tencent ads SDK returned an error: ' . $e->getMessage() . PHP_EOL;
            throw $e;
        } catch (Exception $e) {
            echo 'Other exception: ' . $e->getMessage() . PHP_EOL;
            throw $e;
        }
    }

    // 从区域名称得到区域定向ID
    protected function getTargetingRegion(TencentAds $tads, $regionNameList)
    {
        $targetingTags = $tads->targetingTags()
                              ->get([
                                  'type' => 'REGION',
                              ]);
        $regions = [];
        foreach ($targetingTags->getList() as $region) {
            if (in_array($region->getName(), $regionNameList)) {
                // 名称匹配一致
                $regions [] = $region->getId();
            }
        }

        return $regions;
    }

    // 从IMEI文件上传一个新的人群包
    protected function addCustomAudience(TencentAds $tads, $audienceFilePath)
    {
        $audienceName = 'SDK sample aud ' . uniqid();
        $audienceDescription = 'created by SDK samples';
        $audience = $tads->customAudiences()
                         ->add([
                             'account_id'  => static::$ACCOUNT_ID,
                             "name"        => $audienceName,
                             "type"        => "CUSTOMER_FILE",
                             "description" => $audienceDescription,
                         ]);
        $audienceId = $audience->getAudienceId();
        $audienceFile = $tads->customAudienceFiles()
                             ->add([
                                 'account_id'   => static::$ACCOUNT_ID,
                                 'audience_id'  => $audienceId,
                                 'user_id_type' => "IMEI",
                                 'file'         => $audienceFilePath,
                             ]); // 往人群包里上传
        $audienceFileId = $audienceFile->getCustomAudienceFileId();

        return $audienceId;
    }

    // 创建一个定向包
    protected function addTargeting(TencentAds $tads, $regions, $positionAudiences = [], $negativeAudiences = [])
    {
        $targetingName = 'SDK sample targeting ' . uniqid();
        $targetingDescription = 'created by SDK samples';

        $targetingDetail = [
            'age'                      => [
                [ // 年龄定向,23~45岁
                  'min' => 23,
                  'max' => 45,
                ],
            ],
            'gender'                   => ['MALE'], // 性别定向,男性
            'geo_location'             => [ // 地域定向
                                            'location_types' => ['LIVE_IN'],
                                            'regions'        => $regions,
            ],
            'user_os'                  => ['IOS'], // 操作系统定向
            'custom_audience'          => $positionAudiences, // 定向人群
            'excluded_custom_audience' => $negativeAudiences, // 排除的定向人群
        ];
        $targeting = $tads->targetings()
                          ->add([
                              'account_id'     => static::$ACCOUNT_ID,
                              'targeting_name' => $targetingName,
                              'targeting'      => $targetingDetail,
                              'description'    => $targetingDescription,
                          ]);
        $targetingId = $targeting->getTargetingId();

        return $targetingId;
    }
}

if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
    try {
        $example = new AddTargetings();
        $example->init();
        $example->main();
    } catch (\Exception $e) {
        exit(-1);
    }
}
            
                package com.tencent.ads.examples.AdvancedOperations;

import com.tencent.ads.ApiContextConfig;
import com.tencent.ads.ApiException;
import com.tencent.ads.TencentAds;
import com.tencent.ads.exception.TencentAdsResponseException;
import com.tencent.ads.exception.TencentAdsSDKException;
import com.tencent.ads.model.AgeStruct;
import com.tencent.ads.model.AudienceType;
import com.tencent.ads.model.CustomAudienceFilesAddResponseData;
import com.tencent.ads.model.CustomAudiencesAddRequest;
import com.tencent.ads.model.CustomAudiencesAddResponseData;
import com.tencent.ads.model.GeoLocations;
import com.tencent.ads.model.TargetingTagsGetListStruct;
import com.tencent.ads.model.TargetingTagsGetResponseData;
import com.tencent.ads.model.TargetingsAddRequest;
import com.tencent.ads.model.TargetingsAddResponseData;
import com.tencent.ads.model.WriteTargetingSetting;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/** 本文件提供了一个创建定向的示例 */
public class AddComplexTargetings {

  /** YOUR ACCESS TOKEN */
  public String ACCESS_TOKEN = "YOUR ACCESS TOKEN";
  /** YOUR ACCOUNT ID */
  public Long ACCOUNT_ID = 0L;
  // 本示例会提供创建两个定向人群包,其中一个是包含的人群包,另一个是排除的人群包。
  public String AUDIENCE_FILE_POS_IMEI = "YOUR AUDIENCE FILE PATH";
  public String AUDIENCE_FILE_NEG_IMEI = "YOUR AUDIENCE FILE PATH";
  /** TencentAds */
  public TencentAds tencentAds;

  public void init() {
    this.tencentAds = TencentAds.getInstance();
    this.tencentAds.init(
        new ApiContextConfig().accessToken(ACCESS_TOKEN).isDebug(true)); // debug==true 会打印请求详细信息
    this.tencentAds.useSandbox(); // 默认使用沙箱环境,如果要请求线上,这里需要设为线上环境
  }

  /**
   * 创建定向
   *
   * @return 定向包id
   * @throws ApiException 异常
   */
  public Long addComplexTargetings() throws ApiException {
    // 第一步,获取地域ID,用于地域定向,微信朋友圈广告必须
    List<String> regionNameList = Arrays.asList("北京市", "上海市", "广东省");
    List<Long> regionIdList = getTargetingRegion(regionNameList);

    // 第二步,创建人群包,可选,可创建定向用户群和排除用户群
    Long positionAudiences = addCustomAudience(AUDIENCE_FILE_POS_IMEI);
    Long negativeAudiences = addCustomAudience(AUDIENCE_FILE_NEG_IMEI);

    // 第三步,创建定向包
    Long targetingId = addTargeting(regionIdList, positionAudiences, negativeAudiences);
    return targetingId;
  }

  /**
   * 从区域名称得到区域定向ID
   *
   * @param regionNameList 区域名称列表
   * @return 区域定向ID
   * @throws ApiException 异常
   */
  protected List<Long> getTargetingRegion(List<String> regionNameList) throws ApiException {
    List<Long> res = new ArrayList<Long>();
    TargetingTagsGetResponseData responseData =
        tencentAds.targetingTags().targetingTagsGet("REGION", null, null, null);
    if (responseData != null && responseData.getList() != null) {
      for (TargetingTagsGetListStruct s : responseData.getList()) {
        if (regionNameList.contains(s.getName())) {
          res.add(s.getId());
        }
      }
    }
    return res;
  }

  /**
   * 从IMEI文件上传一个新的人群包
   *
   * @param audienceFilePath 文件地址
   * @return 人群id
   * @throws ApiException 异常
   */
  protected Long addCustomAudience(String audienceFilePath) throws ApiException {
    CustomAudiencesAddRequest addRequest = new CustomAudiencesAddRequest();
    addRequest.setAccountId(ACCOUNT_ID);
    addRequest.setName("SDK sample aud" + UUID.randomUUID().toString().substring(0, 6));
    addRequest.setType(AudienceType.CUSTOMER_FILE);
    addRequest.setDescription("created by SDK samples");
    CustomAudiencesAddResponseData customAudiencesAddResponseData =
        tencentAds.customAudiences().customAudiencesAdd(addRequest);
    if (customAudiencesAddResponseData != null
        && customAudiencesAddResponseData.getAudienceId() != null) {
      Long audienceId = customAudiencesAddResponseData.getAudienceId();
      CustomAudienceFilesAddResponseData customAudienceFilesAddResponseData =
          tencentAds
              .customAudienceFiles()
              .customAudienceFilesAdd(
                  ACCOUNT_ID, audienceId, "IMEI", new File(audienceFilePath), null, null);
      if (customAudienceFilesAddResponseData != null
          && customAudienceFilesAddResponseData.getCustomAudienceFileId() != null) {
        return audienceId;
      }
    }
    return null;
  }

  /**
   * 创建一个定向包
   *
   * @param regionIdList 区域定向ID
   * @param positionAudiences 定向用户群
   * @param negativeAudiences 排除用户群
   * @return 定向包id
   * @throws ApiException 异常
   */
  protected Long addTargeting(
      List<Long> regionIdList, Long positionAudiences, Long negativeAudiences) throws ApiException {
    TargetingsAddRequest targetingsAddRequest = new TargetingsAddRequest();
    targetingsAddRequest.setAccountId(ACCOUNT_ID);
    targetingsAddRequest.setTargetingName(
        "SDK sample targeting" + UUID.randomUUID().toString().substring(0, 6));
    targetingsAddRequest.setTargeting(
        new WriteTargetingSetting()
            .gender(Arrays.asList("MALE"))
            .age(Arrays.asList(new AgeStruct().max(45L).min(23L)))
            .geoLocation(
                new GeoLocations().locationTypes(Arrays.asList("LIVE_IN")).regions(regionIdList))
            .userOs(Arrays.asList("IOS"))
            .customAudience(Arrays.asList(positionAudiences))
            .excludedCustomAudience(Arrays.asList(negativeAudiences)));
    targetingsAddRequest.setDescription("created by SDK samples");
    TargetingsAddResponseData targetingsAddResponseData =
        tencentAds.targetings().targetingsAdd(targetingsAddRequest);
    if (targetingsAddResponseData != null) {
      return targetingsAddResponseData.getTargetingId();
    }
    return null;
  }

  public static void main(String[] args) {
    try {
      AddComplexTargetings addComplexTargetings = new AddComplexTargetings();
      addComplexTargetings.init();
      Long targetingId = addComplexTargetings.addComplexTargetings();
    } catch (TencentAdsResponseException e) {
      e.printStackTrace();
    } catch (TencentAdsSDKException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}