// ...
repositories {
// ...
mavenCentral()
}
// ...
dependencies {
// ...
// Get the latest version from https://mvnrepository.com/artifact/com.appsflyer/af-android-sdk
implementation 'com.appsflyer:af-android-sdk:6.9.0'
}
检查AD_ID权限2022 年初,Google 宣布改变 Google Play 服务的行为和获取 Android 广告 ID。针对 Android 13 (API 33) 及更高版本的应用程序必须在其文件中声明 Google Play 服务正常权限才能访问设备的广告 ID。
-keep class com.appsflyer.** { *; }
package org.cocos2dx.plugin;
import android.content.Context;
import android.util.Log;
import com.appsflyer.AFInAppEventParameterName;
import com.appsflyer.AFInAppEventType;
import com.appsflyer.AppsFlyerLib;
import com.appsflyer.attribution.AppsFlyerRequestListener;
import java.util.HashMap;
import java.util.Map;
public class AppsflyerWrapper {
private static AppsflyerWrapper _instance = null;
public static AppsflyerWrapper getInstance() {
if (_instance == null) {
_instance = new AppsflyerWrapper();
}
return _instance;
}
private final String AF_DEV_KEY = "your ap_dev_key";
private final String LOG_TAG = "AppsflyerWrapper";
private Context sContext;
public void initAppsflyer(Context context) {
Log.d(LOG_TAG, "initAppsflyer");
sContext = context;
AppsFlyerLib.getInstance().init(AF_DEV_KEY, null, context);
AppsFlyerLib.getInstance().start(context, AF_DEV_KEY, new AppsFlyerRequestListener() {
@Override
public void onSuccess() {
Log.d(LOG_TAG, "Launch sent successfully, got 200 response code from server");
}
@Override
public void onError(int i, String s) {
Log.d(LOG_TAG, "Launch failed to be sent:\n" +
"Error code: " + i + "\n"
+ "Error description: " + s);
}
});
}
// 发送应用内事件
// 已定义的事件类型在 AFInAppEventType 中可查看
public void logEvent() {
Map eventValues = new HashMap();
//eventValues.put(AFInAppEventParameterName.PRICE, 1234.56);
//eventValues.put(AFInAppEventParameterName.CONTENT_ID,"1234567");
AppsFlyerLib.getInstance().logEvent(sContext,
AFInAppEventType.LOGIN, eventValues, new AppsFlyerRequestListener() {
@Override
public void onSuccess() {
Log.d(LOG_TAG, "Event sent successfully");
}
@Override
public void onError(int i, String s) {
Log.d(LOG_TAG, "Event failed to be sent:\n" +
"Error code: " + i + "\n"
+ "Error description: " + s);
}
});
}
}
(File -> Add Packages)输入 https://github.com/AppsFlyerSDK/AppsFlyerFramework
选择AppsFlyerFramework添加到项目工程即可
导入依赖库、 初始化需要Apple App ID 和 AppsFlyer dev key2个配置参数,启动有2种方式,不带回调start和带回调的startWithCompletionHandler。直接上代码
// 导入依赖库
#import
// 在didFinishLaunchingWithOptions中初始化SDK
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// ...
[[AppsFlyerLib shared] setAppsFlyerDevKey:@""];
[[AppsFlyerLib shared] setAppleAppID:@""];
// ...
return YES;
}
// 在applicationDidBecomeActive中启动SDK
- (void)applicationDidBecomeActive:(UIApplication *)application {
// ...
//[[AppsFlyerLib shared] start];
[[AppsFlyerLib shared] startWithCompletionHandler:^(NSDictionary *dictionary, NSError *error) {
if (error) {
NSLog(@"%@", error);
return;
}
if (dictionary) {
NSLog(@"%@", dictionary);
return;
}
}];
}
logEvent方法可用于记录应用内事件,并将其发送到AppsFlyer进行处理。
eventName
)是事件名称eventValues
)是事件参数NSDictionary
- (void)logEvent:(NSString *)eventName withValues:(NSDictionary * _Nullable)values;
logEventWithEventName同样可记录事件
eventName
)是事件名称eventValues
)是事件参数NSDictionary
completionHandler
)是一个可选的完成处理程序- (void)logEventWithEventName:(NSString *)eventName eventValues:(NSDictionary * _Nullable)eventValues completionHandler:(void (^ _Nullable)(NSDictionary * _Nullable dictionary, NSError * _Nullable error))completionHandler;
记录应用内事件的时候发生错误,错误代码和字符串说明如下,请参考
错误吗码 | 描述 |
---|---|
10 |
超时,可检查参数minTimeBetweenSessions |
11 |
由于启用了停止追踪,事件不上传 |
40 |
网络错误,可查看错误信息 |
41 |
未设置AppsFlyer dev key |
50 |
状态代码故障,查看回调中的错误信息 |