Pixalate Pre-Bid Blocking SDK 为您提供方便访问 Pixalate 的预出价欺诈阻止 API。
预构建 SDK 的最新版本可在 CocoaPods 上获取,并通过将 pod 'pixalate-prebid-blocking'
添加到您的项目 Podfile 中进行集成。
然后运行 pod install
下载 SDK 的最新版本。
pod 'pixalate-prebid-blocking'
要访问 SDK,将 PXBlocking/PXBlocking.h
导入到您的源代码中。
要使用 Pixalate 阻止 SDK,您必须首先使用 [PXBlocking setGlobalConfig:]
进行配置。
仅需要 API 密钥。所有其他参数都是可选的,并且提供合理的默认值。
// import the main library header
#import <pixalate_prebid_blocking/PXBlocking.h>
// Place this in your app initialization code.
// A sample configuration & initialization -- the values chosen for this example
// are not meaningful.
PXGlobalConfig *pxConfig = [PXGlobalConfig makeWithApiKey:@"<your api key>" builder:^(PXGlobalConfigBuilder *builder) {
builder.threshold = 0.8;
builder.timeoutInterval = 3;
builder.ttl = 60 * 60 * 8;
builder.strategy = [[PXDefaultBlockingStrategy alloc] init]; // set the strategy to use to retrieve blocking parameters from the device.
}];
[PXBlocking setGlobalConfig:pxConfig];
参数名 | 描述 | 默认值 |
---|---|---|
apiKey | 您的开发者 API 密钥,用于访问 Pixalate API 服务。 | -- |
threshold | 达到阻止的概率阈值。 正常范围大约是 0.75 到 0.9。 |
0.75 |
ttl | 在发出另一请求之前结果缓存多长时间。 | 8小时 |
timeoutInterval | 允许请求运行的时间,在此之前中止操作。在罕见网络问题的情况下,这有助于确保 Pixalate SDK 不会成为运行您的广告的瓶颈。 | 2秒 |
blockingStrategy | 用于检索设备参数(如设备 ID 和 IPv4 地址)的阻止策略。 | PXDefaultBlockingStrategy |
一旦设置 SDK,您就可以将其实现到您的广告加载逻辑中。SDK 对广告框架无关,可以轻松集成到您的项目所需的任何工作流程中。
[PXBlocking requestBlockStatus:^(bool block, NSError *error) {
if( error != nil ) {
// An error occurred while executing the request. In this case, `block` will always be false.
}
if( block ) {
// Traffic is above the blocking threshold and should be blocked.
} else {
// Traffic is below the threshold and can be allowed.
// You can load your ads here.
}
}];
在开发过程中,测试阻止和未阻止的行为可能会有所帮助。您可以使用以下替代方法 [PXBlocking requestBlockStatusWithBlockingMode:handler:]
实现。然后,您可以将 PXBlockingModeDefault
传递以使用正常行为,将 PXBlockingModeAlwaysBlock
传递来模拟阻止响应,或将 PXBlockingModeNeverBlock
传递来模拟非阻止响应。
[PXBlocking requestBlockStatusWithBlockingMode:PXBlockingModeAlwaysBlock handler:^(bool block, NSError *error) {
/* ... */
}];
这些调试响应将仍然执行阻止策略,因此可以用于测试自定义阻止策略。
SDK 支持多种日志记录级别,提供额外的上下文以进行调试。可以通过调用 [PXBlocking setLogLevel:]
设置级别,默认为 PXLogLevelInfo
。可以通过将级别设置为 PXLogLevelNone
完全禁用日志记录。
[PXBlocking setLogLevel:PXLogLevelDebug];
Pixalate 为设备 ID 和 IPv4 地址参数提供默认策略。这些值应涵盖大多数常见用例。
如果出于任何原因,您希望添加、删除或修改库使用的阻止策略,您可以创建自定义策略。这将在下面的详细说明中解释。
返回 [[[UIDevice currentDevice] identifierForVendor] UUIDString]
的值。
SDK将通过利用Pixalate服务检索设备的公共IPv4地址。
尽管预竞标欺诈API支持传输浏览器用户代理,但在应用环境中用户代理的概念模糊不清。因此,默认的阻止策略不使用用户代理。
// In your initialization code
PXGlobalConfig *pxConfig = [PXGlobalConfig makeWithApiKey:@"<your api key>" builder:^(PXGlobalConfigBuilder *builder) {
/* ... */
// set the blocking strategy TTL to 5 minutes
builder.strategy = [[PXDefaultBlockingStrategy alloc] initWithTTL:60 * 5];
}];
默认情况下,阻止策略将继承全局配置的TTL和超时间隔。此值可以通过创建一个新实例的PXDefaultBlockingStrategy并在初始化库时将其传递到PXBlockingConfigBuilder来覆盖。
// ExtendedBlockingStrategy.h
@interface ExtendedBlockingStrategy : PXDefaultBlockingStrategy
@end
// ExtendedBlockingStrategy.m
@implementation ExtendedBlockingStrategy
- (void) getIPv4Address:(PXBlockingStrategyResultHandler)resultHandler {
resultHandler(nil,nil); // disable the sending of IPv4 addresses
}
- (void) getUserAgent:(PXBlockingStrategyResultHandler)resultHandler {
NSString *userAgent = /* Retrieve the user agent somehow */;
resultHandler(userAgent,nil); // add behavior not present in the default strategy
}
@end
如果您想修改默认策略(例如添加或删除参数),则可以创建PXDefaultBlockingStrategy的一个子类。PXDefaultBlockingStrategy使用不同的API允许更容易地继承,而不会丢失TTL和超时行为。为了利用这一点,请覆盖各种方法的Impl版本而不是原始版本。例如,getIPv4Address变为getIPv4AddressImpl。其余签名保持不变。
// A contrived example of how to go about implementing such a strategy.
// As it only implements getIPv4 and returns null for the other methods,
// IPv4 is the only parameter that will be included in requests.
#import <pixalate_prebid_blocking/PXBlocking.h>
// MyCustomBlockingStrategy.h
@interface MyCustomBlockingStrategy : NSObject <PXBlockingStrategyProtocol>
@end
// MyCustomBlockingStrategy.m
@implementation MyCustomBlockingStrategy
- (void) getIPv4Address:(PXBlockingStrategyResultHandler)resultHandler {
NSString *ipv4 = /* Retrieve the IPv4 address... */;
// call the handler with the resulting value
resultHandler(@"my-ip4-value", nil);
// if an error is caught, pass it as the second argument:
resultHandler(nil, myError);
}
- (void) getDeviceID:(PXBlockingStrategyResultHandler)resultHandler {
resultHandler(nil,nil);
}
- (void) getUserAgent:(PXBlockingStrategyResultHandler)resultHandler {
resultHandler(nil,nil);
}
@end
// Then, in your initialization code, pass your modified strategy
// into the builder
PXGlobalConfig *pxConfig = [PXGlobalConfig makeWithApiKey:@"<your api key>" builder:^(PXGlobalConfigBuilder *builder) {
/* ... */
// set the blocking strategy TTL to 5 minutes
builder.strategy = [[MyCustomBlockingStrategy alloc] init];
}];
如果您有一个默认策略未涵盖的替代用例,您可以创建自己的阻止策略。
PXBlockingStrategyProtocol默认不提供TTL或超时行为。这提供了对值检索和缓存的不同方法的完全灵活性。