AdServices归因和iAd归因集成

AdServices归因和iAd归因集成

前提:AdServices归因框架的集成必须是iOS14.3以上版本xcode版本12.3以上;旧版本依旧使⽤iAd 归因框架。

第一步:找到framework的添加入口

添加framework

第二步:分别搜索AdServices.framework、AdSupport.framework、iAd.framework,进行添加

AdServices.framework


AdSupport.framework
iAd.framework

第三步:在Build Phases中Link Binary With Libraries 修改 AdServices.framework、AdSupport.framework、iAd.framework的类型为Optional

AdServices.framework、AdSupport.framework、iAd.framework的类型为Optional

第四部:将AdServices.framework、AdSupport.framework、iAd.framework的头文件加入到项目

添加头文件

第五步:代码集成,可在AppDelegate中添加 

5.1 AdServices获取token               

if (@available(iOS 14.3, *)) {

NSError *error;

NSString *token = [AAAttribution attributionTokenWithError:&error];

if (token != nil) {

// 发送POST请求归因数据

[self sendToken:[self getANullableString:@"token" content:token]    completeBlock:^(NSDictionary *attrData) {

NSLog(@"成功==:14.3+ Dict: %@", attrData);

//可将数据发送给服务端

}];

}

} else {

// 老版本请求

if ([[ADClient sharedClient]                     respondsToSelector:@selector(requestAttributionDetailsWithBlock:)]) {

NSLog(@"LogAds:iAd called");

[[ADClient sharedClient] requestAttributionDetailsWithBlock:^(NSDictionary *attrData, NSError *error) {

//异步,会延后

NSLog(@"成功:14- Dict: %@", attrData);

//可将数据发送给服务端

// ... ...

}];

}


}


5.2 AdServices获取归因数据     

/** 读取可能为空的字符串*/

-(nullable NSString *)getANullableString:(NSString *)desc content:(NSString *)content{

if(content == nil){

return @"";

}

return [NSString stringWithFormat:@"%@", content];

}


/** 发送归因token得到数据 */

-(void)sendToken:(NSString *)token completeBlock:(void(^)(NSDictionary* data))completeBlock{

NSString *url = [NSString stringWithFormat:@"https://api-adservices.apple.com/api/v1/"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];

request.HTTPMethod = @"POST";

[request addValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];

NSData* postData = [token dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

NSDictionary * result = NULL;

if (error) {

//请求失败

NSLog(@"请求失败LogAds:sendToken ERR");

if (completeBlock) {

NSMutableDictionary *nulldict = [NSMutableDictionary dictionary];

completeBlock(nulldict);

}

}else{

// 请求成功

NSLog(@"请求成功");

NSError *resError;

NSMutableDictionary *resDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&resError];

result = [[NSDictionary alloc] initWithDictionary:resDic];

if (completeBlock) {

completeBlock(result);

}

}

}];

[dataTask resume];

}


5.3 AdServices 返回归因数据包示例

经测试,IDFA允许用户跟踪后,得到的数据是详细数据包

{

adGroupId = 1234567890;

attribution = 1;

campaignId = 1234567890;

clickDate = "2022-04-27T07:59Z";

conversionType = Download;

countryOrRegion = US;

creativeSetId = 1234567890;

keywordId = 12323222;

orgId = 1234567890;

}

未允许,得到的数据是标准数据包,没有clickDate字段

{

"attribution": true,

"orgId": 40669820,

"campaignId": 542370539,

"conversionType": "Download",

"adGroupId": 542317095,

"countryOrRegion": "US",

"keywordId": 87675432,

"creativeSetId": 542317136

}


5.4 iAd 代码

if ([[ADClient sharedClient] respondsToSelector:@selector(requestAttributionDetailsWithBlock:)]) {

NSLog(@"LogAds:iAd called");

[[ADClient sharedClient] requestAttributionDetailsWithBlock:^(NSDictionary *attrData, NSError *error) {

//异步,会延后

NSLog(@"成功:14- Dict: %@", attrData);

//将数据发送给服务端        

}];

}


5.5iAd返回归因数据包示例

{

"iad-adgroup-id" = 1234567890;

"iad-adgroup-name" = AdGroupName;

"iad-attribution" = true;

"iad-campaign-id" = 1234567890;

"iad-campaign-name" = CampaignName;

"iad-click-date" = "2022-04-27T07:31:36Z";

"iad-conversion-date" = "2022-04-27T07:31:36Z";

"iad-conversion-type" = Download;

"iad-country-or-region" = US;

"iad-creativeset-id" = 1234567890;

"iad-creativeset-name" = CreativeSetName;

"iad-keyword" = Keyword;

"iad-keyword-id" = 12323222;

"iad-keyword-matchtype" = Broad;

"iad-lineitem-id" = 1234567890;

"iad-lineitem-name" = LineName;

"iad-org-id" = 1234567890;

"iad-org-name" = OrgName;

"iad-purchase-date" = "2022-04-27T07:31:36Z";

};

你可能感兴趣的:(AdServices归因和iAd归因集成)