Android和iOS接入Appsflyer SDK

Android接入

1.添加repositories

// ...
repositories {
    // ...
    mavenCentral()
}
// ...

2.添加dependencies

dependencies {
    // ...
    // Get the latest version from https://mvnrepository.com/artifact/com.appsflyer/af-android-sdk
    implementation 'com.appsflyer:af-android-sdk:6.9.0' 
}

3.AndroidManifest.xml中添加权限


        检查AD_ID权限2022 年初,Google 宣布改变 Google Play 服务的行为和获取 Android 广告 ID。针对 Android 13 (API 33) 及更高版本的应用程序必须在其文件中声明 Google Play 服务正常权限才能访问设备的广告 ID。

4.AppsFlyer SDK ProGuard 规则

-keep class com.appsflyer.** { *; }

5.接入代码

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);
                    }
                });
    }
}

iOS接入

1.添加依赖库

(File ->  Add Packages)输入 https://github.com/AppsFlyerSDK/AppsFlyerFramework

选择AppsFlyerFramework添加到项目工程即可

Android和iOS接入Appsflyer SDK_第1张图片Android和iOS接入Appsflyer SDK_第2张图片2.初始化SDK/启动SDK

         导入依赖库、 初始化需要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;
        }
    }]; 
}

3.记录应用内事件

        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 状态代码故障,查看回调中的错误信息

你可能感兴趣的:(Android,android,unity,cocos2d)