123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- const Global = require("Global");
- const BaseDefine = require("BaseDefine");
- var CGPLoginMission = require("CGPLoginMission");
- const KEY_MAP = new Map([
- ["org.queen.test", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.championplayer", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.Victory", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.qexceed", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.reward", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.qplayer", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.q3acard", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teenpatti.queen", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teenpatti.queen2", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.qnobles", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.qgrandparty", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queen", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.royalclub", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queencard", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queensnice", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queensplay", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queensclub", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queenmultiplayer", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queenspokergame", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queensmaster", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queenchampion", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queenbwinner", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queenwinnerking", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queenluckypokert", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teempatti.queen3pattigame", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teenpatti.qindianpoker", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teenpattiq.scratchcard", "aoRrdANBXaid2HX77FxE6P"],
- ["com.teenpatti.qluckylottery", "aoRrdANBXaid2HX77FxE6P"],
- ["com.tp3pattiq.dailylottery", "aoRrdANBXaid2HX77FxE6P"],
- ["com.tpq3pattiwin.lottery", "aoRrdANBXaid2HX77FxE6P"],
- ["com.indiantp3patti.winner", "aoRrdANBXaid2HX77FxE6P"]
- ]);
- const ONCE_LINK_KEY_MAP = new Map([
- ["com.teempatti.championplayer", "icUo"],
- ["com.teempatti.Victory", "9oZ5"],
- ["com.teempatti.qexceed", "uUWu"],
- ["com.teempatti.reward", "yAEo"],
- ["com.teempatti.qplayer", "vt7B"],
- ["com.teempatti.q3acard", "H0vK"],
- ["com.teenpatti.queen", "vKDF"],
- ["com.teenpatti.queen2", "x8wa"],
- ["com.teempatti.qnobles", "SWkn"],
- ["com.teempatti.qgrandparty", "S4jo"],
- ["com.teempatti.queen", "k0Nz"],
- ["com.teempatti.royalclub", "S87x"],
- ["com.teempatti.queencard", "QmIA"],
- ["com.teempatti.queensnice", "ilFV"],
- ["com.teempatti.queensplay", "apzC"],
- ["com.teempatti.queensclub", "cF9a"],
- ["com.teempatti.queenmultiplayer", "sgqo"],
- ["com.teempatti.queenspokergame", "BCbX"],
- ["com.teempatti.queensmaster", "r3Qy"],
- ["com.teempatti.queenchampion", "oK5f"],
- ["com.teempatti.queenbwinner", "VXsS"],
- ["com.teempatti.queenluckypokert", "00dH"],
- ["com.teempatti.queen3pattigame", "CzNs"],
- ["com.teenpatti.qindianpoker", "Q1zG"],
- ["com.teenpattiq.scratchcard", "E9hj"],
- ["com.teenpatti.qluckylottery", "hp1J"],
- ]);
- var EventQueue = new Array();
- class AppFlyerUtil {
- constructor() {
- this.ONCE_LINK_KEY = "";
- this.APPS_FLYER_KEY = "";
- }
- /**
- * 初始化AppsFlyer,根据qq当前包名设置对应key
- */
- init() {
- // let packageName = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Native", "getAppId", "()Ljava/lang/String;");
- let key = "aoRrdANBXaid2HX77FxE6P";//KEY_MAP.get(packageName);
- Global.print("AppsFlyer::初始化:(" + key + ")");
- if (key) {
- // 生成回调
- let successEvent = "AppsFlyerConversion";
- let errorEvent = "AppsFlyerConversionError";
- let onSuccess = () => {
- let data = this.getConversionData();
- let loginMission = new CGPLoginMission({ URL: cc.vv.config.Address, Port: cc.vv.config.Port });
- loginMission.sendAdjustADParam(
- `media_source :${data.get("media_source") ? data.get("media_source") : ''}`
- + `,campaign :${data.get("campaign") ? data.get("campaign") : ''}`
- + `,campaign_id :${data.get("campaign_id") ? data.get("campaign_id") : ''}`);
- cc.game.off(errorEvent, onError, this);
- // 未初始化前触发的事件重发机制
- var interval = setInterval(() => {
- if (EventQueue.length > 0) {
- let [eventType, parameters] = EventQueue.pop();
- this.logEvent(eventType, parameters);
- } else {
- clearInterval(interval);
- }
- }, 500);
- }
- let onError = (s) => {
- Global.print(`AppsFlyer::归因失败:(${s})`);
- cc.game.off(successEvent, onSuccess, this);
- // cc.userOperate.sendRecord("Attribution failure");
- setTimeout(() => {
- cc.vv.reflection.initAppsFlyer(key, successEvent, errorEvent);
- }, 5000)
- }
- cc.game.once(errorEvent, onError, this);
- cc.game.once(successEvent, onSuccess, this);
- cc.vv.reflection.initAppsFlyer(key, successEvent, errorEvent);
- this.APPS_FLYER_KEY = key;
- }
- this.setCurrencyCode("INR");
- }
- /**
- * 获取本地归因数据,据网络情况,时间不同,未归因完成时,Native返回空字符
- * @returns {Map}
- */
- getConversionData() {
- let jsonStr = cc.vv.reflection.appsFlyerGetConversionData();
- Global.print(`AppsFlyer::获取本地归因数据`);
- if (jsonStr) {
- try {
- let json = JSON.parse(jsonStr);
- if (json.campaign) {
- let attr = new Map();
- for (const [key, value] of Object.entries(json)) {
- attr.set(key, value);
- Global.print(` (${key}: ${value})`);
- }
- return attr;
- } else {
- setTimeout(() => {
- this.getConversionData();
- }, 300000);
- }
- } catch (error) {
- Global.print("AppsFlyer::归因数据解析失败");
- }
- }
- return new Map();
- }
- /**
- * 获取本地归因数据(异步)
- * @returns {Promise}
- */
- getConversionDataAsync() {
- Global.print(`AppsFlyer::获取本地归因数据(异步)`);
- const INTERVAL_TIME = 5000;
- const MAX_REQ_COUNT = 12;
- return new Promise((resolve, reject) => {
- if (BaseDefine.GAME_PLATFORM === BaseDefine.WEB_H5_PLATFORM) {
- resolve(new Map());
- return;
- }
- let count = 0;
- let waitFunc = () => {
- if (count >= MAX_REQ_COUNT) {
- reject("获取超时");
- return;
- }
- let data = this.getConversionData();
- if (!data) {
- setTimeout(waitFunc, INTERVAL_TIME);
- count++;
- Global.print(`AppsFlyer::获取本地归因数据(异步)第${count + 1}次`);
- } else {
- resolve(data);
- }
- }
- waitFunc();
- });
- }
- /**
- * 设置AppsFlyer的全局的货币Code, 默认INR(印尼)
- */
- setCurrencyCode(code) {
- code = code || "INR";
- Global.print("AppsFlyer::设置全局货币:(" + code + ")");
- cc.vv.reflection.appsFlyerSetCurrencyCode(code);
- }
- /**
- * 向AppsFlyer发送事件
- * @param eventType 事件类型
- * @param {Map} parameters 事件参数
- */
- logEvent(eventType, parameters = new Map()) {
- // AppsFlyer内置的事件类型及参数字段:https://support.appsflyer.com/hc/zh-cn/articles/115005544169
- // map to JSON
- if (BaseDefine.GAME_PLATFORM === BaseDefine.WEB_H5_PLATFORM) {
- return;
- }
- if (!this.APPS_FLYER_KEY) {
- EventQueue.push([eventType, parameters]);
- Global.print(`AppsFlyer::事件发送失败:(${eventType})(未初始化)`);
- return;
- }
- let obj = new Object();
- for (const [key, value] of parameters) {
- obj[key] = value;
- }
- let json = JSON.stringify(obj);
- // 生成回调
- let successEvent = "AppsFlyerLog";
- let errorEvent = "AppsFlyerLogError";
- let onSuccess = () => {
- Global.print(`AppsFlyer::事件发送成功:(${eventType})`);
- cc.game.off(errorEvent, onError, this);
- }
- let onError = (s) => {
- Global.print(`AppsFlyer::事件发送失败:(${eventType})(${s})`);
- cc.game.off(successEvent, onSuccess, this);
- }
- cc.game.once(successEvent, onSuccess, this);
- cc.game.once(errorEvent, onError, this);
- cc.vv.reflection.appsFlyerLogEvent(eventType, json, successEvent, errorEvent);
- }
- /**
- * 设置OneLink的模板ID,用于链接跳转地址
- */
- setAppInviteOneLink() {
- let packageName = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Native", "getAppId", "()Ljava/lang/String;");
- let key = ONCE_LINK_KEY_MAP.get(packageName);
- if (key) {
- Global.print("AppsFlyer::设置OnceLink模板ID:(" + key + ")");
- cc.vv.reflection.appsFlyerSetAppInviteOnceLink(key);
- this.ONCE_LINK_KEY = key;
- }
- }
- /**
- * 获取AppsFlyer跳转的分享链接(异步),生成失败返回长链接,依旧可以使用,但是会暴露参数
- * @returns {Promise}
- */
- generateShareLinkAsync() {
- return new Promise((resolve, reject) => {
- if (BaseDefine.GAME_PLATFORM === BaseDefine.WEB_H5_PLATFORM) {
- resolve(BaseDefine.SHARE_URL);
- return;
- }
- let shareLink = cc.sys.localStorage.getItem("AgentShareLink");
- if (shareLink && !!shareLink.match(/onelink\.me/i)) {
- resolve(shareLink);
- return;
- }
- //设置模板ID,必填
- if (!this.ONCE_LINK_KEY) {
- this.setAppInviteOneLink();
- }
- let myUserID = cc.vv.globalUserInfo.getGameID();
- Global.print(`AppsFlyer::生成分享链接:(userID:${myUserID})`);
- // 生成回调
- let onSuccess = (s) => {
- Global.print(`AppsFlyer::生成分享链接成功:(${s})`);
- resolve(s);
- cc.sys.localStorage.setItem("AgentShareLink", s);
- cc.game.off("generateLinkError", onError, this);
- }
- let onError = (s) => {
- Global.print(`AppsFlyer::生成分享链接失败`);
- resolve(s);
- cc.game.off("generateLinkSuccess", onSuccess, this);
- }
- cc.game.once("generateLinkSuccess", onSuccess, this);
- cc.game.once("generateLinkError", onError, this);
- cc.vv.reflection.appsFlyerInitShareLinkGenerator("Share"); // 设置分享方式并初始化链接生成器, 必填
- // cc.vv.reflection.appsFlyerSetCampaignOnShareLinkGenerator("100"); // 设置链接渠道
- cc.vv.reflection.appsFlyerSetReferrerCustomerIdOnShareLinkGenerator(myUserID); // 设置分享玩家ID
- cc.vv.reflection.appsFlyerGenerateLink("generateLinkSuccess", "generateLinkError"); // 生成链接
- });
- }
- /**
- * 获取AppsFlyer的唯一标识
- */
- getAppsFlyerID() {
- return cc.vv.reflection.appsFlyerGetAppsFlyerID();
- }
- /**
- * 设置该用户的AppsFlyer自定义玩家ID
- */
- setCustomerUserID(id) {
- cc.vv.reflection.appsFlyerSetCustomerUserID(id);
- }
- /**
- * 获取该用户的AppsFlyer自定义玩家ID
- */
- getCustomerUserID() {
- return cc.vv.reflection.appsFlyerGetCustomerUserID();
- }
- /**
- * 通过AppsFlyer更新当前玩家的渠道及上级数据
- */
- updateSpreadAndAdChannel(userID) {
- this.getConversionDataAsync()
- .then((data) => {
- let spreadID = data.get("af_referrer_customer_id") || 0; // 分享者用户ID
- let campaign = data.get("campaign") || ""; // 广告渠道名称
- let channelID = 0;
- // 匹配 * 符号以后的数字,不包括 * 符号,且不包括被匹配数字后从非数字字符下标起的任意字符,包括数字
- try {
- channelID = campaign.match(/\*\d+/)[0].slice(1);
- } catch (error) {
- Global.print("AppsFlyer::解析渠道错误(" + error + ")");
- } finally {
- Global.print(`AppsFlyer::updateSpreadAndAdChannel: spread=(${spreadID}), channel=${channelID}, campaign=${campaign}`);
- let loginMission = new CGPLoginMission({ URL: cc.vv.config.Address, Port: cc.vv.config.Port });
- loginMission.updateSpreadAndChannel(userID, channelID, spreadID);
- }
- })
- .catch((error) => {
- Global.print(`AppsFlyer::updateSpreadAndAdChannel: 更新渠道失败(${error})`);
- })
- }
- /**
- * 绑定服务器UserID 与 AF 的 AFId
- */
- bindUserIDAndAFID() {
- let loginMission = new CGPLoginMission({ URL: cc.vv.config.Address, Port: cc.vv.config.Port });
- this.getAppsFlyerID() && loginMission.bindUserIDAndAFID(this.getAppsFlyerID());
- }
- }
- module.exports = new AppFlyerUtil();
|