You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.6 KiB

3 weeks ago
import { sdkConfig } from "./sdkConfig";
const config = {
accessToken: sdkConfig.config.pointData.accessToken, // 项目通行证,在:网站后台-->设置-->应用列表中找到Access Token列 复制(首次使用可能需要先新增应用)
clientId: "your_client_id", // 用户唯一标识,如产品为小游戏,则必须填用户openid(注意,不是小游戏的APPID!!!)
autoTrack: {
appLaunch: true, // 自动采集 $MPLaunch
appShow: true, // 自动采集 $MPShow
appHide: true, // 自动采集 $MPHide
},
sendTimeout: 3000, // 网络请求超时时间,单位毫秒,默认值 3000 ms
maxRetries: 3, // 网络请求失败时的重试次数,1 表示不重试。默认值是 3
enablePersistence: true, // 是否使用本地缓存,主实例默认为 true,子实例默认为 false
asyncPersistence: false, // 是否使用异步存储,默认为 false
name: "ge", // 全局变量名称
// debugMode: "debug", // 是否开启测试模式,开启测试模式后,可以在 网站后台--设置--元数据--事件流中查看实时数据上报结果。(测试时使用,上线之后一定要关掉,改成none或者删除)
};
export default class GEMgr {
private static nameInit = "zyzy";
private static versionInit = 1;
private static isInit = false;
private static ge;
public static GESetup(openidInit) {
// console.log("ge setup:",openidInit)
}
public static async GEInit(openidInit) {
if (openidInit == null || openidInit =="") {
// console.log("openidInit openid is null");
return;
}
// console.log("ge init:",openidInit)
config.clientId = openidInit;
this.ge = new GravityAnalyticsAPI(config);
this.ge.setupAndStart();
this.ge.initialize({
name: this.nameInit,
version: this.versionInit,
openid: openidInit,
enable_sync_attribution: false,
})
.then((res) => {
// console.log("initialize success " + res);
this.isInit = true;
})
.catch((err) => {
// console.log("initialize failed, error is " + err);
});
}
public static async GEShowAD(adID) {
if (!this.isInit) {
ZYSDK.ZYSDK.getUserId().then((uid)=>{
this.GEInit(uid)
}).catch((err) => {
console.log('GEMgr getUserId catch', String(err));
})
return;
}
this.ge.adShowEvent("reward", adID, { custom_param: "" });
}
public static async GEReportEvent(msg) {
let isAdNum = 0;
let moduleStr = "";
let actionStr = "";
moduleStr = msg.split("-")[0];
actionStr = msg.substring(moduleStr.length+1);
// 仅认运营约定的广告口径,禁止 indexOf('AD')(会误伤 Android / iPad / 资源快照等)
if (/-AD-/.test(msg) || msg.includes('广告')) {
isAdNum = 1;
}
// 性能辅助打点走 reportPerf,不应进入 Gravity
if (moduleStr === 'PF' || msg.startsWith('PF-')) {
return;
}
// console.log("GEReportEvent:",isAdNum,moduleStr,actionStr);
if (!this.isInit) {
ZYSDK.ZYSDK.getUserId().then((uid)=>{
this.GEInit(uid)
}).catch((err) => {
console.log('GEMgr getUserId catch', String(err));
})
return;
}
const ret = this.ge.track("userAction", {action:actionStr,module:moduleStr,isAD:isAdNum}, new Date());
if (ret && typeof ret.then === 'function') {
await ret.catch(GEMgr._onGeTrackFail);
}
}
/** Gravity track 失败:暴露,不让未接 Promise 变成 Uncaught */
private static _onGeTrackFail(err: unknown): void {
console.warn('[GEMgr] track', err);
}
}