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.
122 lines
3.7 KiB
122 lines
3.7 KiB
import { director, Director, sys } from "cc";
|
|
import GravityAnalytics from "../libs/gravityengine.mg.cocoscreator.min.js";
|
|
import { GEData, ADType, AnyEventData } from "./GeDifine.js";
|
|
|
|
const config = {
|
|
accessToken: "",
|
|
clientId: '', // 用户唯一标识,如微信小程序的openid
|
|
autoTrack: {
|
|
appLaunch: true, // 自动采集 $MPLaunch
|
|
appShow: true, // 自动采集 $MPShow
|
|
appHide: true, // 自动采集 $MPHide
|
|
|
|
},
|
|
name: "gravityEngine", // 全局变量名称
|
|
enablePersistence: true, // 是否缓存
|
|
debugMode: "none", // debug or none
|
|
enable_sync_attribution: true, // 是否开启渠道归因
|
|
};
|
|
|
|
interface waitSendData {
|
|
name: string,
|
|
data: any
|
|
}
|
|
|
|
let ge: GravityAnalyticsAPI;
|
|
|
|
export class GEAnalyticsMgr {
|
|
private static _ins: GEAnalyticsMgr;
|
|
public static get ins() {
|
|
if (!this._ins) {
|
|
this._ins = new GEAnalyticsMgr();
|
|
}
|
|
return this._ins;
|
|
}
|
|
isinit: boolean = false;
|
|
isinitComplete:boolean = false;
|
|
waitList:waitSendData[]=[];
|
|
|
|
init(openid: string, version: number, platform: string, tConfigData:any) {
|
|
if(sys.os == sys.OS.WINDOWS)
|
|
return
|
|
if (this.isinit) return;
|
|
director.on(Director.EVENT_AFTER_DRAW,this.loopSendEvent,this);
|
|
this.isinit = true;
|
|
let Name:string = tConfigData.Name;
|
|
config.accessToken = tConfigData.AccessToken;
|
|
if(!config.accessToken || !config.accessToken.length){
|
|
ConsoleEx.Log("引力打点数据错误")
|
|
return;
|
|
}
|
|
config.clientId = openid;
|
|
ge = new GravityAnalytics(config);
|
|
ConsoleEx.Log("GE setupAndStart start");
|
|
ge.setupAndStart();
|
|
ConsoleEx.Log("GE setupAndStart end");
|
|
ge.initialize({ name: Name, version: version, wx_openid: openid, })
|
|
.then((res) => {
|
|
ConsoleEx.Log(res, "GE 初始化打点成功");
|
|
ge.setSuperProperties({
|
|
channel: platform,
|
|
version_id: `1.0.${version}`
|
|
});
|
|
this.isinitComplete = true;
|
|
})
|
|
.catch((err) => {
|
|
ConsoleEx.Log(err, "GE 初始化失败");
|
|
});
|
|
|
|
}
|
|
|
|
loopSendEvent(){
|
|
if (this.isinit && this.isinitComplete && this.waitList.length > 0) {
|
|
let eData = this.waitList.shift();
|
|
this.sendtrack(eData.name, eData.data);
|
|
}
|
|
}
|
|
|
|
/**上报自定义事件
|
|
* @param name 事件名称
|
|
* @param data 事件参数
|
|
*/
|
|
TrySendtrack(name: string, data: any = null) {
|
|
if (!this.isinit || !this.isinitComplete || this.waitList.length > 0) {
|
|
this.waitList.push({ name: name, data: data });
|
|
return;
|
|
}
|
|
this.sendtrack(name,data);
|
|
}
|
|
|
|
/**上报自定义事件
|
|
* @param name 事件名称
|
|
* @param data 事件参数
|
|
*/
|
|
private sendtrack(name: string, data: any) {
|
|
// if(name != GEData.GEeventName.play_time){
|
|
// }
|
|
ConsoleEx.Log("GE 打点上传成功,name:", name, " || data:", data);
|
|
ge.track(name, data);
|
|
//Moyu.sendDataEvent(name, "", data);
|
|
}
|
|
|
|
/**追加去重 */
|
|
sendtrack_unique(name: string, data: number[]) {
|
|
ge.userUniqAppend({
|
|
[name]: data,
|
|
});
|
|
}
|
|
/**重置重新上传 */
|
|
sendtrack_reset(name: string, data: number) {
|
|
ge.userUnset(name);
|
|
ge.userSetOnce({ [name]: data });
|
|
}
|
|
|
|
/**广告显示 */
|
|
adShowEvent(type:ADType | string,ad_id:string,tmep:any = {}){
|
|
if(this.isinit && this.isinitComplete){
|
|
ConsoleEx.Log("GE adShowEvent打点上传成功,type:", type, " || ad_id:", ad_id);
|
|
ge.adShowEvent(type,ad_id,tmep);
|
|
}
|
|
}
|
|
|
|
}
|
|
|